• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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/content/browser/autofill_driver_impl.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"
25 
26 namespace autofill {
27 namespace {
28 
29 class MockAutofillManagerDelegate
30     : public autofill::TestAutofillManagerDelegate {
31  public:
MockAutofillManagerDelegate()32   MockAutofillManagerDelegate() {}
~MockAutofillManagerDelegate()33   virtual ~MockAutofillManagerDelegate() {}
34 
GetPrefs()35   virtual PrefService* GetPrefs() { return &prefs_; }
36 
GetPrefRegistry()37   user_prefs::PrefRegistrySyncable* GetPrefRegistry() {
38     return prefs_.registry();
39   }
40 
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));
49 
50   MOCK_METHOD0(HideAutofillPopup, void());
51 
52  private:
53   TestingPrefServiceSyncable prefs_;
54 
55   DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate);
56 };
57 
58 // Subclass AutofillDriverImpl so we can create an AutofillDriverImpl instance.
59 class TestAutofillDriverImpl : public AutofillDriverImpl {
60  public:
TestAutofillDriverImpl(content::WebContents * web_contents,AutofillManagerDelegate * delegate)61   TestAutofillDriverImpl(content::WebContents* web_contents,
62                          AutofillManagerDelegate* delegate)
63       : AutofillDriverImpl(
64           web_contents,
65           delegate,
66           g_browser_process->GetApplicationLocale(),
67           AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {}
~TestAutofillDriverImpl()68   virtual ~TestAutofillDriverImpl() {}
69 
70  private:
71   DISALLOW_COPY_AND_ASSIGN(TestAutofillDriverImpl);
72 };
73 
74 }  // namespace
75 
76 class AutofillDriverImplBrowserTest
77     : public InProcessBrowserTest,
78       public content::WebContentsObserver {
79  public:
AutofillDriverImplBrowserTest()80   AutofillDriverImplBrowserTest() {}
~AutofillDriverImplBrowserTest()81   virtual ~AutofillDriverImplBrowserTest() {}
82 
SetUpOnMainThread()83   virtual void SetUpOnMainThread() OVERRIDE {
84     web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
85     ASSERT_TRUE(web_contents_ != NULL);
86     Observe(web_contents_);
87     AutofillManager::RegisterProfilePrefs(manager_delegate_.GetPrefRegistry());
88 
89     autofill_driver_.reset(new TestAutofillDriverImpl(web_contents_,
90                                                       &manager_delegate_));
91   }
92 
93   // Normally the WebContents will automatically delete the driver, but here
94   // the driver is owned by this test, so we have to manually destroy.
WebContentsDestroyed(content::WebContents * web_contents)95   virtual void WebContentsDestroyed(content::WebContents* web_contents)
96       OVERRIDE {
97     DCHECK_EQ(web_contents_, web_contents);
98     autofill_driver_.reset();
99   }
100 
WasHidden()101   virtual void WasHidden() OVERRIDE {
102     if (!web_contents_hidden_callback_.is_null())
103       web_contents_hidden_callback_.Run();
104   }
105 
NavigationEntryCommitted(const content::LoadCommittedDetails & load_details)106   virtual void NavigationEntryCommitted(
107       const content::LoadCommittedDetails& load_details) OVERRIDE {
108     if (!nav_entry_committed_callback_.is_null())
109       nav_entry_committed_callback_.Run();
110   }
111 
112  protected:
113   content::WebContents* web_contents_;
114 
115   base::Closure web_contents_hidden_callback_;
116   base::Closure nav_entry_committed_callback_;
117 
118   testing::NiceMock<MockAutofillManagerDelegate> manager_delegate_;
119   scoped_ptr<TestAutofillDriverImpl> autofill_driver_;
120 };
121 
IN_PROC_BROWSER_TEST_F(AutofillDriverImplBrowserTest,SwitchTabAndHideAutofillPopup)122 IN_PROC_BROWSER_TEST_F(AutofillDriverImplBrowserTest,
123                        SwitchTabAndHideAutofillPopup) {
124   // Notification is different on platforms. On linux this will be called twice,
125   // while on windows only once.
126   EXPECT_CALL(manager_delegate_, HideAutofillPopup())
127       .Times(testing::AtLeast(1));
128 
129   scoped_refptr<content::MessageLoopRunner> runner =
130       new content::MessageLoopRunner;
131   web_contents_hidden_callback_ = runner->QuitClosure();
132   chrome::AddSelectedTabWithURL(browser(), GURL(content::kAboutBlankURL),
133                                 content::PAGE_TRANSITION_AUTO_TOPLEVEL);
134   runner->Run();
135   web_contents_hidden_callback_.Reset();
136 }
137 
IN_PROC_BROWSER_TEST_F(AutofillDriverImplBrowserTest,TestPageNavigationHidingAutofillPopup)138 IN_PROC_BROWSER_TEST_F(AutofillDriverImplBrowserTest,
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));
144 
145   scoped_refptr<content::MessageLoopRunner> runner =
146       new content::MessageLoopRunner;
147   nav_entry_committed_callback_ = runner->QuitClosure();
148   browser()->OpenURL(content::OpenURLParams(
149       GURL(chrome::kChromeUIBookmarksURL), content::Referrer(),
150       CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
151   browser()->OpenURL(content::OpenURLParams(
152       GURL(chrome::kChromeUIAboutURL), content::Referrer(),
153       CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
154   runner->Run();
155   nav_entry_committed_callback_.Reset();
156 }
157 
158 }  // namespace autofill
159