Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: ui/views/widget/native_widget_mac_unittest.mm

Issue 1796773003: Implement NativeWidgetMac::ReorderNativeViews (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using NativeViewHost*->NSView* map. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #import "base/mac/scoped_objc_class_swizzler.h" 11 #import "base/mac/scoped_objc_class_swizzler.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/test/test_timeouts.h" 16 #include "base/test/test_timeouts.h"
17 #import "testing/gtest_mac.h" 17 #import "testing/gtest_mac.h"
18 #include "third_party/skia/include/core/SkBitmap.h" 18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "third_party/skia/include/core/SkCanvas.h" 19 #include "third_party/skia/include/core/SkCanvas.h"
20 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h" 20 #import "ui/base/cocoa/constrained_window/constrained_window_animation.h"
21 #import "ui/base/cocoa/window_size_constants.h" 21 #import "ui/base/cocoa/window_size_constants.h"
22 #import "ui/events/test/cocoa_test_event_utils.h" 22 #import "ui/events/test/cocoa_test_event_utils.h"
23 #include "ui/events/test/event_generator.h" 23 #include "ui/events/test/event_generator.h"
24 #import "ui/gfx/mac/coordinate_conversion.h" 24 #import "ui/gfx/mac/coordinate_conversion.h"
25 #include "ui/views/bubble/bubble_delegate.h" 25 #include "ui/views/bubble/bubble_delegate.h"
26 #import "ui/views/cocoa/bridged_native_widget.h" 26 #import "ui/views/cocoa/bridged_native_widget.h"
27 #import "ui/views/cocoa/native_widget_mac_nswindow.h" 27 #import "ui/views/cocoa/native_widget_mac_nswindow.h"
28 #include "ui/views/controls/button/label_button.h" 28 #include "ui/views/controls/button/label_button.h"
29 #include "ui/views/controls/label.h" 29 #include "ui/views/controls/label.h"
30 #include "ui/views/controls/native/native_view_host.h"
30 #include "ui/views/native_cursor.h" 31 #include "ui/views/native_cursor.h"
31 #include "ui/views/test/native_widget_factory.h" 32 #include "ui/views/test/native_widget_factory.h"
32 #include "ui/views/test/test_widget_observer.h" 33 #include "ui/views/test/test_widget_observer.h"
33 #include "ui/views/test/widget_test.h" 34 #include "ui/views/test/widget_test.h"
34 #include "ui/views/widget/native_widget_mac.h" 35 #include "ui/views/widget/native_widget_mac.h"
35 #include "ui/views/widget/native_widget_private.h" 36 #include "ui/views/widget/native_widget_private.h"
36 #include "ui/views/window/dialog_delegate.h" 37 #include "ui/views/window/dialog_delegate.h"
37 38
38 // Donates an implementation of -[NSAnimation stopAnimation] which calls the 39 // Donates an implementation of -[NSAnimation stopAnimation] which calls the
39 // original implementation, then quits a nested run loop. 40 // original implementation, then quits a nested run loop.
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 205
205 int gained_visible_count_ = 0; 206 int gained_visible_count_ = 0;
206 int lost_visible_count_ = 0; 207 int lost_visible_count_ = 0;
207 int target_gained_visible_count_ = 0; 208 int target_gained_visible_count_ = 0;
208 int target_lost_visible_count_ = 0; 209 int target_lost_visible_count_ = 0;
209 base::RunLoop* run_loop_ = nullptr; 210 base::RunLoop* run_loop_ = nullptr;
210 211
211 DISALLOW_COPY_AND_ASSIGN(WidgetChangeObserver); 212 DISALLOW_COPY_AND_ASSIGN(WidgetChangeObserver);
212 }; 213 };
213 214
215 class NativeHostHolder {
216 public:
217 NativeHostHolder(NSView* view, NativeViewHost* host)
218 : view(view), host(host) {
219 host->set_owned_by_client();
220 }
221
222 void AttachNativeView() {
223 DCHECK(!host->native_view());
224 host->Attach(view.get());
225 }
226
227 void Detach() { host->Detach(); }
228
229 gfx::NativeView GetView() const { return view.get(); }
tapted 2016/03/17 08:18:59 nit: these accessors can be in hacker_style -- tha
230 NativeViewHost* GetHost() const { return host.get(); }
tapted 2016/03/17 08:18:58 GetHost -> host()
231
232 private:
233 base::scoped_nsobject<NSView> view;
tapted 2016/03/17 08:18:59 view->view_
234 scoped_ptr<NativeViewHost> host;
tapted 2016/03/17 08:18:58 host->host_
235 };
tapted 2016/03/17 08:18:59 nit: DISALLOW_COPY_AND_ASSIGN(..)
236
237 void CheckViewsOrderEqual(NSArray* actual, NSArray* expected) {
tapted 2016/03/17 08:18:58 Comment why this can't just be isEqualToArray. (I
238 if ([expected count] == 1) {
239 EXPECT_TRUE([actual containsObject:[expected firstObject]]);
240 return;
241 }
242
243 for (NSUInteger i = 0; i < [expected count] - 1; ++i) {
244 NSUInteger first = [actual indexOfObject:[expected objectAtIndex:i]];
245 NSUInteger second = [actual indexOfObject:[expected objectAtIndex:i + 1]];
246 EXPECT_LT(first, second);
247 EXPECT_NE(first, NSNotFound);
248 EXPECT_NE(second, NSNotFound);
249 }
250 }
251
214 // Test visibility states triggered externally. 252 // Test visibility states triggered externally.
215 TEST_F(NativeWidgetMacTest, HideAndShowExternally) { 253 TEST_F(NativeWidgetMacTest, HideAndShowExternally) {
216 Widget* widget = CreateTopLevelPlatformWidget(); 254 Widget* widget = CreateTopLevelPlatformWidget();
217 NSWindow* ns_window = widget->GetNativeWindow(); 255 NSWindow* ns_window = widget->GetNativeWindow();
218 WidgetChangeObserver observer(widget); 256 WidgetChangeObserver observer(widget);
219 257
220 // Should initially be hidden. 258 // Should initially be hidden.
221 EXPECT_FALSE(widget->IsVisible()); 259 EXPECT_FALSE(widget->IsVisible());
222 EXPECT_FALSE([ns_window isVisible]); 260 EXPECT_FALSE([ns_window isVisible]);
223 EXPECT_EQ(0, observer.gained_visible_count()); 261 EXPECT_EQ(0, observer.gained_visible_count());
(...skipping 1105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1329 1367
1330 [widget->GetNativeWindow() makeFirstResponder:child_view]; 1368 [widget->GetNativeWindow() makeFirstResponder:child_view];
1331 EXPECT_FALSE(manager->GetFocusedView()); 1369 EXPECT_FALSE(manager->GetFocusedView());
1332 1370
1333 [widget->GetNativeWindow() makeFirstResponder:widget->GetNativeView()]; 1371 [widget->GetNativeWindow() makeFirstResponder:widget->GetNativeView()];
1334 EXPECT_EQ(manager->GetFocusedView(), widget->GetRootView()); 1372 EXPECT_EQ(manager->GetFocusedView(), widget->GetRootView());
1335 1373
1336 widget->CloseNow(); 1374 widget->CloseNow();
1337 } 1375 }
1338 1376
1377 // Test that NativeViewHost::Attach/Detach method save NativeView z-order
tapted 2016/03/17 08:18:58 nit: Test that NativeViewHost::Attach()/Detach()
1378 TEST_F(NativeWidgetMacTest, NativeViewsOrder) {
1379 Widget* widget = CreateTopLevelPlatformWidget();
1380
1381 const int native_views_count = 3;
tapted 2016/03/17 08:18:59 optional nit: kNativeViewCount
1382 std::vector<scoped_ptr<NativeHostHolder>> hosts;
1383 for (int i = 0; i < native_views_count; ++i) {
1384 scoped_ptr<NativeHostHolder> holder(
1385 new NativeHostHolder([[NSView alloc] init], new NativeViewHost));
tapted 2016/03/17 08:18:59 it would be nicer to move the initialization into
1386 widget->GetRootView()->AddChildView(holder->GetHost());
1387 holder->AttachNativeView();
1388 hosts.push_back(std::move(holder));
1389 }
1390 CheckViewsOrderEqual(
1391 [widget->GetNativeView() subviews],
1392 [NSArray arrayWithObjects:hosts[0]->GetView(), hosts[1]->GetView(),
tapted 2016/03/17 08:18:58 you can use an array literal, like @[ hosts[0]->v
1393 hosts[2]->GetView(), nil]);
1394
1395 hosts[1]->Detach();
1396 CheckViewsOrderEqual(
1397 [widget->GetNativeView() subviews],
1398 [NSArray arrayWithObjects:hosts[0]->GetView(), hosts[2]->GetView(), nil]);
1399
1400 hosts[1]->AttachNativeView();
1401 CheckViewsOrderEqual(
1402 [widget->GetNativeView() subviews],
1403 [NSArray arrayWithObjects:hosts[0]->GetView(), hosts[1]->GetView(),
1404 hosts[2]->GetView(), nil]);
1405 widget->CloseNow();
1406 }
1407
tapted 2016/03/17 08:18:58 I think there should be a bit more coverage. There
1339 } // namespace test 1408 } // namespace test
1340 } // namespace views 1409 } // namespace views
1341 1410
1342 @implementation TestStopAnimationWaiter 1411 @implementation TestStopAnimationWaiter
1343 - (void)setWindowStateForEnd { 1412 - (void)setWindowStateForEnd {
1344 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd); 1413 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd);
1345 } 1414 }
1346 @end 1415 @end
1347 1416
1348 @implementation NativeWidgetMacTestWindow 1417 @implementation NativeWidgetMacTestWindow
(...skipping 17 matching lines...) Expand all
1366 lastDirtyRect_ = dirtyRect; 1435 lastDirtyRect_ = dirtyRect;
1367 } 1436 }
1368 1437
1369 @end 1438 @end
1370 1439
1371 @implementation FocusableTestNSView 1440 @implementation FocusableTestNSView
1372 - (BOOL)acceptsFirstResponder { 1441 - (BOOL)acceptsFirstResponder {
1373 return YES; 1442 return YES;
1374 } 1443 }
1375 @end 1444 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698