| 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_controlle
r.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_test_utils.h" | |
| 18 #include "content/public/browser/navigation_details.h" | |
| 19 #include "content/public/browser/navigation_entry.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | |
| 22 #include "content/public/test/web_contents_tester.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 #include "ui/base/page_transition_types.h" | |
| 25 #include "ui/gfx/range/range.h" | |
| 26 | |
| 27 #if defined(OS_WIN) | |
| 28 #include "ui/base/win/scoped_ole_initializer.h" | |
| 29 #endif | |
| 30 | |
| 31 namespace autofill { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 base::string16 BackingCard() { | |
| 36 return base::ASCIIToUTF16("Visa - 1111"); | |
| 37 } | |
| 38 | |
| 39 base::string16 FrontingCard() { | |
| 40 return base::ASCIIToUTF16("Mastercard - 4444"); | |
| 41 } | |
| 42 | |
| 43 base::string16 RangeOfString(const base::string16& string, | |
| 44 const gfx::Range& range) { | |
| 45 return string.substr(range.start(), range.end() - range.start()); | |
| 46 } | |
| 47 | |
| 48 class GeneratedCreditCardBubbleControllerTest : public testing::Test { | |
| 49 public: | |
| 50 GeneratedCreditCardBubbleControllerTest() | |
| 51 : test_web_contents_( | |
| 52 content::WebContentsTester::CreateTestWebContents( | |
| 53 &profile_, NULL)) {} | |
| 54 | |
| 55 void SetUp() override { | |
| 56 // Attaches immediately to |test_web_contents_| so a test version will exist | |
| 57 // before a non-test version can be created. | |
| 58 new TestGeneratedCreditCardBubbleController(test_web_contents_.get()); | |
| 59 ASSERT_TRUE(controller()->IsInstalled()); | |
| 60 } | |
| 61 | |
| 62 protected: | |
| 63 TestGeneratedCreditCardBubbleController* controller() { | |
| 64 return static_cast<TestGeneratedCreditCardBubbleController*>( | |
| 65 TestGeneratedCreditCardBubbleController::FromWebContents( | |
| 66 test_web_contents_.get())); | |
| 67 } | |
| 68 | |
| 69 int GeneratedCardBubbleTimesShown() { | |
| 70 return profile_.GetPrefs()->GetInteger( | |
| 71 ::prefs::kAutofillGeneratedCardBubbleTimesShown); | |
| 72 } | |
| 73 | |
| 74 void Show() { | |
| 75 ASSERT_TRUE(controller()->IsInstalled()); | |
| 76 TestGeneratedCreditCardBubbleController::Show(test_web_contents_.get(), | |
| 77 FrontingCard(), | |
| 78 BackingCard()); | |
| 79 } | |
| 80 | |
| 81 void NavigateWithTransition(ui::PageTransition transition) { | |
| 82 content::LoadCommittedDetails details; | |
| 83 content::FrameNavigateParams params; | |
| 84 | |
| 85 // The transition is in two places; fill in both. | |
| 86 scoped_ptr<content::NavigationEntry> navigation_entry( | |
| 87 content::NavigationEntry::Create()); | |
| 88 navigation_entry->SetTransitionType(transition); | |
| 89 params.transition = transition; | |
| 90 details.entry = navigation_entry.get(); | |
| 91 | |
| 92 ASSERT_NE(nullptr, controller()); | |
| 93 controller()->DidNavigateMainFrame(details, params); | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 content::TestBrowserThreadBundle thread_bundle_; | |
| 98 #if defined(OS_WIN) | |
| 99 // Without this there will be drag and drop failures. http://crbug.com/227221 | |
| 100 ui::ScopedOleInitializer ole_initializer_; | |
| 101 #endif | |
| 102 TestingProfile profile_; | |
| 103 scoped_ptr<content::WebContents> test_web_contents_; | |
| 104 }; | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 TEST_F(GeneratedCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) { | |
| 109 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); | |
| 110 | |
| 111 // Ensure that showing the generated card UI bumps the persistent count. | |
| 112 Show(); | |
| 113 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); | |
| 114 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
| 115 | |
| 116 Show(); | |
| 117 Show(); | |
| 118 EXPECT_EQ(3, GeneratedCardBubbleTimesShown()); | |
| 119 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
| 120 } | |
| 121 | |
| 122 TEST_F(GeneratedCreditCardBubbleControllerTest, TitleText) { | |
| 123 Show(); | |
| 124 EXPECT_FALSE(controller()->TitleText().empty()); | |
| 125 } | |
| 126 | |
| 127 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsText) { | |
| 128 // Ensure that while showing the generated card UI that the bubble's text | |
| 129 // contains "Visa - 1111" and "Mastercard - 4444". | |
| 130 Show(); | |
| 131 base::string16 contents_text = controller()->ContentsText(); | |
| 132 EXPECT_NE(base::string16::npos, contents_text.find(BackingCard())); | |
| 133 EXPECT_NE(base::string16::npos, contents_text.find(FrontingCard())); | |
| 134 | |
| 135 // Make sure that |bubble_text_| is regenerated the same way in |Setup()|. | |
| 136 Show(); | |
| 137 EXPECT_EQ(contents_text, controller()->ContentsText()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsTextRanges) { | |
| 141 // Check that the highlighted ranges in the bubble's text are correct. | |
| 142 Show(); | |
| 143 const base::string16& contents_text = controller()->ContentsText(); | |
| 144 const std::vector<TextRange>& ranges = controller()->ContentsTextRanges(); | |
| 145 | |
| 146 ASSERT_EQ(3U, ranges.size()); | |
| 147 | |
| 148 EXPECT_EQ(FrontingCard(), RangeOfString(contents_text, ranges[0].range)); | |
| 149 EXPECT_FALSE(ranges[0].is_link); | |
| 150 | |
| 151 EXPECT_EQ(BackingCard(), RangeOfString(contents_text, ranges[1].range)); | |
| 152 EXPECT_FALSE(ranges[1].is_link); | |
| 153 | |
| 154 EXPECT_TRUE(ranges[2].is_link); | |
| 155 | |
| 156 Show(); | |
| 157 EXPECT_EQ(ranges, controller()->ContentsTextRanges()); | |
| 158 } | |
| 159 | |
| 160 TEST_F(GeneratedCreditCardBubbleControllerTest, AnchorIcon) { | |
| 161 Show(); | |
| 162 EXPECT_FALSE(controller()->AnchorIcon().IsEmpty()); | |
| 163 } | |
| 164 | |
| 165 TEST_F(GeneratedCreditCardBubbleControllerTest, HideOnLinkClick) { | |
| 166 EXPECT_FALSE(controller()->GetTestingBubble()); | |
| 167 Show(); | |
| 168 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
| 169 | |
| 170 // However, if the user clicks a link the bubble should hide. | |
| 171 NavigateWithTransition(ui::PAGE_TRANSITION_LINK); | |
| 172 EXPECT_FALSE(controller()); | |
| 173 } | |
| 174 | |
| 175 TEST_F(GeneratedCreditCardBubbleControllerTest, StayOnSomeNavigations) { | |
| 176 EXPECT_FALSE(controller()->GetTestingBubble()); | |
| 177 Show(); | |
| 178 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
| 179 | |
| 180 // If the user reloads or the page redirects or submits a form, the bubble | |
| 181 // should stay showing. | |
| 182 NavigateWithTransition(ui::PAGE_TRANSITION_CLIENT_REDIRECT); | |
| 183 NavigateWithTransition(ui::PAGE_TRANSITION_FORM_SUBMIT); | |
| 184 NavigateWithTransition(ui::PAGE_TRANSITION_RELOAD); | |
| 185 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); | |
| 186 } | |
| 187 | |
| 188 } // namespace autofill | |
| OLD | NEW |