~alan-griffiths/miral/basic-Unity8-compatibility

« back to all changes in this revision

Viewing changes to test/active_window.cpp

  • Committer: Alan Griffiths
  • Date: 2016-11-29 17:20:28 UTC
  • mfrom: (459.2.3 miral1)
  • Revision ID: alan@octopull.co.uk-20161129172028-9dr4beujixyu6wg4
[libmiral] tests of active window logic

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <gtest/gtest.h>
30
30
 
31
31
using namespace testing;
 
32
using namespace miral;
32
33
using namespace miral::toolkit;
33
34
using namespace std::chrono_literals;
34
35
 
59
60
    mir::test::Signal signal;
60
61
};
61
62
 
62
 
struct ActiveWindow : public miral::TestServer
 
63
struct ActiveWindow : public TestServer
63
64
{
64
65
    FocusChangeSync sync1;
65
66
    FocusChangeSync sync2;
79
80
        return surface;
80
81
    }
81
82
 
 
83
    auto create_tip(Connection const& connection, char const* name, Surface const& parent, FocusChangeSync& sync) -> Surface
 
84
    {
 
85
        MirRectangle aux_rect{10, 10, 10, 10};
 
86
        auto const spec = SurfaceSpec::for_tip(connection, 50, 50, mir_pixel_format_argb_8888, parent, &aux_rect, mir_edge_attachment_any)
 
87
            .set_buffer_usage(mir_buffer_usage_software)
 
88
            .set_event_handler(&FocusChangeSync::raise_signal_on_focus_change, &sync)
 
89
            .set_name(name);
 
90
 
 
91
        Surface const surface{spec.create_surface()};
 
92
 
 
93
        // Expect this to timeout: A tip should not receive focus
 
94
        sync.exec([&]{ mir_buffer_stream_swap_buffers_sync(mir_surface_get_buffer_stream(surface)); });
 
95
        EXPECT_FALSE(sync.signal_raised());
 
96
 
 
97
        return surface;
 
98
    }
 
99
 
 
100
    auto create_dialog(Connection const& connection, char const* name, Surface const& parent, FocusChangeSync& sync) -> Surface
 
101
    {
 
102
        auto const spec = SurfaceSpec::for_dialog(connection, 50, 50, mir_pixel_format_argb_8888, parent)
 
103
            .set_buffer_usage(mir_buffer_usage_software)
 
104
            .set_event_handler(&FocusChangeSync::raise_signal_on_focus_change, &sync)
 
105
            .set_name(name);
 
106
 
 
107
        Surface const surface{spec.create_surface()};
 
108
 
 
109
        sync.exec([&]{ mir_buffer_stream_swap_buffers_sync(mir_surface_get_buffer_stream(surface)); });
 
110
        EXPECT_TRUE(sync.signal_raised());
 
111
 
 
112
        return surface;
 
113
    }
 
114
 
82
115
    void assert_no_active_window()
