1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/browser_process.h"
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_tabstrip.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/url_constants.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_pref_service_syncable.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/autofill/content/browser/content_autofill_driver.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "components/autofill/core/browser/autofill_manager.h"
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/autofill/core/browser/test_autofill_client.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/navigation_controller.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/page_navigator.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/common/url_constants.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/test/test_utils.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/rect.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace autofill {
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class MockAutofillClient : public TestAutofillClient {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  MockAutofillClient() {}
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual ~MockAutofillClient() {}
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual PrefService* GetPrefs() { return &prefs_; }
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  user_prefs::PrefRegistrySyncable* GetPrefRegistry() {
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return prefs_.registry();
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  MOCK_METHOD7(ShowAutofillPopup,
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               void(const gfx::RectF& element_bounds,
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    base::i18n::TextDirection text_direction,
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                    const std::vector<base::string16>& values,
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                    const std::vector<base::string16>& labels,
45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                    const std::vector<base::string16>& icons,
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    const std::vector<int>& identifiers,
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                    base::WeakPtr<AutofillPopupDelegate> delegate));
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MOCK_METHOD0(HideAutofillPopup, void());
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestingPrefServiceSyncable prefs_;
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Subclass ContentAutofillDriver so we can create an ContentAutofillDriver
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// instance.
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class TestContentAutofillDriver : public ContentAutofillDriver {
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TestContentAutofillDriver(content::WebContents* web_contents,
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                            AutofillClient* client)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      : ContentAutofillDriver(
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            web_contents,
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            client,
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            g_browser_process->GetApplicationLocale(),
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {}
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~TestContentAutofillDriver() {}
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver);
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ContentAutofillDriverBrowserTest : public InProcessBrowserTest,
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         public content::WebContentsObserver {
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ContentAutofillDriverBrowserTest() {}
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~ContentAutofillDriverBrowserTest() {}
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    content::WebContents* web_contents =
84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        browser()->tab_strip_model()->GetActiveWebContents();
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ASSERT_TRUE(web_contents != NULL);
86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Observe(web_contents);
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    AutofillManager::RegisterProfilePrefs(autofill_client_.GetPrefRegistry());
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    autofill_driver_.reset(
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        new TestContentAutofillDriver(web_contents, &autofill_client_));
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
93eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Normally the WebContents will automatically delete the driver, but here
94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // the driver is owned by this test, so we have to manually destroy.
95010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void WebContentsDestroyed() OVERRIDE {
967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    autofill_driver_.reset();
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void WasHidden() OVERRIDE {
1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!web_contents_hidden_callback_.is_null())
1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      web_contents_hidden_callback_.Run();
1024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  virtual void NavigationEntryCommitted(
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      const content::LoadCommittedDetails& load_details) OVERRIDE {
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if (!nav_entry_committed_callback_.is_null())
1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      nav_entry_committed_callback_.Run();
1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* web_contents_;
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::Closure web_contents_hidden_callback_;
1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::Closure nav_entry_committed_callback_;
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  testing::NiceMock<MockAutofillClient> autofill_client_;
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<TestContentAutofillDriver> autofill_driver_;
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest,
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       SwitchTabAndHideAutofillPopup) {
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification is different on platforms. On linux this will be called twice,
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // while on windows only once.
124f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(testing::AtLeast(1));
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_refptr<content::MessageLoopRunner> runner =
1274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      new content::MessageLoopRunner;
1284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  web_contents_hidden_callback_ = runner->QuitClosure();
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  chrome::AddSelectedTabWithURL(browser(),
130f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                GURL(url::kAboutBlankURL),
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
1324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  runner->Run();
1334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  web_contents_hidden_callback_.Reset();
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest,
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       TestPageNavigationHidingAutofillPopup) {
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification is different on platforms. On linux this will be called twice,
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // while on windows only once.
140f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(testing::AtLeast(1));
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_refptr<content::MessageLoopRunner> runner =
1434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      new content::MessageLoopRunner;
1444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  nav_entry_committed_callback_ = runner->QuitClosure();
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIBookmarksURL),
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            content::Referrer(),
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            CURRENT_TAB,
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                            ui::PAGE_TRANSITION_TYPED,
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            false));
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIAboutURL),
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            content::Referrer(),
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            CURRENT_TAB,
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                            ui::PAGE_TRANSITION_TYPED,
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                            false));
1554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  runner->Run();
1564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  nav_entry_committed_callback_.Reset();
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace autofill
160