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

« back to all changes in this revision

Viewing changes to tests/unit-tests/graphics/android/test_android_fb.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
ImportĀ upstreamĀ versionĀ 0.8.0+14.10.20141010

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/graphics/display_buffer.h"
20
 
#include "mir/graphics/display_configuration.h"
21
 
#include "mir/logging/logger.h"
22
 
#include "src/platform/graphics/android/android_display.h"
23
 
#include "src/server/report/null_report_factory.h"
24
 
#include "mir_test_doubles/mock_display_report.h"
25
 
#include "mir_test_doubles/mock_display_device.h"
26
 
#include "mir_test_doubles/mock_egl.h"
27
 
#include "mir_test_doubles/stub_display_buffer.h"
28
 
#include "mir_test_doubles/stub_display_builder.h"
29
 
#include "mir_test_doubles/stub_gl_config.h"
30
 
#include "mir_test_doubles/mock_gl_config.h"
31
 
#include "mir_test_doubles/stub_gl_program_factory.h"
32
 
#include "mir/graphics/android/mir_native_window.h"
33
 
#include "mir_test_doubles/stub_driver_interpreter.h"
34
 
 
35
 
#include <gtest/gtest.h>
36
 
#include <memory>
37
 
#include <stdexcept>
38
 
#include <unordered_set>
39
 
#include <algorithm>
40
 
 
41
 
namespace mg=mir::graphics;
42
 
namespace mga=mir::graphics::android;
43
 
namespace mtd=mir::test::doubles;
44
 
namespace geom=mir::geometry;
45
 
 
46
 
class AndroidDisplay : public ::testing::Test
47
 
{
48
 
public:
49
 
    AndroidDisplay()
50
 
        : dummy_display{mock_egl.fake_egl_display},
51
 
          dummy_context{mock_egl.fake_egl_context},
52
 
          dummy_config{mock_egl.fake_configs[0]},
53
 
          null_display_report{mir::report::null_display_report()},
54
 
          stub_db_factory{std::make_shared<mtd::StubDisplayBuilder>()},
55
 
          stub_gl_config{std::make_shared<mtd::StubGLConfig>()},
56
 
          stub_gl_program_factory{std::make_shared<mtd::StubGLProgramFactory>()}
57
 
    {
58
 
    }
59
 
 
60
 
protected:
61
 
    testing::NiceMock<mtd::MockEGL> mock_egl;
62
 
    EGLDisplay const dummy_display;
63
 
    EGLContext const dummy_context;
64
 
    EGLConfig const dummy_config;
65
 
 
66
 
    std::shared_ptr<mg::DisplayReport> const null_display_report;
67
 
    std::shared_ptr<mtd::StubDisplayBuilder> const stub_db_factory;
68
 
    std::shared_ptr<mtd::StubGLConfig> const stub_gl_config;
69
 
    std::shared_ptr<mtd::StubGLProgramFactory> const stub_gl_program_factory;
70
 
};
71
 
 
72
 
TEST_F(AndroidDisplay, creation_creates_egl_resources_properly)
73
 
