~ubuntu-branches/ubuntu/utopic/mir/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/unit-tests/scene/test_surface_data.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-03-10 19:28:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: package-import@ubuntu.com-20140310192846-rq9qm3ec26yrelo2
Tags: upstream-0.1.6+14.04.20140310
ImportĀ upstreamĀ versionĀ 0.1.6+14.04.20140310

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright Ā© 2013 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 "src/server/scene/surface_data.h"
20
 
 
21
 
#include "mir/geometry/rectangle.h"
22
 
 
23
 
#include "mir_test/fake_shared.h"
24
 
 
25
 
#include <algorithm>
26
 
#include <gtest/gtest.h>
27
 
#include <gmock/gmock.h>
28
 
 
29
 
namespace mc = mir::compositor;
30
 
namespace mt = mir::test;
31
 
namespace mi = mir::input;
32
 
namespace ms = mir::scene;
33
 
namespace geom = mir::geometry;
34
 
 
35
 
namespace
36
 
{
37
 
class MockCallback
38
 
{
39
 
public:
40
 
    MOCK_METHOD0(call, void());
41
 
};
42
 
 
43
 
struct SurfaceDataTest : public testing::Test
44
 
{
45
 
    void SetUp()
46
 
    {
47
 
        name = std::string("aa");
48
 
        top_left = geom::Point{geom::X{4}, geom::Y{7}};
49
 
        size = geom::Size{5, 9};
50
 
        rect = geom::Rectangle{top_left, size};
51
 
        null_change_cb = []{};
52
 
        mock_change_cb = std::bind(&MockCallback::call, &mock_callback);
53
 
    }
54
 
    std::string name;
55
 
    geom::Point top_left;
56
 
    geom::Size size;
57
 
    geom::Rectangle rect;
58
 
 
59
 
    testing::NiceMock<MockCallback> mock_callback;
60
 
    std::function<void()> null_change_cb;
61
 
    std::function<void()> mock_change_cb;
62
 
};
63
 
 
64
 
}
65
 
 
66
 
TEST_F(SurfaceDataTest, basics)
67
 
{
68
 
    ms::SurfaceData data{name, rect, null_change_cb, false};
69
 
    EXPECT_EQ(name, data.name());
70
 
    EXPECT_EQ(rect.size, data.size());
71
 
    EXPECT_EQ(rect.top_left, data.position());
72
 
    EXPECT_FALSE(data.shaped());
73
 
}
74
 
 
75
 
TEST_F(SurfaceDataTest, update_position)
76
 
{
77
 
    EXPECT_CALL(mock_callback, call())
78
 
        .Times(1);
79
 
 
80
 
    ms::SurfaceData storage{name, rect, mock_change_cb, false};
81
 
    EXPECT_EQ(rect.top_left, storage.position());
82
 
 
83
 
    auto new_top_left = geom::Point{geom::X{6}, geom::Y{10}};
84
 
    storage.move_to(new_top_left);
85
 
    EXPECT_EQ(new_top_left, storage.position());
86
 
}
87
 
 
88
 
TEST_F(SurfaceDataTest, update_size)
89
 
{
90
 
    geom::Size const new_size{34, 56};
91
 
 
92
 
    EXPECT_CALL(mock_callback, call())
93
 
        .Times(1);
94
 
 
95
 
    ms::SurfaceData storage{name, rect, mock_change_cb, false};
96
 
    EXPECT_EQ(rect.size, storage.size());
97
 
    EXPECT_NE(new_size, storage.size());
98
 
 
99
 
    auto old_transformation = storage.transformation();
100
 
 
101
 
    storage.resize(new_size);
102
 
    EXPECT_EQ(new_size, storage.size());
103
 
    EXPECT_NE(old_transformation, storage.transformation());
104
 
}
105
 
 
106
 
TEST_F(SurfaceDataTest, test_surface_set_rotation_updates_transform)
107
 
