~gerboland/miral/gtest-bug-workaround

« back to all changes in this revision

Viewing changes to test/mru_window_list.cpp

  • Committer: Alan Griffiths
  • Date: 2016-05-31 16:45:44 UTC
  • mfrom: (174.1.2 miral)
  • Revision ID: alan@octopull.co.uk-20160531164544-yy2ym872438umhbj
Track the active window in an MRU list and use that to select the new active Window when the old one closes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as 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: Alan Griffiths <alan@octopull.co.uk>
 
17
 */
 
18
 
 
19
#include <miral/mru_window_list.h>
 
20
 
 
21
#include <mir/test/doubles/stub_surface.h>
 
22
#include <mir/test/doubles/stub_session.h>
 
23
 
 
24
#include <gtest/gtest.h>
 
25
#include <gmock/gmock.h>
 
26
 
 
27
using StubSurface = mir::test::doubles::StubSurface;
 
28
using namespace testing;
 
29
 
 
30
namespace
 
31
{
 
32
struct StubSession : mir::test::doubles::StubSession
 
33
{
 
34
    StubSession(int number_of_surfaces)
 
35
    {
 
36
        for (auto i = 0; i != number_of_surfaces; ++i)
 
37
            surfaces.push_back(std::make_shared<mir::test::doubles::StubSurface>());
 
38
    }
 
39
 
 
40
    std::shared_ptr<mir::scene::Surface> surface(mir::frontend::SurfaceId surface) const override
 
41
    {
 
42
        return surfaces.at(surface.as_value());
 
43
    }
 
44
 
 
45
    std::vector<std::shared_ptr<StubSurface>> surfaces;
 
46
};
 
47
 
 
48
MATCHER(IsNullWindow, std::string("is not null"))
 
49
{
 
50
    return !arg;
 
51
}
 
52
}
 
53
 
 
54
struct MRUWindowList : testing::Test
 
55
{
 
56
    miral::MRUWindowList mru_list;
 
57
 
 
58
    std::shared_ptr<StubSession> const stub_session{std::make_shared<StubSession>(3)};
 
59
    miral::Application app{stub_session};
 
60
    miral::Window window_a{app, mir::frontend::SurfaceId{0}};
 
61
    miral::Window window_b{app, mir::frontend::SurfaceId{1}};
 
62
    miral::Window window_c{app, mir::frontend::SurfaceId{2}};
 
63
};
 
64
 
 
65
TEST_F(MRUWindowList, when_created_is_empty)
 
66
{
 
67
    EXPECT_THAT(mru_list.top(), IsNullWindow());
 
68
}
 
69
 
 
70
TEST_F(MRUWindowList, given_empty_list_when_a_window_pushed_that_window_is_top)
 
71
{
 
72
    mru_list.push(window_a);
 
73
    EXPECT_THAT(mru_list.top(), Eq(window_a));
 
74
}
 
75
 
 
76
TEST_F(MRUWindowList, given_non_empty_list_when_a_window_pushed_that_window_is_top)
 
77
{
 
78
    mru_list.push(window_a);
 
79
    mru_list.push(window_b);
 
80
    mru_list.push(window_c);
 
81
    EXPECT_THAT(mru_list.top(), Eq(window_c));
 
82
}
 
83
 
 
84
TEST_F(MRUWindowList, given_non_empty_list_when_top_window_is_erased_that_window_is_no_longer_on_top)
 
85
{
 
86
    mru_list.push(window_a);
 
87
    mru_list.push(window_b);
 
88
    mru_list.push(window_c);
 
89
    mru_list.erase(window_c);
 
90
    EXPECT_THAT(mru_list.top(), Ne(window_c));
 
91
}
 
92
 
 
93
TEST_F(MRUWindowList, a_window_pushed_twice_is_not_enumerated_twice)
 
94
{
 
95
    mru_list.push(window_a);
 
96
    mru_list.push(window_b);
 
97
    mru_list.push(window_a);
 
98
 
 
99
    int count{0};
 
100
 
 
101
    mru_list.enumerate([&](miral::Window& window)
 
102
        { if (window == window_a) ++count; return true; });
 
103
 
 
104
    EXPECT_THAT(count, Eq(1));
 
105
}
 
106
 
 
107
TEST_F(MRUWindowList, after_multiple_pushes_windows_are_enumerated_in_mru_order)
 
108
{
 
109
    mru_list.push(window_a);
 
110
    mru_list.push(window_b);
 
111
    mru_list.push(window_c);
 
112
    mru_list.push(window_a);
 
113
    mru_list.push(window_b);
 
114
    mru_list.push(window_a);
 
115
 
 
116
    mru_list.push(window_c);
 
117
    mru_list.push(window_b);
 
118
    mru_list.push(window_a);
 
119
 
 
120
    std::vector<miral::Window> as_enumerated;
 
121
 
 
122
    mru_list.enumerate([&](miral::Window& window)
 
123
       { as_enumerated.push_back(window); return true; });
 
124
 
 
125
    EXPECT_THAT(as_enumerated, ElementsAre(window_a, window_b, window_c));
 
126
}
 
127
 
 
128
TEST_F(MRUWindowList, when_enumerator_returns_false_enumeration_is_short_circuited)
 
129
{
 
130
    mru_list.push(window_a);
 
131
    mru_list.push(window_b);
 
132
    mru_list.push(window_c);
 
133
 
 
134
    int count{0};
 
135
 
 
136
    mru_list.enumerate([&](miral::Window&) { ++count; return false; });
 
137
 
 
138
    EXPECT_THAT(count, Eq(1));
 
139
}