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/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
12 #import "testing/gtest_mac.h" | 13 #import "testing/gtest_mac.h" |
13 #import "ui/events/test/cocoa_test_event_utils.h" | 14 #import "ui/events/test/cocoa_test_event_utils.h" |
14 #include "ui/events/test/event_generator.h" | 15 #include "ui/events/test/event_generator.h" |
15 #import "ui/gfx/mac/coordinate_conversion.h" | 16 #import "ui/gfx/mac/coordinate_conversion.h" |
16 #import "ui/views/cocoa/bridged_native_widget.h" | 17 #import "ui/views/cocoa/bridged_native_widget.h" |
| 18 #include "ui/views/controls/button/label_button.h" |
17 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
18 #include "ui/views/native_cursor.h" | 20 #include "ui/views/native_cursor.h" |
19 #include "ui/views/test/test_widget_observer.h" | 21 #include "ui/views/test/test_widget_observer.h" |
20 #include "ui/views/test/widget_test.h" | 22 #include "ui/views/test/widget_test.h" |
21 #include "ui/views/widget/native_widget_private.h" | 23 #include "ui/views/widget/native_widget_private.h" |
22 | 24 |
23 namespace views { | 25 namespace views { |
24 namespace test { | 26 namespace test { |
25 | 27 |
26 // Tests for parts of NativeWidgetMac not covered by BridgedNativeWidget, which | 28 // Tests for parts of NativeWidgetMac not covered by BridgedNativeWidget, which |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 child->GetWindowBoundsInScreen()); | 421 child->GetWindowBoundsInScreen()); |
420 | 422 |
421 // Closing the parent should close and destroy the child. | 423 // Closing the parent should close and destroy the child. |
422 EXPECT_FALSE(child_observer.widget_closed()); | 424 EXPECT_FALSE(child_observer.widget_closed()); |
423 [native_parent close]; | 425 [native_parent close]; |
424 EXPECT_TRUE(child_observer.widget_closed()); | 426 EXPECT_TRUE(child_observer.widget_closed()); |
425 | 427 |
426 EXPECT_EQ(0u, [[native_parent childWindows] count]); | 428 EXPECT_EQ(0u, [[native_parent childWindows] count]); |
427 } | 429 } |
428 | 430 |
| 431 // Use Native APIs to query the tooltip text that would be shown once the |
| 432 // tooltip delay had elapsed. |
| 433 base::string16 TooltipTextForWidget(Widget* widget) { |
| 434 // For Mac, the actual location doesn't matter, since there is only one native |
| 435 // view and it fills the window. This just assumes the window is at least big |
| 436 // big enough for a constant coordinate to be within it. |
| 437 NSPoint point = NSMakePoint(30, 30); |
| 438 NSView* view = [widget->GetNativeView() hitTest:point]; |
| 439 NSString* text = |
| 440 [view view:view stringForToolTip:0 point:point userData:nullptr]; |
| 441 return base::SysNSStringToUTF16(text); |
| 442 } |
| 443 |
| 444 // Tests tooltips. The test doesn't wait for tooltips to appear. That is, the |
| 445 // test assumes Cocoa calls stringForToolTip: at appropriate times and that, |
| 446 // when a tooltip is already visible, changing it causes an update. These were |
| 447 // tested manually by inserting a base::RunLoop.Run(). |
| 448 TEST_F(NativeWidgetMacTest, Tooltips) { |
| 449 Widget* widget = CreateTopLevelPlatformWidget(); |
| 450 gfx::Rect screen_rect(50, 50, 100, 100); |
| 451 widget->SetBounds(screen_rect); |
| 452 |
| 453 const base::string16 tooltip_back = base::ASCIIToUTF16("Back"); |
| 454 const base::string16 tooltip_front = base::ASCIIToUTF16("Front"); |
| 455 const base::string16 long_tooltip(2000, 'W'); |
| 456 |
| 457 // Create a nested layout to test corner cases. |
| 458 LabelButton* back = new LabelButton(nullptr, base::string16()); |
| 459 back->SetBounds(10, 10, 80, 80); |
| 460 widget->GetContentsView()->AddChildView(back); |
| 461 widget->Show(); |
| 462 |
| 463 ui::test::EventGenerator event_generator(GetContext(), |
| 464 widget->GetNativeWindow()); |
| 465 |
| 466 // Initially, there should be no tooltip. |
| 467 event_generator.MoveMouseTo(gfx::Point(50, 50)); |
| 468 EXPECT_TRUE(TooltipTextForWidget(widget).empty()); |
| 469 |
| 470 // Create a new button for the "front", and set the tooltip, but don't add it |
| 471 // to the view hierarchy yet. |
| 472 LabelButton* front = new LabelButton(nullptr, base::string16()); |
| 473 front->SetBounds(20, 20, 40, 40); |
| 474 front->SetTooltipText(tooltip_front); |
| 475 |
| 476 // Changing the tooltip text shouldn't require an additional mousemove to take |
| 477 // effect. |
| 478 EXPECT_TRUE(TooltipTextForWidget(widget).empty()); |
| 479 back->SetTooltipText(tooltip_back); |
| 480 EXPECT_EQ(tooltip_back, TooltipTextForWidget(widget)); |
| 481 |
| 482 // Adding a new view under the mouse should also take immediate effect. |
| 483 back->AddChildView(front); |
| 484 EXPECT_EQ(tooltip_front, TooltipTextForWidget(widget)); |
| 485 |
| 486 // A long tooltip will be wrapped by Cocoa, but the full string should appear. |
| 487 // Note that render widget hosts clip at 1024 to prevent DOS, but in toolkit- |
| 488 // views the UI is more trusted. |
| 489 front->SetTooltipText(long_tooltip); |
| 490 EXPECT_EQ(long_tooltip, TooltipTextForWidget(widget)); |
| 491 |
| 492 // Move the mouse to a different view - tooltip should change. |
| 493 event_generator.MoveMouseTo(gfx::Point(15, 15)); |
| 494 EXPECT_EQ(tooltip_back, TooltipTextForWidget(widget)); |
| 495 |
| 496 // Move the mouse off of any view, tooltip should clear. |
| 497 event_generator.MoveMouseTo(gfx::Point(5, 5)); |
| 498 EXPECT_TRUE(TooltipTextForWidget(widget).empty()); |
| 499 } |
| 500 |
429 } // namespace test | 501 } // namespace test |
430 } // namespace views | 502 } // namespace views |
OLD | NEW |