OLD | NEW |
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/scoped_temp_dir.h" | |
9 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
10 #include "base/string_util.h" | 9 #include "base/string_util.h" |
11 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
12 #include "base/time.h" | 11 #include "base/time.h" |
13 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
14 #include "chrome/browser/password_manager/password_form_data.h" | 13 #include "chrome/browser/password_manager/password_form_data.h" |
15 #include "chrome/browser/password_manager/password_store_change.h" | 14 #include "chrome/browser/password_manager/password_store_change.h" |
16 #include "chrome/browser/password_manager/password_store_consumer.h" | 15 #include "chrome/browser/password_manager/password_store_consumer.h" |
17 #include "chrome/browser/password_manager/password_store_default.h" | 16 #include "chrome/browser/password_manager/password_store_default.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
(...skipping 17 matching lines...) Expand all Loading... |
36 using testing::Property; | 35 using testing::Property; |
37 using testing::WithArg; | 36 using testing::WithArg; |
38 using webkit::forms::PasswordForm; | 37 using webkit::forms::PasswordForm; |
39 | 38 |
40 namespace { | 39 namespace { |
41 | 40 |
42 class MockPasswordStoreConsumer : public PasswordStoreConsumer { | 41 class MockPasswordStoreConsumer : public PasswordStoreConsumer { |
43 public: | 42 public: |
44 MOCK_METHOD2(OnPasswordStoreRequestDone, | 43 MOCK_METHOD2(OnPasswordStoreRequestDone, |
45 void(CancelableRequestProvider::Handle, | 44 void(CancelableRequestProvider::Handle, |
46 const std::vector<webkit::forms::PasswordForm*>&)); | 45 const std::vector<PasswordForm*>&)); |
47 }; | 46 }; |
48 | 47 |
49 // This class will add and remove a mock notification observer from | 48 // This class will add and remove a mock notification observer from |
50 // the DB thread. | 49 // the DB thread. |
51 class DBThreadObserverHelper : | 50 class DBThreadObserverHelper |
52 public base::RefCountedThreadSafe<DBThreadObserverHelper, | 51 : public base::RefCountedThreadSafe<DBThreadObserverHelper, |
53 BrowserThread::DeleteOnDBThread> { | 52 BrowserThread::DeleteOnDBThread> { |
54 public: | 53 public: |
55 DBThreadObserverHelper() : done_event_(true, false) {} | 54 DBThreadObserverHelper() : done_event_(true, false) {} |
56 | 55 |
57 void Init(PasswordStore* password_store) { | 56 void Init(PasswordStore* password_store) { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
59 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
60 BrowserThread::DB, | 59 BrowserThread::DB, |
61 FROM_HERE, | 60 FROM_HERE, |
62 base::Bind(&DBThreadObserverHelper::AddObserverTask, | 61 base::Bind(&DBThreadObserverHelper::AddObserverTask, |
63 this, | 62 this, |
(...skipping 21 matching lines...) Expand all Loading... |
85 done_event_.Signal(); | 84 done_event_.Signal(); |
86 } | 85 } |
87 | 86 |
88 WaitableEvent done_event_; | 87 WaitableEvent done_event_; |
89 content::NotificationRegistrar registrar_; | 88 content::NotificationRegistrar registrar_; |
90 content::NotificationObserverMock observer_; | 89 content::NotificationObserverMock observer_; |
91 }; | 90 }; |
92 | 91 |
93 } // anonymous namespace | 92 } // anonymous namespace |
94 | 93 |
95 typedef std::vector<PasswordForm*> VectorOfForms; | |
96 | |
97 class PasswordStoreDefaultTest : public testing::Test { | 94 class PasswordStoreDefaultTest : public testing::Test { |
98 protected: | 95 protected: |
99 PasswordStoreDefaultTest() | 96 PasswordStoreDefaultTest() |
100 : ui_thread_(BrowserThread::UI, &message_loop_), | 97 : ui_thread_(BrowserThread::UI, &message_loop_), |
101 db_thread_(BrowserThread::DB) { | 98 db_thread_(BrowserThread::DB) { |
102 } | 99 } |
103 | 100 |
104 virtual void SetUp() { | 101 virtual void SetUp() { |
105 ASSERT_TRUE(db_thread_.Start()); | 102 ASSERT_TRUE(db_thread_.Start()); |
106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
107 | 103 |
108 profile_.reset(new TestingProfile()); | 104 profile_.reset(new TestingProfile()); |
109 | 105 |
110 login_db_.reset(new LoginDatabase()); | 106 login_db_.reset(new LoginDatabase()); |
111 ASSERT_TRUE(login_db_->Init(temp_dir_.path().Append( | 107 ASSERT_TRUE(login_db_->Init(profile_->GetPath().Append( |
112 FILE_PATH_LITERAL("login_test")))); | 108 FILE_PATH_LITERAL("login_test")))); |
113 } | 109 } |
114 | 110 |
115 virtual void TearDown() { | 111 virtual void TearDown() { |
116 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 112 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
117 MessageLoop::current()->Run(); | 113 MessageLoop::current()->Run(); |
118 db_thread_.Stop(); | 114 db_thread_.Stop(); |
119 } | 115 } |
120 | 116 |
121 MessageLoopForUI message_loop_; | 117 MessageLoopForUI message_loop_; |
122 content::TestBrowserThread ui_thread_; | 118 content::TestBrowserThread ui_thread_; |
123 // PasswordStore, WDS schedule work on this thread. | 119 // PasswordStore, WDS schedule work on this thread. |
124 content::TestBrowserThread db_thread_; | 120 content::TestBrowserThread db_thread_; |
125 | 121 |
126 scoped_ptr<LoginDatabase> login_db_; | 122 scoped_ptr<LoginDatabase> login_db_; |
127 scoped_ptr<TestingProfile> profile_; | 123 scoped_ptr<TestingProfile> profile_; |
128 ScopedTempDir temp_dir_; | |
129 }; | 124 }; |
130 | 125 |
131 ACTION(STLDeleteElements0) { | 126 ACTION(STLDeleteElements0) { |
132 STLDeleteContainerPointers(arg0.begin(), arg0.end()); | 127 STLDeleteContainerPointers(arg0.begin(), arg0.end()); |
133 } | 128 } |
134 | 129 |
135 ACTION(QuitUIMessageLoop) { | 130 ACTION(QuitUIMessageLoop) { |
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
137 MessageLoop::current()->Quit(); | 132 MessageLoop::current()->Quit(); |
138 } | 133 } |
139 | 134 |
140 TEST_F(PasswordStoreDefaultTest, NonASCIIData) { | 135 TEST_F(PasswordStoreDefaultTest, NonASCIIData) { |
141 scoped_refptr<PasswordStoreDefault> store( | 136 scoped_refptr<PasswordStoreDefault> store( |
142 new PasswordStoreDefault(login_db_.release(), profile_.get())); | 137 new PasswordStoreDefault(login_db_.release(), profile_.get())); |
143 store->Init(); | 138 store->Init(); |
144 | 139 |
145 // Some non-ASCII password form data. | 140 // Some non-ASCII password form data. |
146 PasswordFormData form_data[] = { | 141 static const PasswordFormData form_data[] = { |
147 { PasswordForm::SCHEME_HTML, | 142 { PasswordForm::SCHEME_HTML, |
148 "http://foo.example.com", | 143 "http://foo.example.com", |
149 "http://foo.example.com/origin", | 144 "http://foo.example.com/origin", |
150 "http://foo.example.com/action", | 145 "http://foo.example.com/action", |
151 L"มีสีสัน", | 146 L"มีสีสัน", |
152 L"お元気ですか?", | 147 L"お元気ですか?", |
153 L"盆栽", | 148 L"盆栽", |
154 L"أحب كرة", | 149 L"أحب كرة", |
155 L"£éä국수çà", | 150 L"£éä국수çà", |
156 true, false, 1 }, | 151 true, false, 1 }, |
157 }; | 152 }; |
158 | 153 |
159 // Build the expected forms vector and add the forms to the store. | 154 // Build the expected forms vector and add the forms to the store. |
160 VectorOfForms expected_forms; | 155 std::vector<PasswordForm*> expected_forms; |
161 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(form_data); ++i) { | 156 for (unsigned int i = 0; i < ARRAYSIZE_UNSAFE(form_data); ++i) { |
162 PasswordForm* form = CreatePasswordFormFromData(form_data[i]); | 157 PasswordForm* form = CreatePasswordFormFromData(form_data[i]); |
163 expected_forms.push_back(form); | 158 expected_forms.push_back(form); |
164 store->AddLogin(*form); | 159 store->AddLogin(*form); |
165 } | 160 } |
166 | 161 |
167 // The PasswordStore schedules tasks to run on the DB thread so we schedule | 162 // The PasswordStore schedules tasks to run on the DB thread so we schedule |
168 // yet another task to notify us that it's safe to carry on with the test. | 163 // yet another task to notify us that it's safe to carry on with the test. |
| 164 // The PasswordStore doesn't really understand that it's "done" once the tasks |
| 165 // we posted above have completed, so there's no formal notification for that. |
169 WaitableEvent done(false, false); | 166 WaitableEvent done(false, false); |
170 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 167 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
171 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | 168 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); |
172 done.Wait(); | 169 done.Wait(); |
173 | 170 |
174 MockPasswordStoreConsumer consumer; | 171 MockPasswordStoreConsumer consumer; |
175 | 172 |
176 // Make sure we quit the MessageLoop even if the test fails. | 173 // Make sure we quit the MessageLoop even if the test fails. |
177 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) | 174 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) |
178 .WillByDefault(QuitUIMessageLoop()); | 175 .WillByDefault(QuitUIMessageLoop()); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 // Deleting the login should trigger a notification. | 264 // Deleting the login should trigger a notification. |
268 store->RemoveLogin(*form); | 265 store->RemoveLogin(*form); |
269 | 266 |
270 // Wait for PasswordStore to send the notification. | 267 // Wait for PasswordStore to send the notification. |
271 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 268 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
272 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); | 269 base::Bind(&WaitableEvent::Signal, base::Unretained(&done))); |
273 done.Wait(); | 270 done.Wait(); |
274 | 271 |
275 store->ShutdownOnUIThread(); | 272 store->ShutdownOnUIThread(); |
276 } | 273 } |
OLD | NEW |