Chromium Code Reviews| 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" |
| 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 Loading... | |
| 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() | |
| 218 : view_([[NSView alloc] init]), host_(new NativeViewHost()) { | |
| 219 host_->set_owned_by_client(); | |
|
tapted
2016/03/17 23:04:15
oh neat - I forgot you could do this
| |
| 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 view() const { return view_.get(); } | |
| 230 NativeViewHost* host() const { return host_.get(); } | |
| 231 | |
| 232 private: | |
| 233 base::scoped_nsobject<NSView> view_; | |
| 234 scoped_ptr<NativeViewHost> host_; | |
| 235 | |
| 236 DISALLOW_COPY_AND_ASSIGN(NativeHostHolder); | |
| 237 }; | |
| 238 | |
| 214 // Test visibility states triggered externally. | 239 // Test visibility states triggered externally. |
| 215 TEST_F(NativeWidgetMacTest, HideAndShowExternally) { | 240 TEST_F(NativeWidgetMacTest, HideAndShowExternally) { |
| 216 Widget* widget = CreateTopLevelPlatformWidget(); | 241 Widget* widget = CreateTopLevelPlatformWidget(); |
| 217 NSWindow* ns_window = widget->GetNativeWindow(); | 242 NSWindow* ns_window = widget->GetNativeWindow(); |
| 218 WidgetChangeObserver observer(widget); | 243 WidgetChangeObserver observer(widget); |
| 219 | 244 |
| 220 // Should initially be hidden. | 245 // Should initially be hidden. |
| 221 EXPECT_FALSE(widget->IsVisible()); | 246 EXPECT_FALSE(widget->IsVisible()); |
| 222 EXPECT_FALSE([ns_window isVisible]); | 247 EXPECT_FALSE([ns_window isVisible]); |
| 223 EXPECT_EQ(0, observer.gained_visible_count()); | 248 EXPECT_EQ(0, observer.gained_visible_count()); |
| (...skipping 1105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1329 | 1354 |
| 1330 [widget->GetNativeWindow() makeFirstResponder:child_view]; | 1355 [widget->GetNativeWindow() makeFirstResponder:child_view]; |
| 1331 EXPECT_FALSE(manager->GetFocusedView()); | 1356 EXPECT_FALSE(manager->GetFocusedView()); |
| 1332 | 1357 |
| 1333 [widget->GetNativeWindow() makeFirstResponder:widget->GetNativeView()]; | 1358 [widget->GetNativeWindow() makeFirstResponder:widget->GetNativeView()]; |
| 1334 EXPECT_EQ(manager->GetFocusedView(), widget->GetRootView()); | 1359 EXPECT_EQ(manager->GetFocusedView(), widget->GetRootView()); |
| 1335 | 1360 |
| 1336 widget->CloseNow(); | 1361 widget->CloseNow(); |
| 1337 } | 1362 } |
| 1338 | 1363 |
| 1364 class NativeWidgetMacViewsOrderTest : public WidgetTest { | |
| 1365 public: | |
| 1366 NativeWidgetMacViewsOrderTest() {} | |
| 1367 | |
| 1368 protected: | |
| 1369 void SetUp() override { | |
|
tapted
2016/03/17 23:04:15
nit:
// testing::Test:
| |
| 1370 WidgetTest::SetUp(); | |
| 1371 | |
| 1372 widget_ = CreateTopLevelPlatformWidget(); | |
| 1373 | |
| 1374 ASSERT_EQ(1u, [[widget_->GetNativeView() subviews] count]); | |
| 1375 compositor_view_ = [[widget_->GetNativeView() subviews] firstObject]; | |
| 1376 | |
| 1377 native_host_parent_ = new View(); | |
| 1378 widget_->GetContentsView()->AddChildView(native_host_parent_); | |
| 1379 | |
| 1380 const int kNativeViewCount = 3; | |
| 1381 for (int i = 0; i < kNativeViewCount; ++i) { | |
| 1382 scoped_ptr<NativeHostHolder> holder(new NativeHostHolder()); | |
| 1383 native_host_parent_->AddChildView(holder->host()); | |
| 1384 holder->AttachNativeView(); | |
| 1385 hosts_.push_back(std::move(holder)); | |
| 1386 } | |
| 1387 EXPECT_EQ(native_host_parent_->child_count(), kNativeViewCount); | |
|
tapted
2016/03/17 23:04:15
nit: EXPECT_EQ(kNativeViewCount, native_host_paren
| |
| 1388 EXPECT_TRUE(([[widget_->GetNativeView() subviews] isEqualToArray:@[ | |
| 1389 compositor_view_, hosts_[0]->view(), hosts_[1]->view(), hosts_[2]->view() | |
| 1390 ]])); | |
| 1391 } | |
| 1392 | |
| 1393 void TearDown() override { | |
| 1394 widget_->CloseNow(); | |
| 1395 WidgetTest::TearDown(); | |
| 1396 } | |
| 1397 | |
| 1398 NSView* GetContentNativeView() { return widget_->GetNativeView(); } | |
| 1399 NSView* compositor_view() { return compositor_view_; } | |
| 1400 View* native_host_parent() { return native_host_parent_; } | |
| 1401 const std::vector<scoped_ptr<NativeHostHolder>>& hosts() const { | |
| 1402 return hosts_; | |
| 1403 } | |
| 1404 | |
| 1405 private: | |
|
tapted
2016/03/17 23:04:15
It's actually fine to have protected members in te
| |
| 1406 Widget* widget_ = nullptr; | |
| 1407 View* native_host_parent_ = nullptr; | |
| 1408 NSView* compositor_view_ = nil; | |
| 1409 std::vector<scoped_ptr<NativeHostHolder>> hosts_; | |
| 1410 | |
| 1411 DISALLOW_COPY_AND_ASSIGN(NativeWidgetMacViewsOrderTest); | |
| 1412 }; | |
| 1413 | |
| 1414 // Test that NativeViewHost::Attach()/Detach() method saves the NativeView | |
| 1415 // z-order. | |
| 1416 TEST_F(NativeWidgetMacViewsOrderTest, NativeViewAttached) { | |
| 1417 hosts()[1]->Detach(); | |
| 1418 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1419 compositor_view(), hosts()[0]->view(), hosts()[2]->view() | |
| 1420 ]])); | |
| 1421 | |
| 1422 hosts()[1]->AttachNativeView(); | |
| 1423 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1424 compositor_view(), hosts()[0]->view(), hosts()[1]->view(), | |
| 1425 hosts()[2]->view() | |
| 1426 ]])); | |
| 1427 } | |
| 1428 | |
| 1429 // Tests that NativeViews order changes according to views::View hierarchy. | |
| 1430 TEST_F(NativeWidgetMacViewsOrderTest, ReorderViews) { | |
| 1431 native_host_parent()->ReorderChildView(hosts()[2]->host(), 1); | |
| 1432 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1433 compositor_view(), hosts()[0]->view(), hosts()[2]->view(), | |
| 1434 hosts()[1]->view() | |
| 1435 ]])); | |
| 1436 | |
| 1437 native_host_parent()->RemoveChildView(hosts()[2]->host()); | |
| 1438 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1439 compositor_view(), hosts()[0]->view(), hosts()[1]->view() | |
| 1440 ]])); | |
| 1441 | |
| 1442 View* new_parent = new View(); | |
| 1443 native_host_parent()->RemoveChildView(hosts()[1]->host()); | |
| 1444 native_host_parent()->AddChildView(new_parent); | |
| 1445 new_parent->AddChildView(hosts()[1]->host()); | |
| 1446 new_parent->AddChildView(hosts()[2]->host()); | |
| 1447 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1448 compositor_view(), hosts()[0]->view(), hosts()[1]->view(), | |
| 1449 hosts()[2]->view() | |
| 1450 ]])); | |
| 1451 | |
| 1452 native_host_parent()->ReorderChildView(new_parent, 0); | |
| 1453 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1454 compositor_view(), hosts()[1]->view(), hosts()[2]->view(), | |
| 1455 hosts()[0]->view() | |
| 1456 ]])); | |
| 1457 } | |
| 1458 | |
| 1459 // Test that unassociated native views stay on top after reordering. | |
| 1460 TEST_F(NativeWidgetMacViewsOrderTest, UnassociatedViewsIsAbove) { | |
| 1461 base::scoped_nsobject<NSView> child_view([[NSView alloc] init]); | |
| 1462 [GetContentNativeView() addSubview:child_view.get()]; | |
|
tapted
2016/03/17 23:04:15
nit: no .get()
| |
| 1463 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1464 compositor_view(), hosts()[0]->view(), hosts()[1]->view(), | |
| 1465 hosts()[2]->view(), child_view.get() | |
| 1466 ]])); | |
| 1467 | |
| 1468 native_host_parent()->ReorderChildView(hosts()[2]->host(), 1); | |
| 1469 EXPECT_TRUE(([[GetContentNativeView() subviews] isEqualToArray:@[ | |
| 1470 compositor_view(), hosts()[0]->view(), hosts()[2]->view(), | |
| 1471 hosts()[1]->view(), child_view.get() | |
| 1472 ]])); | |
| 1473 } | |
| 1474 | |
| 1339 } // namespace test | 1475 } // namespace test |
| 1340 } // namespace views | 1476 } // namespace views |
| 1341 | 1477 |
| 1342 @implementation TestStopAnimationWaiter | 1478 @implementation TestStopAnimationWaiter |
| 1343 - (void)setWindowStateForEnd { | 1479 - (void)setWindowStateForEnd { |
| 1344 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd); | 1480 views::test::ScopedSwizzleWaiter::GetMethodAndMarkCalled()(self, _cmd); |
| 1345 } | 1481 } |
| 1346 @end | 1482 @end |
| 1347 | 1483 |
| 1348 @implementation NativeWidgetMacTestWindow | 1484 @implementation NativeWidgetMacTestWindow |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1366 lastDirtyRect_ = dirtyRect; | 1502 lastDirtyRect_ = dirtyRect; |
| 1367 } | 1503 } |
| 1368 | 1504 |
| 1369 @end | 1505 @end |
| 1370 | 1506 |
| 1371 @implementation FocusableTestNSView | 1507 @implementation FocusableTestNSView |
| 1372 - (BOOL)acceptsFirstResponder { | 1508 - (BOOL)acceptsFirstResponder { |
| 1373 return YES; | 1509 return YES; |
| 1374 } | 1510 } |
| 1375 @end | 1511 @end |
| OLD | NEW |