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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #import "base/mac/scoped_nsobject.h" | |
9 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
10 #include "base/values.h" | 9 #include "base/values.h" |
11 #import "ios/chrome/browser/passwords/js_credential_manager.h" | 10 #import "ios/chrome/browser/passwords/js_credential_manager.h" |
12 #include "ios/web/public/web_state/credential.h" | 11 #include "ios/web/public/web_state/credential.h" |
13 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" | 12 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
14 #import "ios/web/public/web_state/web_state.h" | 13 #import "ios/web/public/web_state/web_state.h" |
15 #include "ios/web/public/web_state/web_state_observer.h" | 14 #include "ios/web/public/web_state/web_state_observer.h" |
16 #import "ios/web/public/test/web_test_with_web_state.h" | 15 #import "ios/web/public/test/web_test_with_web_state.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "testing/gtest_mac.h" | 18 #include "testing/gtest_mac.h" |
20 #include "url/gurl.h" | 19 #include "url/gurl.h" |
21 | 20 |
| 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 22 #error "This file requires ARC support." |
| 23 #endif |
| 24 |
22 namespace { | 25 namespace { |
23 | 26 |
24 using ::testing::_; | 27 using ::testing::_; |
25 | 28 |
26 // Matcher to match web::Credential. | 29 // Matcher to match web::Credential. |
27 MATCHER_P(IsEqualTo, value, "") { | 30 MATCHER_P(IsEqualTo, value, "") { |
28 return arg.type == value.type && arg.id == value.id && | 31 return arg.type == value.type && arg.id == value.id && |
29 arg.name == value.name && arg.avatar_url == value.avatar_url && | 32 arg.name == value.name && arg.avatar_url == value.avatar_url && |
30 arg.password == value.password && | 33 arg.password == value.password && |
31 arg.federation_origin.Serialize() == | 34 arg.federation_origin.Serialize() == |
(...skipping 20 matching lines...) Expand all Loading... |
52 DISALLOW_COPY_AND_ASSIGN(MockWebStateObserver); | 55 DISALLOW_COPY_AND_ASSIGN(MockWebStateObserver); |
53 }; | 56 }; |
54 | 57 |
55 // Unit tests for the Credential Manager JavaScript and associated plumbing. | 58 // Unit tests for the Credential Manager JavaScript and associated plumbing. |
56 class CredentialManagerJsTest : public web::WebTestWithWebState { | 59 class CredentialManagerJsTest : public web::WebTestWithWebState { |
57 public: | 60 public: |
58 CredentialManagerJsTest() {} | 61 CredentialManagerJsTest() {} |
59 | 62 |
60 void SetUp() override { | 63 void SetUp() override { |
61 web::WebTestWithWebState::SetUp(); | 64 web::WebTestWithWebState::SetUp(); |
62 js_credential_manager_.reset(base::mac::ObjCCastStrict<JSCredentialManager>( | 65 js_credential_manager_ = base::mac::ObjCCastStrict<JSCredentialManager>( |
63 [[web_state()->GetJSInjectionReceiver() | 66 [web_state()->GetJSInjectionReceiver() |
64 instanceOfClass:[JSCredentialManager class]] retain])); | 67 instanceOfClass:[JSCredentialManager class]]); |
65 observer_.reset(new MockWebStateObserver(web_state())); | 68 observer_.reset(new MockWebStateObserver(web_state())); |
66 } | 69 } |
67 | 70 |
68 // Sets up a web page and injects the JSCredentialManager. Must be called | 71 // Sets up a web page and injects the JSCredentialManager. Must be called |
69 // before any interaction with the page. | 72 // before any interaction with the page. |
70 void Inject() { | 73 void Inject() { |
71 LoadHtml(@""); | 74 LoadHtml(@""); |
72 [js_credential_manager_ inject]; | 75 [js_credential_manager_ inject]; |
73 } | 76 } |
74 | 77 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 NSString* message, | 251 NSString* message, |
249 NSString* promise) { | 252 NSString* promise) { |
250 PrepareResolverAndRejecter(promise); | 253 PrepareResolverAndRejecter(promise); |
251 RejectPromise(request_id, error, message); | 254 RejectPromise(request_id, error, message); |
252 CheckRejected(error, message); | 255 CheckRejected(error, message); |
253 CheckNeverResolved(); | 256 CheckNeverResolved(); |
254 } | 257 } |
255 | 258 |
256 private: | 259 private: |
257 // Manager for injected credential manager JavaScript. | 260 // Manager for injected credential manager JavaScript. |
258 base::scoped_nsobject<JSCredentialManager> js_credential_manager_; | 261 JSCredentialManager* js_credential_manager_; |
259 | 262 |
260 // Mock observer for testing. | 263 // Mock observer for testing. |
261 std::unique_ptr<MockWebStateObserver> observer_; | 264 std::unique_ptr<MockWebStateObserver> observer_; |
262 | 265 |
263 DISALLOW_COPY_AND_ASSIGN(CredentialManagerJsTest); | 266 DISALLOW_COPY_AND_ASSIGN(CredentialManagerJsTest); |
264 }; | 267 }; |
265 | 268 |
266 // Tests that navigator.credentials calls use distinct request identifiers. | 269 // Tests that navigator.credentials calls use distinct request identifiers. |
267 TEST_F(CredentialManagerJsTest, RequestIdentifiersDiffer) { | 270 TEST_F(CredentialManagerJsTest, RequestIdentifiersDiffer) { |
268 Inject(); | 271 Inject(); |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 TEST_F(CredentialManagerJsTest, | 499 TEST_F(CredentialManagerJsTest, |
497 RejectPromiseWithSecurityError_notifySignedOut) { | 500 RejectPromiseWithSecurityError_notifySignedOut) { |
498 Inject(); | 501 Inject(); |
499 const int request_id = 0; | 502 const int request_id = 0; |
500 EXPECT_CALL(observer(), SignedOut(request_id, _)); | 503 EXPECT_CALL(observer(), SignedOut(request_id, _)); |
501 TestPromiseRejection(request_id, @"SecurityError", @"foo", | 504 TestPromiseRejection(request_id, @"SecurityError", @"foo", |
502 @"navigator.credentials.notifySignedOut()"); | 505 @"navigator.credentials.notifySignedOut()"); |
503 } | 506 } |
504 | 507 |
505 } // namespace | 508 } // namespace |
OLD | NEW |