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/content/browser/content_autofill_driver_factory.h"
15 #include "components/autofill/core/browser/autofill_manager.h"
16 #include "components/autofill/core/browser/test_autofill_client.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/page_navigator.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/common/url_constants.h"
22 #include "content/public/test/test_utils.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/gfx/geometry/rect.h"
30 class MockAutofillClient
: public TestAutofillClient
{
32 MockAutofillClient() {}
33 virtual ~MockAutofillClient() {}
35 virtual PrefService
* GetPrefs() { return &prefs_
; }
37 user_prefs::PrefRegistrySyncable
* GetPrefRegistry() {
38 return prefs_
.registry();
41 MOCK_METHOD4(ShowAutofillPopup
,
42 void(const gfx::RectF
& element_bounds
,
43 base::i18n::TextDirection text_direction
,
44 const std::vector
<autofill::Suggestion
>& suggestions
,
45 base::WeakPtr
<AutofillPopupDelegate
> delegate
));
47 MOCK_METHOD0(HideAutofillPopup
, void());
50 TestingPrefServiceSyncable prefs_
;
52 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient
);
55 // Subclass ContentAutofillDriver so we can create an ContentAutofillDriver
57 class TestContentAutofillDriver
: public ContentAutofillDriver
{
59 TestContentAutofillDriver(content::RenderFrameHost
* rfh
,
60 AutofillClient
* client
)
61 : ContentAutofillDriver(
64 g_browser_process
->GetApplicationLocale(),
65 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER
) {}
66 ~TestContentAutofillDriver() override
{}
69 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver
);
74 class ContentAutofillDriverBrowserTest
: public InProcessBrowserTest
,
75 public content::WebContentsObserver
{
77 ContentAutofillDriverBrowserTest() {}
78 virtual ~ContentAutofillDriverBrowserTest() {}
80 void SetUpOnMainThread() override
{
81 content::WebContents
* web_contents
=
82 browser()->tab_strip_model()->GetActiveWebContents();
83 ASSERT_TRUE(web_contents
!= NULL
);
84 Observe(web_contents
);
85 AutofillManager::RegisterProfilePrefs(autofill_client_
.GetPrefRegistry());
87 web_contents
->RemoveUserData(
88 ContentAutofillDriverFactory::
89 kContentAutofillDriverFactoryWebContentsUserDataKey
);
90 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
91 web_contents
, &autofill_client_
, "en-US",
92 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER
);
95 void WasHidden() override
{
96 if (!web_contents_hidden_callback_
.is_null())
97 web_contents_hidden_callback_
.Run();
100 void NavigationEntryCommitted(
101 const content::LoadCommittedDetails
& load_details
) override
{
102 if (!nav_entry_committed_callback_
.is_null())
103 nav_entry_committed_callback_
.Run();
107 base::Closure web_contents_hidden_callback_
;
108 base::Closure nav_entry_committed_callback_
;
110 testing::NiceMock
<MockAutofillClient
> autofill_client_
;
113 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest
,
114 SwitchTabAndHideAutofillPopup
) {
115 // Notification is different on platforms. On linux this will be called twice,
116 // while on windows only once.
117 EXPECT_CALL(autofill_client_
, HideAutofillPopup()).Times(testing::AtLeast(1));
119 scoped_refptr
<content::MessageLoopRunner
> runner
=
120 new content::MessageLoopRunner
;
121 web_contents_hidden_callback_
= runner
->QuitClosure();
122 chrome::AddSelectedTabWithURL(browser(),
123 GURL(url::kAboutBlankURL
),
124 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
);
126 web_contents_hidden_callback_
.Reset();
129 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest
,
130 TestPageNavigationHidingAutofillPopup
) {
131 // Notification is different on platforms. On linux this will be called twice,
132 // while on windows only once.
133 EXPECT_CALL(autofill_client_
, HideAutofillPopup()).Times(testing::AtLeast(1));
135 scoped_refptr
<content::MessageLoopRunner
> runner
=
136 new content::MessageLoopRunner
;
137 nav_entry_committed_callback_
= runner
->QuitClosure();
138 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIBookmarksURL
),
141 ui::PAGE_TRANSITION_TYPED
,
143 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIAboutURL
),
146 ui::PAGE_TRANSITION_TYPED
,
149 nav_entry_committed_callback_
.Reset();
152 } // namespace autofill