OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/settings/sync_settings_collection_view_controller
.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #import "base/mac/foundation_util.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "components/browser_sync/profile_sync_service_mock.h" |
| 13 #include "components/google/core/browser/google_util.h" |
| 14 #include "ios/chrome/browser/application_context.h" |
| 15 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 16 #include "ios/chrome/browser/chrome_url_constants.h" |
| 17 #include "ios/chrome/browser/experimental_flags.h" |
| 18 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
| 19 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_test_util.h" |
| 20 #include "ios/chrome/browser/sync/sync_setup_service.h" |
| 21 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" |
| 22 #include "ios/chrome/browser/sync/sync_setup_service_mock.h" |
| 23 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 24 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 25 #import "ios/chrome/browser/ui/settings/cells/sync_switch_item.h" |
| 26 #import "ios/chrome/browser/ui/settings/cells/text_and_error_item.h" |
| 27 #import "ios/chrome/browser/ui/sync/sync_util.h" |
| 28 #include "ios/chrome/grit/ios_strings.h" |
| 29 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 30 #include "testing/gtest_mac.h" |
| 31 #include "ui/base/l10n/l10n_util.h" |
| 32 |
| 33 @interface SyncSettingsCollectionViewController (ExposedForTesting) |
| 34 - (int)titleIdForSyncableDataType:(SyncSetupService::SyncableDatatype)datatype; |
| 35 - (void)onSyncStateChanged; |
| 36 @end |
| 37 |
| 38 namespace { |
| 39 |
| 40 using testing::DefaultValue; |
| 41 using testing::NiceMock; |
| 42 using testing::Return; |
| 43 |
| 44 class SyncSetupServiceMockThatFails : public SyncSetupServiceMock { |
| 45 public: |
| 46 SyncSetupServiceMockThatFails(browser_sync::ProfileSyncService* sync_service, |
| 47 PrefService* prefs) |
| 48 : SyncSetupServiceMock(sync_service, prefs) {} |
| 49 bool IsSyncEnabled() const override { return sync_enabled_; } |
| 50 void SetSyncEnabled(bool sync_enabled) override {} |
| 51 bool IsSyncingAllDataTypes() const override { return sync_all_; } |
| 52 void SetSyncingAllDataTypes(bool sync_all) override {} |
| 53 |
| 54 static void SetSyncEnabledTestValue(bool sync_enabled) { |
| 55 sync_enabled_ = sync_enabled; |
| 56 } |
| 57 static void SetSyncEverythingTestValue(bool sync_all) { |
| 58 sync_all_ = sync_all; |
| 59 } |
| 60 |
| 61 protected: |
| 62 static bool sync_enabled_; |
| 63 static bool sync_all_; |
| 64 }; |
| 65 |
| 66 bool SyncSetupServiceMockThatFails::sync_enabled_ = true; |
| 67 bool SyncSetupServiceMockThatFails::sync_all_ = true; |
| 68 |
| 69 class SyncSetupServiceMockThatSucceeds : public SyncSetupServiceMockThatFails { |
| 70 public: |
| 71 SyncSetupServiceMockThatSucceeds( |
| 72 browser_sync::ProfileSyncService* sync_service, |
| 73 PrefService* prefs) |
| 74 : SyncSetupServiceMockThatFails(sync_service, prefs) {} |
| 75 void SetSyncEnabled(bool sync_enabled) override { |
| 76 sync_enabled_ = sync_enabled; |
| 77 } |
| 78 void SetSyncingAllDataTypes(bool sync_all) override { sync_all_ = sync_all; } |
| 79 }; |
| 80 |
| 81 class SyncSettingsCollectionViewControllerTest |
| 82 : public CollectionViewControllerTest { |
| 83 public: |
| 84 SyncSettingsCollectionViewControllerTest() |
| 85 : default_auth_error_(GoogleServiceAuthError::NONE) {} |
| 86 |
| 87 static std::unique_ptr<KeyedService> CreateSyncSetupService( |
| 88 web::BrowserState* context) { |
| 89 ios::ChromeBrowserState* chrome_browser_state = |
| 90 ios::ChromeBrowserState::FromBrowserState(context); |
| 91 browser_sync::ProfileSyncService* sync_service = |
| 92 IOSChromeProfileSyncServiceFactory::GetForBrowserState( |
| 93 chrome_browser_state); |
| 94 return base::MakeUnique<NiceMock<SyncSetupServiceMock>>( |
| 95 sync_service, chrome_browser_state->GetPrefs()); |
| 96 } |
| 97 |
| 98 static std::unique_ptr<KeyedService> CreateSucceedingSyncSetupService( |
| 99 web::BrowserState* context) { |
| 100 ios::ChromeBrowserState* chrome_browser_state = |
| 101 ios::ChromeBrowserState::FromBrowserState(context); |
| 102 browser_sync::ProfileSyncService* sync_service = |
| 103 IOSChromeProfileSyncServiceFactory::GetForBrowserState( |
| 104 chrome_browser_state); |
| 105 return base::MakeUnique<NiceMock<SyncSetupServiceMockThatSucceeds>>( |
| 106 sync_service, chrome_browser_state->GetPrefs()); |
| 107 } |
| 108 |
| 109 static std::unique_ptr<KeyedService> CreateFailingSyncSetupService( |
| 110 web::BrowserState* context) { |
| 111 ios::ChromeBrowserState* chrome_browser_state = |
| 112 ios::ChromeBrowserState::FromBrowserState(context); |
| 113 browser_sync::ProfileSyncService* sync_service = |
| 114 IOSChromeProfileSyncServiceFactory::GetForBrowserState( |
| 115 chrome_browser_state); |
| 116 return base::MakeUnique<NiceMock<SyncSetupServiceMockThatFails>>( |
| 117 sync_service, chrome_browser_state->GetPrefs()); |
| 118 } |
| 119 |
| 120 static std::unique_ptr<KeyedService> CreateProfileSyncService( |
| 121 web::BrowserState* context) { |
| 122 browser_sync::ProfileSyncService::InitParams init_params = |
| 123 CreateProfileSyncServiceParamsForTest( |
| 124 nullptr, ios::ChromeBrowserState::FromBrowserState(context)); |
| 125 return base::MakeUnique<NiceMock<browser_sync::ProfileSyncServiceMock>>( |
| 126 &init_params); |
| 127 } |
| 128 |
| 129 void TurnSyncOn() { |
| 130 ON_CALL(*mock_sync_setup_service_, IsSyncEnabled()) |
| 131 .WillByDefault(Return(true)); |
| 132 } |
| 133 |
| 134 void TurnSyncEverythingOn() { |
| 135 ON_CALL(*mock_sync_setup_service_, IsSyncingAllDataTypes()) |
| 136 .WillByDefault(Return(true)); |
| 137 } |
| 138 |
| 139 void SetSyncServiceState(SyncSetupService::SyncServiceState state) { |
| 140 ON_CALL(*mock_sync_setup_service_, GetSyncServiceState()) |
| 141 .WillByDefault(Return(state)); |
| 142 } |
| 143 |
| 144 void TurnSyncPassphraseErrorOn() { |
| 145 SetSyncServiceState(SyncSetupService::kSyncServiceNeedsPassphrase); |
| 146 } |
| 147 |
| 148 void TurnSyncErrorOff() { |
| 149 SetSyncServiceState(SyncSetupService::kNoSyncServiceError); |
| 150 } |
| 151 |
| 152 protected: |
| 153 void SetUp() override { |
| 154 TestChromeBrowserState::Builder test_cbs_builder; |
| 155 test_cbs_builder.AddTestingFactory(SyncSetupServiceFactory::GetInstance(), |
| 156 &CreateSyncSetupService); |
| 157 test_cbs_builder.AddTestingFactory( |
| 158 IOSChromeProfileSyncServiceFactory::GetInstance(), |
| 159 &CreateProfileSyncService); |
| 160 chrome_browser_state_ = test_cbs_builder.Build(); |
| 161 CollectionViewControllerTest::SetUp(); |
| 162 |
| 163 DefaultValue<const GoogleServiceAuthError&>::Set(default_auth_error_); |
| 164 DefaultValue<syncer::SyncCycleSnapshot>::Set(default_sync_cycle_snapshot_); |
| 165 |
| 166 mock_sync_setup_service_ = static_cast<NiceMock<SyncSetupServiceMock>*>( |
| 167 SyncSetupServiceFactory::GetForBrowserState( |
| 168 chrome_browser_state_.get())); |
| 169 // The other mocked functions of SyncSetupServiceMock return bools, so they |
| 170 // will by default return false. |syncServiceState|, however, returns an |
| 171 // enum, and thus always needs its default value set. |
| 172 TurnSyncErrorOff(); |
| 173 |
| 174 mock_profile_sync_service_ = |
| 175 static_cast<browser_sync::ProfileSyncServiceMock*>( |
| 176 IOSChromeProfileSyncServiceFactory::GetForBrowserState( |
| 177 chrome_browser_state_.get())); |
| 178 ON_CALL(*mock_profile_sync_service_, IsEngineInitialized()) |
| 179 .WillByDefault(Return(true)); |
| 180 ON_CALL(*mock_profile_sync_service_, GetRegisteredDataTypes()) |
| 181 .WillByDefault(Return(syncer::ModelTypeSet())); |
| 182 mock_profile_sync_service_->Initialize(); |
| 183 EXPECT_CALL(*mock_profile_sync_service_, GetPreferredDataTypes()) |
| 184 .WillRepeatedly(Return(syncer::UserSelectableTypes())); |
| 185 } |
| 186 |
| 187 CollectionViewController* NewController() override { |
| 188 return [[SyncSettingsCollectionViewController alloc] |
| 189 initWithBrowserState:chrome_browser_state_.get() |
| 190 allowSwitchSyncAccount:YES]; |
| 191 } |
| 192 |
| 193 SyncSettingsCollectionViewController* CreateSyncController() { |
| 194 CreateController(); |
| 195 CheckTitleWithId(IDS_IOS_SYNC_SETTING_TITLE); |
| 196 return static_cast<SyncSettingsCollectionViewController*>(controller()); |
| 197 } |
| 198 |
| 199 void CheckErrorIcon(BOOL expectedShouldDisplayError, |
| 200 NSInteger section, |
| 201 NSInteger item) { |
| 202 CollectionViewModel* model = [controller() collectionViewModel]; |
| 203 NSIndexPath* path = [NSIndexPath indexPathForItem:item inSection:section]; |
| 204 |
| 205 TextAndErrorItem* errorObject = base::mac::ObjCCastStrict<TextAndErrorItem>( |
| 206 [model itemAtIndexPath:path]); |
| 207 EXPECT_TRUE(errorObject); |
| 208 EXPECT_EQ(expectedShouldDisplayError, errorObject.shouldDisplayError); |
| 209 } |
| 210 |
| 211 web::TestWebThreadBundle thread_bundle_; |
| 212 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 213 // Weak, owned by |profile_|. |
| 214 NiceMock<SyncSetupServiceMock>* mock_sync_setup_service_; |
| 215 // Weak, owned by |profile_|. |
| 216 browser_sync::ProfileSyncServiceMock* mock_profile_sync_service_; |
| 217 // Default return values for ProfileSyncServiceMock. |
| 218 GoogleServiceAuthError default_auth_error_; |
| 219 syncer::SyncCycleSnapshot default_sync_cycle_snapshot_; |
| 220 }; |
| 221 |
| 222 TEST_F(SyncSettingsCollectionViewControllerTest, TestModel) { |
| 223 TurnSyncEverythingOn(); |
| 224 TurnSyncOn(); |
| 225 SyncSettingsCollectionViewController* sync_controller = |
| 226 CreateSyncController(); |
| 227 |
| 228 EXPECT_EQ(3, NumberOfSections()); |
| 229 |
| 230 EXPECT_EQ(1, NumberOfItemsInSection(0)); |
| 231 int expected_number_of_items = |
| 232 SyncSetupService::kNumberOfSyncableDatatypes + 1; |
| 233 if (!experimental_flags::IsReadingListEnabled()) |
| 234 expected_number_of_items--; |
| 235 EXPECT_EQ(expected_number_of_items, NumberOfItemsInSection(1)); |
| 236 EXPECT_EQ(2, NumberOfItemsInSection(2)); |
| 237 |
| 238 SyncSwitchItem* syncItem = GetCollectionViewItem(0, 0); |
| 239 EXPECT_EQ(syncItem.isOn, YES); |
| 240 EXPECT_NSEQ(syncItem.text, |
| 241 l10n_util::GetNSString(IDS_IOS_SYNC_SETTING_TITLE)); |
| 242 EXPECT_NSEQ( |
| 243 syncItem.detailText, |
| 244 l10n_util::GetNSString(IDS_IOS_SIGN_IN_TO_CHROME_SETTING_SUBTITLE)); |
| 245 |
| 246 SyncSwitchItem* syncEverythingItem = GetCollectionViewItem(1, 0); |
| 247 EXPECT_EQ(syncEverythingItem.isOn, YES); |
| 248 EXPECT_NSEQ(syncEverythingItem.text, |
| 249 l10n_util::GetNSString(IDS_IOS_SYNC_EVERYTHING_TITLE)); |
| 250 |
| 251 int item = 1; |
| 252 for (int i = 0; i < SyncSetupService::kNumberOfSyncableDatatypes; i++) { |
| 253 SyncSetupService::SyncableDatatype dataType = |
| 254 static_cast<SyncSetupService::SyncableDatatype>(i); |
| 255 if (!experimental_flags::IsReadingListEnabled() && |
| 256 dataType == SyncSetupService::kSyncReadingList) { |
| 257 // Display Reading List only if it is enabled. |
| 258 continue; |
| 259 } |
| 260 SyncSwitchItem* syncDataTypeItem = GetCollectionViewItem(1, item++); |
| 261 EXPECT_NSEQ(syncDataTypeItem.text, |
| 262 l10n_util::GetNSString( |
| 263 [sync_controller titleIdForSyncableDataType:dataType])); |
| 264 } |
| 265 TextAndErrorItem* encryptionItem = GetCollectionViewItem(2, 0); |
| 266 EXPECT_NSEQ(encryptionItem.text, |
| 267 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_TITLE)); |
| 268 CheckErrorIcon(NO, 2, 0); |
| 269 } |
| 270 |
| 271 TEST_F(SyncSettingsCollectionViewControllerTest, TestEnabledCellsSyncOff) { |
| 272 CreateSyncController(); |
| 273 |
| 274 for (int item = 0; item < NumberOfItemsInSection(1); ++item) { |
| 275 SyncSwitchItem* object = GetCollectionViewItem(1, item); |
| 276 EXPECT_FALSE(object.enabled); |
| 277 } |
| 278 |
| 279 TextAndErrorItem* encryptionItem = GetCollectionViewItem(2, 0); |
| 280 EXPECT_FALSE(encryptionItem.enabled); |
| 281 } |
| 282 |
| 283 TEST_F(SyncSettingsCollectionViewControllerTest, |
| 284 TestEnabledCellsSyncEverythingOn) { |
| 285 TurnSyncOn(); |
| 286 TurnSyncEverythingOn(); |
| 287 CreateSyncController(); |
| 288 |
| 289 SyncSwitchItem* syncEverythingItem = GetCollectionViewItem(1, 0); |
| 290 EXPECT_TRUE(syncEverythingItem.enabled); |
| 291 for (int item = 1; item < NumberOfItemsInSection(1); ++item) { |
| 292 SyncSwitchItem* object = GetCollectionViewItem(1, item); |
| 293 EXPECT_FALSE(object.enabled); |
| 294 } |
| 295 |
| 296 TextAndErrorItem* encryptionItem = GetCollectionViewItem(2, 0); |
| 297 EXPECT_TRUE(encryptionItem.enabled); |
| 298 } |
| 299 |
| 300 TEST_F(SyncSettingsCollectionViewControllerTest, TestShouldDisplayError) { |
| 301 SyncSettingsCollectionViewController* sync_controller = |
| 302 CreateSyncController(); |
| 303 EXPECT_FALSE([sync_controller shouldDisplaySyncError]); |
| 304 EXPECT_FALSE([sync_controller shouldDisableSettingsOnSyncError]); |
| 305 EXPECT_FALSE([sync_controller shouldDisplayEncryptionError]); |
| 306 |
| 307 SetSyncServiceState(SyncSetupService::kSyncServiceSignInNeedsUpdate); |
| 308 EXPECT_TRUE([sync_controller shouldDisplaySyncError]); |
| 309 EXPECT_TRUE([sync_controller shouldDisableSettingsOnSyncError]); |
| 310 EXPECT_FALSE([sync_controller shouldDisplayEncryptionError]); |
| 311 |
| 312 SetSyncServiceState(SyncSetupService::kSyncServiceCouldNotConnect); |
| 313 EXPECT_TRUE([sync_controller shouldDisplaySyncError]); |
| 314 EXPECT_TRUE([sync_controller shouldDisableSettingsOnSyncError]); |
| 315 EXPECT_FALSE([sync_controller shouldDisplayEncryptionError]); |
| 316 |
| 317 SetSyncServiceState(SyncSetupService::kSyncServiceServiceUnavailable); |
| 318 EXPECT_TRUE([sync_controller shouldDisplaySyncError]); |
| 319 EXPECT_TRUE([sync_controller shouldDisableSettingsOnSyncError]); |
| 320 EXPECT_FALSE([sync_controller shouldDisplayEncryptionError]); |
| 321 |
| 322 SetSyncServiceState(SyncSetupService::kSyncServiceNeedsPassphrase); |
| 323 EXPECT_TRUE([sync_controller shouldDisplaySyncError]); |
| 324 EXPECT_FALSE([sync_controller shouldDisableSettingsOnSyncError]); |
| 325 EXPECT_TRUE([sync_controller shouldDisplayEncryptionError]); |
| 326 |
| 327 SetSyncServiceState(SyncSetupService::kSyncServiceUnrecoverableError); |
| 328 EXPECT_TRUE([sync_controller shouldDisplaySyncError]); |
| 329 EXPECT_TRUE([sync_controller shouldDisableSettingsOnSyncError]); |
| 330 EXPECT_FALSE([sync_controller shouldDisplayEncryptionError]); |
| 331 |
| 332 SetSyncServiceState(SyncSetupService::kNoSyncServiceError); |
| 333 EXPECT_FALSE([sync_controller shouldDisplaySyncError]); |
| 334 EXPECT_FALSE([sync_controller shouldDisableSettingsOnSyncError]); |
| 335 EXPECT_FALSE([sync_controller shouldDisplayEncryptionError]); |
| 336 } |
| 337 |
| 338 TEST_F(SyncSettingsCollectionViewControllerTest, |
| 339 TestSyncStateChangedWithEncryptionError) { |
| 340 TurnSyncOn(); |
| 341 TurnSyncEverythingOn(); |
| 342 |
| 343 SyncSettingsCollectionViewController* sync_controller = |
| 344 CreateSyncController(); |
| 345 |
| 346 TextAndErrorItem* encryptionItem = GetCollectionViewItem(2, 0); |
| 347 EXPECT_NSEQ(encryptionItem.text, |
| 348 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_TITLE)); |
| 349 CheckErrorIcon(NO, 2, 0); |
| 350 |
| 351 TurnSyncPassphraseErrorOn(); |
| 352 [sync_controller onSyncStateChanged]; |
| 353 encryptionItem = GetCollectionViewItem(3, 0); |
| 354 EXPECT_NSEQ(encryptionItem.text, |
| 355 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_TITLE)); |
| 356 CheckErrorIcon(YES, 3, 0); |
| 357 |
| 358 TurnSyncErrorOff(); |
| 359 [sync_controller onSyncStateChanged]; |
| 360 encryptionItem = GetCollectionViewItem(2, 0); |
| 361 EXPECT_NSEQ(encryptionItem.text, |
| 362 l10n_util::GetNSString(IDS_IOS_SYNC_ENCRYPTION_TITLE)); |
| 363 CheckErrorIcon(NO, 2, 0); |
| 364 } |
| 365 |
| 366 } // namespace |
OLD | NEW |