Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(710)

Side by Side Diff: chrome/browser/autofill/autofill_external_delegate_browsertest.cc

Issue 12340065: Move the UI related code from AutofillExternalDelegate to AutofillManagerDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "chrome/browser/autofill/autofill_manager.h" 6 #include "chrome/browser/autofill/autofill_manager.h"
7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h"
8 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" 8 #include "chrome/browser/autofill/test_autofill_manager_delegate.h"
9 #include "chrome/browser/ui/autofill/autofill_popup_view.h"
10 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_tabstrip.h" 10 #include "chrome/browser/ui/browser_tabstrip.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/url_constants.h" 12 #include "chrome/common/url_constants.h"
14 #include "chrome/test/base/in_process_browser_test.h" 13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/testing_pref_service_syncable.h"
15 #include "content/public/browser/navigation_controller.h" 15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/notification_service.h" 16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/notification_types.h" 17 #include "content/public/browser/notification_types.h"
18 #include "content/public/browser/page_navigator.h" 18 #include "content/public/browser/page_navigator.h"
19 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/url_constants.h" 20 #include "content/public/common/url_constants.h"
21 #include "content/public/test/test_utils.h" 21 #include "content/public/test/test_utils.h"
22 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/rect.h" 24 #include "ui/gfx/rect.h"
24 25
25 namespace { 26 namespace {
26 27
27 class MockAutofillExternalDelegate : public AutofillExternalDelegate { 28 class MockAutofillManagerDelegate
29 : public autofill::TestAutofillManagerDelegate {
28 public: 30 public:
29 explicit MockAutofillExternalDelegate(content::WebContents* web_contents) 31 virtual PrefService* GetPrefs() { return &prefs_; }
30 : AutofillExternalDelegate(
31 web_contents,
32 AutofillManager::FromWebContents(web_contents)),
33 popup_hidden_(true) {}
34 ~MockAutofillExternalDelegate() {}
35 32
36 virtual void DidSelectSuggestion(int unique_id) OVERRIDE {} 33 PrefRegistrySyncable* GetPrefRegistry() {
37 34 return prefs_.registry();
38 virtual void ClearPreviewedForm() OVERRIDE {}
39
40 AutofillPopupControllerImpl* GetController() {
41 return controller();
42 } 35 }
43 36
44 virtual void ApplyAutofillSuggestions( 37 MOCK_METHOD6(ShowAutofillPopup,
45 const std::vector<string16>& autofill_values, 38 void(const gfx::RectF& element_bounds,
46 const std::vector<string16>& autofill_labels, 39 const std::vector<string16>& values,
47 const std::vector<string16>& autofill_icons, 40 const std::vector<string16>& labels,
48 const std::vector<int>& autofill_unique_ids) OVERRIDE { 41 const std::vector<string16>& icons,
49 popup_hidden_ = false; 42 const std::vector<int>& identifiers,
43 AutofillPopupDelegate* delegate));
50 44
51 AutofillExternalDelegate::ApplyAutofillSuggestions(autofill_values, 45 MOCK_METHOD0(HideAutofillPopup, void());
52 autofill_labels,
53 autofill_icons,
54 autofill_unique_ids);
55 }
56
57 virtual void HideAutofillPopup() OVERRIDE {
58 popup_hidden_ = true;
59
60 AutofillExternalDelegate::HideAutofillPopup();
61 }
62
63 bool popup_hidden() const { return popup_hidden_; }
64 46
65 private: 47 private:
66 bool popup_hidden_; 48 TestingPrefServiceSyncable prefs_;
49 };
50
51 // Subclass AutofillManager so we can create AutofillManager instance.
52 class TestAutofillManager : public AutofillManager {
53 public:
54 TestAutofillManager(content::WebContents* web_contents,
55 autofill::AutofillManagerDelegate* delegate)
56 : AutofillManager(web_contents, delegate) {}
57 virtual ~TestAutofillManager() {}
Ilya Sherman 2013/02/26 22:55:51 nit: Please leave a blank line below this one.
kaiwang 2013/02/27 00:00:10 Done.
58 private:
59 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
60 };
61
62 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
63 public:
64 TestAutofillExternalDelegate(content::WebContents* web_contents,
65 AutofillManager* autofill_manager)
66 : AutofillExternalDelegate(web_contents, autofill_manager) {}
67 ~TestAutofillExternalDelegate() {}
67 }; 68 };
68 69
69 } // namespace 70 } // namespace
70 71
71 class AutofillExternalDelegateBrowserTest 72 class AutofillExternalDelegateBrowserTest
72 : public InProcessBrowserTest, 73 : public InProcessBrowserTest,
73 public content::WebContentsObserver { 74 public content::WebContentsObserver {
74 public: 75 public:
75 AutofillExternalDelegateBrowserTest() {} 76 AutofillExternalDelegateBrowserTest() {}
76 virtual ~AutofillExternalDelegateBrowserTest() {} 77 virtual ~AutofillExternalDelegateBrowserTest() {}
77 78
78 virtual void SetUpOnMainThread() OVERRIDE { 79 virtual void SetUpOnMainThread() OVERRIDE {
79 web_contents_ = browser()->tab_strip_model()->GetActiveWebContents(); 80 web_contents_ = browser()->tab_strip_model()->GetActiveWebContents();
80 ASSERT_TRUE(web_contents_ != NULL); 81 ASSERT_TRUE(web_contents_ != NULL);
81 Observe(web_contents_); 82 Observe(web_contents_);
82 83
84 AutofillManager::RegisterUserPrefs(manager_delegate_.GetPrefRegistry());
85
86 autofill_manager_.reset(
87 new TestAutofillManager(web_contents_, &manager_delegate_));
83 autofill_external_delegate_.reset( 88 autofill_external_delegate_.reset(
84 new MockAutofillExternalDelegate(web_contents_)); 89 new TestAutofillExternalDelegate(web_contents_,
90 autofill_manager_.get()));
85 } 91 }
86 92
87 // Normally the WebContents will automatically delete the delegate, but here 93 // Normally the WebContents will automatically delete the delegate, but here
88 // the delegate is owned by this test, so we have to manually destroy. 94 // the delegate is owned by this test, so we have to manually destroy.
89 virtual void WebContentsDestroyed(content::WebContents* web_contents) 95 virtual void WebContentsDestroyed(content::WebContents* web_contents)
90 OVERRIDE { 96 OVERRIDE {
91 DCHECK_EQ(web_contents_, web_contents); 97 DCHECK_EQ(web_contents_, web_contents);
92 autofill_external_delegate_.reset(); 98 autofill_external_delegate_.reset();
99 autofill_manager_.reset();
93 } 100 }
94 101
95 protected: 102 protected:
96 content::WebContents* web_contents_; 103 content::WebContents* web_contents_;
97 scoped_ptr<MockAutofillExternalDelegate> autofill_external_delegate_; 104
105 testing::NiceMock<MockAutofillManagerDelegate> manager_delegate_;
106 scoped_ptr<TestAutofillManager> autofill_manager_;
107 scoped_ptr<TestAutofillExternalDelegate> autofill_external_delegate_;
98 }; 108 };
99 109
100 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, 110 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest,
101 SwitchTabAndHideAutofillPopup) { 111 SwitchTabAndHideAutofillPopup) {
102 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); 112 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get());
103 113
114 // Both content::NOTIFICATION_NAV_ENTRY_COMMITTED and
115 // content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED will be triggered.
116 // So HideAutofillPopup will be called twice.
117 EXPECT_CALL(manager_delegate_, HideAutofillPopup()).Times(2);
118
104 content::WindowedNotificationObserver observer( 119 content::WindowedNotificationObserver observer(
105 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, 120 content::NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED,
106 content::Source<content::WebContents>(web_contents_)); 121 content::Source<content::WebContents>(web_contents_));
107 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL), 122 chrome::AddSelectedTabWithURL(browser(), GURL(chrome::kAboutBlankURL),
108 content::PAGE_TRANSITION_AUTO_TOPLEVEL); 123 content::PAGE_TRANSITION_AUTO_TOPLEVEL);
109 observer.Wait(); 124 observer.Wait();
110
111 EXPECT_TRUE(autofill_external_delegate_->popup_hidden());
112 } 125 }
113 126
114 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest, 127 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest,
115 TestPageNavigationHidingAutofillPopup) { 128 TestPageNavigationHidingAutofillPopup) {
116 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get()); 129 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get());
117 130
118 EXPECT_FALSE(autofill_external_delegate_->popup_hidden()); 131 EXPECT_CALL(manager_delegate_, HideAutofillPopup()).Times(2);
Ilya Sherman 2013/02/26 22:55:51 nit: Please include the comment from lines 114-116
kaiwang 2013/02/27 00:00:10 Done.
119 132
120 content::WindowedNotificationObserver observer( 133 content::WindowedNotificationObserver observer(
121 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 134 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
122 content::Source<content::NavigationController>( 135 content::Source<content::NavigationController>(
123 &(web_contents_->GetController()))); 136 &(web_contents_->GetController())));
124 browser()->OpenURL(content::OpenURLParams( 137 browser()->OpenURL(content::OpenURLParams(
125 GURL(chrome::kChromeUIBookmarksURL), content::Referrer(), 138 GURL(chrome::kChromeUIBookmarksURL), content::Referrer(),
126 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); 139 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
127 browser()->OpenURL(content::OpenURLParams( 140 browser()->OpenURL(content::OpenURLParams(
128 GURL(chrome::kChromeUIAboutURL), content::Referrer(), 141 GURL(chrome::kChromeUIAboutURL), content::Referrer(),
129 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); 142 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
130 observer.Wait(); 143 observer.Wait();
131
132 EXPECT_TRUE(autofill_external_delegate_->popup_hidden());
133 } 144 }
134
135 // Tests that closing the widget does not leak any resources. This test is
136 // only really meaningful when run on the memory bots.
137 IN_PROC_BROWSER_TEST_F(AutofillExternalDelegateBrowserTest,
138 CloseWidgetAndNoLeaking) {
139 autofill::GenerateTestAutofillPopup(autofill_external_delegate_.get());
140
141 // Delete the view from under the delegate to ensure that the
142 // delegate and the controller can handle the popup getting deleted elsewhere.
143 autofill_external_delegate_->GetController()->view()->Hide();
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698