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