~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to tests/unit-tests/client/mesa/test_client_buffer.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-08 02:04:38 UTC
  • mto: This revision was merged to the branch mainline in revision 58.
  • Revision ID: package-import@ubuntu.com-20140108020438-e1npu0pm7qdv5wc4
Tags: upstream-0.1.3+14.04.20140108
Import upstream version 0.1.3+14.04.20140108

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir_toolkit/mir_client_library.h"
 
20
#include "src/client/mesa/client_buffer.h"
 
21
#include "src/client/mesa/client_buffer_factory.h"
 
22
#include "src/client/mesa/buffer_file_ops.h"
 
23
 
 
24
#include <sys/mman.h>
 
25
#include <gmock/gmock.h>
 
26
#include <gtest/gtest.h>
 
27
 
 
28
#include <stdexcept>
 
29
 
 
30
namespace geom=mir::geometry;
 
31
namespace mclg=mir::client::mesa;
 
32
 
 
33
namespace
 
34
{
 
35
 
 
36
class MockBufferFileOps : public mclg::BufferFileOps
 
37
{
 
38
public:
 
39
    MOCK_CONST_METHOD1(close, int(int fd));
 
40
    MOCK_CONST_METHOD3(map, void*(int fd, off_t offset, size_t size));
 
41
    MOCK_CONST_METHOD2(unmap, void(void* addr, size_t size));
 
42
};
 
43
 
 
44
struct MesaClientBufferTest : public testing::Test
 
45
{
 
46
    void SetUp()
 
47
    {
 
48
        width = geom::Width(12);
 
49
        height =geom::Height(14);
 
50
        stride = geom::Stride(66);
 
51
        pf = mir_pixel_format_abgr_8888;
 
52
        size = geom::Size{width, height};
 
53
        buffer_file_ops = std::make_shared<testing::NiceMock<MockBufferFileOps>>();
 
54
 
 
55
        package = std::make_shared<MirBufferPackage>();
 
56
        package->fd[0] = 167;
 
57
        package->fd_items = 1;
 
58
        package->stride = stride.as_uint32_t();
 
59
        package->width = width.as_int();
 
60
        package->height = height.as_int();
 
61
 
 
62
        package_copy = std::make_shared<MirBufferPackage>(*package.get());
 
63
    }
 
64
    geom::Width width;
 
65
    geom::Height height;
 
66
    geom::Stride stride;
 
67
    MirPixelFormat pf;
 
68
    geom::Size size;
 
69
 
 
70
    std::shared_ptr<testing::NiceMock<MockBufferFileOps>> buffer_file_ops;
 
71
    std::shared_ptr<MirBufferPackage> package;
 
72
    std::shared_ptr<MirBufferPackage> package_copy;
 
73
 
 
74
};
 
75
 
 
76
}
 
77
 
 
78
TEST_F(MesaClientBufferTest, width_and_height)
 
79
{
 
80
    using namespace testing;
 
81
 
 
82
    mclg::ClientBuffer buffer(buffer_file_ops, package, size, pf);
 
83
 
 
84
    EXPECT_EQ(buffer.size().height, height);
 
85
    EXPECT_EQ(buffer.size().width, width);
 
86
    EXPECT_EQ(buffer.pixel_format(), pf);
 
87
}
 
88
 
 
89
TEST_F(MesaClientBufferTest, buffer_returns_correct_stride)
 
90
{
 
91
    using namespace testing;
 
92
 
 
93
    mclg::ClientBuffer buffer(buffer_file_ops, package, size, pf);
 
94
 
 
95
    EXPECT_EQ(buffer.stride(), stride);
 
96
}
 
97
 
 
98
TEST_F(MesaClientBufferTest, buffer_returns_set_package)
 
99
{
 
100
    using namespace testing;
 
101
 
 
102
    mclg::ClientBuffer buffer(buffer_file_ops, package, size, pf);
 
103
 
 
104
    auto package_return = buffer.native_buffer_handle();
 
105
    EXPECT_EQ(package_return->data_items, package_copy->data_items);
 
106
    EXPECT_EQ(package_return->fd_items, package_copy->fd_items);
 
107
    EXPECT_EQ(package_return->stride, package_copy->stride);
 
108
    for (auto i=0; i<mir_buffer_package_max; i++)
 
109
        EXPECT_EQ(package_return->data[i], package_copy->data[i]);
 
110
    for (auto i=0; i<mir_buffer_package_max; i++)
 
111
        EXPECT_EQ(package_return->fd[i], package_copy->fd[i]);
 
112
}
 
113
 
 
114
TEST_F(MesaClientBufferTest, secure_for_cpu_write_maps_buffer_fd)
 
115
{
 
116
    using namespace testing;
 
117
    void *map_addr{reinterpret_cast<void*>(0xabcdef)};
 
118
 
 
119
    EXPECT_CALL(*buffer_file_ops, map(package->fd[0],_,_))
 
120
        .WillOnce(Return(map_addr));
 
121
    EXPECT_CALL(*buffer_file_ops, unmap(map_addr,_))
 
122
        .Times(1);
 
123
    EXPECT_CALL(*buffer_file_ops, close(package->fd[0]))
 
124
        .Times(1);
 
125
 
 
126
    mclg::ClientBuffer buffer(buffer_file_ops, package, size, pf);
 
127
 
 
128
    auto mem_region = buffer.secure_for_cpu_write();
 
129
    ASSERT_EQ(map_addr, mem_region->vaddr.get());
 
130
    ASSERT_EQ(width, mem_region->width);
 
131
    ASSERT_EQ(height, mem_region->height);
 
132
    ASSERT_EQ(stride, mem_region->stride);
 
133
    ASSERT_EQ(pf, mem_region->format);
 
134
}
 
135
 
 
136
TEST_F(MesaClientBufferTest, secure_for_cpu_write_throws_on_map_failure)
 
137
{
 
138
    using namespace testing;
 
139
 
 
140
    EXPECT_CALL(*buffer_file_ops, map(package->fd[0],_,_))
 
141
        .WillOnce(Return(MAP_FAILED));
 
142
    EXPECT_CALL(*buffer_file_ops, unmap(_,_))
 
143
        .Times(0);
 
144
    EXPECT_CALL(*buffer_file_ops, close(package->fd[0]))
 
145
        .Times(1);
 
146
 
 
147
    mclg::ClientBuffer buffer(buffer_file_ops, package, size, pf);
 
148
 
 
149
    EXPECT_THROW({
 
150
        auto mem_region = buffer.secure_for_cpu_write();
 
151
    }, std::runtime_error);
 
152
}
 
153
 
 
154
TEST_F(MesaClientBufferTest, buffer_fd_closed_on_buffer_destruction)
 
155
{
 
156
    using namespace testing;
 
157
 
 
158
    EXPECT_CALL(*buffer_file_ops, close(package->fd[0]))
 
159
        .Times(1);
 
160
 
 
161
    mclg::ClientBuffer buffer(buffer_file_ops, package, size, pf);
 
162
}
 
163
 
 
164
TEST_F(MesaClientBufferTest, factory_gets_size_from_package)
 
165
{
 
166
    using namespace testing;
 
167
 
 
168
    mclg::ClientBufferFactory factory(buffer_file_ops);
 
169
 
 
170
    geom::Size unused_size{0, 0};
 
171
    auto buffer = factory.create_buffer(package, unused_size, pf);
 
172
 
 
173
    auto const& buf_size = buffer->size();
 
174
    EXPECT_EQ(package->width, buf_size.width.as_int());
 
175
    EXPECT_EQ(package->height, buf_size.height.as_int());
 
176
 
 
177
    EXPECT_NE(unused_size, buf_size);
 
178
}