Index: ui/views/widget/native_widget_mac_unittest.mm |
diff --git a/ui/views/widget/native_widget_mac_unittest.mm b/ui/views/widget/native_widget_mac_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9a86e4af3e996b04fe194f0b5815b8100fb2d4c4 |
--- /dev/null |
+++ b/ui/views/widget/native_widget_mac_unittest.mm |
@@ -0,0 +1,197 @@ |
+// Copyright 2014 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. |
+ |
+#import "ui/views/widget/native_widget_mac.h" |
+ |
+#import <Cocoa/Cocoa.h> |
+ |
+#include "ui/views/test/test_widget_observer.h" |
+#include "ui/views/test/widget_test.h" |
+ |
+namespace views { |
+namespace test { |
+ |
+// Tests for parts of NativeWidgetMac not covered by BridgedNativeWidget, which |
+// need access to Cocoa APIs. |
+typedef WidgetTest NativeWidgetMacTest; |
+ |
+class WidgetChangeObserver : public TestWidgetObserver { |
+ public: |
+ WidgetChangeObserver(Widget* widget) |
+ : TestWidgetObserver(widget), |
+ bounds_changed_count_(0), |
+ gained_visible_count_(0), |
+ lost_visible_count_(0) {} |
+ |
+ gfx::Rect last_bounds_when_changed() const { |
+ return last_bounds_when_changed_; |
+ } |
+ int bounds_changed_count() const { return bounds_changed_count_; } |
+ int gained_visible_count() const { return gained_visible_count_; } |
+ int lost_visible_count() const { return lost_visible_count_; } |
+ |
+ private: |
+ // WidgetObserver: |
+ virtual void OnWidgetVisibilityChanged(Widget* widget, |
+ bool visible) OVERRIDE { |
+ ++(visible ? gained_visible_count_ : lost_visible_count_); |
+ } |
+ |
+ virtual void OnWidgetBoundsChanged(Widget* widget, |
+ const gfx::Rect& new_bounds) OVERRIDE { |
+ ++bounds_changed_count_; |
+ last_bounds_when_changed_ = new_bounds; |
+ } |
+ |
+ gfx::Rect last_bounds_when_changed_; |
+ int bounds_changed_count_; |
+ |
+ int gained_visible_count_; |
+ int lost_visible_count_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WidgetChangeObserver); |
+}; |
+ |
+// Test visibility states triggered externally. |
+TEST_F(NativeWidgetMacTest, HideAndShowExternally) { |
+ Widget* widget = CreateTopLevelPlatformWidget(); |
+ NSWindow* ns_window = widget->GetNativeWindow(); |
+ WidgetChangeObserver observer(widget); |
+ |
+ // Should initially be hidden. |
+ EXPECT_FALSE(widget->IsVisible()); |
+ EXPECT_FALSE([ns_window isVisible]); |
+ EXPECT_EQ(0, observer.gained_visible_count()); |
+ EXPECT_EQ(0, observer.lost_visible_count()); |
+ |
+ widget->Show(); |
+ EXPECT_TRUE(widget->IsVisible()); |
+ EXPECT_TRUE([ns_window isVisible]); |
+ EXPECT_EQ(1, observer.gained_visible_count()); |
+ EXPECT_EQ(0, observer.lost_visible_count()); |
+ |
+ widget->Hide(); |
+ EXPECT_FALSE(widget->IsVisible()); |
+ EXPECT_FALSE([ns_window isVisible]); |
+ EXPECT_EQ(1, observer.gained_visible_count()); |
+ EXPECT_EQ(1, observer.lost_visible_count()); |
+ |
+ widget->Show(); |
+ EXPECT_TRUE(widget->IsVisible()); |
+ EXPECT_TRUE([ns_window isVisible]); |
+ EXPECT_EQ(2, observer.gained_visible_count()); |
+ EXPECT_EQ(1, observer.lost_visible_count()); |
+ |
+ // Test when hiding individual windows. |
+ [ns_window orderOut:nil]; |
+ EXPECT_FALSE(widget->IsVisible()); |
+ EXPECT_FALSE([ns_window isVisible]); |
+ EXPECT_EQ(2, observer.gained_visible_count()); |
+ EXPECT_EQ(2, observer.lost_visible_count()); |
+ |
+ [ns_window orderFront:nil]; |
+ EXPECT_TRUE(widget->IsVisible()); |
+ EXPECT_TRUE([ns_window isVisible]); |
+ EXPECT_EQ(3, observer.gained_visible_count()); |
+ EXPECT_EQ(2, observer.lost_visible_count()); |
+ |
+ // Test when hiding the entire application. This doesn't send an orderOut: |
+ // to the NSWindow. |
+ [NSApp hide:nil]; |
+ EXPECT_FALSE(widget->IsVisible()); |
+ EXPECT_FALSE([ns_window isVisible]); |
+ EXPECT_EQ(3, observer.gained_visible_count()); |
+ EXPECT_EQ(3, observer.lost_visible_count()); |
+ |
+ [NSApp unhideWithoutActivation]; |
+ EXPECT_TRUE(widget->IsVisible()); |
+ EXPECT_TRUE([ns_window isVisible]); |
+ EXPECT_EQ(4, observer.gained_visible_count()); |
+ EXPECT_EQ(3, observer.lost_visible_count()); |
+ |
+ // Hide again to test unhiding with an activation. |
+ [NSApp hide:nil]; |
+ EXPECT_EQ(4, observer.lost_visible_count()); |
+ [NSApp unhide:nil]; |
+ EXPECT_EQ(5, observer.gained_visible_count()); |
+ |
+ // No change when closing. |
+ widget->CloseNow(); |
+ EXPECT_EQ(4, observer.lost_visible_count()); |
+ EXPECT_EQ(5, observer.gained_visible_count()); |
+} |
+ |
+// Test minimized states triggered externally, implied visibility and restored |
+// bounds whilst minimized. |
+TEST_F(NativeWidgetMacTest, MiniaturizeExternally) { |
+ Widget* widget = CreateTopLevelPlatformWidget(); |
+ NSWindow* ns_window = widget->GetNativeWindow(); |
+ WidgetChangeObserver observer(widget); |
+ |
+ widget->Show(); |
+ EXPECT_EQ(1, observer.gained_visible_count()); |
+ EXPECT_EQ(0, observer.lost_visible_count()); |
+ const gfx::Rect restored_bounds = widget->GetRestoredBounds(); |
+ EXPECT_FALSE(restored_bounds.IsEmpty()); |
+ |
+ [ns_window miniaturize:nil]; |
+ |
+ EXPECT_TRUE(widget->IsMinimized()); |
+ EXPECT_FALSE(widget->IsVisible()); |
+ EXPECT_EQ(1, observer.gained_visible_count()); |
+ EXPECT_EQ(1, observer.lost_visible_count()); |
+ EXPECT_EQ(restored_bounds, widget->GetRestoredBounds()); |
+ |
+ [ns_window deminiaturize:nil]; |
+ EXPECT_FALSE(widget->IsMinimized()); |
+ EXPECT_TRUE(widget->IsVisible()); |
+ EXPECT_EQ(2, observer.gained_visible_count()); |
+ EXPECT_EQ(1, observer.lost_visible_count()); |
+ EXPECT_EQ(restored_bounds, widget->GetRestoredBounds()); |
+ |
+ EXPECT_FALSE([ns_window isMiniaturized]); |
+ widget->Minimize(); |
+ EXPECT_TRUE(widget->IsMinimized()); |
+ EXPECT_TRUE([ns_window isMiniaturized]); |
+ EXPECT_EQ(2, observer.gained_visible_count()); |
+ EXPECT_EQ(2, observer.lost_visible_count()); |
+ EXPECT_EQ(restored_bounds, widget->GetRestoredBounds()); |
+ |
+ widget->Restore(); // If miniaturized, should deminiaturize. |
+ EXPECT_FALSE(widget->IsMinimized()); |
+ EXPECT_FALSE([ns_window isMiniaturized]); |
+ EXPECT_EQ(3, observer.gained_visible_count()); |
+ EXPECT_EQ(2, observer.lost_visible_count()); |
+ EXPECT_EQ(restored_bounds, widget->GetRestoredBounds()); |
+ |
+ widget->Restore(); // If not miniaturized, does nothing. |
+ EXPECT_FALSE(widget->IsMinimized()); |
+ EXPECT_FALSE([ns_window isMiniaturized]); |
+ EXPECT_EQ(3, observer.gained_visible_count()); |
+ EXPECT_EQ(2, observer.lost_visible_count()); |
+ EXPECT_EQ(restored_bounds, widget->GetRestoredBounds()); |
+} |
+ |
+// Test moving and resizing the window externally. |
+TEST_F(NativeWidgetMacTest, MoveAndResizeExternally) { |
+ Widget* widget = new Widget; // Note: will be owned by NativeWidgetMac. |
+ |
+ // Start observing early. |
+ WidgetChangeObserver observer(widget); |
+ widget->Init(Widget::InitParams()); |
+ |
+ EXPECT_EQ(0, observer.bounds_changed_count()); |
+ widget->Show(); |
+ |
+ // Should be one change when going on screen. |
+ EXPECT_EQ(1, observer.bounds_changed_count()); |
+ |
+ // Make sure it hasn't changed since. |
+ EXPECT_EQ(observer.last_bounds_when_changed(), |
+ widget->GetWindowBoundsInScreen()); |
+ EXPECT_EQ(observer.last_bounds_when_changed(), widget->GetRestoredBounds()); |
+} |
+ |
+} // namespace test |
+} // namespace views |