1 // Copyright 2014 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.
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/content_autofill_driver.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/page_navigator.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/common/url_constants.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/gfx/rect.h"
29 class MockAutofillManagerDelegate
30 : public autofill::TestAutofillManagerDelegate
{
32 MockAutofillManagerDelegate() {}
33 virtual ~MockAutofillManagerDelegate() {}
35 virtual PrefService
* GetPrefs() { return &prefs_
; }
37 user_prefs::PrefRegistrySyncable
* GetPrefRegistry() {
38 return prefs_
.registry();
41 MOCK_METHOD7(ShowAutofillPopup
,
42 void(const gfx::RectF
& element_bounds
,
43 base::i18n::TextDirection text_direction
,
44 const std::vector
<base::string16
>& values
,
45 const std::vector
<base::string16
>& labels
,
46 const std::vector
<base::string16
>& icons
,
47 const std::vector
<int>& identifiers
,
48 base::WeakPtr
<AutofillPopupDelegate
> delegate
));
50 MOCK_METHOD0(HideAutofillPopup
, void());
53 TestingPrefServiceSyncable prefs_
;
55 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate
);
58 // Subclass ContentAutofillDriver so we can create an ContentAutofillDriver
60 class TestContentAutofillDriver
: public ContentAutofillDriver
{
62 TestContentAutofillDriver(content::WebContents
* web_contents
,
63 AutofillManagerDelegate
* delegate
)
64 : ContentAutofillDriver(
67 g_browser_process
->GetApplicationLocale(),
68 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER
) {}
69 virtual ~TestContentAutofillDriver() {}
72 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver
);
77 class ContentAutofillDriverBrowserTest
: public InProcessBrowserTest
,
78 public content::WebContentsObserver
{
80 ContentAutofillDriverBrowserTest() {}
81 virtual ~ContentAutofillDriverBrowserTest() {}
83 virtual void SetUpOnMainThread() OVERRIDE
{
84 content::WebContents
* web_contents
=
85 browser()->tab_strip_model()->GetActiveWebContents();
86 ASSERT_TRUE(web_contents
!= NULL
);
87 Observe(web_contents
);
88 AutofillManager::RegisterProfilePrefs(manager_delegate_
.GetPrefRegistry());
90 autofill_driver_
.reset(
91 new TestContentAutofillDriver(web_contents
, &manager_delegate_
));
94 // Normally the WebContents will automatically delete the driver, but here
95 // the driver is owned by this test, so we have to manually destroy.
96 virtual void WebContentsDestroyed() OVERRIDE
{
97 autofill_driver_
.reset();
100 virtual void WasHidden() OVERRIDE
{
101 if (!web_contents_hidden_callback_
.is_null())
102 web_contents_hidden_callback_
.Run();
105 virtual void NavigationEntryCommitted(
106 const content::LoadCommittedDetails
& load_details
) OVERRIDE
{
107 if (!nav_entry_committed_callback_
.is_null())
108 nav_entry_committed_callback_
.Run();
112 content::WebContents
* web_contents_
;
114 base::Closure web_contents_hidden_callback_
;
115 base::Closure nav_entry_committed_callback_
;
117 testing::NiceMock
<MockAutofillManagerDelegate
> manager_delegate_
;
118 scoped_ptr
<TestContentAutofillDriver
> autofill_driver_
;
121 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest
,
122 SwitchTabAndHideAutofillPopup
) {
123 // Notification is different on platforms. On linux this will be called twice,
124 // while on windows only once.
125 EXPECT_CALL(manager_delegate_
, HideAutofillPopup())
126 .Times(testing::AtLeast(1));
128 scoped_refptr
<content::MessageLoopRunner
> runner
=
129 new content::MessageLoopRunner
;
130 web_contents_hidden_callback_
= runner
->QuitClosure();
131 chrome::AddSelectedTabWithURL(browser(),
132 GURL(content::kAboutBlankURL
),
133 content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
135 web_contents_hidden_callback_
.Reset();
138 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest
,
139 TestPageNavigationHidingAutofillPopup
) {
140 // Notification is different on platforms. On linux this will be called twice,
141 // while on windows only once.
142 EXPECT_CALL(manager_delegate_
, HideAutofillPopup())
143 .Times(testing::AtLeast(1));
145 scoped_refptr
<content::MessageLoopRunner
> runner
=
146 new content::MessageLoopRunner
;
147 nav_entry_committed_callback_
= runner
->QuitClosure();
148 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIBookmarksURL
),
151 content::PAGE_TRANSITION_TYPED
,
153 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIAboutURL
),
156 content::PAGE_TRANSITION_TYPED
,
159 nav_entry_committed_callback_
.Reset();
162 } // namespace autofill