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 #import "ui/views/widget/native_widget_mac.h" | 5 #import "ui/views/widget/native_widget_mac.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #import "base/mac/foundation_util.h" | 9 #import "base/mac/foundation_util.h" |
10 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 @end | 46 @end |
47 | 47 |
48 // Test NSWindow that provides hooks via method overrides to verify behavior. | 48 // Test NSWindow that provides hooks via method overrides to verify behavior. |
49 @interface NativeWidgetMacTestWindow : NativeWidgetMacNSWindow { | 49 @interface NativeWidgetMacTestWindow : NativeWidgetMacNSWindow { |
50 @private | 50 @private |
51 int invalidateShadowCount_; | 51 int invalidateShadowCount_; |
52 } | 52 } |
53 @property(readonly, nonatomic) int invalidateShadowCount; | 53 @property(readonly, nonatomic) int invalidateShadowCount; |
54 @end | 54 @end |
55 | 55 |
56 // Used to mock BridgedContentView so that calls to drawRect: can be | |
57 // intercepted. | |
58 @interface MockBridgedView : NSView { | |
59 @private | |
60 // No of times drawRect: has been called. | |
tapted
2016/02/09 06:47:12
nit: No -> Number, drawRect: -> -[NSView drawRect:
karandeepb
2016/02/10 00:39:44
Done.
| |
61 NSUInteger drawRectCount_; | |
62 | |
63 // The dirtyRect parameter passed to last invocation of drawRect:. | |
64 NSRect lastDirtyRect_; | |
65 } | |
66 | |
67 @property(assign, nonatomic) NSUInteger drawRectCount; | |
68 @property(assign, nonatomic) NSRect lastDirtyRect; | |
69 @end | |
70 | |
56 namespace views { | 71 namespace views { |
57 namespace test { | 72 namespace test { |
58 | 73 |
59 // BridgedNativeWidget friend to access private members. | 74 // BridgedNativeWidget friend to access private members. |
60 class BridgedNativeWidgetTestApi { | 75 class BridgedNativeWidgetTestApi { |
61 public: | 76 public: |
62 explicit BridgedNativeWidgetTestApi(NSWindow* window) { | 77 explicit BridgedNativeWidgetTestApi(NSWindow* window) { |
63 bridge_ = NativeWidgetMac::GetBridgeForNativeWindow(window); | 78 bridge_ = NativeWidgetMac::GetBridgeForNativeWindow(window); |
64 } | 79 } |
65 | 80 |
(...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1082 NSRect expected = [[[NSScreen screens] firstObject] visibleFrame]; | 1097 NSRect expected = [[[NSScreen screens] firstObject] visibleFrame]; |
1083 NSRect actual = gfx::ScreenRectToNSRect(widget.GetWorkAreaBoundsInScreen()); | 1098 NSRect actual = gfx::ScreenRectToNSRect(widget.GetWorkAreaBoundsInScreen()); |
1084 EXPECT_FALSE(NSIsEmptyRect(actual)); | 1099 EXPECT_FALSE(NSIsEmptyRect(actual)); |
1085 EXPECT_NSEQ(expected, actual); | 1100 EXPECT_NSEQ(expected, actual); |
1086 | 1101 |
1087 [widget.GetNativeWindow() close]; | 1102 [widget.GetNativeWindow() close]; |
1088 actual = gfx::ScreenRectToNSRect(widget.GetWorkAreaBoundsInScreen()); | 1103 actual = gfx::ScreenRectToNSRect(widget.GetWorkAreaBoundsInScreen()); |
1089 EXPECT_TRUE(NSIsEmptyRect(actual)); | 1104 EXPECT_TRUE(NSIsEmptyRect(actual)); |
1090 } | 1105 } |
1091 | 1106 |
1107 // Test that NativeWidgetMac::SchedulePaintInRect correctly passes the dirtyRect | |
1108 // parameter to BridgedContentView::drawRect, for a titled window (window with a | |
1109 // toolbar). | |
1110 TEST_F(NativeWidgetMacTest, SchedulePaintInRect_Titled) { | |
1111 Widget* widget = CreateTopLevelPlatformWidget(); | |
1112 | |
1113 gfx::Rect screen_rect(50, 50, 100, 100); | |
1114 widget->SetBounds(screen_rect); | |
1115 | |
1116 // Setup the mock content view for the NSWindow, so that we can intercept | |
1117 // drawRect. | |
1118 NSWindow* window = widget->GetNativeWindow(); | |
1119 MockBridgedView* mock_bridged_view = [[MockBridgedView alloc] init]; | |
tapted
2016/02/09 06:47:12
scoped_nsobject (I think this is a memory leak cur
karandeepb
2016/02/10 00:39:44
Done.
| |
1120 [window setContentView:mock_bridged_view]; | |
1121 | |
1122 base::RunLoop().RunUntilIdle(); | |
tapted
2016/02/09 06:47:12
Comment here, like,
// Ensure the initial draw of
karandeepb
2016/02/10 00:39:44
Done.
| |
1123 | |
1124 // Add a dummy view to the widget. This will cause SchedulePaint to be called | |
1125 // on the dummy view. | |
1126 View* dummy_view = new View(); | |
1127 gfx::Rect dummy_bounds(25, 30, 10, 15); | |
1128 dummy_view->SetBoundsRect(dummy_bounds); | |
1129 // Reset drawRect count. | |
1130 [mock_bridged_view setDrawRectCount:0]; | |
1131 widget->GetContentsView()->AddChildView(dummy_view); | |
1132 | |
1133 // SchedulePaint is asyncronous. Wait for drawRect: to be called. | |
1134 base::RunLoop().RunUntilIdle(); | |
1135 | |
1136 EXPECT_EQ(1u, [mock_bridged_view drawRectCount]); | |
1137 int client_area_height = widget->GetClientAreaBoundsInScreen().height(); | |
1138 // These are expected dummy_view bounds in AppKit coordinate system. The y | |
1139 // coordinate of rect origin is calculated as - | |
1140 // client_area_height - 30 (dummy_view's y coordinate) - 15 (dummy view's | |
1141 // height). | |
1142 gfx::Rect expected_appkit_bounds(25, client_area_height - 45, 10, 15); | |
1143 EXPECT_NSEQ(expected_appkit_bounds.ToCGRect(), | |
1144 [mock_bridged_view lastDirtyRect]); | |
1145 widget->CloseNow(); | |
1146 } | |
1147 | |
1148 // Test that NativeWidgetMac::SchedulePaintInRect correctly passes the dirtyRect | |
1149 // parameter to BridgedContentView::drawRect, for a borderless window. | |
1150 TEST_F(NativeWidgetMacTest, SchedulePaintInRect_Borderless) { | |
1151 Widget* widget = CreateTopLevelFramelessPlatformWidget(); | |
1152 | |
1153 gfx::Rect screen_rect(50, 50, 100, 100); | |
1154 widget->SetBounds(screen_rect); | |
1155 | |
1156 // Setup the mock content view for the NSWindow, so that we can intercept | |
1157 // drawRect. | |
1158 NSWindow* window = widget->GetNativeWindow(); | |
1159 MockBridgedView* mock_bridged_view = [[MockBridgedView alloc] init]; | |
tapted
2016/02/09 06:47:12
scoped_nsobject
karandeepb
2016/02/10 00:39:44
Done.
| |
1160 [window setContentView:mock_bridged_view]; | |
1161 | |
1162 base::RunLoop().RunUntilIdle(); | |
1163 | |
1164 // Add a dummy view to the widget. This will cause SchedulePaint to be called | |
1165 // on the dummy view. | |
1166 View* dummy_view = new View(); | |
1167 gfx::Rect dummy_bounds(25, 30, 10, 15); | |
1168 dummy_view->SetBoundsRect(dummy_bounds); | |
1169 // Reset drawRect count. | |
1170 [mock_bridged_view setDrawRectCount:0]; | |
1171 widget->GetRootView()->AddChildView(dummy_view); | |
1172 | |
1173 // SchedulePaint is asyncronous. Wait for drawRect: to be called. | |
1174 base::RunLoop().RunUntilIdle(); | |
1175 | |
1176 EXPECT_EQ(1u, [mock_bridged_view drawRectCount]); | |
1177 // These are expected dummy_view bounds in AppKit coordinate system. The y | |
1178 // coordinate of rect origin is calculated as - | |
tapted
2016/02/09 06:47:13
nit: `as -` -> `as:`
karandeepb
2016/02/10 00:39:44
Done.
| |
1179 // 100(client area height) - 30 (dummy_view's y coordinate) - 15 (dummy view's | |
1180 // height). | |
1181 gfx::Rect expected_appkit_bounds(25, 55, 10, 15); | |
1182 EXPECT_NSEQ(expected_appkit_bounds.ToCGRect(), | |
1183 [mock_bridged_view lastDirtyRect]); | |
1184 widget->CloseNow(); | |
1185 } | |
1186 | |
1092 } // namespace test | 1187 } // namespace test |
1093 } // namespace views | 1188 } // namespace views |
1094 | 1189 |
1095 @implementation TestStopAnimationWaiter | 1190 @implementation TestStopAnimationWaiter |
1096 - (void)setWindowStateForEnd { | 1191 - (void)setWindowStateForEnd { |
1097 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd); | 1192 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd); |
1098 } | 1193 } |
1099 @end | 1194 @end |
1100 | 1195 |
1101 @implementation NativeWidgetMacTestWindow | 1196 @implementation NativeWidgetMacTestWindow |
1102 | 1197 |
1103 @synthesize invalidateShadowCount = invalidateShadowCount_; | 1198 @synthesize invalidateShadowCount = invalidateShadowCount_; |
1104 | 1199 |
1105 - (void)invalidateShadow { | 1200 - (void)invalidateShadow { |
1106 ++invalidateShadowCount_; | 1201 ++invalidateShadowCount_; |
1107 [super invalidateShadow]; | 1202 [super invalidateShadow]; |
1108 } | 1203 } |
1109 | 1204 |
1110 @end | 1205 @end |
1206 | |
1207 @implementation MockBridgedView | |
1208 | |
1209 @synthesize drawRectCount = drawRectCount_; | |
1210 @synthesize lastDirtyRect = lastDirtyRect_; | |
1211 | |
1212 - (void)drawRect:(NSRect)dirtyRect { | |
1213 ++drawRectCount_; | |
1214 lastDirtyRect_ = dirtyRect; | |
1215 } | |
1216 @end | |
tapted
2016/02/09 06:47:12
nit: blank line before
karandeepb
2016/02/10 00:39:44
Done.
| |
OLD | NEW |