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

Side by Side Diff: chrome/browser/ui/autofill/autofill_popup_controller_unittest.cc

Issue 12217024: Use WeakPtr to simplify AutofillPopupControllerImpl memory management (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments and fix test 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/weak_ptr.h"
6 #include "base/utf_string_conversions.h" 6 #include "base/utf_string_conversions.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/ui/autofill/autofill_popup_controller_impl.h"
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h"
12 #include "ui/gfx/display.h" 12 #include "ui/gfx/display.h"
13 #include "ui/gfx/rect.h" 13 #include "ui/gfx/rect.h"
14 14
15 using ::testing::_; 15 using ::testing::_;
16 using ::testing::AtLeast; 16 using ::testing::AtLeast;
17 using base::WeakPtr;
17 using WebKit::WebAutofillClient; 18 using WebKit::WebAutofillClient;
18 19
19 namespace { 20 namespace {
20 21
21 class MockAutofillExternalDelegate : 22 class MockAutofillExternalDelegate :
22 public autofill::TestAutofillExternalDelegate { 23 public autofill::TestAutofillExternalDelegate {
23 public: 24 public:
24 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; 25 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {};
25 virtual ~MockAutofillExternalDelegate() {}; 26 virtual ~MockAutofillExternalDelegate() {};
26 27
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 return AutofillPopupControllerImpl::subtext_font(); 88 return AutofillPopupControllerImpl::subtext_font();
88 } 89 }
89 #endif 90 #endif
90 int GetDesiredPopupWidth() const { 91 int GetDesiredPopupWidth() const {
91 return AutofillPopupControllerImpl::GetDesiredPopupWidth(); 92 return AutofillPopupControllerImpl::GetDesiredPopupWidth();
92 } 93 }
93 int GetDesiredPopupHeight() const { 94 int GetDesiredPopupHeight() const {
94 return AutofillPopupControllerImpl::GetDesiredPopupHeight(); 95 return AutofillPopupControllerImpl::GetDesiredPopupHeight();
95 } 96 }
96 97
98 WeakPtr<AutofillPopupControllerImpl> GetWeakPtr() {
99 return AutofillPopupControllerImpl::GetWeakPtr();
100 }
101
97 MOCK_METHOD1(InvalidateRow, void(size_t)); 102 MOCK_METHOD1(InvalidateRow, void(size_t));
98 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); 103 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void());
99 MOCK_METHOD0(Hide, void()); 104 MOCK_METHOD0(Hide, void());
100 105
101 private: 106 private:
102 virtual void ShowView() OVERRIDE {} 107 virtual void ShowView() OVERRIDE {}
103 108
104 gfx::Display display_; 109 gfx::Display display_;
105 }; 110 };
106 111
107 } // namespace 112 } // namespace
108 113
109 class AutofillPopupControllerUnitTest : public ::testing::Test { 114 class AutofillPopupControllerUnitTest : public ::testing::Test {
110 public: 115 public:
111 AutofillPopupControllerUnitTest() 116 AutofillPopupControllerUnitTest()
112 : autofill_popup_controller_( 117 : autofill_popup_controller_(
113 new testing::NiceMock<TestAutofillPopupController>( 118 new testing::NiceMock<TestAutofillPopupController>(
114 &external_delegate_, gfx::Rect())) {} 119 &external_delegate_, gfx::Rect())) {}
120
115 virtual ~AutofillPopupControllerUnitTest() { 121 virtual ~AutofillPopupControllerUnitTest() {
116 // This will make sure the controller and the view (if any) are both 122 // This will make sure the controller and the view (if any) are both
117 // cleaned up. 123 // cleaned up.
118 if (autofill_popup_controller_) 124 if (autofill_popup_controller_)
119 autofill_popup_controller_->DoHide(); 125 autofill_popup_controller_->DoHide();
120 } 126 }
121 127
122 AutofillPopupController* popup_controller() { 128 AutofillPopupController* popup_controller() {
123 return autofill_popup_controller_; 129 return autofill_popup_controller_;
124 } 130 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 autofill_popup_controller_->subtexts().size() - 1); 213 autofill_popup_controller_->subtexts().size() - 1);
208 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); 214 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine());
209 EXPECT_LE(0, autofill_popup_controller_->selected_line()); 215 EXPECT_LE(0, autofill_popup_controller_->selected_line());
210 216
211 // Remove the first entry. The popup should be redrawn since its size has 217 // Remove the first entry. The popup should be redrawn since its size has
212 // changed. 218 // changed.
213 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); 219 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup());
214 autofill_popup_controller_->SetSelectedLine(0); 220 autofill_popup_controller_->SetSelectedLine(0);
215 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); 221 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
216 222
217 // Remove the last entry. The popup should then be hidden since there are
218 // no Autofill entries left.
219 EXPECT_CALL(external_delegate_, ControllerDestroyed());
220
221 autofill_popup_controller_->SetSelectedLine(0); 223 autofill_popup_controller_->SetSelectedLine(0);
222 // The controller self-deletes here, don't double delete. 224 EXPECT_CALL(*autofill_popup_controller_, Hide());
223 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); 225 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
224 autofill_popup_controller_ = NULL;
225 } 226 }
226 227
227 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { 228 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) {
228 // Set up the popup. 229 // Set up the popup.
229 std::vector<string16> names(3, string16()); 230 std::vector<string16> names(3, string16());
230 std::vector<int> autofill_ids; 231 std::vector<int> autofill_ids;
231 autofill_ids.push_back(1); 232 autofill_ids.push_back(1);
232 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); 233 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator);
233 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); 234 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
234 autofill_popup_controller_->Show(names, names, names, autofill_ids); 235 autofill_popup_controller_->Show(names, names, names, autofill_ids);
235 236
236 autofill_popup_controller_->SetSelectedLine(0); 237 autofill_popup_controller_->SetSelectedLine(0);
237 238
238 // Make sure next skips the unselectable separator. 239 // Make sure next skips the unselectable separator.
239 autofill_popup_controller_->SelectNextLine(); 240 autofill_popup_controller_->SelectNextLine();
240 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); 241 EXPECT_EQ(2, autofill_popup_controller_->selected_line());
241 242
242 // Make sure previous skips the unselectable separator. 243 // Make sure previous skips the unselectable separator.
243 autofill_popup_controller_->SelectPreviousLine(); 244 autofill_popup_controller_->SelectPreviousLine();
244 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); 245 EXPECT_EQ(0, autofill_popup_controller_->selected_line());
245 } 246 }
246 247
247 TEST_F(AutofillPopupControllerUnitTest, GetOrCreate) { 248 TEST_F(AutofillPopupControllerUnitTest, GetOrCreate) {
248 MockAutofillExternalDelegate delegate; 249 MockAutofillExternalDelegate delegate;
249 250
250 AutofillPopupControllerImpl* controller = 251 WeakPtr<AutofillPopupControllerImpl> controller;
251 AutofillPopupControllerImpl::GetOrCreate( 252 controller = AutofillPopupControllerImpl::GetOrCreate(
252 NULL, 253 controller, &delegate, NULL, gfx::Rect());
Ilya Sherman 2013/02/06 05:12:52 Rather than passing the controller as an argument
kaiwang 2013/02/06 05:56:21 Done.
253 &delegate,
254 NULL,
255 gfx::Rect());
256 EXPECT_TRUE(controller); 254 EXPECT_TRUE(controller);
257 255
258 // This should not inform |delegate| of its destruction.
259 EXPECT_CALL(delegate, ControllerDestroyed()).Times(0);
260 controller->Hide(); 256 controller->Hide();
261 257
262 controller = 258 controller = AutofillPopupControllerImpl::GetOrCreate(
263 AutofillPopupControllerImpl::GetOrCreate( 259 WeakPtr<AutofillPopupControllerImpl>(), &delegate, NULL, gfx::Rect());
264 NULL,
265 &delegate,
266 NULL,
267 gfx::Rect());
268 EXPECT_TRUE(controller); 260 EXPECT_TRUE(controller);
269 AutofillPopupControllerImpl* controller2 = 261
270 AutofillPopupControllerImpl::GetOrCreate( 262 WeakPtr<AutofillPopupControllerImpl> controller2 =
271 controller, 263 AutofillPopupControllerImpl::GetOrCreate(controller, &delegate, NULL,
272 &delegate, 264 gfx::Rect());
273 NULL, 265 EXPECT_EQ(controller.get(), controller2.get());
274 gfx::Rect());
275 EXPECT_EQ(controller, controller2);
276 controller->Hide(); 266 controller->Hide();
277 267
278 testing::NiceMock<TestAutofillPopupController>* test_controller = 268 testing::NiceMock<TestAutofillPopupController>* test_controller =
279 new testing::NiceMock<TestAutofillPopupController>(&delegate, 269 new testing::NiceMock<TestAutofillPopupController>(&delegate,
280 gfx::Rect()); 270 gfx::Rect());
281 EXPECT_CALL(*test_controller, Hide()); 271 EXPECT_CALL(*test_controller, Hide());
282 272
283 gfx::Rect bounds(0, 0, 1, 2); 273 gfx::Rect bounds(0, 0, 1, 2);
284 AutofillPopupControllerImpl* controller3 = 274 AutofillPopupControllerImpl* controller3 =
285 AutofillPopupControllerImpl::GetOrCreate( 275 AutofillPopupControllerImpl::GetOrCreate(
286 test_controller, 276 test_controller->GetWeakPtr(),
287 &delegate, 277 &delegate,
288 NULL, 278 NULL,
289 bounds); 279 bounds);
290 EXPECT_EQ( 280 EXPECT_EQ(
291 bounds, 281 bounds,
292 static_cast<AutofillPopupController*>(controller3)->element_bounds()); 282 static_cast<AutofillPopupController*>(controller3)->element_bounds());
293 controller3->Hide(); 283 controller3->Hide();
294 284
295 EXPECT_CALL(delegate, ControllerDestroyed());
296 delete test_controller; 285 delete test_controller;
297 } 286 }
298 287
299 #if !defined(OS_ANDROID) 288 #if !defined(OS_ANDROID)
300 TEST_F(AutofillPopupControllerUnitTest, ElideText) { 289 TEST_F(AutofillPopupControllerUnitTest, ElideText) {
301 std::vector<string16> names; 290 std::vector<string16> names;
302 names.push_back(ASCIIToUTF16("Text that will need to be trimmed")); 291 names.push_back(ASCIIToUTF16("Text that will need to be trimmed"));
303 names.push_back(ASCIIToUTF16("Untrimmed")); 292 names.push_back(ASCIIToUTF16("Untrimmed"));
304 293
305 std::vector<string16> subtexts; 294 std::vector<string16> subtexts;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 TestAutofillPopupController autofill_popup_controller(&external_delegate_, 374 TestAutofillPopupController autofill_popup_controller(&external_delegate_,
386 element_bounds[i]); 375 element_bounds[i]);
387 autofill_popup_controller.set_display(display); 376 autofill_popup_controller.set_display(display);
388 autofill_popup_controller.Show(names, names, names, autofill_ids); 377 autofill_popup_controller.Show(names, names, names, autofill_ids);
389 378
390 EXPECT_EQ(expected_popup_bounds[i].ToString(), 379 EXPECT_EQ(expected_popup_bounds[i].ToString(),
391 autofill_popup_controller.popup_bounds().ToString()) << 380 autofill_popup_controller.popup_bounds().ToString()) <<
392 "Popup bounds failed to match for test " << i; 381 "Popup bounds failed to match for test " << i;
393 } 382 }
394 } 383 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/autofill/autofill_popup_controller_impl.cc ('k') | chrome/browser/ui/autofill/autofill_popup_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698