OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/browser/passwords/credential_manager.h" | 5 #include "ios/chrome/browser/passwords/credential_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/mac/bind_objc_block.h" | |
11 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
12 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 13 #include "base/values.h" |
15 #include "components/password_manager/core/browser/password_bubble_experiment.h" | 14 #include "components/password_manager/core/browser/password_bubble_experiment.h" |
16 #include "components/password_manager/core/browser/stub_password_manager_client.
h" | 15 #include "components/password_manager/core/browser/stub_password_manager_client.
h" |
17 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" | 16 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" |
18 #include "components/password_manager/core/browser/test_password_store.h" | 17 #include "components/password_manager/core/browser/test_password_store.h" |
19 #include "components/password_manager/core/common/credential_manager_types.h" | 18 #include "components/password_manager/core/common/credential_manager_types.h" |
20 #include "components/password_manager/core/common/password_manager_pref_names.h" | 19 #include "components/password_manager/core/common/password_manager_pref_names.h" |
21 #include "components/prefs/pref_registry_simple.h" | 20 #include "components/prefs/pref_registry_simple.h" |
22 #include "components/prefs/testing_pref_service.h" | 21 #include "components/prefs/testing_pref_service.h" |
23 #import "ios/web/public/web_state/web_state.h" | 22 #import "ios/web/public/web_state/web_state.h" |
24 #import "ios/chrome/browser/passwords/js_credential_manager.h" | 23 #import "ios/chrome/browser/passwords/js_credential_manager.h" |
25 #import "ios/testing/ocmock_complex_type_helper.h" | 24 #import "ios/testing/ocmock_complex_type_helper.h" |
26 #import "ios/web/public/test/web_test_with_web_state.h" | 25 #import "ios/web/public/test/web_test_with_web_state.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "testing/gtest_mac.h" | 28 #include "testing/gtest_mac.h" |
30 #include "third_party/ocmock/OCMock/OCMock.h" | 29 #include "third_party/ocmock/OCMock/OCMock.h" |
31 #include "third_party/ocmock/gtest_support.h" | 30 #include "third_party/ocmock/gtest_support.h" |
32 #include "url/gurl.h" | 31 #include "url/gurl.h" |
33 | 32 |
| 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 34 #error "This file requires ARC support." |
| 35 #endif |
| 36 |
34 using testing::Return; | 37 using testing::Return; |
35 | 38 |
36 namespace { | 39 namespace { |
37 // Type of a function invoked when a promise is resolved. | 40 // Type of a function invoked when a promise is resolved. |
38 typedef void (^ResolvePromiseBlock)(NSInteger request_id, | 41 typedef void (^ResolvePromiseBlock)(NSInteger request_id, |
39 const web::Credential& credential); | 42 const web::Credential& credential); |
40 } // namespace | 43 } // namespace |
41 | 44 |
42 // A helper to mock methods that have C++ object parameters. | 45 // A helper to mock methods that have C++ object parameters. |
43 @interface MockJSCredentialManager : OCMockComplexTypeHelper | 46 @interface MockJSCredentialManager : OCMockComplexTypeHelper |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 credential.id = form.username_value; | 101 credential.id = form.username_value; |
99 credential.password = form.password_value; | 102 credential.password = form.password_value; |
100 return credential; | 103 return credential; |
101 } | 104 } |
102 | 105 |
103 typedef BOOL (^StringPredicate)(NSString*); | 106 typedef BOOL (^StringPredicate)(NSString*); |
104 | 107 |
105 // Returns a block that takes a string argument and returns whether it is equal | 108 // Returns a block that takes a string argument and returns whether it is equal |
106 // to |string|. | 109 // to |string|. |
107 StringPredicate EqualsString(const char* string) { | 110 StringPredicate EqualsString(const char* string) { |
108 return [[^BOOL(NSString* other) { | 111 return [^BOOL(NSString* other) { |
109 return [base::SysUTF8ToNSString(string) isEqualToString:other]; | 112 return [base::SysUTF8ToNSString(string) isEqualToString:other]; |
110 } copy] autorelease]; | 113 } copy]; |
111 } | 114 } |
112 | 115 |
113 // A stub PasswordManagerClient for testing. | 116 // A stub PasswordManagerClient for testing. |
114 class StubPasswordManagerClient | 117 class StubPasswordManagerClient |
115 : public password_manager::StubPasswordManagerClient { | 118 : public password_manager::StubPasswordManagerClient { |
116 public: | 119 public: |
117 StubPasswordManagerClient() | 120 StubPasswordManagerClient() |
118 : password_manager::StubPasswordManagerClient(), | 121 : password_manager::StubPasswordManagerClient(), |
119 password_store_(nullptr) { | 122 password_store_(nullptr) { |
120 prefs_.registry()->RegisterBooleanPref( | 123 prefs_.registry()->RegisterBooleanPref( |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 // Tests for CredentialManager. | 187 // Tests for CredentialManager. |
185 class CredentialManagerTest : public web::WebTestWithWebState { | 188 class CredentialManagerTest : public web::WebTestWithWebState { |
186 public: | 189 public: |
187 CredentialManagerTest() {} | 190 CredentialManagerTest() {} |
188 ~CredentialManagerTest() override {} | 191 ~CredentialManagerTest() override {} |
189 | 192 |
190 void SetUp() override { | 193 void SetUp() override { |
191 web::WebTestWithWebState::SetUp(); | 194 web::WebTestWithWebState::SetUp(); |
192 id originalMock = | 195 id originalMock = |
193 [OCMockObject niceMockForClass:[JSCredentialManager class]]; | 196 [OCMockObject niceMockForClass:[JSCredentialManager class]]; |
194 mock_js_credential_manager_.reset([[MockJSCredentialManager alloc] | 197 mock_js_credential_manager_ = [[MockJSCredentialManager alloc] |
195 initWithRepresentedObject:originalMock]); | 198 initWithRepresentedObject:originalMock]; |
196 credential_manager_.reset(new CredentialManager( | 199 credential_manager_.reset( |
197 web_state(), &stub_client_, &stub_driver_, | 200 new CredentialManager(web_state(), &stub_client_, &stub_driver_, |
198 static_cast<id>(mock_js_credential_manager_.get()))); | 201 static_cast<id>(mock_js_credential_manager_))); |
199 LoadHtml(@"", GURL(kTestURL)); | 202 LoadHtml(@"", GURL(kTestURL)); |
200 } | 203 } |
201 | 204 |
202 // Sets up an expectation that the promise identified by |request_id| will be | 205 // Sets up an expectation that the promise identified by |request_id| will be |
203 // resolved with |credential|. |verified| must point to a variable that will | 206 // resolved with |credential|. |verified| must point to a variable that will |
204 // be checked by the caller to ensure that the expectations were run. (This | 207 // be checked by the caller to ensure that the expectations were run. (This |
205 // is necessary because OCMock doesn't handle methods with C++ object | 208 // is necessary because OCMock doesn't handle methods with C++ object |
206 // parameters.) | 209 // parameters.) |
207 void ExpectPromiseResolved(bool* verified, | 210 void ExpectPromiseResolved(bool* verified, |
208 int request_id, | 211 int request_id, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 259 } |
257 | 260 |
258 protected: | 261 protected: |
259 // Mock for PasswordManagerClient. | 262 // Mock for PasswordManagerClient. |
260 StubPasswordManagerClient stub_client_; | 263 StubPasswordManagerClient stub_client_; |
261 | 264 |
262 // Stub for PasswordManagerDriver. | 265 // Stub for PasswordManagerDriver. |
263 password_manager::StubPasswordManagerDriver stub_driver_; | 266 password_manager::StubPasswordManagerDriver stub_driver_; |
264 | 267 |
265 // Mock for JSCredentialManager. | 268 // Mock for JSCredentialManager. |
266 base::scoped_nsobject<MockJSCredentialManager> mock_js_credential_manager_; | 269 MockJSCredentialManager* mock_js_credential_manager_; |
267 | 270 |
268 // CredentialManager for testing. | 271 // CredentialManager for testing. |
269 std::unique_ptr<CredentialManager> credential_manager_; | 272 std::unique_ptr<CredentialManager> credential_manager_; |
270 | 273 |
271 private: | 274 private: |
272 explicit CredentialManagerTest(const CredentialManagerTest&) = delete; | 275 explicit CredentialManagerTest(const CredentialManagerTest&) = delete; |
273 CredentialManagerTest& operator=(const CredentialManagerTest&) = delete; | 276 CredentialManagerTest& operator=(const CredentialManagerTest&) = delete; |
274 }; | 277 }; |
275 | 278 |
276 // Tests that a credential request is rejected properly when the PasswordStore | 279 // Tests that a credential request is rejected properly when the PasswordStore |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 WaitForBackgroundTasks(); | 609 WaitForBackgroundTasks(); |
607 EXPECT_EQ(1U, store->stored_passwords().size()); | 610 EXPECT_EQ(1U, store->stored_passwords().size()); |
608 const std::vector<autofill::PasswordForm>& passwords_after_signout = | 611 const std::vector<autofill::PasswordForm>& passwords_after_signout = |
609 store->stored_passwords().find(current_origin)->second; | 612 store->stored_passwords().find(current_origin)->second; |
610 EXPECT_EQ(2U, passwords_after_signout.size()); | 613 EXPECT_EQ(2U, passwords_after_signout.size()); |
611 for (const autofill::PasswordForm& form : passwords_after_signout) | 614 for (const autofill::PasswordForm& form : passwords_after_signout) |
612 EXPECT_TRUE(form.skip_zero_click); | 615 EXPECT_TRUE(form.skip_zero_click); |
613 } | 616 } |
614 | 617 |
615 } // namespace | 618 } // namespace |
OLD | NEW |