summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_external_delegate_browsertest.cc
blob: c7ed801f05549ff9f109342fd8e0845e00c847bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/scoped_ptr.h"
#include "chrome/browser/autofill/autofill_manager.h"
#include "chrome/browser/autofill/test_autofill_external_delegate.h"
#include "chrome/browser/autofill/test_autofill_manager_delegate.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect.h"

namespace {

class MockAutofillManagerDelegate
    : public autofill::TestAutofillManagerDelegate {
 public:
  virtual PrefService* GetPrefs() { return &prefs_; }

  PrefRegistrySyncable* GetPrefRegistry() {
    return prefs_.registry();
  }

  MOCK_METHOD6(ShowAutofillPopup,
               void(const gfx::RectF& element_bounds,
                    const std::vector<string16>& values,
                    const std::vector<string16>& labels,
                    const std::vector<string16>& icons,
                    const std::vector<int>& identifiers,
                    AutofillPopupDelegate* delegate));

  MOCK_METHOD0(HideAutofillPopup, void());

 private:
  TestingPrefServiceSyncable prefs_;
};

// Subclass AutofillManager so we can create AutofillManager instance.
class TestAutofillManager : public AutofillManager {
 public:
  TestAutofillManager(content::WebContents* web_contents,
                      autofill::AutofillManagerDelegate* delegate)
      : AutofillManager(web_contents, delegate) {}
  virtual ~TestAutofillManager() {}

 private:
  DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
};

class TestAutofillExternalDelegate : public AutofillExternalDelegate {
 public:
  TestAutofillExternalDelegate(content::WebContents* web_contents,
                               AutofillManager* autofill_manager)
      : AutofillExternalDelegate(web_contents, autofill_manager) {}
  ~TestAutofillExternalDelegate() {}
};

}  // namespace

class AutofillExternalDelegateBrowserTest
    : public InProcessBrowserTest,
      public content::WebContentsObserver {
 public:
  AutofillExternalDelegateBrowserTest() {}
  virtual ~AutofillExternalDelegateBrowserTest() {}

  virtual void SetUpOnMainThread() OVERRIDE {
    web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
    ASSERT_TRUE(web_contents_ != NULL);
    Observe(web_contents_);

    AutofillManager::RegisterUserPrefs(manager_delegate_.GetPrefRegistry());

    autofill_manager_.reset(
        new TestAutofillManager(web_contents_, &manager_delegate_));
    autofill_external_delegate_.reset(
        new TestAutofillExternalDelegate(web_contents_,
                                         autofill_manager_.get()));
  }

  // Normally the WebContents will automatically delete the delegate, but here
  // the delegate is owned by this test, so we have to manually destroy.
  virtual void WebContentsDestroyed(content::WebContents* web_contents)
      OVERRIDE {
    DCHECK_EQ(web_contents_, web_contents);
    autofill_external_delegate_.reset();
    autofill_manager_.reset();
  }

 protected:
  content::WebContents* web_contents_;

  testing::NiceMock<MockAutofillManagerDelegate> manager_delegate_;
  scoped_ptr<TestAutofillManager> autofill_manager_;
  scoped_ptr<TestAutofillExternalDelegate> autofill_external_delegate_;
};

IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest,
                       SwitchTabAndHideAutofillPopup) {
  autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get());

  // Notification is different on platforms. On linux this will be called twice,
  // while on windows only once.
  EXPECT_CALL(manager_delegate_, HideAutofillPopup())
      .Times(testing::AtLeast(1));

  content::WindowedNotificationObserver observer(
      content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED,
      content::Source<content::WebContents>(web_contents_));
  chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL),
                                content::PAGE_TRANSITION_AUTO_TOPLEVEL);
  observer.Wait();
}

IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest,
                       TestPageNavigationHidingAutofillPopup) {
  autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get());

  // Notification is different on platforms. On linux this will be called twice,
  // while on windows only once.
  EXPECT_CALL(manager_delegate_, HideAutofillPopup())
      .Times(testing::AtLeast(1));

  content::WindowedNotificationObserver observer(
      content::NOTIFICATION_NAV_ENTRY_COMMITTED,
      content::Source<content::NavigationController>(
          &(web_contents_->GetController())));
  browser()->OpenURL(content::OpenURLParams(
      GURL(chrome::kChromeUIBookmarksURL), content::Referrer(),
      CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
  browser()->OpenURL(content::OpenURLParams(
      GURL(chrome::kChromeUIAboutURL), content::Referrer(),
      CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
  observer.Wait();
}