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.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
13 #include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
14 #include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_view.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/autofill/core/browser/autofill_common_test.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/page_transition_types.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/public/test/web_contents_tester.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/range/range.h"
26 #include "ui/base/win/scoped_ole_initializer.h"
33 base::string16 BackingCard() {
34 return ASCIIToUTF16("Visa - 1111");
37 base::string16 FrontingCard() {
38 return ASCIIToUTF16("Mastercard - 4444");
41 base::string16 RangeOfString(const base::string16& string,
42 const gfx::Range& range) {
43 return string.substr(range.start(), range.end() - range.start());
46 class GeneratedCreditCardBubbleControllerTest : public testing::Test {
48 GeneratedCreditCardBubbleControllerTest()
50 content::WebContentsTester::CreateTestWebContents(
53 virtual void SetUp() OVERRIDE {
54 // Attaches immediately to |test_web_contents_| so a test version will exist
55 // before a non-test version can be created.
56 new TestGeneratedCreditCardBubbleController(test_web_contents_.get());
57 ASSERT_TRUE(controller()->IsInstalled());
61 TestGeneratedCreditCardBubbleController* controller() {
62 return static_cast<TestGeneratedCreditCardBubbleController*>(
63 TestGeneratedCreditCardBubbleController::FromWebContents(
64 test_web_contents_.get()));
67 int GeneratedCardBubbleTimesShown() {
68 return profile_.GetPrefs()->GetInteger(
69 ::prefs::kAutofillGeneratedCardBubbleTimesShown);
73 ASSERT_TRUE(controller()->IsInstalled());
74 TestGeneratedCreditCardBubbleController::Show(test_web_contents_.get(),
80 NavigateWithTransition(content::PAGE_TRANSITION_LINK);
84 NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT);
88 void NavigateWithTransition(content::PageTransition trans) {
89 content::WebContentsTester::For(test_web_contents_.get())->TestDidNavigate(
90 test_web_contents_->GetRenderViewHost(), 1, GURL("about:blank"), trans);
93 content::TestBrowserThreadBundle thread_bundle_;
95 // Without this there will be drag and drop failures. http://crbug.com/227221
96 ui::ScopedOleInitializer ole_initializer_;
98 TestingProfile profile_;
99 scoped_ptr<content::WebContents> test_web_contents_;
104 TEST_F(GeneratedCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) {
105 ASSERT_EQ(0, GeneratedCardBubbleTimesShown());
107 // Ensure that showing the generated card UI bumps the persistent count.
109 EXPECT_EQ(1, GeneratedCardBubbleTimesShown());
110 EXPECT_TRUE(controller()->GetTestingBubble()->showing());
114 EXPECT_EQ(3, GeneratedCardBubbleTimesShown());
115 EXPECT_TRUE(controller()->GetTestingBubble()->showing());
118 TEST_F(GeneratedCreditCardBubbleControllerTest, TitleText) {
120 EXPECT_FALSE(controller()->TitleText().empty());
123 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsText) {
124 // Ensure that while showing the generated card UI that the bubble's text
125 // contains "Visa - 1111" and "Mastercard - 4444".
127 base::string16 contents_text = controller()->ContentsText();
128 EXPECT_NE(base::string16::npos, contents_text.find(BackingCard()));
129 EXPECT_NE(base::string16::npos, contents_text.find(FrontingCard()));
131 // Make sure that |bubble_text_| is regenerated the same way in |Setup()|.
133 EXPECT_EQ(contents_text, controller()->ContentsText());
136 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsTextRanges) {
137 // Check that the highlighted ranges in the bubble's text are correct.
139 const base::string16& contents_text = controller()->ContentsText();
140 const std::vector<TextRange>& ranges = controller()->ContentsTextRanges();
142 ASSERT_EQ(3U, ranges.size());
144 EXPECT_EQ(FrontingCard(), RangeOfString(contents_text, ranges[0].range));
145 EXPECT_FALSE(ranges[0].is_link);
147 EXPECT_EQ(BackingCard(), RangeOfString(contents_text, ranges[1].range));
148 EXPECT_FALSE(ranges[1].is_link);
150 EXPECT_TRUE(ranges[2].is_link);
153 EXPECT_EQ(ranges, controller()->ContentsTextRanges());
156 TEST_F(GeneratedCreditCardBubbleControllerTest, AnchorIcon) {
158 EXPECT_FALSE(controller()->AnchorIcon().IsEmpty());
161 TEST_F(GeneratedCreditCardBubbleControllerTest, HideOnNavigate) {
162 // When a user navigates away from a page (or refreshes) normally, the bubble
164 EXPECT_FALSE(controller()->GetTestingBubble());
166 EXPECT_TRUE(controller()->GetTestingBubble()->showing());
169 EXPECT_FALSE(controller());
172 TEST_F(GeneratedCreditCardBubbleControllerTest, StayOnRedirect) {
173 // If a page redirects right after submitting, the bubble should remain.
174 EXPECT_FALSE(controller()->GetTestingBubble());
176 EXPECT_TRUE(controller()->GetTestingBubble()->showing());
179 EXPECT_TRUE(controller()->GetTestingBubble()->showing());
182 } // namespace autofill