OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/web_contents/aura/native_view_screen_bounds_observer.h
" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/aura/root_window.h" |
| 10 #include "ui/aura/test/aura_test_base.h" |
| 11 #include "ui/aura/window.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 class NativeViewScreenBoundsObserverDelegateStub |
| 16 : public NativeViewScreenBoundsObserverDelegate { |
| 17 public: |
| 18 NativeViewScreenBoundsObserverDelegateStub() : position_changed_count_(0), |
| 19 bounds_changed_count_(0) { |
| 20 } |
| 21 virtual ~NativeViewScreenBoundsObserverDelegateStub() { } |
| 22 |
| 23 void ClearCounts() { |
| 24 position_changed_count_ = 0; |
| 25 bounds_changed_count_ = 0; |
| 26 } |
| 27 |
| 28 int GetPositionChangedCount() { |
| 29 return position_changed_count_; |
| 30 } |
| 31 |
| 32 int GetBoundsChangedCount() { |
| 33 return bounds_changed_count_; |
| 34 } |
| 35 |
| 36 // Overriden from NativeViewScreenBoundsObserverDelegate |
| 37 virtual void OnScreenPositionChanged() OVERRIDE { |
| 38 position_changed_count_++; |
| 39 } |
| 40 virtual void OnScreenBoundsChanged() OVERRIDE { |
| 41 bounds_changed_count_++; |
| 42 } |
| 43 |
| 44 private: |
| 45 int position_changed_count_; |
| 46 int bounds_changed_count_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(NativeViewScreenBoundsObserverDelegateStub); |
| 49 }; |
| 50 |
| 51 class NativeViewScreenBoundsObserverTest : public aura::test::AuraTestBase { |
| 52 public: |
| 53 NativeViewScreenBoundsObserverTest() {} |
| 54 virtual ~NativeViewScreenBoundsObserverTest() { } |
| 55 |
| 56 virtual void TearDown() { |
| 57 observer_.reset(); |
| 58 aura::test::AuraTestBase::TearDown(); |
| 59 } |
| 60 |
| 61 bool IsWindowInSet(aura::Window* window, std::set<aura::Window*> windows) { |
| 62 return (windows.end() != windows.find(window)); |
| 63 } |
| 64 |
| 65 bool IsWindowObserved(aura::Window* window) { |
| 66 return window && window->HasObserver(observer_.get()); |
| 67 } |
| 68 |
| 69 std::set<aura::Window*> observed() { return observer_->observed_; } |
| 70 |
| 71 std::set<aura::Window*> CollectAllWindowsToRoot(aura::Window* window) { |
| 72 return observer_->CollectAllWindowsToRoot(window); |
| 73 } |
| 74 |
| 75 void AddToObserved(const std::set<aura::Window*>& windows) { |
| 76 observer_->AddToObserved(windows); |
| 77 } |
| 78 |
| 79 void RemoveFromObserved(const std::set<aura::Window*>& windows) { |
| 80 observer_->RemoveFromObserved(windows); |
| 81 } |
| 82 |
| 83 scoped_ptr<NativeViewScreenBoundsObserver> observer_; |
| 84 NativeViewScreenBoundsObserverDelegateStub delegate_; |
| 85 }; |
| 86 |
| 87 TEST_F(NativeViewScreenBoundsObserverTest, Creation) { |
| 88 // observed count is the number of windows created + 1 due to the root_window |
| 89 // being at the top. |
| 90 aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
| 91 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 92 delegate_window)); |
| 93 EXPECT_EQ(2u, observed().size()); |
| 94 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 95 |
| 96 aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 97 parent_window->AddChild(delegate_window); |
| 98 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 99 delegate_window)); |
| 100 EXPECT_EQ(3u, observed().size()); |
| 101 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 102 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 103 |
| 104 aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
| 105 grandparent_window->AddChild(parent_window); |
| 106 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 107 delegate_window)); |
| 108 EXPECT_EQ(4u, observed().size()); |
| 109 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 110 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 111 EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
| 112 } |
| 113 |
| 114 TEST_F(NativeViewScreenBoundsObserverTest, OnWindowParentChanged) { |
| 115 aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
| 116 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 117 delegate_window)); |
| 118 EXPECT_EQ(2u, observed().size()); |
| 119 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 120 |
| 121 aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 122 parent_window->AddChild(delegate_window); |
| 123 EXPECT_EQ(3u, observed().size()); |
| 124 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 125 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 126 |
| 127 aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
| 128 grandparent_window->AddChild(parent_window); |
| 129 EXPECT_EQ(4u, observed().size()); |
| 130 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 131 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 132 EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
| 133 |
| 134 aura::Window* new_parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 135 new_parent_window->AddChild(delegate_window); |
| 136 EXPECT_EQ(3u, observed().size()); |
| 137 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 138 EXPECT_FALSE(IsWindowObserved(parent_window)); |
| 139 EXPECT_FALSE(IsWindowObserved(grandparent_window)); |
| 140 EXPECT_TRUE(IsWindowObserved(new_parent_window)); |
| 141 |
| 142 grandparent_window->AddChild(new_parent_window); |
| 143 EXPECT_EQ(4u, observed().size()); |
| 144 EXPECT_TRUE(IsWindowObserved(delegate_window)); |
| 145 EXPECT_FALSE(IsWindowObserved(parent_window)); |
| 146 EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
| 147 EXPECT_TRUE(IsWindowObserved(new_parent_window)); |
| 148 } |
| 149 |
| 150 TEST_F(NativeViewScreenBoundsObserverTest, OnWindowBoundsChanged) { |
| 151 gfx::Rect base_bounds(0, 0, 400, 400); |
| 152 gfx::Rect resized_bounds(0, 0, 200, 200); |
| 153 gfx::Rect moved_bounds(100, 100, 400, 400); |
| 154 |
| 155 aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
| 156 delegate_window->SetBounds(base_bounds); |
| 157 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 158 delegate_window)); |
| 159 delegate_.ClearCounts(); |
| 160 delegate_window->SetBounds(resized_bounds); |
| 161 EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
| 162 EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
| 163 |
| 164 delegate_window->SetBounds(base_bounds); |
| 165 delegate_.ClearCounts(); |
| 166 delegate_window->SetBounds(moved_bounds); |
| 167 EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
| 168 EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
| 169 |
| 170 aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 171 parent_window->SetBounds(base_bounds); |
| 172 parent_window->AddChild(delegate_window); |
| 173 delegate_.ClearCounts(); |
| 174 parent_window->SetBounds(resized_bounds); |
| 175 EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
| 176 EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
| 177 |
| 178 parent_window->SetBounds(base_bounds); |
| 179 delegate_.ClearCounts(); |
| 180 parent_window->SetBounds(moved_bounds); |
| 181 EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
| 182 EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
| 183 |
| 184 aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
| 185 grandparent_window->SetBounds(base_bounds); |
| 186 grandparent_window->AddChild(delegate_window); |
| 187 delegate_.ClearCounts(); |
| 188 grandparent_window->SetBounds(resized_bounds); |
| 189 EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
| 190 EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
| 191 |
| 192 grandparent_window->SetBounds(base_bounds); |
| 193 delegate_.ClearCounts(); |
| 194 grandparent_window->SetBounds(moved_bounds); |
| 195 EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
| 196 EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
| 197 |
| 198 // Testing that changing the hiearchy gets handled correctly wrt to calling |
| 199 // OnBounds/PositionChanged. |
| 200 aura::Window* new_parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 201 new_parent_window->SetBounds(base_bounds); |
| 202 new_parent_window->AddChild(delegate_window); |
| 203 grandparent_window->AddChild(new_parent_window); |
| 204 delegate_.ClearCounts(); |
| 205 new_parent_window->SetBounds(resized_bounds); |
| 206 EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
| 207 EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
| 208 |
| 209 new_parent_window->SetBounds(base_bounds); |
| 210 delegate_.ClearCounts(); |
| 211 new_parent_window->SetBounds(moved_bounds); |
| 212 EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
| 213 EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
| 214 |
| 215 parent_window->SetBounds(base_bounds); |
| 216 delegate_.ClearCounts(); |
| 217 parent_window->SetBounds(resized_bounds); |
| 218 EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
| 219 EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
| 220 |
| 221 parent_window->SetBounds(base_bounds); |
| 222 delegate_.ClearCounts(); |
| 223 parent_window->SetBounds(moved_bounds); |
| 224 EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
| 225 EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
| 226 } |
| 227 |
| 228 TEST_F(NativeViewScreenBoundsObserverTest, CollectAllWindowsToRoot) { |
| 229 // size is the number of windows created + 1 due to the root_window being at |
| 230 // the top. |
| 231 aura::Window* child_window = CreateNormalWindow(0, root_window(), NULL); |
| 232 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, child_window)); |
| 233 std::set<aura::Window*> windows_to_root; |
| 234 windows_to_root = CollectAllWindowsToRoot(child_window); |
| 235 EXPECT_EQ(2u, windows_to_root.size()); |
| 236 EXPECT_TRUE(IsWindowInSet(child_window, windows_to_root)); |
| 237 |
| 238 aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 239 parent_window->AddChild(child_window); |
| 240 windows_to_root = CollectAllWindowsToRoot(child_window); |
| 241 EXPECT_EQ(3u, windows_to_root.size()); |
| 242 EXPECT_TRUE(IsWindowInSet(child_window, windows_to_root)); |
| 243 EXPECT_TRUE(IsWindowInSet(parent_window, windows_to_root)); |
| 244 |
| 245 aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
| 246 grandparent_window->AddChild(parent_window); |
| 247 windows_to_root = CollectAllWindowsToRoot(child_window); |
| 248 EXPECT_EQ(4u, windows_to_root.size()); |
| 249 EXPECT_TRUE(IsWindowInSet(child_window, windows_to_root)); |
| 250 EXPECT_TRUE(IsWindowInSet(parent_window, windows_to_root)); |
| 251 EXPECT_TRUE(IsWindowInSet(grandparent_window, windows_to_root)); |
| 252 } |
| 253 |
| 254 TEST_F(NativeViewScreenBoundsObserverTest, AddToObserved) { |
| 255 // size is the naive expectation + 2 due to the root_window being the parent |
| 256 // of the delegate window and neither of those getting removed through out the |
| 257 // test. size is the number of windows created + 1 due to the root_window |
| 258 // being the parent of the created windows and it getting added to the |
| 259 // observed set. |
| 260 aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
| 261 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 262 delegate_window)); |
| 263 |
| 264 std::set<aura::Window*> windows; |
| 265 aura::Window* child_window = CreateNormalWindow(0, root_window(), NULL); |
| 266 windows.insert(child_window); |
| 267 AddToObserved(windows); |
| 268 EXPECT_EQ(3u, observed().size()); |
| 269 EXPECT_TRUE(IsWindowObserved(child_window)); |
| 270 |
| 271 aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 272 windows.clear(); |
| 273 windows.insert(parent_window); |
| 274 AddToObserved(windows); |
| 275 EXPECT_EQ(4u, observed().size()); |
| 276 EXPECT_TRUE(IsWindowObserved(child_window)); |
| 277 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 278 |
| 279 aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
| 280 windows.clear(); |
| 281 windows.insert(grandparent_window); |
| 282 AddToObserved(windows); |
| 283 EXPECT_EQ(5u, observed().size()); |
| 284 EXPECT_TRUE(IsWindowObserved(child_window)); |
| 285 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 286 EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
| 287 } |
| 288 |
| 289 TEST_F(NativeViewScreenBoundsObserverTest, RemoveFromObserved) { |
| 290 // size is the naive expectation + 2 due to the root_window being the |
| 291 // parent of the delegate window and neither of those getting removed through |
| 292 // out the test. |
| 293 aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
| 294 observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
| 295 delegate_window)); |
| 296 |
| 297 aura::Window* child_window = CreateNormalWindow(0, root_window(), NULL); |
| 298 aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
| 299 aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
| 300 std::set<aura::Window*> base_windows; |
| 301 base_windows.insert(child_window); |
| 302 base_windows.insert(parent_window); |
| 303 base_windows.insert(grandparent_window); |
| 304 |
| 305 std::set<aura::Window*> removed_windows; |
| 306 AddToObserved(base_windows); |
| 307 RemoveFromObserved(removed_windows); |
| 308 EXPECT_EQ(5u, observed().size()); |
| 309 EXPECT_TRUE(IsWindowObserved(child_window)); |
| 310 EXPECT_TRUE(IsWindowObserved(parent_window)); |
| 311 EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
| 312 |
| 313 removed_windows = base_windows; |
| 314 RemoveFromObserved(removed_windows); |
| 315 EXPECT_EQ(2u, observed().size()); |
| 316 EXPECT_FALSE(IsWindowObserved(child_window)); |
| 317 EXPECT_FALSE(IsWindowObserved(parent_window)); |
| 318 EXPECT_FALSE(IsWindowObserved(grandparent_window)); |
| 319 |
| 320 AddToObserved(base_windows); |
| 321 removed_windows.clear(); |
| 322 removed_windows.insert(parent_window); |
| 323 RemoveFromObserved(removed_windows); |
| 324 EXPECT_EQ(4u, observed().size()); |
| 325 EXPECT_TRUE(IsWindowObserved(child_window)); |
| 326 EXPECT_FALSE(IsWindowObserved(parent_window)); |
| 327 EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
| 328 } |
| 329 |
| 330 } // namespace content |
OLD | NEW |