| 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 #import "ios/chrome/browser/ui/authentication/authentication_flow.h" | 5 #import "ios/chrome/browser/ui/authentication/authentication_flow.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #import "base/mac/scoped_block.h" | 9 #import "base/mac/scoped_block.h" |
| 10 #import "base/mac/scoped_nsobject.h" | |
| 11 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 12 #include "base/test/ios/wait_util.h" | 11 #include "base/test/ios/wait_util.h" |
| 13 #include "components/pref_registry/pref_registry_syncable.h" | 12 #include "components/pref_registry/pref_registry_syncable.h" |
| 14 #include "components/sync_preferences/pref_service_mock_factory.h" | 13 #include "components/sync_preferences/pref_service_mock_factory.h" |
| 15 #include "components/sync_preferences/pref_service_syncable.h" | 14 #include "components/sync_preferences/pref_service_syncable.h" |
| 16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 15 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 17 #include "ios/chrome/browser/prefs/browser_prefs.h" | 16 #include "ios/chrome/browser/prefs/browser_prefs.h" |
| 18 #include "ios/chrome/browser/signin/authentication_service_factory.h" | 17 #include "ios/chrome/browser/signin/authentication_service_factory.h" |
| 19 #import "ios/chrome/browser/signin/authentication_service_fake.h" | 18 #import "ios/chrome/browser/signin/authentication_service_fake.h" |
| 20 #import "ios/chrome/browser/ui/authentication/authentication_flow_performer.h" | 19 #import "ios/chrome/browser/ui/authentication/authentication_flow_performer.h" |
| 21 #include "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service
.h" | 20 #include "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service
.h" |
| 22 #include "ios/web/public/test/test_web_thread_bundle.h" | 21 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 23 #import "testing/gtest_mac.h" | 22 #import "testing/gtest_mac.h" |
| 24 #import "testing/platform_test.h" | 23 #import "testing/platform_test.h" |
| 25 #import "third_party/ocmock/gtest_support.h" | 24 #import "third_party/ocmock/gtest_support.h" |
| 26 #import "third_party/ocmock/ocmock_extensions.h" | 25 #import "third_party/ocmock/ocmock_extensions.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 28 | 27 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." |
| 30 #endif |
| 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 31 class AuthenticationFlowTest : public PlatformTest { | 34 class AuthenticationFlowTest : public PlatformTest { |
| 32 protected: | 35 protected: |
| 33 void SetUp() override { | 36 void SetUp() override { |
| 34 PlatformTest::SetUp(); | 37 PlatformTest::SetUp(); |
| 35 | 38 |
| 36 TestChromeBrowserState::Builder builder; | 39 TestChromeBrowserState::Builder builder; |
| 37 builder.AddTestingFactory( | 40 builder.AddTestingFactory( |
| 38 AuthenticationServiceFactory::GetInstance(), | 41 AuthenticationServiceFactory::GetInstance(), |
| 39 AuthenticationServiceFake::CreateAuthenticationService); | 42 AuthenticationServiceFake::CreateAuthenticationService); |
| 40 builder.SetPrefService(CreatePrefService()); | 43 builder.SetPrefService(CreatePrefService()); |
| 41 browser_state_ = builder.Build(); | 44 browser_state_ = builder.Build(); |
| 42 ios::FakeChromeIdentityService* identityService = | 45 ios::FakeChromeIdentityService* identityService = |
| 43 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider(); | 46 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider(); |
| 44 identityService->AddIdentities(@[ @"identity1", @"identity2" ]); | 47 identityService->AddIdentities(@[ @"identity1", @"identity2" ]); |
| 45 identity1_ = | 48 identity1_ = |
| 46 [identityService->GetAllIdentitiesSortedForDisplay() objectAtIndex:0]; | 49 [identityService->GetAllIdentitiesSortedForDisplay() objectAtIndex:0]; |
| 47 identity2_ = | 50 identity2_ = |
| 48 [identityService->GetAllIdentitiesSortedForDisplay() objectAtIndex:1]; | 51 [identityService->GetAllIdentitiesSortedForDisplay() objectAtIndex:1]; |
| 49 sign_in_completion_.reset( | 52 sign_in_completion_ = ^(BOOL success) { |
| 50 ^(BOOL success) { | 53 finished_ = true; |
| 51 finished_ = true; | 54 signed_in_success_ = success; |
| 52 signed_in_success_ = success; | 55 }; |
| 53 }, | |
| 54 base::scoped_policy::RETAIN); | |
| 55 finished_ = false; | 56 finished_ = false; |
| 56 signed_in_success_ = false; | 57 signed_in_success_ = false; |
| 57 } | 58 } |
| 58 | 59 |
| 59 std::unique_ptr<sync_preferences::PrefServiceSyncable> CreatePrefService() { | 60 std::unique_ptr<sync_preferences::PrefServiceSyncable> CreatePrefService() { |
| 60 sync_preferences::PrefServiceMockFactory factory; | 61 sync_preferences::PrefServiceMockFactory factory; |
| 61 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( | 62 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( |
| 62 new user_prefs::PrefRegistrySyncable); | 63 new user_prefs::PrefRegistrySyncable); |
| 63 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs = | 64 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs = |
| 64 factory.CreateSyncable(registry.get()); | 65 factory.CreateSyncable(registry.get()); |
| 65 RegisterBrowserStatePrefs(registry.get()); | 66 RegisterBrowserStatePrefs(registry.get()); |
| 66 return prefs; | 67 return prefs; |
| 67 } | 68 } |
| 68 | 69 |
| 69 AuthenticationFlowPerformer* GetAuthenticationFlowPerformer() { | 70 AuthenticationFlowPerformer* GetAuthenticationFlowPerformer() { |
| 70 return static_cast<AuthenticationFlowPerformer*>(performer_.get()); | 71 return static_cast<AuthenticationFlowPerformer*>(performer_); |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Creates a new AuthenticationFlow with default values for fields that are | 74 // Creates a new AuthenticationFlow with default values for fields that are |
| 74 // not directly useful. | 75 // not directly useful. |
| 75 void CreateAuthenticationFlow(ShouldClearData shouldClearData, | 76 void CreateAuthenticationFlow(ShouldClearData shouldClearData, |
| 76 PostSignInAction postSignInAction) { | 77 PostSignInAction postSignInAction) { |
| 77 ChromeIdentity* identity = identity1_; | 78 ChromeIdentity* identity = identity1_; |
| 78 view_controller_.reset( | 79 view_controller_ = [OCMockObject niceMockForClass:[UIViewController class]]; |
| 79 [[OCMockObject niceMockForClass:[UIViewController class]] retain]); | 80 authentication_flow_ = |
| 80 authentication_flow_.reset([[AuthenticationFlow alloc] | 81 [[AuthenticationFlow alloc] initWithBrowserState:browser_state_.get() |
| 81 initWithBrowserState:browser_state_.get() | 82 identity:identity |
| 82 identity:identity | 83 shouldClearData:shouldClearData |
| 83 shouldClearData:shouldClearData | 84 postSignInAction:postSignInAction |
| 84 postSignInAction:postSignInAction | 85 presentingViewController:view_controller_]; |
| 85 presentingViewController:view_controller_]); | 86 performer_ = |
| 86 performer_.reset([[OCMockObject | 87 [OCMockObject mockForClass:[AuthenticationFlowPerformer class]]; |
| 87 mockForClass:[AuthenticationFlowPerformer class]] retain]); | |
| 88 [authentication_flow_ | 88 [authentication_flow_ |
| 89 setPerformerForTesting:GetAuthenticationFlowPerformer()]; | 89 setPerformerForTesting:GetAuthenticationFlowPerformer()]; |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Checks if the AuthenticationFlow operation has completed, and whether it | 92 // Checks if the AuthenticationFlow operation has completed, and whether it |
| 93 // was successful. | 93 // was successful. |
| 94 void CheckSignInCompletion(bool expectedSignedIn) { | 94 void CheckSignInCompletion(bool expectedSignedIn) { |
| 95 base::test::ios::WaitUntilCondition(^bool { | 95 base::test::ios::WaitUntilCondition(^bool { |
| 96 return finished_; | 96 return finished_; |
| 97 }); | 97 }); |
| 98 EXPECT_EQ(true, finished_); | 98 EXPECT_EQ(true, finished_); |
| 99 EXPECT_EQ(expectedSignedIn, signed_in_success_); | 99 EXPECT_EQ(expectedSignedIn, signed_in_success_); |
| 100 [performer_ verify]; | 100 [performer_ verify]; |
| 101 } | 101 } |
| 102 | 102 |
| 103 web::TestWebThreadBundle thread_bundle_; | 103 web::TestWebThreadBundle thread_bundle_; |
| 104 base::scoped_nsobject<AuthenticationFlow> authentication_flow_; | 104 AuthenticationFlow* authentication_flow_; |
| 105 std::unique_ptr<TestChromeBrowserState> browser_state_; | 105 std::unique_ptr<TestChromeBrowserState> browser_state_; |
| 106 ChromeIdentity* identity1_; | 106 ChromeIdentity* identity1_; |
| 107 ChromeIdentity* identity2_; | 107 ChromeIdentity* identity2_; |
| 108 base::scoped_nsobject<OCMockObject> performer_; | 108 OCMockObject* performer_; |
| 109 base::mac::ScopedBlock<signin_ui::CompletionCallback> sign_in_completion_; | 109 signin_ui::CompletionCallback sign_in_completion_; |
| 110 base::scoped_nsobject<UIViewController> view_controller_; | 110 UIViewController* view_controller_; |
| 111 | 111 |
| 112 // State of the flow | 112 // State of the flow |
| 113 bool finished_; | 113 bool finished_; |
| 114 bool signed_in_success_; | 114 bool signed_in_success_; |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 // Tests a Sign In of a normal account on the same profile, merging user data | 117 // Tests a Sign In of a normal account on the same profile, merging user data |
| 118 // and showing the sync settings. | 118 // and showing the sync settings. |
| 119 TEST_F(AuthenticationFlowTest, TestSignInSimple) { | 119 TEST_F(AuthenticationFlowTest, TestSignInSimple) { |
| 120 CreateAuthenticationFlow(SHOULD_CLEAR_DATA_MERGE_DATA, | 120 CreateAuthenticationFlow(SHOULD_CLEAR_DATA_MERGE_DATA, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 CreateAuthenticationFlow(SHOULD_CLEAR_DATA_MERGE_DATA, | 246 CreateAuthenticationFlow(SHOULD_CLEAR_DATA_MERGE_DATA, |
| 247 POST_SIGNIN_ACTION_START_SYNC); | 247 POST_SIGNIN_ACTION_START_SYNC); |
| 248 | 248 |
| 249 NSError* error = [NSError errorWithDomain:@"foo" code:0 userInfo:nil]; | 249 NSError* error = [NSError errorWithDomain:@"foo" code:0 userInfo:nil]; |
| 250 [[[performer_ expect] andDo:^(NSInvocation*) { | 250 [[[performer_ expect] andDo:^(NSInvocation*) { |
| 251 [authentication_flow_ didFailFetchManagedStatus:error]; | 251 [authentication_flow_ didFailFetchManagedStatus:error]; |
| 252 }] fetchManagedStatus:browser_state_.get() | 252 }] fetchManagedStatus:browser_state_.get() |
| 253 forIdentity:identity1_]; | 253 forIdentity:identity1_]; |
| 254 | 254 |
| 255 [[[performer_ expect] andDo:^(NSInvocation* invocation) { | 255 [[[performer_ expect] andDo:^(NSInvocation* invocation) { |
| 256 ProceduralBlock completionBlock; | 256 __unsafe_unretained ProceduralBlock completionBlock; |
| 257 [invocation getArgument:&completionBlock atIndex:3]; | 257 [invocation getArgument:&completionBlock atIndex:3]; |
| 258 completionBlock(); | 258 completionBlock(); |
| 259 }] showAuthenticationError:[OCMArg any] | 259 }] showAuthenticationError:[OCMArg any] |
| 260 withCompletion:[OCMArg any] | 260 withCompletion:[OCMArg any] |
| 261 viewController:view_controller_]; | 261 viewController:view_controller_]; |
| 262 | 262 |
| 263 [authentication_flow_ startSignInWithCompletion:sign_in_completion_]; | 263 [authentication_flow_ startSignInWithCompletion:sign_in_completion_]; |
| 264 | 264 |
| 265 CheckSignInCompletion(false); | 265 CheckSignInCompletion(false); |
| 266 } | 266 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 294 toBrowserState:browser_state_.get()]; | 294 toBrowserState:browser_state_.get()]; |
| 295 | 295 |
| 296 [[performer_ expect] commitSyncForBrowserState:browser_state_.get()]; | 296 [[performer_ expect] commitSyncForBrowserState:browser_state_.get()]; |
| 297 | 297 |
| 298 [authentication_flow_ startSignInWithCompletion:sign_in_completion_]; | 298 [authentication_flow_ startSignInWithCompletion:sign_in_completion_]; |
| 299 | 299 |
| 300 CheckSignInCompletion(true); | 300 CheckSignInCompletion(true); |
| 301 } | 301 } |
| 302 | 302 |
| 303 } // namespace | 303 } // namespace |
| OLD | NEW |