Commit 81986bb5 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Add unit tests for CWVSyncControllerDelegate.

Bug: 770984
Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: Iad12bba9a759ba69771606144d8cec7f68b61eb2
Reviewed-on: https://chromium-review.googlesource.com/c/1257891Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: John Wu <jzw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597342}
parent e6db68f8
...@@ -19,12 +19,15 @@ ...@@ -19,12 +19,15 @@
#include "components/signin/ios/browser/profile_oauth2_token_service_ios_delegate.h" #include "components/signin/ios/browser/profile_oauth2_token_service_ios_delegate.h"
#include "components/signin/ios/browser/profile_oauth2_token_service_ios_provider.h" #include "components/signin/ios/browser/profile_oauth2_token_service_ios_provider.h"
#include "components/sync/driver/fake_sync_client.h" #include "components/sync/driver/fake_sync_client.h"
#include "components/sync/driver/sync_service_observer.h"
#include "google_apis/gaia/google_service_auth_error.h"
#import "ios/web/public/test/fakes/test_web_state.h" #import "ios/web/public/test/fakes/test_web_state.h"
#include "ios/web/public/test/test_web_thread_bundle.h" #include "ios/web/public/test/test_web_thread_bundle.h"
#include "ios/web_view/internal/web_view_browser_state.h" #include "ios/web_view/internal/web_view_browser_state.h"
#import "ios/web_view/public/cwv_identity.h" #import "ios/web_view/public/cwv_identity.h"
#import "ios/web_view/public/cwv_sync_controller_data_source.h" #import "ios/web_view/public/cwv_sync_controller_data_source.h"
#import "ios/web_view/public/cwv_sync_controller_delegate.h" #import "ios/web_view/public/cwv_sync_controller_delegate.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h" #import "testing/gtest_mac.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -36,6 +39,9 @@ ...@@ -36,6 +39,9 @@
namespace ios_web_view { namespace ios_web_view {
using testing::_;
using testing::Invoke;
class CWVSyncControllerTest : public PlatformTest { class CWVSyncControllerTest : public PlatformTest {
protected: protected:
CWVSyncControllerTest() CWVSyncControllerTest()
...@@ -64,6 +70,9 @@ class CWVSyncControllerTest : public PlatformTest { ...@@ -64,6 +70,9 @@ class CWVSyncControllerTest : public PlatformTest {
account_tracker_service_.Initialize(browser_state_.GetPrefs(), account_tracker_service_.Initialize(browser_state_.GetPrefs(),
base::FilePath()); base::FilePath());
EXPECT_CALL(*profile_sync_service_, AddObserver(_))
.WillOnce(Invoke(this, &CWVSyncControllerTest::AddObserver));
sync_controller_ = [[CWVSyncController alloc] sync_controller_ = [[CWVSyncController alloc]
initWithProfileSyncService:profile_sync_service_.get() initWithProfileSyncService:profile_sync_service_.get()
accountTrackerService:&account_tracker_service_ accountTrackerService:&account_tracker_service_
...@@ -71,6 +80,19 @@ class CWVSyncControllerTest : public PlatformTest { ...@@ -71,6 +80,19 @@ class CWVSyncControllerTest : public PlatformTest {
tokenService:&token_service_]; tokenService:&token_service_];
}; };
~CWVSyncControllerTest() override {
EXPECT_CALL(*profile_sync_service_, RemoveObserver(_));
}
void AddObserver(syncer::SyncServiceObserver* observer) {
sync_service_observer_ = observer;
}
void OnConfigureDone(const syncer::DataTypeManager::ConfigureResult& result) {
sync_service_observer_->OnSyncConfigurationCompleted(
profile_sync_service_.get());
}
web::TestWebThreadBundle web_thread_bundle_; web::TestWebThreadBundle web_thread_bundle_;
ios_web_view::WebViewBrowserState browser_state_; ios_web_view::WebViewBrowserState browser_state_;
web::TestWebState web_state_; web::TestWebState web_state_;
...@@ -81,13 +103,14 @@ class CWVSyncControllerTest : public PlatformTest { ...@@ -81,13 +103,14 @@ class CWVSyncControllerTest : public PlatformTest {
FakeGaiaCookieManagerService gaia_cookie_manager_service_; FakeGaiaCookieManagerService gaia_cookie_manager_service_;
FakeSigninManager signin_manager_; FakeSigninManager signin_manager_;
CWVSyncController* sync_controller_; CWVSyncController* sync_controller_;
syncer::SyncServiceObserver* sync_service_observer_;
}; };
// Verifies CWVSyncControllerDataSource methods are invoked with the correct // Verifies CWVSyncControllerDataSource methods are invoked with the correct
// parameters. // parameters.
TEST_F(CWVSyncControllerTest, DataSourceCallbacks) { TEST_F(CWVSyncControllerTest, DataSourceCallbacks) {
// [delegate expect] returns an autoreleased object, but it must be destroyed // [data_source expect] returns an autoreleased object, but it must be
// before this test exits to avoid holding on to |sync_controller_|. // destroyed before this test exits to avoid holding on to |sync_controller_|.
@autoreleasepool { @autoreleasepool {
id data_source = OCMProtocolMock(@protocol(CWVSyncControllerDataSource)); id data_source = OCMProtocolMock(@protocol(CWVSyncControllerDataSource));
...@@ -99,6 +122,9 @@ TEST_F(CWVSyncControllerTest, DataSourceCallbacks) { ...@@ -99,6 +122,9 @@ TEST_F(CWVSyncControllerTest, DataSourceCallbacks) {
}] }]
completionHandler:[OCMArg any]]; completionHandler:[OCMArg any]];
EXPECT_CALL(*profile_sync_service_, RequestStart());
EXPECT_CALL(*profile_sync_service_, SetFirstSetupComplete());
CWVIdentity* identity = CWVIdentity* identity =
[[CWVIdentity alloc] initWithEmail:@"johndoe@chromium.org" [[CWVIdentity alloc] initWithEmail:@"johndoe@chromium.org"
fullName:@"John Doe" fullName:@"John Doe"
...@@ -114,4 +140,40 @@ TEST_F(CWVSyncControllerTest, DataSourceCallbacks) { ...@@ -114,4 +140,40 @@ TEST_F(CWVSyncControllerTest, DataSourceCallbacks) {
} }
} }
// Verifies CWVSyncControllerDelegate methods are invoked with the correct
// parameters.
TEST_F(CWVSyncControllerTest, DelegateCallbacks) {
// [delegate expect] returns an autoreleased object, but it must be destroyed
// before this test exits to avoid holding on to |sync_controller_|.
@autoreleasepool {
id delegate = OCMProtocolMock(@protocol(CWVSyncControllerDelegate));
sync_controller_.delegate = delegate;
[[delegate expect] syncControllerDidStartSync:sync_controller_];
EXPECT_CALL(*profile_sync_service_, OnConfigureDone(_))
.WillOnce(Invoke(
this,
&CWVSyncControllerTest_DelegateCallbacks_Test::OnConfigureDone));
syncer::DataTypeManager::ConfigureResult result;
profile_sync_service_->OnConfigureDone(result);
[[delegate expect]
syncController:sync_controller_
didFailWithError:[OCMArg checkWithBlock:^BOOL(NSError* error) {
return error.code == CWVSyncErrorInvalidGAIACredentials;
}]];
GoogleServiceAuthError auth_error(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
[sync_controller_ didUpdateAuthError:auth_error];
[[delegate expect] syncController:sync_controller_
didStopSyncWithReason:CWVStopSyncReasonServer];
[sync_controller_
didSignoutWithSourceMetric:signin_metrics::ProfileSignout::
SERVER_FORCED_DISABLE];
[delegate verify];
}
}
} // namespace ios_web_view } // namespace ios_web_view
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment