generated_credit_card_bubble_controller_unittest.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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_view.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/gfx/range/range.h" 23 24#if defined(OS_WIN) 25#include "ui/base/win/scoped_ole_initializer.h" 26#endif 27 28namespace autofill { 29 30namespace { 31 32base::string16 BackingCard() { 33 return ASCIIToUTF16("Visa - 1111"); 34} 35 36base::string16 FrontingCard() { 37 return ASCIIToUTF16("Mastercard - 4444"); 38} 39 40base::string16 RangeOfString(const base::string16& string, 41 const gfx::Range& range) { 42 return string.substr(range.start(), range.end() - range.start()); 43} 44 45class TestGeneratedCreditCardBubbleController 46 : public GeneratedCreditCardBubbleController { 47 public: 48 explicit TestGeneratedCreditCardBubbleController( 49 content::WebContents* contents) 50 : GeneratedCreditCardBubbleController(contents) { 51 contents->SetUserData(UserDataKey(), this); 52 } 53 54 virtual ~TestGeneratedCreditCardBubbleController() {} 55 56 bool IsInstalled() const { 57 return web_contents()->GetUserData(UserDataKey()) == this; 58 } 59 60 TestGeneratedCreditCardBubbleView* GetTestingBubble() { 61 return static_cast<TestGeneratedCreditCardBubbleView*>( 62 GeneratedCreditCardBubbleController::bubble().get()); 63 } 64 65 protected: 66 virtual base::WeakPtr<GeneratedCreditCardBubbleView> CreateBubble() OVERRIDE { 67 return TestGeneratedCreditCardBubbleView::Create(GetWeakPtr()); 68 } 69 70 virtual bool CanShow() const OVERRIDE { 71 return true; 72 } 73 74 private: 75 DISALLOW_COPY_AND_ASSIGN(TestGeneratedCreditCardBubbleController); 76}; 77 78class GeneratedCreditCardBubbleControllerTest : public testing::Test { 79 public: 80 GeneratedCreditCardBubbleControllerTest() 81 : test_web_contents_( 82 content::WebContentsTester::CreateTestWebContents( 83 &profile_, NULL)) {} 84 85 virtual void SetUp() OVERRIDE { 86 // Attaches immediately to |test_web_contents_| so a test version will exist 87 // before a non-test version can be created. 88 new TestGeneratedCreditCardBubbleController(test_web_contents_.get()); 89 ASSERT_TRUE(controller()->IsInstalled()); 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 FrontingCard(), 108 BackingCard()); 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 136TEST_F(GeneratedCreditCardBubbleControllerTest, GeneratedCardBubbleTimesShown) { 137 ASSERT_EQ(0, GeneratedCardBubbleTimesShown()); 138 139 // Ensure that showing the generated card UI bumps the persistent count. 140 Show(); 141 EXPECT_EQ(1, GeneratedCardBubbleTimesShown()); 142 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); 143 144 Show(); 145 Show(); 146 EXPECT_EQ(3, GeneratedCardBubbleTimesShown()); 147 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); 148} 149 150TEST_F(GeneratedCreditCardBubbleControllerTest, TitleText) { 151 Show(); 152 EXPECT_FALSE(controller()->TitleText().empty()); 153} 154 155TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsText) { 156 // Ensure that while showing the generated card UI that the bubble's text 157 // contains "Visa - 1111" and "Mastercard - 4444". 158 Show(); 159 base::string16 contents_text = controller()->ContentsText(); 160 EXPECT_NE(base::string16::npos, contents_text.find(BackingCard())); 161 EXPECT_NE(base::string16::npos, contents_text.find(FrontingCard())); 162 163 // Make sure that |bubble_text_| is regenerated the same way in |Setup()|. 164 Show(); 165 EXPECT_EQ(contents_text, controller()->ContentsText()); 166} 167 168TEST_F(GeneratedCreditCardBubbleControllerTest, ContentsTextRanges) { 169 // Check that the highlighted ranges in the bubble's text are correct. 170 Show(); 171 const base::string16& contents_text = controller()->ContentsText(); 172 const std::vector<TextRange>& ranges = controller()->ContentsTextRanges(); 173 174 ASSERT_EQ(3U, ranges.size()); 175 176 EXPECT_EQ(FrontingCard(), RangeOfString(contents_text, ranges[0].range)); 177 EXPECT_FALSE(ranges[0].is_link); 178 179 EXPECT_EQ(BackingCard(), RangeOfString(contents_text, ranges[1].range)); 180 EXPECT_FALSE(ranges[1].is_link); 181 182 EXPECT_TRUE(ranges[2].is_link); 183 184 Show(); 185 EXPECT_EQ(ranges, controller()->ContentsTextRanges()); 186} 187 188TEST_F(GeneratedCreditCardBubbleControllerTest, AnchorIcon) { 189 Show(); 190 EXPECT_FALSE(controller()->AnchorIcon().IsEmpty()); 191} 192 193TEST_F(GeneratedCreditCardBubbleControllerTest, HideOnNavigate) { 194 // When a user navigates away from a page (or refreshes) normally, the bubble 195 // should be hidden. 196 EXPECT_FALSE(controller()->GetTestingBubble()); 197 Show(); 198 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); 199 200 Navigate(); 201 EXPECT_FALSE(controller()); 202} 203 204TEST_F(GeneratedCreditCardBubbleControllerTest, StayOnRedirect) { 205 // If a page redirects right after submitting, the bubble should remain. 206 EXPECT_FALSE(controller()->GetTestingBubble()); 207 Show(); 208 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); 209 210 Redirect(); 211 EXPECT_TRUE(controller()->GetTestingBubble()->showing()); 212} 213 214} // namespace autofill 215