{
108
 
    EXPECT_CALL(mock_callback, call())
109
 
        .Times(1);
110
 
 
111
 
    ms::SurfaceData storage{name, rect, mock_change_cb, false};
112
 
    auto original_transformation = storage.transformation();
113
 
 
114
 
    storage.apply_rotation(60.0f, glm::vec3{0.0f, 0.0f, 1.0f});
115
 
    auto rotated_transformation = storage.transformation();
116
 
    EXPECT_NE(original_transformation, rotated_transformation);
117
 
}
118
 
 
119
 
TEST_F(SurfaceDataTest, test_surface_transformation_cache_refreshes)
120
 
{
121
 
    using namespace testing;
122
 
 
123
 
    const geom::Size sz{geom::Width{85}, geom::Height{43}};
124
 
    const geom::Rectangle origin{geom::Point{geom::X{77}, geom::Y{88}}, sz};
125
 
    const geom::Rectangle moved_pt{geom::Point{geom::X{55}, geom::Y{66}}, sz};
126
 
    ms::SurfaceData storage{name, origin, null_change_cb, false};
127
 
 
128
 
    glm::mat4 t0 = storage.transformation();
129
 
    storage.move_to(moved_pt.top_left);
130
 
    EXPECT_NE(t0, storage.transformation());
131
 
    storage.move_to(origin.top_left);
132
 
    EXPECT_EQ(t0, storage.transformation());
133
 
 
134
 
    storage.apply_rotation(60.0f, glm::vec3{0.0f, 0.0f, 1.0f});
135
 
    glm::mat4 t1 = storage.transformation();
136
 
    EXPECT_NE(t0, t1);
137
 
}
138
 
 
139
 
TEST_F(SurfaceDataTest, test_surface_set_alpha_notifies_changes)
140
 
{
141
 
    using namespace testing;
142
 
    EXPECT_CALL(mock_callback, call())
143
 
        .Times(1);
144
 
 
145
 
    ms::SurfaceData surface_state{name, rect, mock_change_cb, false};
146
 
 
147
 
    float alpha = 0.5f;
148
 
    surface_state.apply_alpha(0.5f);
149
 
    EXPECT_THAT(alpha, FloatEq(surface_state.alpha()));
150
 
}
151
 
 
152
 
TEST_F(SurfaceDataTest, test_surface_is_opaque_by_default)
153
 
{
154
 
    using namespace testing;
155
 
    ms::SurfaceData surface_state{name, rect, null_change_cb, false};
156
 
    EXPECT_THAT(1.0f, FloatEq(surface_state.alpha()));
157
 
    EXPECT_FALSE(surface_state.shaped());
158
 
}
159
 
 
160
 
TEST_F(SurfaceDataTest, test_surface_apply_rotation)
161
 
{
162
 
    EXPECT_CALL(mock_callback, call())
163
 
        .Times(1);
164
 
 
165
 
    ms::SurfaceData surface_state{name, rect, mock_change_cb, false};
166
 
    surface_state.apply_rotation(60.0f, glm::vec3{0.0f, 0.0f, 1.0f});
167
 
}
168
 
 
169
 
TEST_F(SurfaceDataTest, test_surface_should_be_rendered_in)
170
 
{
171
 
    ms::SurfaceData surface_state{name, rect, mock_change_cb, false};
172
 
    geom::Rectangle output_rect{geom::Point{0,0}, geom::Size{100, 100}};
173
 
 
174
 
    //not renderable by default
175
 
    EXPECT_FALSE(surface_state.should_be_rendered_in(rect));
176
 
 
177
 
    surface_state.set_hidden(false);
178
 
    //not renderable if no first frame has been posted by client, regardless of hide state
179
 
    EXPECT_FALSE(surface_state.should_be_rendered_in(output_rect));
180
 
    surface_state.set_hidden(true);
181
 
    EXPECT_FALSE(surface_state.should_be_rendered_in(output_rect));
182
 
 
183
 
    surface_state.frame_posted();
184
 
    EXPECT_FALSE(surface_state.should_be_rendered_in(output_rect));
185
 
 
186
 
    surface_state.set_hidden(false);
187
 
    EXPECT_TRUE(surface_state.should_be_rendered_in(output_rect));
188
 
 
189
 
    // Not renderable if not overlapping with supplied rect
190
 
    geom::Rectangle output_rect1{geom::Point{100,100}, geom::Size{100, 100}};
191
 
    EXPECT_FALSE(surface_state.should_be_rendered_in(output_rect1));
192
 
}
193
 
 
194
 
