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