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.
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/browser/autofill_manager.h"
14 #include "components/autofill/browser/test_autofill_external_delegate.h"
15 #include "components/autofill/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/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/rect.h"
30 class MockAutofillManagerDelegate
31 : public autofill::TestAutofillManagerDelegate
{
33 MockAutofillManagerDelegate() {}
34 virtual ~MockAutofillManagerDelegate() {}
36 virtual PrefService
* GetPrefs() { return &prefs_
; }
38 user_prefs::PrefRegistrySyncable
* GetPrefRegistry() {
39 return prefs_
.registry();
42 MOCK_METHOD6(ShowAutofillPopup
,
43 void(const gfx::RectF
& element_bounds
,
44 const std::vector
<string16
>& values
,
45 const std::vector
<string16
>& labels
,
46 const std::vector
<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 AutofillManager so we can create AutofillManager instance.
59 class TestAutofillManager
: public AutofillManager
{
61 TestAutofillManager(content::WebContents
* web_contents
,
62 autofill::AutofillManagerDelegate
* delegate
)
63 : AutofillManager(web_contents
,
65 g_browser_process
->GetApplicationLocale(),
66 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER
) {}
67 virtual ~TestAutofillManager() {}
70 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager
);
73 // Subclass AutofillExternalDelegate so we can create an
74 // AutofillExternalDelegate instance.
75 class TestAutofillExternalDelegate
: public AutofillExternalDelegate
{
77 TestAutofillExternalDelegate(content::WebContents
* web_contents
,
78 AutofillManager
* autofill_manager
)
79 : AutofillExternalDelegate(web_contents
, autofill_manager
) {}
80 virtual ~TestAutofillExternalDelegate() {}
85 class AutofillExternalDelegateBrowserTest
86 : public InProcessBrowserTest
,
87 public content::WebContentsObserver
{
89 AutofillExternalDelegateBrowserTest() {}
90 virtual ~AutofillExternalDelegateBrowserTest() {}
92 virtual void SetUpOnMainThread() OVERRIDE
{
93 web_contents_
= browser()->tab_strip_model()->GetActiveWebContents();
94 ASSERT_TRUE(web_contents_
!= NULL
);
95 Observe(web_contents_
);
97 AutofillManager::RegisterUserPrefs(manager_delegate_
.GetPrefRegistry());
99 autofill_manager_
.reset(
100 new TestAutofillManager(web_contents_
, &manager_delegate_
));
101 autofill_external_delegate_
.reset(
102 new TestAutofillExternalDelegate(web_contents_
,
103 autofill_manager_
.get()));
106 // Normally the WebContents will automatically delete the delegate, but here
107 // the delegate is owned by this test, so we have to manually destroy.
108 virtual void WebContentsDestroyed(content::WebContents
* web_contents
)
110 DCHECK_EQ(web_contents_
, web_contents
);
111 autofill_external_delegate_
.reset();
112 autofill_manager_
.reset();
116 content::WebContents
* web_contents_
;
118 testing::NiceMock
<MockAutofillManagerDelegate
> manager_delegate_
;
119 scoped_ptr
<TestAutofillManager
> autofill_manager_
;
120 scoped_ptr
<TestAutofillExternalDelegate
> autofill_external_delegate_
;
123 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest
,
124 SwitchTabAndHideAutofillPopup
) {
125 autofill::GenerateTestAutofillPopup(autofill_external_delegate_
.get());
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));
132 content::WindowedNotificationObserver
observer(
133 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED
,
134 content::Source
<content::WebContents
>(web_contents_
));
135 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL
),
136 content::PAGE_TRANSITION_AUTO_TOPLEVEL
);
140 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest
,
141 TestPageNavigationHidingAutofillPopup
) {
142 autofill::GenerateTestAutofillPopup(autofill_external_delegate_
.get());
144 // Notification is different on platforms. On linux this will be called twice,
145 // while on windows only once.
146 EXPECT_CALL(manager_delegate_
, HideAutofillPopup())
147 .Times(testing::AtLeast(1));
149 content::WindowedNotificationObserver
observer(
150 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
151 content::Source
<content::NavigationController
>(
152 &(web_contents_
->GetController())));
153 browser()->OpenURL(content::OpenURLParams(
154 GURL(chrome::kChromeUIBookmarksURL
), content::Referrer(),
155 CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
156 browser()->OpenURL(content::OpenURLParams(
157 GURL(chrome::kChromeUIAboutURL
), content::Referrer(),
158 CURRENT_TAB
, content::PAGE_TRANSITION_TYPED
, false));
162 } // namespace autofill