Index: content/browser/web_contents/aura/native_view_screen_bounds_observer_unittest.cc |
diff --git a/content/browser/web_contents/aura/native_view_screen_bounds_observer_unittest.cc b/content/browser/web_contents/aura/native_view_screen_bounds_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8bec17875189e84cf2482fd66aeb8c6045bf8712 |
--- /dev/null |
+++ b/content/browser/web_contents/aura/native_view_screen_bounds_observer_unittest.cc |
@@ -0,0 +1,330 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/web_contents/aura/native_view_screen_bounds_observer.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/aura/root_window.h" |
+#include "ui/aura/test/aura_test_base.h" |
+#include "ui/aura/window.h" |
+ |
+namespace content { |
+ |
+class NativeViewScreenBoundsObserverDelegateStub |
+ : public NativeViewScreenBoundsObserverDelegate { |
+ public: |
+ NativeViewScreenBoundsObserverDelegateStub() : position_changed_count_(0), |
+ bounds_changed_count_(0) { |
+ } |
+ virtual ~NativeViewScreenBoundsObserverDelegateStub() { } |
+ |
+ void ClearCounts() { |
+ position_changed_count_ = 0; |
+ bounds_changed_count_ = 0; |
+ } |
+ |
+ int GetPositionChangedCount() { |
+ return position_changed_count_; |
+ } |
+ |
+ int GetBoundsChangedCount() { |
+ return bounds_changed_count_; |
+ } |
+ |
+ // Overriden from NativeViewScreenBoundsObserverDelegate |
+ virtual void OnScreenPositionChanged() OVERRIDE { |
+ position_changed_count_++; |
+ } |
+ virtual void OnScreenBoundsChanged() OVERRIDE { |
+ bounds_changed_count_++; |
+ } |
+ |
+ private: |
+ int position_changed_count_; |
+ int bounds_changed_count_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(NativeViewScreenBoundsObserverDelegateStub); |
+}; |
+ |
+class NativeViewScreenBoundsObserverTest : public aura::test::AuraTestBase { |
+ public: |
+ NativeViewScreenBoundsObserverTest() {} |
+ virtual ~NativeViewScreenBoundsObserverTest() { } |
+ |
+ virtual void TearDown() { |
+ observer_.reset(); |
+ aura::test::AuraTestBase::TearDown(); |
+ } |
+ |
+ bool IsWindowInSet(aura::Window* window, std::set<aura::Window*> windows) { |
+ return (windows.end() != windows.find(window)); |
+ } |
+ |
+ bool IsWindowObserved(aura::Window* window) { |
+ return window && window->HasObserver(observer_.get()); |
+ } |
+ |
+ std::set<aura::Window*> observed() { return observer_->observed_; } |
+ |
+ std::set<aura::Window*> CollectAllWindowsToRoot(aura::Window* window) { |
+ return observer_->CollectAllWindowsToRoot(window); |
+ } |
+ |
+ void AddToObserved(const std::set<aura::Window*>& windows) { |
+ observer_->AddToObserved(windows); |
+ } |
+ |
+ void RemoveFromObserved(const std::set<aura::Window*>& windows) { |
+ observer_->RemoveFromObserved(windows); |
+ } |
+ |
+ scoped_ptr<NativeViewScreenBoundsObserver> observer_; |
+ NativeViewScreenBoundsObserverDelegateStub delegate_; |
+}; |
+ |
+TEST_F(NativeViewScreenBoundsObserverTest, Creation) { |
+ // observed count is the number of windows created + 1 due to the root_window |
+ // being at the top. |
+ aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ EXPECT_EQ(2u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ |
+ aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ parent_window->AddChild(delegate_window); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ EXPECT_EQ(3u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ |
+ aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
+ grandparent_window->AddChild(parent_window); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ EXPECT_EQ(4u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
+} |
+ |
+TEST_F(NativeViewScreenBoundsObserverTest, OnWindowParentChanged) { |
+ aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ EXPECT_EQ(2u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ |
+ aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ parent_window->AddChild(delegate_window); |
+ EXPECT_EQ(3u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ |
+ aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
+ grandparent_window->AddChild(parent_window); |
+ EXPECT_EQ(4u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
+ |
+ aura::Window* new_parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ new_parent_window->AddChild(delegate_window); |
+ EXPECT_EQ(3u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ EXPECT_FALSE(IsWindowObserved(parent_window)); |
+ EXPECT_FALSE(IsWindowObserved(grandparent_window)); |
+ EXPECT_TRUE(IsWindowObserved(new_parent_window)); |
+ |
+ grandparent_window->AddChild(new_parent_window); |
+ EXPECT_EQ(4u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(delegate_window)); |
+ EXPECT_FALSE(IsWindowObserved(parent_window)); |
+ EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
+ EXPECT_TRUE(IsWindowObserved(new_parent_window)); |
+} |
+ |
+TEST_F(NativeViewScreenBoundsObserverTest, OnWindowBoundsChanged) { |
+ gfx::Rect base_bounds(0, 0, 400, 400); |
+ gfx::Rect resized_bounds(0, 0, 200, 200); |
+ gfx::Rect moved_bounds(100, 100, 400, 400); |
+ |
+ aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
+ delegate_window->SetBounds(base_bounds); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ delegate_.ClearCounts(); |
+ delegate_window->SetBounds(resized_bounds); |
+ EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
+ |
+ delegate_window->SetBounds(base_bounds); |
+ delegate_.ClearCounts(); |
+ delegate_window->SetBounds(moved_bounds); |
+ EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
+ |
+ aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ parent_window->SetBounds(base_bounds); |
+ parent_window->AddChild(delegate_window); |
+ delegate_.ClearCounts(); |
+ parent_window->SetBounds(resized_bounds); |
+ EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
+ |
+ parent_window->SetBounds(base_bounds); |
+ delegate_.ClearCounts(); |
+ parent_window->SetBounds(moved_bounds); |
+ EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
+ |
+ aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
+ grandparent_window->SetBounds(base_bounds); |
+ grandparent_window->AddChild(delegate_window); |
+ delegate_.ClearCounts(); |
+ grandparent_window->SetBounds(resized_bounds); |
+ EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
+ |
+ grandparent_window->SetBounds(base_bounds); |
+ delegate_.ClearCounts(); |
+ grandparent_window->SetBounds(moved_bounds); |
+ EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
+ |
+ // Testing that changing the hiearchy gets handled correctly wrt to calling |
+ // OnBounds/PositionChanged. |
+ aura::Window* new_parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ new_parent_window->SetBounds(base_bounds); |
+ new_parent_window->AddChild(delegate_window); |
+ grandparent_window->AddChild(new_parent_window); |
+ delegate_.ClearCounts(); |
+ new_parent_window->SetBounds(resized_bounds); |
+ EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(1, delegate_.GetBoundsChangedCount()); |
+ |
+ new_parent_window->SetBounds(base_bounds); |
+ delegate_.ClearCounts(); |
+ new_parent_window->SetBounds(moved_bounds); |
+ EXPECT_EQ(1, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
+ |
+ parent_window->SetBounds(base_bounds); |
+ delegate_.ClearCounts(); |
+ parent_window->SetBounds(resized_bounds); |
+ EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
+ |
+ parent_window->SetBounds(base_bounds); |
+ delegate_.ClearCounts(); |
+ parent_window->SetBounds(moved_bounds); |
+ EXPECT_EQ(0, delegate_.GetPositionChangedCount()); |
+ EXPECT_EQ(0, delegate_.GetBoundsChangedCount()); |
+} |
+ |
+TEST_F(NativeViewScreenBoundsObserverTest, CollectAllWindowsToRoot) { |
+ // size is the number of windows created + 1 due to the root_window being at |
+ // the top. |
+ aura::Window* child_window = CreateNormalWindow(0, root_window(), NULL); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, child_window)); |
+ std::set<aura::Window*> windows_to_root; |
+ windows_to_root = CollectAllWindowsToRoot(child_window); |
+ EXPECT_EQ(2u, windows_to_root.size()); |
+ EXPECT_TRUE(IsWindowInSet(child_window, windows_to_root)); |
+ |
+ aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ parent_window->AddChild(child_window); |
+ windows_to_root = CollectAllWindowsToRoot(child_window); |
+ EXPECT_EQ(3u, windows_to_root.size()); |
+ EXPECT_TRUE(IsWindowInSet(child_window, windows_to_root)); |
+ EXPECT_TRUE(IsWindowInSet(parent_window, windows_to_root)); |
+ |
+ aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
+ grandparent_window->AddChild(parent_window); |
+ windows_to_root = CollectAllWindowsToRoot(child_window); |
+ EXPECT_EQ(4u, windows_to_root.size()); |
+ EXPECT_TRUE(IsWindowInSet(child_window, windows_to_root)); |
+ EXPECT_TRUE(IsWindowInSet(parent_window, windows_to_root)); |
+ EXPECT_TRUE(IsWindowInSet(grandparent_window, windows_to_root)); |
+} |
+ |
+TEST_F(NativeViewScreenBoundsObserverTest, AddToObserved) { |
+ // size is the naive expectation + 2 due to the root_window being the parent |
+ // of the delegate window and neither of those getting removed through out the |
+ // test. size is the number of windows created + 1 due to the root_window |
+ // being the parent of the created windows and it getting added to the |
+ // observed set. |
+ aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ |
+ std::set<aura::Window*> windows; |
+ aura::Window* child_window = CreateNormalWindow(0, root_window(), NULL); |
+ windows.insert(child_window); |
+ AddToObserved(windows); |
+ EXPECT_EQ(3u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(child_window)); |
+ |
+ aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ windows.clear(); |
+ windows.insert(parent_window); |
+ AddToObserved(windows); |
+ EXPECT_EQ(4u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(child_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ |
+ aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
+ windows.clear(); |
+ windows.insert(grandparent_window); |
+ AddToObserved(windows); |
+ EXPECT_EQ(5u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(child_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
+} |
+ |
+TEST_F(NativeViewScreenBoundsObserverTest, RemoveFromObserved) { |
+ // size is the naive expectation + 2 due to the root_window being the |
+ // parent of the delegate window and neither of those getting removed through |
+ // out the test. |
+ aura::Window* delegate_window = CreateNormalWindow(0, root_window(), NULL); |
+ observer_.reset(new NativeViewScreenBoundsObserver(&delegate_, |
+ delegate_window)); |
+ |
+ aura::Window* child_window = CreateNormalWindow(0, root_window(), NULL); |
+ aura::Window* parent_window = CreateNormalWindow(0, root_window(), NULL); |
+ aura::Window* grandparent_window = CreateNormalWindow(0, root_window(), NULL); |
+ std::set<aura::Window*> base_windows; |
+ base_windows.insert(child_window); |
+ base_windows.insert(parent_window); |
+ base_windows.insert(grandparent_window); |
+ |
+ std::set<aura::Window*> removed_windows; |
+ AddToObserved(base_windows); |
+ RemoveFromObserved(removed_windows); |
+ EXPECT_EQ(5u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(child_window)); |
+ EXPECT_TRUE(IsWindowObserved(parent_window)); |
+ EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
+ |
+ removed_windows = base_windows; |
+ RemoveFromObserved(removed_windows); |
+ EXPECT_EQ(2u, observed().size()); |
+ EXPECT_FALSE(IsWindowObserved(child_window)); |
+ EXPECT_FALSE(IsWindowObserved(parent_window)); |
+ EXPECT_FALSE(IsWindowObserved(grandparent_window)); |
+ |
+ AddToObserved(base_windows); |
+ removed_windows.clear(); |
+ removed_windows.insert(parent_window); |
+ RemoveFromObserved(removed_windows); |
+ EXPECT_EQ(4u, observed().size()); |
+ EXPECT_TRUE(IsWindowObserved(child_window)); |
+ EXPECT_FALSE(IsWindowObserved(parent_window)); |
+ EXPECT_TRUE(IsWindowObserved(grandparent_window)); |
+} |
+ |
+} // namespace content |