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 - 5888"); | |
35 } | |
36 base::string16 NewCard() { | |
37 return ASCIIToUTF16("Discover - 7582"); | |
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 EXPECT_TRUE(IsInstalled()); | |
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 } | |
90 | |
91 protected: | |
92 TestAutofillCreditCardBubbleController* controller() { | |
93 return static_cast<TestAutofillCreditCardBubbleController*>( | |
94 TestAutofillCreditCardBubbleController::FromWebContents( | |
95 test_web_contents_.get())); | |
96 } | |
97 | |
98 int GeneratedCardBubbleTimesShown() { | |
99 return profile()->GetPrefs()->GetInteger( | |
100 ::prefs::kAutofillGeneratedCardBubbleTimesShown); | |
101 } | |
102 | |
103 Profile* profile() { return &profile_; } | |
104 | |
105 content::WebContentsTester* test_web_contents() { | |
106 return content::WebContentsTester::For(test_web_contents_.get()); | |
107 } | |
108 | |
109 void ShowGeneratedCardUI() { | |
110 ASSERT_TRUE(controller()->IsInstalled()); | |
111 TestAutofillCreditCardBubbleController::ShowGeneratedCardUI( | |
112 test_web_contents_.get(), BackingCard(), FrontingCard()); | |
113 } | |
114 | |
115 void ShowNewCardSavedBubble() { | |
116 EXPECT_TRUE(controller()->IsInstalled()); | |
117 TestAutofillCreditCardBubbleController::ShowNewCardSavedBubble( | |
118 test_web_contents_.get(), NewCard()); | |
119 } | |
120 | |
121 void Navigate() { | |
122 NavigateWithTransition(content::PAGE_TRANSITION_LINK); | |
123 } | |
124 | |
125 void Redirect() { | |
126 NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT); | |
127 } | |
128 | |
129 private: | |
130 void NavigateWithTransition(content::PageTransition trans) { | |
131 test_web_contents()->TestDidNavigate( | |
132 test_web_contents_->GetRenderViewHost(), 1, GURL("about:blank"), trans); | |
133 } | |
134 | |
135 content::TestBrowserThreadBundle thread_bundle_; | |
136 #if defined(OS_WIN) | |
137 // Without this there will be drag and drop failures. http://crbug.com/227221 | |
138 ui::ScopedOleInitializer ole_initializer_; | |
139 #endif | |
140 TestingProfile profile_; | |
141 scoped_ptr<content::WebContents> test_web_contents_; | |
142 }; | |
143 | |
144 } // namespace | |
145 | |
146 TEST_F(AutofillCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) { | |
147 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); | |
148 | |
149 ShowGeneratedCardUI(); | |
150 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); | |
151 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
152 | |
153 ShowGeneratedCardUI(); | |
154 ShowGeneratedCardUI(); | |
155 EXPECT_EQ(3, GeneratedCardBubbleTimesShown()); | |
156 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
157 } | |
158 | |
159 TEST_F(AutofillCreditCardBubbleControllerTest, BubbleText) { | |
160 ShowGeneratedCardUI(); | |
161 base::string16 generated_text = controller()->BubbleText(); | |
162 EXPECT_NE(generated_text.find(BackingCard()), base::string16::npos); | |
163 EXPECT_NE(generated_text.find(FrontingCard()), base::string16::npos); | |
164 EXPECT_EQ(generated_text.find(NewCard()), base::string16::npos); | |
165 | |
166 ShowNewCardSavedBubble(); | |
167 base::string16 new_text = controller()->BubbleText(); | |
168 EXPECT_NE(new_text, generated_text); | |
169 EXPECT_EQ(new_text.find(BackingCard()), base::string16::npos); | |
170 EXPECT_EQ(new_text.find(FrontingCard()), base::string16::npos); | |
171 EXPECT_NE(new_text.find(NewCard()), base::string16::npos); | |
172 | |
173 ShowGeneratedCardUI(); | |
174 EXPECT_EQ(generated_text, controller()->BubbleText()); | |
175 | |
176 ShowNewCardSavedBubble(); | |
177 EXPECT_EQ(new_text, controller()->BubbleText()); | |
178 } | |
179 | |
180 TEST_F(AutofillCreditCardBubbleControllerTest, BubbleTextRanges) { | |
181 ShowGeneratedCardUI(); | |
182 base::string16 text = controller()->BubbleText(); | |
183 std::vector<ui::Range> ranges = controller()->BubbleTextRanges(); | |
184 | |
185 ASSERT_EQ(ranges.size(), 2U); | |
186 EXPECT_EQ(BackingCard(), RangeOfString(text, ranges[0])); | |
187 EXPECT_EQ(FrontingCard(), RangeOfString(text, ranges[1])); | |
188 | |
189 ShowNewCardSavedBubble(); | |
190 text = controller()->BubbleText(); | |
191 ranges = controller()->BubbleTextRanges(); | |
192 | |
193 ASSERT_EQ(ranges.size(), 1U); | |
194 EXPECT_EQ(NewCard(), RangeOfString(text, ranges[0])); | |
195 } | |
196 | |
197 TEST_F(AutofillCreditCardBubbleControllerTest, HideOnNavigate) { | |
198 EXPECT_FALSE(controller()->GetTestingBubble()); | |
199 ShowGeneratedCardUI(); | |
200 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
201 | |
202 Navigate(); | |
203 EXPECT_FALSE(controller()); | |
204 | |
205 SetUp(); | |
206 | |
207 EXPECT_FALSE(controller()->GetTestingBubble()); | |
208 ShowNewCardSavedBubble(); | |
209 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
210 | |
211 Navigate(); | |
212 EXPECT_FALSE(controller()); | |
213 } | |
214 | |
215 TEST_F(AutofillCreditCardBubbleControllerTest, StayOnRedirect) { | |
216 EXPECT_FALSE(controller()->GetTestingBubble()); | |
217 ShowGeneratedCardUI(); | |
218 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
219 | |
220 Redirect(); | |
221 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
222 | |
223 SetUp(); | |
224 | |
225 EXPECT_FALSE(controller()->GetTestingBubble()); | |
226 ShowNewCardSavedBubble(); | |
227 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
228 | |
229 Redirect(); | |
230 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
231 } | |
232 | |
233 } // namespace autofill | |
OLD | NEW |