Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / autofill / content_autofill_driver_browsertest.cc
blobe87f5a2f8b63f7ecd00af9f9c894c7621863f9b1
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"
27 namespace autofill {
28 namespace {
30 class MockAutofillClient : public TestAutofillClient {
31 public:
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());
49 private:
50 TestingPrefServiceSyncable prefs_;
52 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
55 // Subclass ContentAutofillDriver so we can create an ContentAutofillDriver
56 // instance.
57 class TestContentAutofillDriver : public ContentAutofillDriver {
58 public:
59 TestContentAutofillDriver(content::RenderFrameHost* rfh,
60 AutofillClient* client)
61 : ContentAutofillDriver(
62 rfh,
63 client,
64 g_browser_process->GetApplicationLocale(),
65 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {}
66 ~TestContentAutofillDriver() override {}
68 private:
69 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver);
72 } // namespace
74 class ContentAutofillDriverBrowserTest : public InProcessBrowserTest,
75 public content::WebContentsObserver {
76 public:
77 ContentAutofillDriverBrowserTest() {}
78 virtual ~ContentAutofillDriverBrowserTest() {}
80 virtual 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 virtual void WasHidden() override {
96 if (!web_contents_hidden_callback_.is_null())
97 web_contents_hidden_callback_.Run();
100 virtual void NavigationEntryCommitted(
101 const content::LoadCommittedDetails& load_details) override {
102 if (!nav_entry_committed_callback_.is_null())
103 nav_entry_committed_callback_.Run();
106 protected:
107 content::WebContents* web_contents_;
109 base::Closure web_contents_hidden_callback_;
110 base::Closure nav_entry_committed_callback_;
112 testing::NiceMock<MockAutofillClient> autofill_client_;
115 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest,
116 SwitchTabAndHideAutofillPopup) {
117 // Notification is different on platforms. On linux this will be called twice,
118 // while on windows only once.
119 EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(testing::AtLeast(1));
121 scoped_refptr<content::MessageLoopRunner> runner =
122 new content::MessageLoopRunner;
123 web_contents_hidden_callback_ = runner->QuitClosure();
124 chrome::AddSelectedTabWithURL(browser(),
125 GURL(url::kAboutBlankURL),
126 ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
127 runner->Run();
128 web_contents_hidden_callback_.Reset();
131 IN_PROC_BROWSER_TEST_F(ContentAutofillDriverBrowserTest,
132 TestPageNavigationHidingAutofillPopup) {
133 // Notification is different on platforms. On linux this will be called twice,
134 // while on windows only once.
135 EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(testing::AtLeast(1));
137 scoped_refptr<content::MessageLoopRunner> runner =
138 new content::MessageLoopRunner;
139 nav_entry_committed_callback_ = runner->QuitClosure();
140 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIBookmarksURL),
141 content::Referrer(),
142 CURRENT_TAB,
143 ui::PAGE_TRANSITION_TYPED,
144 false));
145 browser()->OpenURL(content::OpenURLParams(GURL(chrome::kChromeUIAboutURL),
146 content::Referrer(),
147 CURRENT_TAB,
148 ui::PAGE_TRANSITION_TYPED,
149 false));
150 runner->Run();
151 nav_entry_committed_callback_.Reset();
154 } // namespace autofill