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/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.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/autofill/core/browser/autofill_common_test.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/common/page_transition_types.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "content/public/test/web_contents_tester.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "ui/base/range/range.h" |
| 23 |
| 24 #if defined(OS_WIN) |
| 25 #include "ui/base/win/scoped_ole_initializer.h" |
| 26 #endif |
| 27 |
| 28 namespace autofill { |
| 29 |
| 30 namespace { |
| 31 |
| 32 base::string16 BackingCard() { |
| 33 return ASCIIToUTF16("Visa - 1111"); |
| 34 } |
| 35 |
| 36 base::string16 FrontingCard() { |
| 37 return ASCIIToUTF16("Mastercard - 4444"); |
| 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 TestGeneratedCreditCardBubbleController |
| 46 : public GeneratedCreditCardBubbleController { |
| 47 public: |
| 48 explicit TestGeneratedCreditCardBubbleController( |
| 49 content::WebContents* contents) |
| 50 : GeneratedCreditCardBubbleController(contents) { |
| 51 contents->SetUserData(UserDataKey(), this); |
| 52 CHECK(IsInstalled()); |
| 53 } |
| 54 |
| 55 virtual ~TestGeneratedCreditCardBubbleController() {} |
| 56 |
| 57 bool IsInstalled() const { |
| 58 return web_contents()->GetUserData(UserDataKey()) == this; |
| 59 } |
| 60 |
| 61 TestGeneratedCreditCardBubble* GetTestingBubble() { |
| 62 return static_cast<TestGeneratedCreditCardBubble*>( |
| 63 GeneratedCreditCardBubbleController::bubble().get()); |
| 64 } |
| 65 |
| 66 protected: |
| 67 virtual base::WeakPtr<GeneratedCreditCardBubble> CreateBubble() OVERRIDE { |
| 68 return TestGeneratedCreditCardBubble::Create(GetWeakPtr()); |
| 69 } |
| 70 |
| 71 virtual bool CanShow() const OVERRIDE { |
| 72 return true; |
| 73 } |
| 74 |
| 75 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(TestGeneratedCreditCardBubbleController); |
| 77 }; |
| 78 |
| 79 class GeneratedCreditCardBubbleControllerTest : public testing::Test { |
| 80 public: |
| 81 GeneratedCreditCardBubbleControllerTest() |
| 82 : test_web_contents_( |
| 83 content::WebContentsTester::CreateTestWebContents( |
| 84 &profile_, NULL)) {} |
| 85 |
| 86 virtual void SetUp() OVERRIDE { |
| 87 // Attaches immediately to |test_web_contents_| so a test version will exist |
| 88 // before a non-test version can be created. |
| 89 new TestGeneratedCreditCardBubbleController(test_web_contents_.get()); |
| 90 } |
| 91 |
| 92 protected: |
| 93 TestGeneratedCreditCardBubbleController* controller() { |
| 94 return static_cast<TestGeneratedCreditCardBubbleController*>( |
| 95 TestGeneratedCreditCardBubbleController::FromWebContents( |
| 96 test_web_contents_.get())); |
| 97 } |
| 98 |
| 99 int GeneratedCardBubbleTimesShown() { |
| 100 return profile_.GetPrefs()->GetInteger( |
| 101 ::prefs::kAutofillGeneratedCardBubbleTimesShown); |
| 102 } |
| 103 |
| 104 void Show() { |
| 105 ASSERT_TRUE(controller()->IsInstalled()); |
| 106 TestGeneratedCreditCardBubbleController::Show(test_web_contents_.get(), |
| 107 BackingCard(), |
| 108 FrontingCard()); |
| 109 } |
| 110 |
| 111 void Navigate() { |
| 112 NavigateWithTransition(content::PAGE_TRANSITION_LINK); |
| 113 } |
| 114 |
| 115 void Redirect() { |
| 116 NavigateWithTransition(content::PAGE_TRANSITION_CLIENT_REDIRECT); |
| 117 } |
| 118 |
| 119 private: |
| 120 void NavigateWithTransition(content::PageTransition trans) { |
| 121 content::WebContentsTester::For(test_web_contents_.get())->TestDidNavigate( |
| 122 test_web_contents_->GetRenderViewHost(), 1, GURL("about:blank"), trans); |
| 123 } |
| 124 |
| 125 content::TestBrowserThreadBundle thread_bundle_; |
| 126 #if defined(OS_WIN) |
| 127 // Without this there will be drag and drop failures. http://crbug.com/227221 |
| 128 ui::ScopedOleInitializer ole_initializer_; |
| 129 #endif |
| 130 TestingProfile profile_; |
| 131 scoped_ptr<content::WebContents> test_web_contents_; |
| 132 }; |
| 133 |
| 134 } // namespace |
| 135 |
| 136 TEST_F(GeneratedCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) { |
| 137 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); |
| 138 |
| 139 Show(); |
| 140 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); |
| 141 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 142 |
| 143 Show(); |
| 144 Show(); |
| 145 EXPECT_EQ(3, GeneratedCardBubbleTimesShown()); |
| 146 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 147 } |
| 148 |
| 149 TEST_F(GeneratedCreditCardBubbleControllerTest, TitleText) { |
| 150 Show(); |
| 151 EXPECT_FALSE(controller()->TitleText().empty()); |
| 152 } |
| 153 |
| 154 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsText) { |
| 155 Show(); |
| 156 base::string16 contents_text = controller()->ContentsText(); |
| 157 EXPECT_NE(contents_text.find(BackingCard()), base::string16::npos); |
| 158 EXPECT_NE(contents_text.find(FrontingCard()), base::string16::npos); |
| 159 |
| 160 Show(); |
| 161 EXPECT_EQ(contents_text, controller()->ContentsText()); |
| 162 } |
| 163 |
| 164 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsTextRanges) { |
| 165 Show(); |
| 166 const base::string16& contents_text = controller()->ContentsText(); |
| 167 const std::vector<TextRange>& ranges = controller()->ContentsTextRanges(); |
| 168 |
| 169 ASSERT_EQ(ranges.size(), 3U); |
| 170 |
| 171 EXPECT_EQ(BackingCard(), RangeOfString(contents_text, ranges[0].range)); |
| 172 EXPECT_FALSE(ranges[0].is_link); |
| 173 |
| 174 EXPECT_EQ(FrontingCard(), RangeOfString(contents_text, ranges[1].range)); |
| 175 EXPECT_FALSE(ranges[1].is_link); |
| 176 |
| 177 EXPECT_TRUE(ranges[2].is_link); |
| 178 |
| 179 Show(); |
| 180 EXPECT_EQ(ranges, controller()->ContentsTextRanges()); |
| 181 } |
| 182 |
| 183 TEST_F(GeneratedCreditCardBubbleControllerTest, AnchorIcon) { |
| 184 Show(); |
| 185 EXPECT_FALSE(controller()->AnchorIcon().IsEmpty()); |
| 186 } |
| 187 |
| 188 TEST_F(GeneratedCreditCardBubbleControllerTest, HideOnNavigate) { |
| 189 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 190 Show(); |
| 191 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 192 |
| 193 Navigate(); |
| 194 EXPECT_FALSE(controller()); |
| 195 } |
| 196 |
| 197 TEST_F(GeneratedCreditCardBubbleControllerTest, StayOnRedirect) { |
| 198 EXPECT_FALSE(controller()->GetTestingBubble()); |
| 199 Show(); |
| 200 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 201 |
| 202 Redirect(); |
| 203 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); |
| 204 } |
| 205 |
| 206 } // namespace autofill |
OLD | NEW |