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

Side by Side Diff: components/password_manager/core/browser/password_store_default_unittest.cc

Issue 866983003: GetLoginsRequest: Use ScopedVector to express ownership of forms (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@324291_scopedvector
Patch Set: Fix Mac unittest Created 5 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
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/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "components/password_manager/core/browser/login_database.h" 15 #include "components/password_manager/core/browser/login_database.h"
16 #include "components/password_manager/core/browser/password_form_data.h" 16 #include "components/password_manager/core/browser/password_form_data.h"
17 #include "components/password_manager/core/browser/password_store_change.h" 17 #include "components/password_manager/core/browser/password_store_change.h"
18 #include "components/password_manager/core/browser/password_store_consumer.h" 18 #include "components/password_manager/core/browser/password_store_consumer.h"
19 #include "components/password_manager/core/browser/password_store_default.h" 19 #include "components/password_manager/core/browser/password_store_default.h"
20 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using autofill::PasswordForm; 23 using autofill::PasswordForm;
24 using testing::_; 24 using testing::_;
25 using testing::ElementsAre;
25 using testing::ElementsAreArray; 26 using testing::ElementsAreArray;
26 using testing::WithArg; 27 using testing::WithArg;
27 28
28 namespace password_manager { 29 namespace password_manager {
29 30
30 namespace { 31 namespace {
31 32
32 class MockPasswordStoreConsumer : public PasswordStoreConsumer { 33 class MockPasswordStoreConsumer : public PasswordStoreConsumer {
33 public: 34 public:
34 MOCK_METHOD1(OnGetPasswordStoreResults, 35 MOCK_METHOD1(OnGetPasswordStoreResultsConstRef,
35 void(const std::vector<PasswordForm*>&)); 36 void(const std::vector<PasswordForm*>&));
37
38 // GMock cannot mock methods with move-only args.
39 void OnGetPasswordStoreResults(ScopedVector<PasswordForm> results) override {
40 OnGetPasswordStoreResultsConstRef(results.get());
41 }
36 }; 42 };
37 43
38 class MockPasswordStoreObserver : public PasswordStore::Observer { 44 class MockPasswordStoreObserver : public PasswordStore::Observer {
39 public: 45 public:
40 MOCK_METHOD1(OnLoginsChanged, void(const PasswordStoreChangeList& changes)); 46 MOCK_METHOD1(OnLoginsChanged, void(const PasswordStoreChangeList& changes));
41 }; 47 };
42 48
43 // A mock LoginDatabase that simulates a failing Init() method. 49 // A mock LoginDatabase that simulates a failing Init() method.
44 class BadLoginDatabase : public LoginDatabase { 50 class BadLoginDatabase : public LoginDatabase {
45 public: 51 public:
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 "http://foo.example.com/action", 113 "http://foo.example.com/action",
108 L"มีสีสัน", 114 L"มีสีสัน",
109 L"お元気ですか?", 115 L"お元気ですか?",
110 L"盆栽", 116 L"盆栽",
111 L"أحب كرة", 117 L"أحب كرة",
112 L"£éä국수çà", 118 L"£éä국수çà",
113 true, false, 1 }, 119 true, false, 1 },
114 }; 120 };
115 121
116 // Build the expected forms vector and add the forms to the store. 122 // Build the expected forms vector and add the forms to the store.
117 std::vector<PasswordForm*> expected_forms; 123 ScopedVector<PasswordForm> expected_forms;
118 for (unsigned int i = 0; i < arraysize(form_data); ++i) { 124 for (unsigned int i = 0; i < arraysize(form_data); ++i) {
119 PasswordForm* form = CreatePasswordFormFromData(form_data[i]); 125 expected_forms.push_back(
120 expected_forms.push_back(form); 126 CreatePasswordFormFromData(form_data[i]).release());
121 store->AddLogin(*form); 127 store->AddLogin(*expected_forms.back());
122 } 128 }
123 129
124 base::MessageLoop::current()->RunUntilIdle(); 130 base::MessageLoop::current()->RunUntilIdle();
125 131
126 MockPasswordStoreConsumer consumer; 132 MockPasswordStoreConsumer consumer;
127 133
128 // We expect to get the same data back, even though it's not all ASCII. 134 // We expect to get the same data back, even though it's not all ASCII.
129 EXPECT_CALL(consumer, 135 EXPECT_CALL(consumer, OnGetPasswordStoreResultsConstRef(
130 OnGetPasswordStoreResults(ContainsAllPasswordForms(expected_forms))) 136 ContainsSamePasswordForms(expected_forms.get())));
131 .WillOnce(WithArg<0>(STLDeleteElements0()));
132 store->GetAutofillableLogins(&consumer); 137 store->GetAutofillableLogins(&consumer);
133 138
134 base::MessageLoop::current()->RunUntilIdle(); 139 base::MessageLoop::current()->RunUntilIdle();
135 140
136 STLDeleteElements(&expected_forms);
137 store->Shutdown(); 141 store->Shutdown();
138 base::MessageLoop::current()->RunUntilIdle(); 142 base::MessageLoop::current()->RunUntilIdle();
139 } 143 }
140 144
141 TEST_F(PasswordStoreDefaultTest, Notifications) { 145 TEST_F(PasswordStoreDefaultTest, Notifications) {
142 scoped_refptr<PasswordStoreDefault> store(new PasswordStoreDefault( 146 scoped_refptr<PasswordStoreDefault> store(new PasswordStoreDefault(
143 base::MessageLoopProxy::current(), base::MessageLoopProxy::current(), 147 base::MessageLoopProxy::current(), base::MessageLoopProxy::current(),
144 make_scoped_ptr(new LoginDatabase(test_login_db_file_path())))); 148 make_scoped_ptr(new LoginDatabase(test_login_db_file_path()))));
145 store->Init(syncer::SyncableService::StartSyncFlare()); 149 store->Init(syncer::SyncableService::StartSyncFlare());
146 150
147 scoped_ptr<PasswordForm> form( 151 scoped_ptr<PasswordForm> form =
148 CreatePasswordFormFromData(CreateTestPasswordFormData())); 152 CreatePasswordFormFromData(CreateTestPasswordFormData());
149 153
150 MockPasswordStoreObserver observer; 154 MockPasswordStoreObserver observer;
151 store->AddObserver(&observer); 155 store->AddObserver(&observer);
152 156
153 const PasswordStoreChange expected_add_changes[] = { 157 const PasswordStoreChange expected_add_changes[] = {
154 PasswordStoreChange(PasswordStoreChange::ADD, *form), 158 PasswordStoreChange(PasswordStoreChange::ADD, *form),
155 }; 159 };
156 160
157 EXPECT_CALL(observer, 161 EXPECT_CALL(observer,
158 OnLoginsChanged(ElementsAreArray(expected_add_changes))); 162 OnLoginsChanged(ElementsAreArray(expected_add_changes)));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 make_scoped_ptr<LoginDatabase>(new BadLoginDatabase))); 204 make_scoped_ptr<LoginDatabase>(new BadLoginDatabase)));
201 205
202 bad_store->Init(syncer::SyncableService::StartSyncFlare()); 206 bad_store->Init(syncer::SyncableService::StartSyncFlare());
203 base::MessageLoop::current()->RunUntilIdle(); 207 base::MessageLoop::current()->RunUntilIdle();
204 ASSERT_EQ(nullptr, bad_store->login_db()); 208 ASSERT_EQ(nullptr, bad_store->login_db());
205 209
206 testing::StrictMock<MockPasswordStoreObserver> mock_observer; 210 testing::StrictMock<MockPasswordStoreObserver> mock_observer;
207 bad_store->AddObserver(&mock_observer); 211 bad_store->AddObserver(&mock_observer);
208 212
209 // Add a new autofillable login + a blacklisted login. 213 // Add a new autofillable login + a blacklisted login.
210 scoped_ptr<PasswordForm> form( 214 scoped_ptr<PasswordForm> form =
211 CreatePasswordFormFromData(CreateTestPasswordFormData())); 215 CreatePasswordFormFromData(CreateTestPasswordFormData());
212 scoped_ptr<PasswordForm> blacklisted_form(new PasswordForm(*form)); 216 scoped_ptr<PasswordForm> blacklisted_form(new PasswordForm(*form));
213 blacklisted_form->signon_realm = "http://foo.example.com"; 217 blacklisted_form->signon_realm = "http://foo.example.com";
214 blacklisted_form->origin = GURL("http://foo.example.com/origin"); 218 blacklisted_form->origin = GURL("http://foo.example.com/origin");
215 blacklisted_form->action = GURL("http://foo.example.com/action"); 219 blacklisted_form->action = GURL("http://foo.example.com/action");
216 blacklisted_form->blacklisted_by_user = true; 220 blacklisted_form->blacklisted_by_user = true;
217 bad_store->AddLogin(*form); 221 bad_store->AddLogin(*form);
218 bad_store->AddLogin(*blacklisted_form); 222 bad_store->AddLogin(*blacklisted_form);
219 base::MessageLoop::current()->RunUntilIdle(); 223 base::MessageLoop::current()->RunUntilIdle();
220 224
221 // Get all logins; autofillable logins; blacklisted logins. 225 // Get all logins; autofillable logins; blacklisted logins.
222 testing::StrictMock<MockPasswordStoreConsumer> mock_consumer; 226 testing::StrictMock<MockPasswordStoreConsumer> mock_consumer;
223 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); 227 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(ElementsAre()));
vasilii 2015/02/05 19:23:27 What about IsEmpty()?
vabr (Chromium) 2015/02/06 14:16:05 Done.
224 bad_store->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer); 228 bad_store->GetLogins(*form, PasswordStore::DISALLOW_PROMPT, &mock_consumer);
225 base::MessageLoop::current()->RunUntilIdle(); 229 base::MessageLoop::current()->RunUntilIdle();
226 testing::Mock::VerifyAndClearExpectations(&mock_consumer); 230 testing::Mock::VerifyAndClearExpectations(&mock_consumer);
227 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); 231 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(ElementsAre()));
228 bad_store->GetAutofillableLogins(&mock_consumer); 232 bad_store->GetAutofillableLogins(&mock_consumer);
229 base::MessageLoop::current()->RunUntilIdle(); 233 base::MessageLoop::current()->RunUntilIdle();
230 testing::Mock::VerifyAndClearExpectations(&mock_consumer); 234 testing::Mock::VerifyAndClearExpectations(&mock_consumer);
231 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResults(testing::ElementsAre())); 235 EXPECT_CALL(mock_consumer, OnGetPasswordStoreResultsConstRef(ElementsAre()));
232 bad_store->GetBlacklistLogins(&mock_consumer); 236 bad_store->GetBlacklistLogins(&mock_consumer);
233 base::MessageLoop::current()->RunUntilIdle(); 237 base::MessageLoop::current()->RunUntilIdle();
234 238
235 // Report metrics. 239 // Report metrics.
236 bad_store->ReportMetrics("Test Username", true); 240 bad_store->ReportMetrics("Test Username", true);
237 base::MessageLoop::current()->RunUntilIdle(); 241 base::MessageLoop::current()->RunUntilIdle();
238 242
239 // Change the login. 243 // Change the login.
240 form->password_value = base::ASCIIToUTF16("a different password"); 244 form->password_value = base::ASCIIToUTF16("a different password");
241 bad_store->UpdateLogin(*form); 245 bad_store->UpdateLogin(*form);
242 base::MessageLoop::current()->RunUntilIdle(); 246 base::MessageLoop::current()->RunUntilIdle();
243 247
244 // Delete one login; a range of logins. 248 // Delete one login; a range of logins.
245 bad_store->RemoveLogin(*form); 249 bad_store->RemoveLogin(*form);
246 base::MessageLoop::current()->RunUntilIdle(); 250 base::MessageLoop::current()->RunUntilIdle();
247 bad_store->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max()); 251 bad_store->RemoveLoginsCreatedBetween(base::Time(), base::Time::Max());
248 base::MessageLoop::current()->RunUntilIdle(); 252 base::MessageLoop::current()->RunUntilIdle();
249 bad_store->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max()); 253 bad_store->RemoveLoginsSyncedBetween(base::Time(), base::Time::Max());
250 base::MessageLoop::current()->RunUntilIdle(); 254 base::MessageLoop::current()->RunUntilIdle();
251 255
252 // Ensure no notifications and no explosions during shutdown either. 256 // Ensure no notifications and no explosions during shutdown either.
253 bad_store->RemoveObserver(&mock_observer); 257 bad_store->RemoveObserver(&mock_observer);
254 bad_store->Shutdown(); 258 bad_store->Shutdown();
255 base::MessageLoop::current()->RunUntilIdle(); 259 base::MessageLoop::current()->RunUntilIdle();
256 } 260 }
257 261
258 } // namespace password_manager 262 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698