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/memory/scoped_ptr.h" 6 #include "chrome/browser/browser_process.h" 7 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/browser_tabstrip.h" 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" 10 #include "chrome/common/url_constants.h" 11 #include "chrome/test/base/in_process_browser_test.h" 12 #include "chrome/test/base/testing_pref_service_syncable.h" 13 #include "components/autofill/content/browser/autofill_driver_impl.h" 14 #include "components/autofill/core/browser/autofill_manager.h" 15 #include "components/autofill/core/browser/test_autofill_manager_delegate.h" 16 #include "content/public/browser/navigation_controller.h" 17 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_types.h" 19 #include "content/public/browser/page_navigator.h" 20 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents_observer.h" 22 #include "content/public/common/url_constants.h" 23 #include "content/public/test/test_utils.h" 24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "ui/gfx/rect.h" 27 28 namespace autofill { 29 namespace { 30 31 class MockAutofillManagerDelegate 32 : public autofill::TestAutofillManagerDelegate { 33 public: 34 MockAutofillManagerDelegate() {} 35 virtual ~MockAutofillManagerDelegate() {} 36 37 virtual PrefService* GetPrefs() { return &prefs_; } 38 39 user_prefs::PrefRegistrySyncable* GetPrefRegistry() { 40 return prefs_.registry(); 41 } 42 43 MOCK_METHOD7(ShowAutofillPopup, 44 void(const gfx::RectF& element_bounds, 45 base::i18n::TextDirection text_direction, 46 const std::vector<string16>& values, 47 const std::vector<string16>& labels, 48 const std::vector<string16>& icons, 49 const std::vector<int>& identifiers, 50 base::WeakPtr<AutofillPopupDelegate> delegate)); 51 52 MOCK_METHOD0(HideAutofillPopup, void()); 53 54 private: 55 TestingPrefServiceSyncable prefs_; 56 57 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); 58 }; 59 60 // Subclass AutofillDriverImpl so we can create an AutofillDriverImpl instance. 61 class TestAutofillDriverImpl : public AutofillDriverImpl { 62 public: 63 TestAutofillDriverImpl(content::WebContents* web_contents, 64 AutofillManagerDelegate* delegate) 65 : AutofillDriverImpl( 66 web_contents, 67 delegate, 68 g_browser_process->GetApplicationLocale(), 69 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {} 70 virtual ~TestAutofillDriverImpl() {} 71 72 private: 73 DISALLOW_COPY_AND_ASSIGN(TestAutofillDriverImpl); 74 }; 75 76 } // namespace 77 78 class AutofillDriverImplBrowserTest 79 : public InProcessBrowserTest, 80 public content::WebContentsObserver { 81 public: 82 AutofillDriverImplBrowserTest() {} 83 virtual ~AutofillDriverImplBrowserTest() {} 84 85 virtual void SetUpOnMainThread() OVERRIDE { 86 web_contents_ = browser()->tab_strip_model()->GetActiveWebContents(); 87 ASSERT_TRUE(web_contents_ != NULL); 88 Observe(web_contents_); 89 AutofillManager::RegisterProfilePrefs(manager_delegate_.GetPrefRegistry()); 90 91 autofill_driver_.reset(new TestAutofillDriverImpl(web_contents_, 92 &manager_delegate_)); 93 } 94 95 // Normally the WebContents will automatically delete the driver, but here 96 // the driver is owned by this test, so we have to manually destroy. 97 virtual void WebContentsDestroyed(content::WebContents* web_contents) 98 OVERRIDE { 99 DCHECK_EQ(web_contents_, web_contents); 100 autofill_driver_.reset(); 101 } 102 103 protected: 104 content::WebContents* web_contents_; 105 106 testing::NiceMock<MockAutofillManagerDelegate> manager_delegate_; 107 scoped_ptr<TestAutofillDriverImpl> autofill_driver_; 108 }; 109 110 IN_PROC_BROWSER_TEST_F(AutofillDriverImplBrowserTest, 111 SwitchTabAndHideAutofillPopup) { 112 // Notification is different on platforms. On linux this will be called twice, 113 // while on windows only once. 114 EXPECT_CALL(manager_delegate_, HideAutofillPopup()) 115 .Times(testing::AtLeast(1)); 116 117 content::WindowedNotificationObserver observer( 118 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, 119 content::Source<content::WebContents>(web_contents_)); 120 chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL), 121 content::PAGE_TRANSITION_AUTO_TOPLEVEL); 122 observer.Wait(); 123 } 124 125 IN_PROC_BROWSER_TEST_F(AutofillDriverImplBrowserTest, 126 TestPageNavigationHidingAutofillPopup) { 127 // Notification is different on platforms. On linux this will be called twice, 128 // while on windows only once. 129 EXPECT_CALL(manager_delegate_, HideAutofillPopup()) 130 .Times(testing::AtLeast(1)); 131 132 content::WindowedNotificationObserver observer( 133 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 134 content::Source<content::NavigationController>( 135 &(web_contents_->GetController()))); 136 browser()->OpenURL(content::OpenURLParams( 137 GURL(chrome::kChromeUIBookmarksURL), content::Referrer(), 138 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); 139 browser()->OpenURL(content::OpenURLParams( 140 GURL(chrome::kChromeUIAboutURL), content::Referrer(), 141 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); 142 observer.Wait(); 143 } 144 145 } // namespace autofill 146