OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #import "base/mac/foundation_util.h" |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 10 #import "chrome/browser/ui/cocoa/extensions/toolbar_actions_bar_bubble_mac.h" |
| 11 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 12 #import "chrome/browser/ui/cocoa/run_loop_testing.h" |
| 13 #include "chrome/browser/ui/toolbar/test_toolbar_actions_bar_bubble_delegate.h" |
| 14 #import "ui/events/test/cocoa_test_event_utils.h" |
| 15 |
| 16 // A simple class to observe when a window is destructing. |
| 17 @interface WindowObserver : NSObject { |
| 18 BOOL windowIsClosing_; |
| 19 } |
| 20 |
| 21 - (id)initWithWindow:(NSWindow*)window; |
| 22 |
| 23 - (void)dealloc; |
| 24 |
| 25 - (void)onWindowClosing:(NSNotification*)notification; |
| 26 |
| 27 @property(nonatomic, assign) BOOL windowIsClosing; |
| 28 |
| 29 @end |
| 30 |
| 31 @implementation WindowObserver |
| 32 |
| 33 - (id)initWithWindow:(NSWindow*)window { |
| 34 if ((self = [super init])) { |
| 35 [[NSNotificationCenter defaultCenter] |
| 36 addObserver:self |
| 37 selector:@selector(onWindowClosing:) |
| 38 name:NSWindowWillCloseNotification |
| 39 object:window]; |
| 40 } |
| 41 return self; |
| 42 } |
| 43 |
| 44 - (void)dealloc { |
| 45 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 46 [super dealloc]; |
| 47 } |
| 48 |
| 49 - (void)onWindowClosing:(NSNotification*)notification { |
| 50 windowIsClosing_ = YES; |
| 51 } |
| 52 |
| 53 @synthesize windowIsClosing = windowIsClosing_; |
| 54 |
| 55 @end |
| 56 |
| 57 class ToolbarActionsBarBubbleMacTest : public CocoaTest { |
| 58 public: |
| 59 ToolbarActionsBarBubbleMacTest() {} |
| 60 ~ToolbarActionsBarBubbleMacTest() override {} |
| 61 |
| 62 // Create and display a new bubble with the given |delegate|. |
| 63 ToolbarActionsBarBubbleMac* CreateAndShowBubble( |
| 64 TestToolbarActionsBarBubbleDelegate* delegate); |
| 65 |
| 66 // Test that clicking on the corresponding button produces the |
| 67 // |expected_action|, and closes the bubble. |
| 68 void TestBubbleButton( |
| 69 ToolbarActionsBarBubbleDelegate::CloseAction expected_action); |
| 70 |
| 71 base::string16 HeadingString() { return base::ASCIIToUTF16("Heading"); } |
| 72 base::string16 BodyString() { return base::ASCIIToUTF16("Body"); } |
| 73 base::string16 ActionString() { return base::ASCIIToUTF16("Action"); } |
| 74 base::string16 DismissString() { return base::ASCIIToUTF16("Dismiss"); } |
| 75 base::string16 LearnMoreString() { return base::ASCIIToUTF16("LearnMore"); } |
| 76 |
| 77 private: |
| 78 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBarBubbleMacTest); |
| 79 }; |
| 80 |
| 81 ToolbarActionsBarBubbleMac* ToolbarActionsBarBubbleMacTest::CreateAndShowBubble( |
| 82 TestToolbarActionsBarBubbleDelegate* delegate) { |
| 83 ToolbarActionsBarBubbleMac* bubble = |
| 84 [[ToolbarActionsBarBubbleMac alloc] |
| 85 initWithParentWindow:test_window() |
| 86 anchorPoint:NSZeroPoint |
| 87 delegate:delegate->GetDelegate()]; |
| 88 EXPECT_FALSE(delegate->shown()); |
| 89 [bubble showWindow:nil]; |
| 90 [base::mac::ObjCCastStrict<InfoBubbleWindow>([bubble window]) |
| 91 setAllowedAnimations:info_bubble::kAnimateNone]; |
| 92 chrome::testing::NSRunLoopRunAllPending(); |
| 93 EXPECT_FALSE(delegate->close_action()); |
| 94 EXPECT_TRUE(delegate->shown()); |
| 95 return bubble; |
| 96 } |
| 97 |
| 98 void ToolbarActionsBarBubbleMacTest::TestBubbleButton( |
| 99 ToolbarActionsBarBubbleDelegate::CloseAction expected_action) { |
| 100 TestToolbarActionsBarBubbleDelegate delegate( |
| 101 HeadingString(), BodyString(), ActionString()); |
| 102 delegate.set_dismiss_button(DismissString()); |
| 103 delegate.set_learn_more_button(LearnMoreString()); |
| 104 ToolbarActionsBarBubbleMac* bubble = CreateAndShowBubble(&delegate); |
| 105 base::scoped_nsobject<WindowObserver> windowObserver( |
| 106 [[WindowObserver alloc] initWithWindow:[bubble window]]); |
| 107 EXPECT_FALSE([windowObserver windowIsClosing]); |
| 108 |
| 109 // Find the appropriate button to click. |
| 110 NSButton* button = nil; |
| 111 switch (expected_action) { |
| 112 case ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE: |
| 113 button = [bubble actionButton]; |
| 114 break; |
| 115 case ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS: |
| 116 button = [bubble dismissButton]; |
| 117 break; |
| 118 case ToolbarActionsBarBubbleDelegate::CLOSE_LEARN_MORE: |
| 119 button = [bubble learnMoreButton]; |
| 120 break; |
| 121 } |
| 122 ASSERT_TRUE(button); |
| 123 |
| 124 // Click the button. |
| 125 std::pair<NSEvent*, NSEvent*> events = |
| 126 cocoa_test_event_utils::MouseClickInView(button, 1); |
| 127 [NSApp postEvent:events.second atStart:YES]; |
| 128 [NSApp sendEvent:events.first]; |
| 129 chrome::testing::NSRunLoopRunAllPending(); |
| 130 |
| 131 // The bubble should be closed, and the delegate should be told that the |
| 132 // button was clicked. |
| 133 ASSERT_TRUE(delegate.close_action()); |
| 134 EXPECT_EQ(expected_action, *delegate.close_action()); |
| 135 EXPECT_TRUE([windowObserver windowIsClosing]); |
| 136 } |
| 137 |
| 138 // Test clicking on the action button and dismissing the bubble. |
| 139 TEST_F(ToolbarActionsBarBubbleMacTest, CloseActionAndDismiss) { |
| 140 // Test all the possible actions. |
| 141 TestBubbleButton(ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE); |
| 142 TestBubbleButton(ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS); |
| 143 TestBubbleButton(ToolbarActionsBarBubbleDelegate::CLOSE_LEARN_MORE); |
| 144 |
| 145 { |
| 146 // Test dismissing the bubble without clicking the button. |
| 147 TestToolbarActionsBarBubbleDelegate delegate( |
| 148 HeadingString(), BodyString(), ActionString()); |
| 149 ToolbarActionsBarBubbleMac* bubble = CreateAndShowBubble(&delegate); |
| 150 base::scoped_nsobject<WindowObserver> windowObserver( |
| 151 [[WindowObserver alloc] initWithWindow:[bubble window]]); |
| 152 EXPECT_FALSE([windowObserver windowIsClosing]); |
| 153 |
| 154 // Close the bubble. The delegate should be told it was dismissed. |
| 155 [bubble close]; |
| 156 chrome::testing::NSRunLoopRunAllPending(); |
| 157 ASSERT_TRUE(delegate.close_action()); |
| 158 EXPECT_EQ(ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS, |
| 159 *delegate.close_action()); |
| 160 EXPECT_TRUE([windowObserver windowIsClosing]); |
| 161 } |
| 162 } |
| 163 |
| 164 // Test the basic layout of the bubble. |
| 165 TEST_F(ToolbarActionsBarBubbleMacTest, ToolbarActionsBarBubbleLayout) { |
| 166 { |
| 167 TestToolbarActionsBarBubbleDelegate delegate( |
| 168 HeadingString(), BodyString(), ActionString()); |
| 169 ToolbarActionsBarBubbleMac* bubble = CreateAndShowBubble(&delegate); |
| 170 // There should be no "learn more" or "dismiss" buttons. |
| 171 EXPECT_FALSE([bubble learnMoreButton]); |
| 172 EXPECT_FALSE([bubble dismissButton]); |
| 173 |
| 174 [bubble close]; |
| 175 chrome::testing::NSRunLoopRunAllPending(); |
| 176 } |
| 177 |
| 178 { |
| 179 TestToolbarActionsBarBubbleDelegate delegate( |
| 180 HeadingString(), BodyString(), ActionString()); |
| 181 delegate.set_dismiss_button(DismissString()); |
| 182 delegate.set_learn_more_button(LearnMoreString()); |
| 183 ToolbarActionsBarBubbleMac* bubble = CreateAndShowBubble(&delegate); |
| 184 EXPECT_TRUE([bubble learnMoreButton]); |
| 185 EXPECT_TRUE([bubble dismissButton]); |
| 186 |
| 187 [bubble close]; |
| 188 chrome::testing::NSRunLoopRunAllPending(); |
| 189 } |
| 190 } |
OLD | NEW |