| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/autofill/autofill_manager.h" | 6 #include "chrome/browser/autofill/autofill_manager.h" |
| 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 8 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 8 #include "chrome/browser/autofill/test_autofill_manager_delegate.h" |
| 9 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | |
| 10 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" | 10 #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 14 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 15 #include "content/public/browser/navigation_controller.h" | 15 #include "content/public/browser/navigation_controller.h" |
| 16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
| 18 #include "content/public/browser/page_navigator.h" | 18 #include "content/public/browser/page_navigator.h" |
| 19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/url_constants.h" | 20 #include "content/public/common/url_constants.h" |
| 21 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "ui/gfx/rect.h" | 24 #include "ui/gfx/rect.h" |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 class MockAutofillExternalDelegate : public AutofillExternalDelegate { | 28 class MockAutofillManagerDelegate |
| 29 : public autofill::TestAutofillManagerDelegate { |
| 28 public: | 30 public: |
| 29 explicit MockAutofillExternalDelegate(content::WebContents* web_contents) | 31 virtual PrefService* GetPrefs() { return &prefs_; } |
| 30 : AutofillExternalDelegate( | |
| 31 web_contents, | |
| 32 AutofillManager::FromWebContents(web_contents)), | |
| 33 popup_hidden_(true) {} | |
| 34 ~MockAutofillExternalDelegate() {} | |
| 35 | 32 |
| 36 virtual void DidSelectSuggestion(int unique_id) OVERRIDE {} | 33 PrefRegistrySyncable* GetPrefRegistry() { |
| 37 | 34 return prefs_.registry(); |
| 38 virtual void ClearPreviewedForm() OVERRIDE {} | |
| 39 | |
| 40 AutofillPopupControllerImpl* GetController() { | |
| 41 return controller(); | |
| 42 } | 35 } |
| 43 | 36 |
| 44 virtual void ApplyAutofillSuggestions( | 37 MOCK_METHOD6(ShowAutofillPopup, |
| 45 const std::vector<string16>& autofill_values, | 38 void(const gfx::RectF& element_bounds, |
| 46 const std::vector<string16>& autofill_labels, | 39 const std::vector<string16>& values, |
| 47 const std::vector<string16>& autofill_icons, | 40 const std::vector<string16>& labels, |
| 48 const std::vector<int>& autofill_unique_ids) OVERRIDE { | 41 const std::vector<string16>& icons, |
| 49 popup_hidden_ = false; | 42 const std::vector<int>& identifiers, |
| 43 AutofillPopupDelegate* delegate)); |
| 50 | 44 |
| 51 AutofillExternalDelegate::ApplyAutofillSuggestions(autofill_values, | 45 MOCK_METHOD0(HideAutofillPopup, void()); |
| 52 autofill_labels, | |
| 53 autofill_icons, | |
| 54 autofill_unique_ids); | |
| 55 } | |
| 56 | |
| 57 virtual void HideAutofillPopup() OVERRIDE { | |
| 58 popup_hidden_ = true; | |
| 59 | |
| 60 AutofillExternalDelegate::HideAutofillPopup(); | |
| 61 } | |
| 62 | |
| 63 bool popup_hidden() const { return popup_hidden_; } | |
| 64 | 46 |
| 65 private: | 47 private: |
| 66 bool popup_hidden_; | 48 TestingPrefServiceSyncable prefs_; |
| 49 }; |
| 50 |
| 51 // Subclass AutofillManager so we can create AutofillManager instance. |
| 52 class TestAutofillManager : public AutofillManager { |
| 53 public: |
| 54 TestAutofillManager(content::WebContents* web_contents, |
| 55 autofill::AutofillManagerDelegate* delegate) |
| 56 : AutofillManager(web_contents, delegate) {} |
| 57 virtual ~TestAutofillManager() {} |
| 58 |
| 59 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); |
| 61 }; |
| 62 |
| 63 class TestAutofillExternalDelegate : public AutofillExternalDelegate { |
| 64 public: |
| 65 TestAutofillExternalDelegate(content::WebContents* web_contents, |
| 66 AutofillManager* autofill_manager) |
| 67 : AutofillExternalDelegate(web_contents, autofill_manager) {} |
| 68 ~TestAutofillExternalDelegate() {} |
| 67 }; | 69 }; |
| 68 | 70 |
| 69 } // namespace | 71 } // namespace |
| 70 | 72 |
| 71 class AutofillExternalDelegateBrowserTest | 73 class AutofillExternalDelegateBrowserTest |
| 72 : public InProcessBrowserTest, | 74 : public InProcessBrowserTest, |
| 73 public content::WebContentsObserver { | 75 public content::WebContentsObserver { |
| 74 public: | 76 public: |
| 75 AutofillExternalDelegateBrowserTest() {} | 77 AutofillExternalDelegateBrowserTest() {} |
| 76 virtual ~AutofillExternalDelegateBrowserTest() {} | 78 virtual ~AutofillExternalDelegateBrowserTest() {} |
| 77 | 79 |
| 78 virtual void SetUpOnMainThread() OVERRIDE { | 80 virtual void SetUpOnMainThread() OVERRIDE { |
| 79 web_contents_ = browser()->tab_strip_model()->GetActiveWebContents(); | 81 web_contents_ = browser()->tab_strip_model()->GetActiveWebContents(); |
| 80 ASSERT_TRUE(web_contents_ != NULL); | 82 ASSERT_TRUE(web_contents_ != NULL); |
| 81 Observe(web_contents_); | 83 Observe(web_contents_); |
| 82 | 84 |
| 85 AutofillManager::RegisterUserPrefs(manager_delegate_.GetPrefRegistry()); |
| 86 |
| 87 autofill_manager_.reset( |
| 88 new TestAutofillManager(web_contents_, &manager_delegate_)); |
| 83 autofill_external_delegate_.reset( | 89 autofill_external_delegate_.reset( |
| 84 new MockAutofillExternalDelegate(web_contents_)); | 90 new TestAutofillExternalDelegate(web_contents_, |
| 91 autofill_manager_.get())); |
| 85 } | 92 } |
| 86 | 93 |
| 87 // Normally the WebContents will automatically delete the delegate, but here | 94 // Normally the WebContents will automatically delete the delegate, but here |
| 88 // the delegate is owned by this test, so we have to manually destroy. | 95 // the delegate is owned by this test, so we have to manually destroy. |
| 89 virtual void WebContentsDestroyed(content::WebContents* web_contents) | 96 virtual void WebContentsDestroyed(content::WebContents* web_contents) |
| 90 OVERRIDE { | 97 OVERRIDE { |
| 91 DCHECK_EQ(web_contents_, web_contents); | 98 DCHECK_EQ(web_contents_, web_contents); |
| 92 autofill_external_delegate_.reset(); | 99 autofill_external_delegate_.reset(); |
| 100 autofill_manager_.reset(); |
| 93 } | 101 } |
| 94 | 102 |
| 95 protected: | 103 protected: |
| 96 content::WebContents* web_contents_; | 104 content::WebContents* web_contents_; |
| 97 scoped_ptr<MockAutofillExternalDelegate> autofill_external_delegate_; | 105 |
| 106 testing::NiceMock<MockAutofillManagerDelegate> manager_delegate_; |
| 107 scoped_ptr<TestAutofillManager> autofill_manager_; |
| 108 scoped_ptr<TestAutofillExternalDelegate> autofill_external_delegate_; |
| 98 }; | 109 }; |
| 99 | 110 |
| 100 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, | 111 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, |
| 101 SwitchTabAndHideAutofillPopup) { | 112 SwitchTabAndHideAutofillPopup) { |
| 102 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); | 113 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); |
| 103 | 114 |
| 115 // Notification is different on platforms. On linux this will be called twice, |
| 116 // while on windows only once. |
| 117 EXPECT_CALL(manager_delegate_, HideAutofillPopup()) |
| 118 .Times(testing::AtLeast(1)); |
| 119 |
| 104 content::WindowedNotificationObserver observer( | 120 content::WindowedNotificationObserver observer( |
| 105 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | 121 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, |
| 106 content::Source<content::WebContents>(web_contents_)); | 122 content::Source<content::WebContents>(web_contents_)); |
| 107 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL), | 123 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL), |
| 108 content::PAGE_TRANSITION_AUTO_TOPLEVEL); | 124 content::PAGE_TRANSITION_AUTO_TOPLEVEL); |
| 109 observer.Wait(); | 125 observer.Wait(); |
| 110 | |
| 111 EXPECT_TRUE(autofill_external_delegate_->popup_hidden()); | |
| 112 } | 126 } |
| 113 | 127 |
| 114 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, | 128 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, |
| 115 TestPageNavigationHidingAutofillPopup) { | 129 TestPageNavigationHidingAutofillPopup) { |
| 116 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); | 130 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); |
| 117 | 131 |
| 118 EXPECT_FALSE(autofill_external_delegate_->popup_hidden()); | 132 // Notification is different on platforms. On linux this will be called twice, |
| 133 // while on windows only once. |
| 134 EXPECT_CALL(manager_delegate_, HideAutofillPopup()) |
| 135 .Times(testing::AtLeast(1)); |
| 119 | 136 |
| 120 content::WindowedNotificationObserver observer( | 137 content::WindowedNotificationObserver observer( |
| 121 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 138 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 122 content::Source<content::NavigationController>( | 139 content::Source<content::NavigationController>( |
| 123 &(web_contents_->GetController()))); | 140 &(web_contents_->GetController()))); |
| 124 browser()->OpenURL(content::OpenURLParams( | 141 browser()->OpenURL(content::OpenURLParams( |
| 125 GURL(chrome::kChromeUIBookmarksURL), content::Referrer(), | 142 GURL(chrome::kChromeUIBookmarksURL), content::Referrer(), |
| 126 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); | 143 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
| 127 browser()->OpenURL(content::OpenURLParams( | 144 browser()->OpenURL(content::OpenURLParams( |
| 128 GURL(chrome::kChromeUIAboutURL), content::Referrer(), | 145 GURL(chrome::kChromeUIAboutURL), content::Referrer(), |
| 129 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); | 146 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
| 130 observer.Wait(); | 147 observer.Wait(); |
| 131 | |
| 132 EXPECT_TRUE(autofill_external_delegate_->popup_hidden()); | |
| 133 } | 148 } |
| 134 | |
| 135 // Tests that closing the widget does not leak any resources. This test is | |
| 136 // only really meaningful when run on the memory bots. | |
| 137 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, | |
| 138 CloseWidgetAndNoLeaking) { | |
| 139 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); | |
| 140 | |
| 141 // Delete the view from under the delegate to ensure that the | |
| 142 // delegate and the controller can handle the popup getting deleted elsewhere. | |
| 143 autofill_external_delegate_->GetController()->view()->Hide(); | |
| 144 } | |
| OLD | NEW |