83
116
    {
84
 
        invoke_tools([&](miral::WindowManagerTools& tools)
 
117
        invoke_tools([&](WindowManagerTools& tools)
85
118
            {
86
119
                auto const window = tools.active_window();
87
120
                ASSERT_FALSE(window);
90
123
 
91
124
    void assert_active_window_is(char const* const name)
92
125
    {
93
 
        invoke_tools([&](miral::WindowManagerTools& tools)
 
126
        invoke_tools([&](WindowManagerTools& tools)
94
127
            {
95
128
                 auto const window = tools.active_window();
96
129
                 ASSERT_TRUE(window);
177
210
    EXPECT_THAT(sync2.signal_raised(), Eq(false));
178
211
    assert_active_window_is(test_name);
179
212
}
 
213
 
 
214
TEST_F(ActiveWindow, switching_from_a_second_window_makes_first_active)
 
215
{
 
216
    char const* const test_name = __PRETTY_FUNCTION__;
 
217
    auto const connection = connect_client(test_name);
 
218
 
 
219
    auto const first_surface = create_surface(connection, test_name, sync1);
 
220
    auto const surface = create_surface(connection, "second", sync2);
 
221
 
 
222
    sync1.exec([&]{ invoke_tools([](WindowManagerTools& tools){ tools.focus_next_within_application(); }); });
 
223
 
 
224
    EXPECT_TRUE(sync1.signal_raised());
 
225
    assert_active_window_is(test_name);
 
226
}
 
227
 
 
228
TEST_F(ActiveWindow, switching_from_a_second_application_makes_first_active)
 
229
{
 
230
    char const* const test_name = __PRETTY_FUNCTION__;
 
231
    auto const connection = connect_client(test_name);
 
232
    auto const second_connection = connect_client("second");
 
233
 
 
234
    auto const first_surface = create_surface(connection, test_name, sync1);
 
235
    auto const surface = create_surface(second_connection, "second", sync2);
 
236
 
 
237
    sync1.exec([&]{ invoke_tools([](WindowManagerTools& tools){ tools.focus_next_application(); }); });
 
238
 
 
239
    EXPECT_TRUE(sync1.signal_raised());
 
240
    assert_active_window_is(test_name);
 
241
}
 
242
 
 
243
TEST_F(ActiveWindow, closing_a_second_application_makes_first_active)
 
244
{
 
245
    char const* const test_name = __PRETTY_FUNCTION__;
 
246
    auto const connection = connect_client(test_name);
 
247
    auto second_connection = connect_client("second");
 
248
 
 
249
    auto const first_surface = create_surface(connection, test_name, sync1);
 
250
    auto surface = create_surface(second_connection, "second", sync2);
 
251
 
 
252
    sync1.exec([&]{ surface.reset(); second_connection.reset(); });
 
253
 
 
254
    EXPECT_TRUE(sync1.signal_raised());
 
255
    assert_active_window_is(test_name);
 
256
}
 
257
 
 
258
TEST_F(ActiveWindow, selecting_a_tip_makes_parent_active)
 
259
{
 
260
    char const* const test_name = __PRETTY_FUNCTION__;
 
261
    auto const connection = connect_client(test_name);
 
262
 
 
263
    auto const parent = create_surface(connection, test_name, sync1);
 
264
 
 
265
    Window parent_window;
 
266
    invoke_tools([&](WindowManagerTools& tools){ parent_window = tools.active_window(); });
 
267
 
 
268
    // Steal the focus
 
269
    auto second_connection = connect_client("second");
 
270
    auto second_surface = create_surface(second_connection, "second", sync2);
 
271
 
 
272
    auto const tip = create_tip(connection, "tip", parent, sync2);
 
273
 
 
274
    sync1.exec([&]
 
275
        {
 
276
            invoke_tools([&](WindowManagerTools& tools)
 
277
                { tools.select_active_window(*tools.info_for(parent_window).children().begin()); });
 
278
        });
 
279
    EXPECT_TRUE(sync1.signal_raised());
 
280
 
 
281
    assert_active_window_is(test_name);
 
282
}
 
283
 
 
284
TEST_F(ActiveWindow, selecting_a_parent_makes_dialog_active)
 
285
{
 
286
    char const* const test_name = __PRETTY_FUNCTION__;
 
287
    auto const connection = connect_client(test_name);
 
288
 
 
289
    auto const parent = create_surface(connection, test_name, sync1);
 
290
 
 
291
    Window parent_window;
 
292
    invoke_tools([&](WindowManagerTools& tools){ parent_window = tools.active_window(); });
 
293
 
 
294
    auto const dialog = create_dialog(connection, "dialog", parent, sync2);
 
295
 
 
296
    // Steal the focus
 
297
    auto second_connection = connect_client("second");
 
298
    auto second_surface = create_surface(second_connection, "second", sync1);
 
299
 
 
300
    sync2.exec([&]{ invoke_tools([&](WindowManagerTools& tools){ tools.select_active_window(parent_window); }); });
 
301
 
 
302
    EXPECT_TRUE(sync2.signal_raised());
 
303
    assert_active_window_is("dialog");
 
304
}