{
74
 
    using namespace testing;
75
 
    EGLSurface fake_surface = (EGLSurface) 0x715;
76
 
    EGLint const expected_pbuffer_attr[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
77
 
    EGLint const expected_context_attr[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
78
 
 
79
 
    //on constrution
80
 
    EXPECT_CALL(mock_egl, eglGetDisplay(EGL_DEFAULT_DISPLAY))
81
 
        .Times(1)
82
 
        .WillOnce(Return(dummy_display));
83
 
    EXPECT_CALL(mock_egl, eglInitialize(dummy_display, _, _))
84
 
        .Times(1)
85
 
        .WillOnce(DoAll(SetArgPointee<1>(1), SetArgPointee<2>(4), Return(EGL_TRUE)));
86
 
    EXPECT_CALL(mock_egl, eglCreateContext(
87
 
        dummy_display, _, EGL_NO_CONTEXT, mtd::AttrMatches(expected_context_attr)))
88
 
        .Times(1)
89
 
        .WillOnce(Return(dummy_context));
90
 
    EXPECT_CALL(mock_egl, eglCreatePbufferSurface(
91
 
        dummy_display, _, mtd::AttrMatches(expected_pbuffer_attr)))
92
 
        .Times(1)
93
 
        .WillOnce(Return(fake_surface));
94
 
    EXPECT_CALL(mock_egl, eglMakeCurrent(dummy_display, fake_surface, fake_surface, dummy_context))
95
 
        .Times(1);
96
 
 
97
 
    //on destruction
98
 
    EXPECT_CALL(mock_egl, eglGetCurrentContext())
99
 
        .Times(1)
100
 
        .WillOnce(Return(dummy_context));
101
 
    EXPECT_CALL(mock_egl, eglMakeCurrent(dummy_display,EGL_NO_SURFACE,EGL_NO_SURFACE,EGL_NO_CONTEXT))
102
 
        .Times(1);
103
 
    EXPECT_CALL(mock_egl, eglDestroySurface(dummy_display, fake_surface))
104
 
        .Times(1);
105
 
    EXPECT_CALL(mock_egl, eglDestroyContext(dummy_display, dummy_context))
106
 
        .Times(1);
107
 
    EXPECT_CALL(mock_egl, eglTerminate(dummy_display))
108
 
        .Times(1);
109
 
 
110
 
    mga::AndroidDisplay display(
111
 
        stub_db_factory,
112
 
        stub_gl_program_factory,
113
 
        stub_gl_config,
114
 
        null_display_report);
115
 
}
116
 
 
117
 
TEST_F(AndroidDisplay, selects_usable_configuration)
118
 
{
119
 
    using namespace testing;
120
 
    int const incorrect_visual_id = 2;
121
 
    int const correct_visual_id = 1;
122
 
    EGLint const num_cfgs = 45;
123
 
    EGLint const expected_cfg_attr [] = {
124
 
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
125
 
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
126
 
        EGL_DEPTH_SIZE, 0,
127
 
        EGL_STENCIL_SIZE, 0,
128
 
        EGL_NONE };
129
 
    EGLConfig selected_config;
130
 
    EGLConfig cfgs[45];
131
 
    for(auto i = 0; i < num_cfgs; i++)
132
 
        cfgs[i] = reinterpret_cast<EGLConfig>(i);
133
 
    int config_to_select_index = 37;
134
 
    EGLConfig correct_config = cfgs[config_to_select_index];
135
 
 
136
 
    ON_CALL(mock_egl, eglGetConfigAttrib(_, _, EGL_NATIVE_VISUAL_ID, _))
137
 
        .WillByDefault(DoAll(SetArgPointee<3>(incorrect_visual_id), Return(EGL_TRUE)));
138
 
    ON_CALL(mock_egl, eglGetConfigAttrib(dummy_display, correct_config, EGL_NATIVE_VISUAL_ID,_))
139
 
        .WillByDefault(DoAll(SetArgPointee<3>(correct_visual_id), Return(EGL_TRUE)));
140
 
    ON_CALL(mock_egl, eglCreateContext(_,_,_,_))
141
 
        .WillByDefault(DoAll(SaveArg<1>(&selected_config),Return(dummy_context)));
142
 
 
143
 
    auto config_filler = [&]
144
 
    (EGLDisplay, EGLint const*, EGLConfig* out_cfgs, EGLint, EGLint* out_num_cfgs) -> EGLBoolean
145
 
    {
146
 
        memcpy(out_cfgs, cfgs, sizeof(EGLConfig) * num_cfgs);
147
 
        *out_num_cfgs = num_cfgs;
148
 
        return EGL_TRUE;
149
 
    };
150
 
 
151
 
    EXPECT_CALL(mock_egl, eglGetConfigs(dummy_display, NULL, 0, _))
152
 
        .Times(1)
153
 
        .WillOnce(DoAll(SetArgPointee<3>(num_cfgs), Return(EGL_TRUE)));
154
 
    EXPECT_CALL(mock_egl, eglChooseConfig(dummy_display, mtd::AttrMatches(expected_cfg_attr),_,num_cfgs,_))
155
 
        .Times(1)
156
 
        .WillOnce(Invoke(config_filler));
157
 
 
158
 
    mga::AndroidDisplay display(
159
 
        stub_db_factory,
160
 
        stub_gl_program_factory,
161
 
        stub_gl_config,
162
 
        null_display_report);
163
 
    EXPECT_EQ(correct_config, selected_config);
164
 
}
165
 
 
166
 
TEST_F(AndroidDisplay, respects_gl_config)
167
 
{
168
 
    using namespace testing;
169
 
 
170
 
    auto const mock_gl_config = std::make_shared<mtd::MockGLConfig>();
171
 
    EGLint const depth_bits{24};
172
 
    EGLint const stencil_bits{8};
173
 
 
174
 
    EXPECT_CALL(*mock_gl_config, depth_buffer_bits())
175
 
        .WillOnce(Return(depth_bits));
176
 
    EXPECT_CALL(*mock_gl_config, stencil_buffer_bits())
177
 
        .WillOnce(Return(stencil_bits));
178
 
 
179
 
    EXPECT_CALL(mock_egl,
180
 
                eglChooseConfig(
181
 
                    _,
182
 
                    AllOf(mtd::EGLConfigContainsAttrib(EGL_DEPTH_SIZE, depth_bits),
183
 
                          mtd::EGLConfigContainsAttrib(EGL_STENCIL_SIZE, stencil_bits)),
184
 
                    _,_,_));
185
 
 
186
 
    mga::AndroidDisplay display(
187
 
        stub_db_factory,
188
 
        stub_gl_program_factory,
189
 
        mock_gl_config,
190
 
        null_display_report);
191
 
}
192
 
 
193
 
TEST_F(AndroidDisplay, logs_creation_events)
194
 
{
195
 
    using namespace testing;
196
 
 
197
 
    auto const mock_display_report = std::make_shared<mtd::MockDisplayReport>();
198
 
 
199
 
    EXPECT_CALL(*mock_display_report, report_successful_setup_of_native_resources())
200
 
        .Times(1);
201
 
    EXPECT_CALL(*mock_display_report, report_egl_configuration(_,_))
202
 
        .Times(1);
203
 
    EXPECT_CALL(*mock_display_report, report_successful_egl_make_current_on_construction())
204
 
        .Times(1);
205
 
    EXPECT_CALL(*mock_display_report, report_successful_display_construction())
206
 
        .Times(1);
207
 
 
208
 
    mga::AndroidDisplay display(
209
 
        stub_db_factory,
210
 
        stub_gl_program_factory,
211
 
        stub_gl_config,
212
 
        mock_display_report);
213
 
}
214
 
 
215
 
TEST_F(AndroidDisplay, throws_on_eglMakeCurrent_failure)
216
 
{
217
 
    using namespace testing;
218
 
 
219
 
    auto const mock_display_report = std::make_shared<NiceMock<mtd::MockDisplayReport>>();
220
 
 
221
 
    EXPECT_CALL(*mock_display_report, report_successful_setup_of_native_resources())
222
 
        .Times(1);
223
 
    EXPECT_CALL(mock_egl, eglMakeCurrent(dummy_display, _, _, _))
224
 
        .Times(1)
225
 
        .WillOnce(Return(EGL_FALSE));
226
 
    EXPECT_CALL(*mock_display_report, report_successful_egl_make_current_on_construction())
227
 
        .Times(0);
228
 
    EXPECT_CALL(*mock_display_report, report_successful_display_construction())
229
 
        .Times(0);
230
 
 
231
 
    EXPECT_THROW({
232
 
        mga::AndroidDisplay display(
233
 
            stub_db_factory,
234
 
            stub_gl_program_factory,
235
 
            stub_gl_config,
236
 
            mock_display_report);
237
 
    }, std::runtime_error);
238
 
}
239
 
 
240
 
TEST_F(AndroidDisplay, logs_error_because_of_surface_creation_failure)
241
 
{
242
 
    using namespace testing;
243
 
 
244
 
    auto const mock_display_report = std::make_shared<mtd::MockDisplayReport>();
245
 
 
246
 
    EXPECT_CALL(*mock_display_report, report_successful_setup_of_native_resources())
247
 
        .Times(0);
248
 
    EXPECT_CALL(*mock_display_report, report_successful_egl_make_current_on_construction())
249
 
        .Times(0);
250
 
    EXPECT_CALL(*mock_display_report, report_successful_display_construction())
251
 
        .Times(0);
252
 
 
253
 
    EXPECT_CALL(mock_egl, eglCreatePbufferSurface(_,_,_))
254
 
        .Times(1)
255
 
        .WillOnce(Return(EGL_NO_SURFACE));
256
 
 
257
 
    EXPECT_THROW({
258
 
        mga::AndroidDisplay display(
259
 
            stub_db_factory,
260
 
            stub_gl_program_factory,
261
 
            stub_gl_config,
262
 
            mock_display_report);
263
 
    }, std::runtime_error);
264
 
}
265
 
 
266
 
TEST_F(AndroidDisplay, configures_display_buffer)
267
 
{
268
 
    using namespace testing;
269
 
    mga::AndroidDisplay display(
270
 
        stub_db_factory,
271
 
        stub_gl_program_factory,
272
 
        stub_gl_config,
273
 
        null_display_report);
274
 
 
275
 
    auto configuration = display.configuration();
276
 
    configuration->for_each_output([&](mg::UserDisplayConfigurationOutput& output)
277
 
    {
278
 
        output.power_mode = mir_power_mode_on;
279
 
    });
280
 
    display.configure(*configuration);
281
 
 
282
 
    configuration->for_each_output([&](mg::UserDisplayConfigurationOutput& output)
283
 
    {
284
 
        output.power_mode = mir_power_mode_standby;
285
 
    });
286
 
    display.configure(*configuration);
287
 
 
288
 
    configuration->for_each_output([&](mg::UserDisplayConfigurationOutput& output)
289
 
    {
290
 
        output.power_mode = mir_power_mode_off;
291
 
    });
292
 
    display.configure(*configuration);
293
 
 
294
 
    configuration->for_each_output([&](mg::UserDisplayConfigurationOutput& output)
295
 
    {
296
 
        output.power_mode = mir_power_mode_suspend;
297
 
    });
298
 
    display.configure(*configuration);
299
 
}
300
 
 
301
 
//we only have single display and single mode on android for the time being
302
 
TEST_F(AndroidDisplay, supports_one_output_configuration)
303
 
{
304
 
    mga::AndroidDisplay display(
305
 
        stub_db_factory,
306
 
        stub_gl_program_factory,
307
 
        stub_gl_config,
308
 
        null_display_report);
309
 
    auto config = display.configuration();
310
 
 
311
 
    size_t num_configs = 0;
312
 
    config->for_each_output([&](mg::DisplayConfigurationOutput const&)
313
 
    {
314
 
        num_configs++;
315
 
    });
316
 
 
317
 
    EXPECT_EQ(1u, num_configs);
318
 
}