| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 9 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #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/autofill_credit_card_bubble_controller.h" |
| 12 #include "chrome/browser/ui/autofill/test_autofill_credit_card_bubble.h" | 13 #include "chrome/browser/ui/autofill/test_autofill_credit_card_bubble.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/autofill/core/browser/autofill_common_test.h" |
| 17 #include "components/autofill/core/browser/autofill_profile.h" |
| 18 #include "components/autofill/core/browser/credit_card.h" |
| 15 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/common/page_transition_types.h" | 20 #include "content/public/common/page_transition_types.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "content/public/test/web_contents_tester.h" | 22 #include "content/public/test/web_contents_tester.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "ui/base/range/range.h" | 24 #include "ui/base/range/range.h" |
| 21 | 25 |
| 22 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 23 #include "ui/base/win/scoped_ole_initializer.h" | 27 #include "ui/base/win/scoped_ole_initializer.h" |
| 24 #endif | 28 #endif |
| 25 | 29 |
| 26 namespace autofill { | 30 namespace autofill { |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 base::string16 BackingCard() { | 34 base::string16 BackingCard() { |
| 31 return ASCIIToUTF16("Visa - 1111"); | 35 return ASCIIToUTF16("Visa - 1111"); |
| 32 } | 36 } |
| 33 base::string16 FrontingCard() { | 37 base::string16 FrontingCard() { |
| 34 return ASCIIToUTF16("Mastercard - 5888"); | 38 return ASCIIToUTF16("Mastercard - 5888"); |
| 35 } | 39 } |
| 36 base::string16 NewCard() { | 40 scoped_ptr<CreditCard> NewCard() { |
| 37 return ASCIIToUTF16("Discover - 7582"); | 41 return scoped_ptr<CreditCard>(new CreditCard(test::GetVerifiedCreditCard())); |
| 42 } |
| 43 scoped_ptr<AutofillProfile> BillingProfile() { |
| 44 return scoped_ptr<AutofillProfile>( |
| 45 new AutofillProfile(test::GetVerifiedProfile())); |
| 38 } | 46 } |
| 39 | 47 |
| 40 base::string16 RangeOfString(const base::string16& string, | 48 base::string16 RangeOfString(const base::string16& string, |
| 41 const ui::Range& range) { | 49 const ui::Range& range) { |
| 42 return string.substr(range.start(), range.end() - range.start()); | 50 return string.substr(range.start(), range.end() - range.start()); |
| 43 } | 51 } |
| 44 | 52 |
| 45 class TestAutofillCreditCardBubbleController | 53 class TestAutofillCreditCardBubbleController |
| 46 : public AutofillCreditCardBubbleController { | 54 : public AutofillCreditCardBubbleController { |
| 47 public: | 55 public: |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 return profile()->GetPrefs()->GetInteger( | 107 return profile()->GetPrefs()->GetInteger( |
| 100 ::prefs::kAutofillGeneratedCardBubbleTimesShown); | 108 ::prefs::kAutofillGeneratedCardBubbleTimesShown); |
| 101 } | 109 } |
| 102 | 110 |
| 103 Profile* profile() { return &profile_; } | 111 Profile* profile() { return &profile_; } |
| 104 | 112 |
| 105 content::WebContentsTester* test_web_contents() { | 113 content::WebContentsTester* test_web_contents() { |
| 106 return content::WebContentsTester::For(test_web_contents_.get()); | 114 return content::WebContentsTester::For(test_web_contents_.get()); |
| 107 } | 115 } |
| 108 | 116 |
| 109 void ShowGeneratedCardUI() { | 117 void ShowGeneratedCardBubble() { |
| 110 ASSERT_TRUE(controller()->IsInstalled()); | 118 ASSERT_TRUE(controller()->IsInstalled()); |
| 111 TestAutofillCreditCardBubbleController::ShowGeneratedCardUI( | 119 TestAutofillCreditCardBubbleController::ShowGeneratedCardBubble( |
| 112 test_web_contents_.get(), BackingCard(), FrontingCard()); | 120 test_web_contents_.get(), BackingCard(), FrontingCard()); |
| 113 } | 121 } |
| 114 | 122 |
| 115 void ShowNewCardSavedBubble() { | 123 void ShowNewCardSavedBubble() { |
| 116 EXPECT_TRUE(controller()->IsInstalled()); | 124 ASSERT_TRUE(controller()->IsInstalled()); |
| 117 TestAutofillCreditCardBubbleController::ShowNewCardSavedBubble( | 125 TestAutofillCreditCardBubbleController::ShowNewCardSavedBubble( |
| 118 test_web_contents_.get(), NewCard()); | 126 test_web_contents_.get(), NewCard(), BillingProfile()); |
| 119 } | 127 } |
| 120 | 128 |
| 121 void Navigate() { | 129 void Navigate() { |
| 122 NavigateWithTransition(content::PAGE_TRANSITION_LINK); | 130 NavigateWithTransition(content::PAGE_TRANSITION_LINK); |
| 123 } | 131 } |
| 124 | 132 |
| 125 void Redirect() { | 133 void Redirect() { |
| 126 NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT); | 134 NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT); |
| 127 } | 135 } |
| 128 | 136 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 139 #endif | 147 #endif |
| 140 TestingProfile profile_; | 148 TestingProfile profile_; |
| 141 scoped_ptr<content::WebContents> test_web_contents_; | 149 scoped_ptr<content::WebContents> test_web_contents_; |
| 142 }; | 150 }; |
| 143 | 151 |
| 144 } // namespace | 152 } // namespace |
| 145 | 153 |
| 146 TEST_F(AutofillCreditCardBubbleControllerTest, ShouldShowGeneratedCardBubble) { | 154 TEST_F(AutofillCreditCardBubbleControllerTest, ShouldShowGeneratedCardBubble) { |
| 147 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); | 155 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); |
| 148 | 156 |
| 149 ShowGeneratedCardUI(); | 157 ShowGeneratedCardBubble(); |
| 150 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); | 158 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); |
| 151 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 159 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 152 | 160 |
| 153 ShowGeneratedCardUI(); | 161 ShowGeneratedCardBubble(); |
| 154 ShowGeneratedCardUI(); | 162 ShowGeneratedCardBubble(); |
| 155 ShowGeneratedCardUI(); | 163 ShowGeneratedCardBubble(); |
| 156 ShowGeneratedCardUI(); | 164 ShowGeneratedCardBubble(); |
| 157 EXPECT_EQ(5, GeneratedCardBubbleTimesShown()); | 165 EXPECT_EQ(5, GeneratedCardBubbleTimesShown()); |
| 158 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 166 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 159 | 167 |
| 160 ShowGeneratedCardUI(); | 168 ShowGeneratedCardBubble(); |
| 161 EXPECT_EQ(5, GeneratedCardBubbleTimesShown()); | 169 EXPECT_EQ(5, GeneratedCardBubbleTimesShown()); |
| 162 EXPECT_FALSE(controller()->GetTestingBubble()); | 170 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 163 } | 171 } |
| 164 | 172 |
| 165 TEST_F(AutofillCreditCardBubbleControllerTest, BubbleText) { | 173 TEST_F(AutofillCreditCardBubbleControllerTest, TitleText) { |
| 166 ShowGeneratedCardUI(); | 174 ShowGeneratedCardBubble(); |
| 167 base::string16 generated_text = controller()->BubbleText(); | 175 EXPECT_FALSE(controller()->TitleText().empty()); |
| 176 |
| 177 ShowNewCardSavedBubble(); |
| 178 EXPECT_TRUE(controller()->TitleText().empty()); |
| 179 } |
| 180 |
| 181 TEST_F(AutofillCreditCardBubbleControllerTest, HeaderText) { |
| 182 ShowGeneratedCardBubble(); |
| 183 base::string16 generated_text = controller()->HeaderText(); |
| 168 EXPECT_NE(generated_text.find(BackingCard()), base::string16::npos); | 184 EXPECT_NE(generated_text.find(BackingCard()), base::string16::npos); |
| 169 EXPECT_NE(generated_text.find(FrontingCard()), base::string16::npos); | 185 EXPECT_NE(generated_text.find(FrontingCard()), base::string16::npos); |
| 170 EXPECT_EQ(generated_text.find(NewCard()), base::string16::npos); | |
| 171 | 186 |
| 172 ShowNewCardSavedBubble(); | 187 ShowNewCardSavedBubble(); |
| 173 base::string16 new_text = controller()->BubbleText(); | 188 base::string16 new_text = controller()->HeaderText(); |
| 174 EXPECT_NE(new_text, generated_text); | |
| 175 EXPECT_EQ(new_text.find(BackingCard()), base::string16::npos); | 189 EXPECT_EQ(new_text.find(BackingCard()), base::string16::npos); |
| 176 EXPECT_EQ(new_text.find(FrontingCard()), base::string16::npos); | 190 EXPECT_EQ(new_text.find(FrontingCard()), base::string16::npos); |
| 177 EXPECT_NE(new_text.find(NewCard()), base::string16::npos); | |
| 178 | 191 |
| 179 ShowGeneratedCardUI(); | 192 ShowGeneratedCardBubble(); |
| 180 EXPECT_EQ(generated_text, controller()->BubbleText()); | 193 EXPECT_EQ(generated_text, controller()->HeaderText()); |
| 181 | 194 |
| 182 ShowNewCardSavedBubble(); | 195 ShowNewCardSavedBubble(); |
| 183 EXPECT_EQ(new_text, controller()->BubbleText()); | 196 EXPECT_EQ(new_text, controller()->HeaderText()); |
| 184 } | 197 } |
| 185 | 198 |
| 186 TEST_F(AutofillCreditCardBubbleControllerTest, BubbleTextRanges) { | 199 TEST_F(AutofillCreditCardBubbleControllerTest, HeaderTextRanges) { |
| 187 ShowGeneratedCardUI(); | 200 ShowGeneratedCardBubble(); |
| 188 base::string16 text = controller()->BubbleText(); | 201 const base::string16& header_text = controller()->HeaderText(); |
| 189 std::vector<ui::Range> ranges = controller()->BubbleTextRanges(); | 202 const std::vector<TextRange>& ranges = controller()->HeaderTextRanges(); |
| 190 | 203 |
| 191 ASSERT_EQ(ranges.size(), 2U); | 204 ASSERT_EQ(ranges.size(), 3U); |
| 192 EXPECT_EQ(BackingCard(), RangeOfString(text, ranges[0])); | 205 |
| 193 EXPECT_EQ(FrontingCard(), RangeOfString(text, ranges[1])); | 206 EXPECT_EQ(BackingCard(), RangeOfString(header_text, ranges[0].range)); |
| 207 EXPECT_FALSE(ranges[0].is_link); |
| 208 |
| 209 EXPECT_EQ(FrontingCard(), RangeOfString(header_text, ranges[1].range)); |
| 210 EXPECT_FALSE(ranges[1].is_link); |
| 211 |
| 212 EXPECT_TRUE(ranges[2].is_link); |
| 194 | 213 |
| 195 ShowNewCardSavedBubble(); | 214 ShowNewCardSavedBubble(); |
| 196 text = controller()->BubbleText(); | 215 EXPECT_TRUE(controller()->HeaderTextRanges().empty()); |
| 197 ranges = controller()->BubbleTextRanges(); | 216 } |
| 198 | 217 |
| 199 ASSERT_EQ(ranges.size(), 1U); | 218 TEST_F(AutofillCreditCardBubbleControllerTest, AnchorIcon) { |
| 200 EXPECT_EQ(NewCard(), RangeOfString(text, ranges[0])); | 219 ShowGeneratedCardBubble(); |
| 220 EXPECT_FALSE(controller()->AnchorIcon().IsEmpty()); |
| 221 |
| 222 ShowNewCardSavedBubble(); |
| 223 EXPECT_TRUE(controller()->AnchorIcon().IsEmpty()); |
| 224 } |
| 225 |
| 226 TEST_F(AutofillCreditCardBubbleControllerTest, AnchorToSettingsMenu) { |
| 227 ShowGeneratedCardBubble(); |
| 228 EXPECT_FALSE(controller()->AnchorToSettingsMenu()); |
| 229 controller()->GetTestingBubble()->Hide(); |
| 230 EXPECT_TRUE(controller()); |
| 231 |
| 232 // Because the new card bubble has no anchor to re-show itself, its controller |
| 233 // should be destroyed when the bubble pops. |
| 234 ShowNewCardSavedBubble(); |
| 235 EXPECT_TRUE(controller()->AnchorToSettingsMenu()); |
| 236 controller()->GetTestingBubble()->Hide(); |
| 237 EXPECT_FALSE(controller()); |
| 201 } | 238 } |
| 202 | 239 |
| 203 TEST_F(AutofillCreditCardBubbleControllerTest, HideOnNavigate) { | 240 TEST_F(AutofillCreditCardBubbleControllerTest, HideOnNavigate) { |
| 204 EXPECT_FALSE(controller()->GetTestingBubble()); | 241 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 205 ShowGeneratedCardUI(); | 242 ShowGeneratedCardBubble(); |
| 206 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 243 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 207 | 244 |
| 208 Navigate(); | 245 Navigate(); |
| 209 EXPECT_FALSE(controller()); | 246 EXPECT_FALSE(controller()); |
| 210 | 247 |
| 211 SetUp(); | 248 SetUp(); |
| 212 | 249 |
| 213 EXPECT_FALSE(controller()->GetTestingBubble()); | 250 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 214 ShowNewCardSavedBubble(); | 251 ShowNewCardSavedBubble(); |
| 215 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 252 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 216 | 253 |
| 217 Navigate(); | 254 Navigate(); |
| 218 EXPECT_FALSE(controller()); | 255 EXPECT_FALSE(controller()); |
| 219 } | 256 } |
| 220 | 257 |
| 221 TEST_F(AutofillCreditCardBubbleControllerTest, StayOnRedirect) { | 258 TEST_F(AutofillCreditCardBubbleControllerTest, StayOnRedirect) { |
| 222 EXPECT_FALSE(controller()->GetTestingBubble()); | 259 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 223 ShowGeneratedCardUI(); | 260 ShowGeneratedCardBubble(); |
| 224 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 261 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 225 | 262 |
| 226 Redirect(); | 263 Redirect(); |
| 227 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 264 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 228 | 265 |
| 229 SetUp(); | 266 SetUp(); |
| 230 | 267 |
| 231 EXPECT_FALSE(controller()->GetTestingBubble()); | 268 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 232 ShowNewCardSavedBubble(); | 269 ShowNewCardSavedBubble(); |
| 233 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 270 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 234 | 271 |
| 235 Redirect(); | 272 Redirect(); |
| 236 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | 273 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 237 } | 274 } |
| 238 | 275 |
| 239 } // namespace autofill | 276 } // namespace autofill |
| OLD | NEW |