- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / generated_credit_card_bubble_controller_unittest.cc
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/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"
24
25 #if defined(OS_WIN)
26 #include "ui/base/win/scoped_ole_initializer.h"
27 #endif
28
29 namespace autofill {
30
31 namespace {
32
33 base::string16 BackingCard() {
34   return ASCIIToUTF16("Visa - 1111");
35 }
36
37 base::string16 FrontingCard() {
38   return ASCIIToUTF16("Mastercard - 4444");
39 }
40
41 base::string16 RangeOfString(const base::string16& string,
42                              const gfx::Range& range) {
43   return string.substr(range.start(), range.end() - range.start());
44 }
45
46 class GeneratedCreditCardBubbleControllerTest : public testing::Test {
47  public:
48   GeneratedCreditCardBubbleControllerTest()
49       : test_web_contents_(
50             content::WebContentsTester::CreateTestWebContents(
51                 &profile_, NULL)) {}
52
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());
58   }
59
60  protected:
61   TestGeneratedCreditCardBubbleController* controller() {
62     return static_cast<TestGeneratedCreditCardBubbleController*>(
63         TestGeneratedCreditCardBubbleController::FromWebContents(
64             test_web_contents_.get()));
65   }
66
67   int GeneratedCardBubbleTimesShown() {
68     return profile_.GetPrefs()->GetInteger(
69         ::prefs::kAutofillGeneratedCardBubbleTimesShown);
70   }
71
72   void Show() {
73     ASSERT_TRUE(controller()->IsInstalled());
74     TestGeneratedCreditCardBubbleController::Show(test_web_contents_.get(),
75                                                   FrontingCard(),
76                                                   BackingCard());
77   }
78
79   void Navigate() {
80     NavigateWithTransition(content::PAGE_TRANSITION_LINK);
81   }
82
83   void Redirect() {
84     NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT);
85   }
86
87  private:
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);
91   }
92
93   content::TestBrowserThreadBundle thread_bundle_;
94 #if defined(OS_WIN)
95   // Without this there will be drag and drop failures. http://crbug.com/227221
96   ui::ScopedOleInitializer ole_initializer_;
97 #endif
98   TestingProfile profile_;
99   scoped_ptr<content::WebContents> test_web_contents_;
100 };
101
102 }  // namespace
103
104 TEST_F(GeneratedCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) {
105   ASSERT_EQ(0, GeneratedCardBubbleTimesShown());
106
107   // Ensure that showing the generated card UI bumps the persistent count.
108   Show();
109   EXPECT_EQ(1, GeneratedCardBubbleTimesShown());
110   EXPECT_TRUE(controller()->GetTestingBubble()->showing());
111
112   Show();
113   Show();
114   EXPECT_EQ(3, GeneratedCardBubbleTimesShown());
115   EXPECT_TRUE(controller()->GetTestingBubble()->showing());
116 }
117
118 TEST_F(GeneratedCreditCardBubbleControllerTest, TitleText) {
119   Show();
120   EXPECT_FALSE(controller()->TitleText().empty());
121 }
122
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".
126   Show();
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()));
130
131   // Make sure that |bubble_text_| is regenerated the same way in |Setup()|.
132   Show();
133   EXPECT_EQ(contents_text, controller()->ContentsText());
134 }
135
136 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsTextRanges) {
137   // Check that the highlighted ranges in the bubble's text are correct.
138   Show();
139   const base::string16& contents_text = controller()->ContentsText();
140   const std::vector<TextRange>& ranges = controller()->ContentsTextRanges();
141
142   ASSERT_EQ(3U, ranges.size());
143
144   EXPECT_EQ(FrontingCard(), RangeOfString(contents_text, ranges[0].range));
145   EXPECT_FALSE(ranges[0].is_link);
146
147   EXPECT_EQ(BackingCard(), RangeOfString(contents_text, ranges[1].range));
148   EXPECT_FALSE(ranges[1].is_link);
149
150   EXPECT_TRUE(ranges[2].is_link);
151
152   Show();
153   EXPECT_EQ(ranges, controller()->ContentsTextRanges());
154 }
155
156 TEST_F(GeneratedCreditCardBubbleControllerTest, AnchorIcon) {
157   Show();
158   EXPECT_FALSE(controller()->AnchorIcon().IsEmpty());
159 }
160
161 TEST_F(GeneratedCreditCardBubbleControllerTest, HideOnNavigate) {
162   // When a user navigates away from a page (or refreshes) normally, the bubble
163   // should be hidden.
164   EXPECT_FALSE(controller()->GetTestingBubble());
165   Show();
166   EXPECT_TRUE(controller()->GetTestingBubble()->showing());
167
168   Navigate();
169   EXPECT_FALSE(controller());
170 }
171
172 TEST_F(GeneratedCreditCardBubbleControllerTest, StayOnRedirect) {
173   // If a page redirects right after submitting, the bubble should remain.
174   EXPECT_FALSE(controller()->GetTestingBubble());
175   Show();
176   EXPECT_TRUE(controller()->GetTestingBubble()->showing());
177
178   Redirect();
179   EXPECT_TRUE(controller()->GetTestingBubble()->showing());
180 }
181
182 }  // namespace autofill