2
* Copyright © 2016 Canonical Ltd.
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.
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.
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/>.
16
* Authored by: Alan Griffiths <alan@octopull.co.uk>
19
#include <miral/mru_window_list.h>
21
#include <mir/test/doubles/stub_surface.h>
22
#include <mir/test/doubles/stub_session.h>
24
#include <gtest/gtest.h>
25
#include <gmock/gmock.h>
27
using StubSurface = mir::test::doubles::StubSurface;
28
using namespace testing;
32
struct StubSession : mir::test::doubles::StubSession
34
StubSession(int number_of_surfaces)
36
for (auto i = 0; i != number_of_surfaces; ++i)
37
surfaces.push_back(std::make_shared<mir::test::doubles::StubSurface>());
40
std::shared_ptr<mir::scene::Surface> surface(mir::frontend::SurfaceId surface) const override
42
return surfaces.at(surface.as_value());
45
std::vector<std::shared_ptr<StubSurface>> surfaces;
48
MATCHER(IsNullWindow, std::string("is not null"))
54
struct MRUWindowList : testing::Test
56
miral::MRUWindowList mru_list;
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}};
65
TEST_F(MRUWindowList, when_created_is_empty)
67
EXPECT_THAT(mru_list.top(), IsNullWindow());
70
TEST_F(MRUWindowList, given_empty_list_when_a_window_pushed_that_window_is_top)
72
mru_list.push(window_a);
73
EXPECT_THAT(mru_list.top(), Eq(window_a));
76
TEST_F(MRUWindowList, given_non_empty_list_when_a_window_pushed_that_window_is_top)
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));
84
TEST_F(MRUWindowList, given_non_empty_list_when_top_window_is_erased_that_window_is_no_longer_on_top)
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));
93
TEST_F(MRUWindowList, a_window_pushed_twice_is_not_enumerated_twice)
95
mru_list.push(window_a);
96
mru_list.push(window_b);
97
mru_list.push(window_a);
101
mru_list.enumerate([&](miral::Window& window)
102
{ if (window == window_a) ++count; return true; });
104
EXPECT_THAT(count, Eq(1));
107
TEST_F(MRUWindowList, after_multiple_pushes_windows_are_enumerated_in_mru_order)
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);
116
mru_list.push(window_c);
117
mru_list.push(window_b);
118
mru_list.push(window_a);
120
std::vector<miral::Window> as_enumerated;
122
mru_list.enumerate([&](miral::Window& window)
123
{ as_enumerated.push_back(window); return true; });
125
EXPECT_THAT(as_enumerated, ElementsAre(window_a, window_b, window_c));
128
TEST_F(MRUWindowList, when_enumerator_returns_false_enumeration_is_short_circuited)
130
mru_list.push(window_a);
131
mru_list.push(window_b);
132
mru_list.push(window_c);
136
mru_list.enumerate([&](miral::Window&) { ++count; return false; });
138
EXPECT_THAT(count, Eq(1));