TEST_F(SurfaceDataTest, test_surface_hidden_notifies_changes)
195
 
{
196
 
    using namespace testing;
197
 
    EXPECT_CALL(mock_callback, call())
198
 
        .Times(1);
199
 
 
200
 
    ms::SurfaceData surface_state{name, rect, mock_change_cb, false};
201
 
    surface_state.set_hidden(true);
202
 
}
203
 
 
204
 
TEST_F(SurfaceDataTest, test_surface_frame_posted_notifies_changes)
205
 
{
206
 
    using namespace testing;
207
 
    EXPECT_CALL(mock_callback, call())
208
 
        .Times(1);
209
 
 
210
 
    ms::SurfaceData surface_state{name, rect, mock_change_cb, false};
211
 
    surface_state.frame_posted();
212
 
}
213
 
 
214
 
// a 1x1 window at (1,1) will get events at (1,1)
215
 
TEST_F(SurfaceDataTest, default_region_is_surface_rectangle)
216
 
{
217
 
    geom::Point pt(1,1);
218
 
    geom::Size one_by_one{geom::Width{1}, geom::Height{1}};
219
 
    ms::SurfaceData surface_state{name, geom::Rectangle{pt, one_by_one}, mock_change_cb, false};
220
 
 
221
 
    std::vector<geom::Point> contained_pt
222
 
    {
223
 
        geom::Point{geom::X{1}, geom::Y{1}}
224
 
    };
225
 
 
226
 
    for(auto x = 0; x <= 3; x++)
227
 
    {
228
 
        for(auto y = 0; y <= 3; y++)
229
 
        {
230
 
            auto test_pt = geom::Point{x, y};
231
 
            auto contains = surface_state.contains(test_pt);
232
 
            if (std::find(contained_pt.begin(), contained_pt.end(), test_pt) != contained_pt.end())
233
 
            {
234
 
                EXPECT_TRUE(contains);
235
 
            }
236
 
            else
237
 
            {
238
 
                EXPECT_FALSE(contains);
239
 
            }
240
 
        }
241
 
    }
242
 
}
243
 
 
244
 
TEST_F(SurfaceDataTest, set_input_region)
245
 
{
246
 
    std::vector<geom::Rectangle> const rectangles = {
247
 
        {{geom::X{0}, geom::Y{0}}, {geom::Width{1}, geom::Height{1}}}, //region0
248
 
        {{geom::X{1}, geom::Y{1}}, {geom::Width{1}, geom::Height{1}}}  //region1
249
 
    };
250
 
 
251
 
    ms::SurfaceData surface_state{name, rect, mock_change_cb, false};
252
 
    surface_state.set_input_region(rectangles);
253
 
 
254
 
    std::vector<geom::Point> contained_pt
255
 
    {
256
 
        //region0 points
257
 
        geom::Point{geom::X{0}, geom::Y{0}},
258
 
        //region1 points
259
 
        geom::Point{geom::X{1}, geom::Y{1}},
260
 
    };
261
 
 
262
 
    for(auto x = 0; x <= 3; x++)
263
 
    {
264
 
        for(auto y = 0; y <= 3; y++)
265
 
        {
266
 
            auto test_pt = geom::Point{x, y};
267
 
            auto contains = surface_state.contains(test_pt);
268
 
            if (std::find(contained_pt.begin(), contained_pt.end(), test_pt) != contained_pt.end())
269
 
            {
270
 
                EXPECT_TRUE(contains);
271
 
            }
272
 
            else
273
 
            {
274
 
                EXPECT_FALSE(contains);
275
 
            }
276
 
        }
277
 
    }
278
 
}