Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "athena/wm/window_list_provider_impl.h" | 5 #include "athena/wm/window_list_provider_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "athena/test/athena_test_base.h" | 9 #include "athena/test/athena_test_base.h" |
| 10 #include "athena/wm/public/window_list_provider_observer.h" | |
| 10 #include "ui/aura/test/test_window_delegate.h" | 11 #include "ui/aura/test/test_window_delegate.h" |
| 11 #include "ui/aura/window.h" | 12 #include "ui/aura/window.h" |
| 12 | 13 |
| 13 namespace athena { | 14 namespace athena { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 bool AreWindowListsEqual(const aura::Window::Windows& one, | 18 bool AreWindowListsEqual(const aura::Window::Windows& one, |
| 18 const aura::Window::Windows& two) { | 19 const aura::Window::Windows& two) { |
| 19 return one.size() == two.size() && | 20 return one.size() == two.size() && |
| 20 std::equal(one.begin(), one.end(), two.begin()); | 21 std::equal(one.begin(), one.end(), two.begin()); |
| 21 } | 22 } |
| 22 | 23 |
| 23 scoped_ptr<aura::Window> CreateWindow(aura::WindowDelegate* delegate, | 24 scoped_ptr<aura::Window> CreateWindow(aura::WindowDelegate* delegate, |
| 24 ui::wm::WindowType window_type) { | 25 ui::wm::WindowType window_type) { |
| 25 scoped_ptr<aura::Window> window(new aura::Window(delegate)); | 26 scoped_ptr<aura::Window> window(new aura::Window(delegate)); |
| 26 window->SetType(window_type); | 27 window->SetType(window_type); |
| 27 window->Init(aura::WINDOW_LAYER_SOLID_COLOR); | 28 window->Init(aura::WINDOW_LAYER_SOLID_COLOR); |
| 28 return window.Pass(); | 29 return window.Pass(); |
| 29 } | 30 } |
| 30 | 31 |
| 32 // Return a string which defines the order of windows in |now| using the indices | |
| 33 // of |original|. The string will then have the lowest/oldest window on the left | |
| 34 // and the highest / newest on the right. | |
| 35 std::string GetWindowOrder(const aura::Window::Windows& original, | |
| 36 const aura::Window::Windows& now) { | |
| 37 if (original.size() != now.size()) | |
| 38 return "size has changed."; | |
| 39 std::string output; | |
| 40 for (aura::Window::Windows::const_iterator it = now.begin(); | |
| 41 it != now.end(); ++it) { | |
| 42 for (size_t i = 0; i < original.size(); i++) { | |
| 43 if ((*it) == original[i]) { | |
| 44 output += (output.size() ? " " : std::string()) + | |
| 45 std::to_string(i + 1); | |
|
Dan Beam
2014/12/11 22:28:32
are we allowed to use std::to_string() in chromium
| |
| 46 break; | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 return output; | |
| 51 } | |
| 52 | |
| 53 class WindowListObserver : public WindowListProviderObserver { | |
| 54 public: | |
| 55 explicit WindowListObserver(WindowListProvider* provider) | |
| 56 : calls_(0), | |
| 57 provider_(provider) { | |
| 58 provider_->AddObserver(this); | |
| 59 } | |
| 60 virtual ~WindowListObserver() { | |
| 61 provider_->RemoveObserver(this); | |
| 62 } | |
| 63 | |
| 64 int calls() const { return calls_; } | |
| 65 | |
| 66 // WindowListProviderObserver: | |
| 67 virtual void OnWindowStackingChanged() OVERRIDE { | |
| 68 calls_++; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 // The number of calls to the observer. | |
| 73 int calls_; | |
| 74 | |
| 75 // The associated WindowListProvider which is observed. | |
| 76 WindowListProvider* provider_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(WindowListObserver); | |
| 79 }; | |
| 80 | |
| 81 | |
| 31 } // namespace | 82 } // namespace |
| 32 | 83 |
| 33 typedef test::AthenaTestBase WindowListProviderImplTest; | 84 typedef test::AthenaTestBase WindowListProviderImplTest; |
| 34 | 85 |
| 35 // Tests that the order of windows match the stacking order of the windows in | 86 // Tests that the order of windows match the stacking order of the windows in |
| 36 // the container, even after the order is changed through the aura Window API. | 87 // the container, even after the order is changed through the aura Window API. |
| 37 TEST_F(WindowListProviderImplTest, StackingOrder) { | 88 TEST_F(WindowListProviderImplTest, StackingOrder) { |
| 38 aura::test::TestWindowDelegate delegate; | 89 aura::test::TestWindowDelegate delegate; |
| 39 scoped_ptr<aura::Window> container(new aura::Window(&delegate)); | 90 scoped_ptr<aura::Window> container(new aura::Window(&delegate)); |
| 40 scoped_ptr<aura::Window> first = | 91 scoped_ptr<aura::Window> first = |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 51 new WindowListProviderImpl(container.get())); | 102 new WindowListProviderImpl(container.get())); |
| 52 EXPECT_TRUE(AreWindowListsEqual(container->children(), | 103 EXPECT_TRUE(AreWindowListsEqual(container->children(), |
| 53 list_provider->GetWindowList())); | 104 list_provider->GetWindowList())); |
| 54 | 105 |
| 55 container->StackChildAtTop(first.get()); | 106 container->StackChildAtTop(first.get()); |
| 56 EXPECT_TRUE(AreWindowListsEqual(container->children(), | 107 EXPECT_TRUE(AreWindowListsEqual(container->children(), |
| 57 list_provider->GetWindowList())); | 108 list_provider->GetWindowList())); |
| 58 EXPECT_EQ(first.get(), container->children().back()); | 109 EXPECT_EQ(first.get(), container->children().back()); |
| 59 } | 110 } |
| 60 | 111 |
| 112 // Tests that only normal windows of the associated container will be listed. | |
| 61 TEST_F(WindowListProviderImplTest, ListContainsOnlyNormalWindows) { | 113 TEST_F(WindowListProviderImplTest, ListContainsOnlyNormalWindows) { |
| 62 aura::test::TestWindowDelegate delegate; | 114 aura::test::TestWindowDelegate delegate; |
| 63 scoped_ptr<aura::Window> container(new aura::Window(&delegate)); | 115 scoped_ptr<aura::Window> container(new aura::Window(&delegate)); |
| 64 scoped_ptr<aura::Window> first = | 116 scoped_ptr<aura::Window> first = |
| 65 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); | 117 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); |
| 66 scoped_ptr<aura::Window> second = | 118 scoped_ptr<aura::Window> second = |
| 67 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_POPUP); | 119 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_POPUP); |
| 68 scoped_ptr<aura::Window> third = | 120 scoped_ptr<aura::Window> third = |
| 69 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); | 121 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); |
| 70 scoped_ptr<aura::Window> fourth = | 122 scoped_ptr<aura::Window> fourth = |
| 71 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_MENU); | 123 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_MENU); |
| 72 container->AddChild(first.get()); | 124 container->AddChild(first.get()); |
| 73 container->AddChild(second.get()); | 125 container->AddChild(second.get()); |
| 74 container->AddChild(third.get()); | 126 container->AddChild(third.get()); |
| 75 container->AddChild(fourth.get()); | 127 container->AddChild(fourth.get()); |
| 76 | 128 |
| 77 scoped_ptr<WindowListProvider> list_provider( | 129 scoped_ptr<WindowListProvider> list_provider( |
| 78 new WindowListProviderImpl(container.get())); | 130 new WindowListProviderImpl(container.get())); |
| 131 | |
| 79 const aura::Window::Windows list = list_provider->GetWindowList(); | 132 const aura::Window::Windows list = list_provider->GetWindowList(); |
| 80 EXPECT_EQ(list.end(), std::find(list.begin(), list.end(), second.get())); | 133 EXPECT_EQ(list.end(), std::find(list.begin(), list.end(), second.get())); |
| 81 EXPECT_EQ(list.end(), std::find(list.begin(), list.end(), fourth.get())); | 134 EXPECT_EQ(list.end(), std::find(list.begin(), list.end(), fourth.get())); |
| 82 EXPECT_NE(list.end(), std::find(list.begin(), list.end(), first.get())); | 135 EXPECT_NE(list.end(), std::find(list.begin(), list.end(), first.get())); |
| 83 EXPECT_NE(list.end(), std::find(list.begin(), list.end(), third.get())); | 136 EXPECT_NE(list.end(), std::find(list.begin(), list.end(), third.get())); |
| 84 } | 137 } |
| 85 | 138 |
| 139 // Testing that IsValidWidow, IsWindowInList and AddWindow work as expected. | |
| 140 TEST_F(WindowListProviderImplTest, SimpleChecks) { | |
| 141 aura::test::TestWindowDelegate delegate; | |
| 142 scoped_ptr<aura::Window> container(new aura::Window(&delegate)); | |
| 143 | |
| 144 scoped_ptr<aura::Window> normal_window = | |
| 145 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); | |
| 146 scoped_ptr<aura::Window> popup_window = | |
| 147 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_POPUP); | |
| 148 scoped_ptr<aura::Window> menu_window = | |
| 149 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_MENU); | |
| 150 | |
| 151 scoped_ptr<WindowListProvider> list_provider( | |
| 152 new WindowListProviderImpl(container.get())); | |
| 153 | |
| 154 // Check which windows are valid and which are not. | |
| 155 EXPECT_TRUE(list_provider->IsValidWindow(normal_window.get())); | |
| 156 EXPECT_FALSE(list_provider->IsValidWindow(popup_window.get())); | |
| 157 EXPECT_FALSE(list_provider->IsValidWindow(menu_window.get())); | |
| 158 | |
| 159 // Check that no window is currently in the list. | |
| 160 EXPECT_FALSE(list_provider->IsWindowInList(normal_window.get())); | |
| 161 EXPECT_FALSE(list_provider->IsWindowInList(popup_window.get())); | |
| 162 EXPECT_FALSE(list_provider->IsWindowInList(menu_window.get())); | |
| 163 | |
| 164 // Check that adding the window will add it to the list. | |
| 165 container->AddChild(normal_window.get()); | |
| 166 EXPECT_TRUE(list_provider->IsWindowInList(normal_window.get())); | |
| 167 } | |
| 168 | |
| 169 // Testing that window ordering functions work as expected. | |
| 170 TEST_F(WindowListProviderImplTest, TestWindowOrderingFunctions) { | |
| 171 aura::test::TestWindowDelegate delegate; | |
| 172 scoped_ptr<aura::Window> container(new aura::Window(&delegate)); | |
| 173 | |
| 174 scoped_ptr<aura::Window> window1 = | |
| 175 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); | |
| 176 scoped_ptr<aura::Window> window2 = | |
| 177 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); | |
| 178 scoped_ptr<aura::Window> window3 = | |
| 179 CreateWindow(&delegate, ui::wm::WINDOW_TYPE_NORMAL); | |
| 180 | |
| 181 scoped_ptr<WindowListProvider> list_provider( | |
| 182 new WindowListProviderImpl(container.get())); | |
| 183 scoped_ptr<WindowListObserver> observer( | |
| 184 new WindowListObserver(list_provider.get())); | |
| 185 | |
| 186 EXPECT_FALSE(list_provider->IsWindowInList(window1.get())); | |
| 187 EXPECT_FALSE(list_provider->IsWindowInList(window2.get())); | |
| 188 EXPECT_FALSE(list_provider->IsWindowInList(window3.get())); | |
| 189 | |
| 190 // Add the windows. | |
| 191 container->AddChild(window1.get()); | |
| 192 container->AddChild(window2.get()); | |
| 193 container->AddChild(window3.get()); | |
| 194 aura::Window::Windows original_order = list_provider->GetWindowList(); | |
| 195 ASSERT_EQ(3U, original_order.size()); | |
| 196 EXPECT_EQ(original_order[0], window1.get()); | |
| 197 EXPECT_EQ(original_order[1], window2.get()); | |
| 198 EXPECT_EQ(original_order[2], window3.get()); | |
| 199 | |
| 200 EXPECT_EQ(0, observer.get()->calls()); | |
| 201 | |
| 202 // Move 2 to the front. | |
| 203 list_provider->MoveToFront(window2.get()); | |
| 204 EXPECT_EQ("1 3 2", GetWindowOrder(original_order, | |
| 205 list_provider->GetWindowList())); | |
| 206 EXPECT_EQ(1, observer->calls()); | |
| 207 | |
| 208 // Move 2 to the front again. Should not change anything. | |
| 209 list_provider->MoveToFront(window2.get()); | |
| 210 EXPECT_EQ("1 3 2", GetWindowOrder(original_order, | |
| 211 list_provider->GetWindowList())); | |
| 212 EXPECT_EQ(1, observer->calls()); | |
| 213 | |
| 214 // Move 1 (from the back) in front of 3. | |
| 215 list_provider->StackWindowFrontOf(window1.get(), window3.get()); | |
| 216 EXPECT_EQ("3 1 2", GetWindowOrder(original_order, | |
| 217 list_provider->GetWindowList())); | |
| 218 EXPECT_EQ(2, observer->calls()); | |
| 219 | |
| 220 // Move 2 (from the front) in front of 3. | |
| 221 list_provider->StackWindowFrontOf(window2.get(), window3.get()); | |
| 222 EXPECT_EQ("3 2 1", GetWindowOrder(original_order, | |
| 223 list_provider->GetWindowList())); | |
| 224 EXPECT_EQ(3, observer->calls()); | |
| 225 | |
| 226 // Move 1 (from the front) behind 3. | |
| 227 list_provider->StackWindowBehindTo(window1.get(), window3.get()); | |
| 228 EXPECT_EQ("1 3 2", GetWindowOrder(original_order, | |
| 229 list_provider->GetWindowList())); | |
| 230 EXPECT_EQ(4, observer->calls()); | |
| 231 | |
| 232 // Move 1 (from the back) in front of 2. | |
| 233 list_provider->StackWindowFrontOf(window1.get(), window2.get()); | |
| 234 EXPECT_EQ("3 2 1", GetWindowOrder(original_order, | |
| 235 list_provider->GetWindowList())); | |
| 236 EXPECT_EQ(5, observer->calls()); | |
| 237 | |
| 238 // Test that no change should also report no call. | |
| 239 list_provider->StackWindowFrontOf(window1.get(), window2.get()); | |
| 240 EXPECT_EQ("3 2 1", GetWindowOrder(original_order, | |
| 241 list_provider->GetWindowList())); | |
| 242 EXPECT_EQ(5, observer->calls()); | |
| 243 list_provider->StackWindowBehindTo(window2.get(), window1.get()); | |
| 244 EXPECT_EQ("3 2 1", GetWindowOrder(original_order, | |
| 245 list_provider->GetWindowList())); | |
| 246 EXPECT_EQ(5, observer->calls()); | |
| 247 } | |
| 248 | |
| 86 } // namespace athena | 249 } // namespace athena |
| OLD | NEW |