OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/basictypes.h" | |
6 #include "base/compiler_specific.h" | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/strings/string16.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.h" | |
12 #include "chrome/browser/ui/autofill/test_autofill_credit_card_bubble.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "chrome/test/base/testing_profile.h" | |
15 #include "content/public/browser/web_contents.h" | |
16 #include "content/public/common/page_transition_types.h" | |
17 #include "content/public/test/test_browser_thread_bundle.h" | |
18 #include "content/public/test/web_contents_tester.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "ui/base/range/range.h" | |
21 | |
22 #if defined(OS_WIN) | |
23 #include "ui/base/win/scoped_ole_initializer.h" | |
24 #endif | |
25 | |
26 namespace autofill { | |
27 | |
28 namespace { | |
29 | |
30 base::string16 BackingCard() { | |
31 return ASCIIToUTF16("Visa - 1111"); | |
32 } | |
33 base::string16 FrontingCard() { | |
34 return ASCIIToUTF16("Mastercard - 4444"); | |
35 } | |
36 base::string16 NewCard() { | |
37 return ASCIIToUTF16("Discover - 7777"); | |
38 } | |
39 | |
40 base::string16 RangeOfString(const base::string16& string, | |
41 const ui::Range& range) { | |
42 return string.substr(range.start(), range.end() - range.start()); | |
43 } | |
44 | |
45 class TestAutofillCreditCardBubbleController | |
46 : public AutofillCreditCardBubbleController { | |
47 public: | |
48 explicit TestAutofillCreditCardBubbleController( | |
49 content::WebContents* contents) | |
50 : AutofillCreditCardBubbleController(contents) { | |
51 contents->SetUserData(UserDataKey(), this); | |
52 } | |
53 | |
54 virtual ~TestAutofillCreditCardBubbleController() {} | |
55 | |
56 bool IsInstalled() const { | |
57 return web_contents()->GetUserData(UserDataKey()) == this; | |
58 } | |
59 | |
60 TestAutofillCreditCardBubble* GetTestingBubble() { | |
61 return static_cast<TestAutofillCreditCardBubble*>( | |
62 AutofillCreditCardBubbleController::bubble().get()); | |
63 } | |
64 | |
65 protected: | |
66 virtual base::WeakPtr<AutofillCreditCardBubble> CreateBubble() OVERRIDE { | |
67 return TestAutofillCreditCardBubble::Create(GetWeakPtr()); | |
68 } | |
69 | |
70 virtual bool CanShow() const OVERRIDE { | |
71 return true; | |
72 } | |
73 | |
74 private: | |
75 DISALLOW_COPY_AND_ASSIGN(TestAutofillCreditCardBubbleController); | |
76 }; | |
77 | |
78 class AutofillCreditCardBubbleControllerTest : public testing::Test { | |
79 public: | |
80 AutofillCreditCardBubbleControllerTest() | |
81 : test_web_contents_( | |
82 content::WebContentsTester::CreateTestWebContents( | |
83 profile(), NULL)) {} | |
84 | |
85 virtual void SetUp() OVERRIDE { | |
86 // Attaches immediately to |test_web_contents_| so a test version will exist | |
87 // before a non-test version can be created. | |
88 new TestAutofillCreditCardBubbleController(test_web_contents_.get()); | |
89 ASSERT_TRUE(controller()->IsInstalled()); | |
90 } | |
91 | |
92 protected: | |
93 TestAutofillCreditCardBubbleController* controller() { | |
94 return static_cast<TestAutofillCreditCardBubbleController*>( | |
95 TestAutofillCreditCardBubbleController::FromWebContents( | |
96 test_web_contents_.get())); | |
97 } | |
98 | |
99 int GeneratedCardBubbleTimesShown() { | |
100 return profile()->GetPrefs()->GetInteger( | |
101 ::prefs::kAutofillGeneratedCardBubbleTimesShown); | |
102 } | |
103 | |
104 Profile* profile() { return &profile_; } | |
105 | |
106 content::WebContentsTester* test_web_contents() { | |
107 return content::WebContentsTester::For(test_web_contents_.get()); | |
108 } | |
109 | |
110 void ShowGeneratedCardUI() { | |
111 ASSERT_TRUE(controller()->IsInstalled()); | |
112 TestAutofillCreditCardBubbleController::ShowGeneratedCardUI( | |
113 test_web_contents_.get(), BackingCard(), FrontingCard()); | |
114 } | |
115 | |
116 void ShowNewCardSavedBubble() { | |
117 ASSERT_TRUE(controller()->IsInstalled()); | |
118 TestAutofillCreditCardBubbleController::ShowNewCardSavedBubble( | |
119 test_web_contents_.get(), NewCard()); | |
120 } | |
121 | |
122 void Navigate() { | |
123 NavigateWithTransition(content::PAGE_TRANSITION_LINK); | |
124 } | |
125 | |
126 void Redirect() { | |
127 NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT); | |
128 } | |
129 | |
130 private: | |
131 void NavigateWithTransition(content::PageTransition trans) { | |
132 test_web_contents()->TestDidNavigate( | |
133 test_web_contents_->GetRenderViewHost(), 1, GURL("about:blank"), trans); | |
134 } | |
135 | |
136 content::TestBrowserThreadBundle thread_bundle_; | |
137 #if defined(OS_WIN) | |
138 // Without this there will be drag and drop failures. http://crbug.com/227221 | |
139 ui::ScopedOleInitializer ole_initializer_; | |
140 #endif | |
141 TestingProfile profile_; | |
142 scoped_ptr<content::WebContents> test_web_contents_; | |
143 }; | |
144 | |
145 } // namespace | |
146 | |
147 TEST_F(AutofillCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) { | |
148 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); | |
149 | |
150 // Ensure that showing the generated card UI bumps the persistent count. | |
151 ShowGeneratedCardUI(); | |
152 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); | |
153 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
154 | |
155 ShowGeneratedCardUI(); | |
156 ShowGeneratedCardUI(); | |
157 EXPECT_EQ(3, GeneratedCardBubbleTimesShown()); | |
158 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
159 } | |
160 | |
161 TEST_F(AutofillCreditCardBubbleControllerTest, BubbleText) { | |
162 // Ensure that while showing the generated card UI that the bubble's text | |
163 // contains "Visa - 1111" and "Mastercard - 4444". | |
164 ShowGeneratedCardUI(); | |
165 base::string16 generated_text = controller()->BubbleText(); | |
166 EXPECT_NE(base::string16::npos, generated_text.find(BackingCard())); | |
167 EXPECT_NE(base::string16::npos, generated_text.find(FrontingCard())); | |
168 EXPECT_EQ(base::string16::npos, generated_text.find(NewCard())); | |
169 | |
170 // Ensure that while showing the new card bubble that "Discover - 7777" is in | |
171 // the bubble text. | |
172 ShowNewCardSavedBubble(); | |
173 base::string16 new_text = controller()->BubbleText(); | |
174 EXPECT_NE(new_text, generated_text); | |
175 EXPECT_EQ(base::string16::npos, new_text.find(BackingCard())); | |
176 EXPECT_EQ(base::string16::npos, new_text.find(FrontingCard())); | |
177 EXPECT_NE(base::string16::npos, new_text.find(NewCard())); | |
178 | |
179 // Make sure that |bubble_text_| is regenerated the same way in |Setup()|. | |
180 ShowGeneratedCardUI(); | |
181 EXPECT_EQ(generated_text, controller()->BubbleText()); | |
182 | |
183 ShowNewCardSavedBubble(); | |
184 EXPECT_EQ(new_text, controller()->BubbleText()); | |
185 } | |
186 | |
187 TEST_F(AutofillCreditCardBubbleControllerTest, BubbleTextRanges) { | |
188 // Check that the highlighted ranges in the bubble's text are correct. | |
189 ShowGeneratedCardUI(); | |
190 base::string16 text = controller()->BubbleText(); | |
191 std::vector<ui::Range> ranges = controller()->BubbleTextRanges(); | |
192 | |
193 ASSERT_EQ(2U, ranges.size()); | |
194 EXPECT_EQ(BackingCard(), RangeOfString(text, ranges[0])); | |
195 EXPECT_EQ(FrontingCard(), RangeOfString(text, ranges[1])); | |
196 | |
197 ShowNewCardSavedBubble(); | |
198 text = controller()->BubbleText(); | |
199 ranges = controller()->BubbleTextRanges(); | |
200 | |
201 ASSERT_EQ(1U, ranges.size()); | |
202 EXPECT_EQ(NewCard(), RangeOfString(text, ranges[0])); | |
203 } | |
204 | |
205 TEST_F(AutofillCreditCardBubbleControllerTest, HideOnNavigate) { | |
206 // When a user navigates away from a page (or refreshes) normally, the bubbles | |
207 // should be hidden. | |
208 EXPECT_FALSE(controller()->GetTestingBubble()); | |
209 ShowGeneratedCardUI(); | |
210 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
211 | |
212 Navigate(); | |
213 EXPECT_FALSE(controller()); | |
214 | |
215 SetUp(); | |
216 | |
217 EXPECT_FALSE(controller()->GetTestingBubble()); | |
218 ShowNewCardSavedBubble(); | |
219 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
220 | |
221 Navigate(); | |
222 EXPECT_FALSE(controller()); | |
223 } | |
224 | |
225 TEST_F(AutofillCreditCardBubbleControllerTest, StayOnRedirect) { | |
226 // If a page redirects right after submitting, the bubble should remain. | |
227 EXPECT_FALSE(controller()->GetTestingBubble()); | |
228 ShowGeneratedCardUI(); | |
229 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
230 | |
231 Redirect(); | |
232 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
233 | |
234 SetUp(); | |
235 | |
236 EXPECT_FALSE(controller()->GetTestingBubble()); | |
237 ShowNewCardSavedBubble(); | |
238 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
239 | |
240 Redirect(); | |
241 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
242 } | |
243 | |
244 } // namespace autofill | |
OLD | NEW |