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
|
// 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/json/json_reader.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/browser/ui/autofill/save_card_bubble_controller.h"
#import "chrome/browser/ui/cocoa/autofill/save_card_bubble_view_bridge.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#include "components/autofill/core/browser/credit_card.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "ui/events/test/cocoa_test_event_utils.h"
#include <Carbon/Carbon.h> // For the kVK_* constants.
namespace autofill {
namespace {
class TestSaveCardBubbleController : public SaveCardBubbleController {
public:
TestSaveCardBubbleController() {
ParseLegalMessageJson();
on_save_button_was_called_ = false;
on_cancel_button_was_called_ = false;
on_learn_more_was_called_ = false;
on_legal_message_was_called_ = false;
on_bubble_closed_was_called_ = false;
}
// SaveCardBubbleController:
base::string16 GetWindowTitle() const override { return base::string16(); }
base::string16 GetExplanatoryMessage() const override {
return base::string16();
}
const CreditCard GetCard() const override {
return CreditCard();
}
void OnSaveButton() override { on_save_button_was_called_ = true; }
void OnCancelButton() override { on_cancel_button_was_called_ = true; }
void OnLearnMoreClicked() override { on_learn_more_was_called_ = true; }
void OnLegalMessageLinkClicked(const GURL& url) override {
on_legal_message_was_called_ = true;
legal_message_url_ = url.spec();
}
void OnBubbleClosed() override { on_bubble_closed_was_called_ = true; }
const LegalMessageLines& GetLegalMessageLines() const override {
return lines_;
}
// Testing state.
bool on_save_button_was_called() { return on_save_button_was_called_; }
bool on_cancel_button_was_called() { return on_cancel_button_was_called_; }
bool on_learn_more_was_called() { return on_learn_more_was_called_; }
bool on_legal_message_was_called() { return on_legal_message_was_called_; }
std::string legal_message_url() { return legal_message_url_; }
bool on_bubble_closed_was_called() { return on_bubble_closed_was_called_; }
private:
void ParseLegalMessageJson() {
std::string message_json =
"{"
" \"line\" : ["
" {"
" \"template\": \"Please check out our {0}.\","
" \"template_parameter\": ["
" {"
" \"display_text\": \"terms of service\","
" \"url\": \"http://help.example.com/legal_message\""
" }"
" ]"
" },"
" {"
" \"template\": \"We also have a {0} and {1}.\","
" \"template_parameter\": ["
" {"
" \"display_text\": \"mission statement\","
" \"url\": \"http://www.example.com/our_mission\""
" },"
" {"
" \"display_text\": \"privacy policy\","
" \"url\": \"http://help.example.com/privacy_policy\""
" }"
" ]"
" }"
" ]"
"}";
scoped_ptr<base::Value> value(base::JSONReader::Read(message_json));
ASSERT_TRUE(value);
base::DictionaryValue* dictionary = nullptr;
ASSERT_TRUE(value->GetAsDictionary(&dictionary));
LegalMessageLine::Parse(*dictionary, &lines_);
}
LegalMessageLines lines_;
bool on_save_button_was_called_;
bool on_cancel_button_was_called_;
bool on_learn_more_was_called_;
bool on_legal_message_was_called_;
std::string legal_message_url_;
bool on_bubble_closed_was_called_;
};
class SaveCardBubbleViewTest : public CocoaProfileTest {
public:
void SetUp() override {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
browser_window_controller_ =
[[BrowserWindowController alloc] initWithBrowser:browser()
takeOwnership:NO];
bubble_controller_.reset(new TestSaveCardBubbleController());
// This will show the SaveCardBubbleViewCocoa.
bridge_ = new SaveCardBubbleViewBridge(bubble_controller_.get(),
browser_window_controller_);
}
void TearDown() override {
[browser_window_controller_ close];
CocoaProfileTest::TearDown();
}
protected:
BrowserWindowController* browser_window_controller_;
scoped_ptr<TestSaveCardBubbleController> bubble_controller_;
SaveCardBubbleViewBridge* bridge_;
};
} // namespace
TEST_F(SaveCardBubbleViewTest, SaveShouldClose) {
[bridge_->view_controller_ onSaveButton:nil];
EXPECT_TRUE(bubble_controller_->on_save_button_was_called());
EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called());
EXPECT_FALSE(bubble_controller_->on_learn_more_was_called());
EXPECT_FALSE(bubble_controller_->on_legal_message_was_called());
EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called());
}
TEST_F(SaveCardBubbleViewTest, CancelShouldClose) {
[bridge_->view_controller_ onCancelButton:nil];
EXPECT_FALSE(bubble_controller_->on_save_button_was_called());
EXPECT_TRUE(bubble_controller_->on_cancel_button_was_called());
EXPECT_FALSE(bubble_controller_->on_learn_more_was_called());
EXPECT_FALSE(bubble_controller_->on_legal_message_was_called());
EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called());
}
TEST_F(SaveCardBubbleViewTest, LearnMoreShouldNotClose) {
NSTextView* textView = nil;
NSObject* link = nil;
[bridge_->view_controller_ textView:textView clickedOnLink:link atIndex:0];
EXPECT_FALSE(bubble_controller_->on_save_button_was_called());
EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called());
EXPECT_TRUE(bubble_controller_->on_learn_more_was_called());
EXPECT_FALSE(bubble_controller_->on_legal_message_was_called());
EXPECT_FALSE(bubble_controller_->on_bubble_closed_was_called());
}
TEST_F(SaveCardBubbleViewTest, LegalMessageShouldNotClose) {
NSString* legalText = @"We also have a mission statement and privacy policy.";
base::scoped_nsobject<NSTextView> textView(
[[NSTextView alloc] initWithFrame:NSZeroRect]);
base::scoped_nsobject<NSAttributedString> attributedMessage(
[[NSAttributedString alloc] initWithString:legalText attributes:@{}]);
[[textView textStorage] setAttributedString:attributedMessage];
NSObject* link = nil;
[bridge_->view_controller_ textView:textView clickedOnLink:link atIndex:40];
EXPECT_FALSE(bubble_controller_->on_save_button_was_called());
EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called());
EXPECT_FALSE(bubble_controller_->on_learn_more_was_called());
EXPECT_TRUE(bubble_controller_->on_legal_message_was_called());
std::string url("http://help.example.com/privacy_policy");
EXPECT_EQ(url, bubble_controller_->legal_message_url());
EXPECT_FALSE(bubble_controller_->on_bubble_closed_was_called());
}
TEST_F(SaveCardBubbleViewTest, ReturnInvokesDefaultAction) {
[[bridge_->view_controller_ window]
performKeyEquivalent:cocoa_test_event_utils::KeyEventWithKeyCode(
kVK_Return, '\r', NSKeyDown, 0)];
EXPECT_TRUE(bubble_controller_->on_save_button_was_called());
EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called());
}
TEST_F(SaveCardBubbleViewTest, EscapeCloses) {
[[bridge_->view_controller_ window]
performKeyEquivalent:cocoa_test_event_utils::KeyEventWithKeyCode(
kVK_Escape, '\e', NSKeyDown, 0)];
EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called());
}
} // namespace autofill
|