OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <EarlGrey/EarlGrey.h> |
| 6 #import <XCTest/XCTest.h> |
| 7 |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "components/bookmarks/browser/bookmark_model.h" |
| 10 #include "components/bookmarks/browser/titled_url_match.h" |
| 11 #include "components/browser_sync/profile_sync_service.h" |
| 12 #include "components/strings/grit/components_strings.h" |
| 13 #include "components/sync/base/model_type.h" |
| 14 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 15 #include "ios/chrome/browser/bookmarks/bookmarks_utils.h" |
| 16 #include "ios/chrome/browser/signin/authentication_service.h" |
| 17 #include "ios/chrome/browser/signin/authentication_service_factory.h" |
| 18 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
| 19 #import "ios/chrome/browser/ui/settings/settings_collection_view_controller.h" |
| 20 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 21 #import "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h" |
| 22 #include "ios/chrome/grit/ios_strings.h" |
| 23 #import "ios/chrome/test/app/bookmarks_test_util.h" |
| 24 #import "ios/chrome/test/app/chrome_test_util.h" |
| 25 #import "ios/chrome/test/app/history_test_util.h" |
| 26 #import "ios/chrome/test/app/sync_test_util.h" |
| 27 #import "ios/chrome/test/app/tab_test_util.h" |
| 28 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 29 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 30 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 31 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 32 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" |
| 33 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.
h" |
| 34 #import "ios/testing/wait_util.h" |
| 35 #import "ios/web/public/test/http_server.h" |
| 36 #import "ios/web/public/test/http_server_util.h" |
| 37 #import "net/base/mac/url_conversions.h" |
| 38 #include "ui/base/l10n/l10n_util.h" |
| 39 #include "url/gurl.h" |
| 40 |
| 41 namespace { |
| 42 |
| 43 // Constant for timeout while waiting for asynchronous sync operations. |
| 44 const NSTimeInterval kSyncOperationTimeout = 5.0; |
| 45 |
| 46 // Returns a fake identity. |
| 47 ChromeIdentity* GetFakeIdentity1() { |
| 48 return [FakeChromeIdentity identityWithEmail:@"foo@gmail.com" |
| 49 gaiaID:@"fooID" |
| 50 name:@"Fake Foo"]; |
| 51 } |
| 52 |
| 53 // Opens the signin screen from the settings page. Must be called from the NTP. |
| 54 // User must not be signed in. |
| 55 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 56 void OpenSignInFromSettings() { |
| 57 const CGFloat scroll_displacement = 50.0; |
| 58 |
| 59 [ChromeEarlGreyUI openToolsMenu]; |
| 60 [[[EarlGrey |
| 61 selectElementWithMatcher:grey_accessibilityID(kToolsMenuSettingsId)] |
| 62 usingSearchAction:grey_scrollInDirection(kGREYDirectionDown, |
| 63 scroll_displacement) |
| 64 onElementWithMatcher:grey_accessibilityID(kToolsMenuTableViewId)] |
| 65 performAction:grey_tap()]; |
| 66 |
| 67 id<GREYMatcher> matcher = |
| 68 grey_allOf(grey_accessibilityID(kSettingsSignInCellId), |
| 69 grey_sufficientlyVisible(), nil); |
| 70 [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()]; |
| 71 } |
| 72 |
| 73 // Signs in the identity for the specific |userEmail|. This is performed via the |
| 74 // UI and must be called from the NTP. |
| 75 void SignInIdentity(NSString* userEmail) { |
| 76 OpenSignInFromSettings(); |
| 77 [[EarlGrey |
| 78 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabel( |
| 79 userEmail)] performAction:grey_tap()]; |
| 80 [[EarlGrey selectElementWithMatcher: |
| 81 chrome_test_util::buttonWithAccessibilityLabelId( |
| 82 IDS_IOS_ACCOUNT_CONSISTENCY_SETUP_SIGNIN_BUTTON)] |
| 83 performAction:grey_tap()]; |
| 84 [[EarlGrey selectElementWithMatcher: |
| 85 chrome_test_util::buttonWithAccessibilityLabelId( |
| 86 IDS_IOS_ACCOUNT_CONSISTENCY_CONFIRMATION_OK_BUTTON)] |
| 87 performAction:grey_tap()]; |
| 88 [[EarlGrey |
| 89 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( |
| 90 IDS_DONE)] performAction:grey_tap()]; |
| 91 } |
| 92 |
| 93 // Waits for sync to be initialized or not, based on |isSyncInitialized| and |
| 94 // fails with a GREYAssert if that condition is never met. |
| 95 void AssertSyncInitialized(bool is_initialized) { |
| 96 ConditionBlock condition = ^{ |
| 97 return chrome_test_util::IsSyncInitialized() == is_initialized; |
| 98 }; |
| 99 GREYAssert( |
| 100 testing::WaitUntilConditionOrTimeout(kSyncOperationTimeout, condition), |
| 101 @"Sync was not initialized"); |
| 102 } |
| 103 |
| 104 // Waits for |entity_count| entities of type |entity_type|, and fails with |
| 105 // a GREYAssert if the condition is not met, within a short period of time. |
| 106 void AssertNumberOfEntities(int entity_count, syncer::ModelType entity_type) { |
| 107 ConditionBlock condition = ^{ |
| 108 return chrome_test_util::GetNumberOfSyncEntities(entity_type) == |
| 109 entity_count; |
| 110 }; |
| 111 GREYAssert( |
| 112 testing::WaitUntilConditionOrTimeout(kSyncOperationTimeout, condition), |
| 113 @"Expected %d entities of the specified type", entity_count); |
| 114 } |
| 115 } // namespace |
| 116 |
| 117 // Hermetic sync tests, which use the fake sync server. |
| 118 @interface SyncFakeServerTestCase : ChromeTestCase |
| 119 @end |
| 120 |
| 121 @implementation SyncFakeServerTestCase |
| 122 |
| 123 - (void)tearDown { |
| 124 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 125 testing::kWaitForUIElementTimeout, |
| 126 ^{ |
| 127 return chrome_test_util::BookmarksLoaded(); |
| 128 }), |
| 129 @"Bookmark model did not load"); |
| 130 chrome_test_util::ClearBookmarks(); |
| 131 AssertNumberOfEntities(0, syncer::BOOKMARKS); |
| 132 |
| 133 chrome_test_util::ClearSyncServerData(); |
| 134 AssertNumberOfEntities(0, syncer::AUTOFILL_PROFILE); |
| 135 [super tearDown]; |
| 136 } |
| 137 |
| 138 - (void)setUp { |
| 139 [super setUp]; |
| 140 GREYAssertEqual(chrome_test_util::GetNumberOfSyncEntities(syncer::BOOKMARKS), |
| 141 0, @"No bookmarks should exist before sync tests start."); |
| 142 GREYAssertEqual(chrome_test_util::GetNumberOfSyncEntities(syncer::TYPED_URLS), |
| 143 0, @"No bookmarks should exist before sync tests start."); |
| 144 } |
| 145 |
| 146 // Tests that a bookmark added on the client (before Sync is enabled) is |
| 147 // uploaded to the Sync server once Sync is turned on. |
| 148 - (void)testSyncUploadBookmarkOnFirstSync { |
| 149 [self addBookmark:GURL("https://www.foo.com") withTitle:@"foo"]; |
| 150 |
| 151 // Sign in to sync, after a bookmark has been added. |
| 152 ChromeIdentity* identity = GetFakeIdentity1(); |
| 153 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 154 identity); |
| 155 SignInIdentity(identity.userEmail); |
| 156 |
| 157 // Assert that the correct number of bookmarks have been synced. |
| 158 AssertSyncInitialized(true); |
| 159 AssertNumberOfEntities(1, syncer::BOOKMARKS); |
| 160 } |
| 161 |
| 162 // Tests that a bookmark added on the client is uploaded to the Sync server. |
| 163 - (void)testSyncUploadBookmark { |
| 164 ChromeIdentity* identity = GetFakeIdentity1(); |
| 165 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 166 identity); |
| 167 SignInIdentity(identity.userEmail); |
| 168 |
| 169 // Add a bookmark after sync is initialized. |
| 170 AssertSyncInitialized(true); |
| 171 [self addBookmark:GURL("https://www.goo.com") withTitle:@"goo"]; |
| 172 AssertNumberOfEntities(1, syncer::BOOKMARKS); |
| 173 } |
| 174 |
| 175 // Tests that a bookmark injected in the FakeServer is synced down to the |
| 176 // client. |
| 177 - (void)testSyncDownloadBookmark { |
| 178 [[self class] assertBookmarksWithTitle:@"hoo" expectedCount:0]; |
| 179 chrome_test_util::InjectBookmarkOnFakeSyncServer("http://www.hoo.com", "hoo"); |
| 180 |
| 181 // Sign in to sync, after a bookmark has been injected in the sync server. |
| 182 ChromeIdentity* identity = GetFakeIdentity1(); |
| 183 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 184 identity); |
| 185 SignInIdentity(identity.userEmail); |
| 186 AssertSyncInitialized(true); |
| 187 |
| 188 [[self class] assertBookmarksWithTitle:@"hoo" expectedCount:1]; |
| 189 } |
| 190 |
| 191 // Tests that the local cache guid does not change when sync is restarted. |
| 192 - (void)testSyncCheckSameCacheGuid_SyncRestarted { |
| 193 // Sign in the fake identity. |
| 194 ChromeIdentity* identity = GetFakeIdentity1(); |
| 195 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 196 identity); |
| 197 SignInIdentity(identity.userEmail); |
| 198 AssertSyncInitialized(true); |
| 199 |
| 200 // Store the original guid, then restart sync. |
| 201 std::string original_guid = chrome_test_util::GetSyncCacheGuid(); |
| 202 chrome_test_util::StopSync(); |
| 203 AssertSyncInitialized(false); |
| 204 chrome_test_util::StartSync(); |
| 205 |
| 206 // Verify the guid did not change. |
| 207 AssertSyncInitialized(true); |
| 208 GREYAssertEqual(chrome_test_util::GetSyncCacheGuid(), original_guid, |
| 209 @"Stored guid doesn't match current value"); |
| 210 } |
| 211 |
| 212 // Tests that the local cache guid changes when the user signs out and then |
| 213 // signs back in with the same account. |
| 214 - (void)testSyncCheckDifferentCacheGuid_SignOutAndSignIn { |
| 215 // Sign in a fake identity, and store the initial sync guid. |
| 216 ChromeIdentity* identity = GetFakeIdentity1(); |
| 217 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 218 identity); |
| 219 SignInIdentity(identity.userEmail); |
| 220 AssertSyncInitialized(true); |
| 221 std::string original_guid = chrome_test_util::GetSyncCacheGuid(); |
| 222 |
| 223 // Sign out the current user. |
| 224 ios::ChromeBrowserState* browser_state = |
| 225 chrome_test_util::GetOriginalBrowserState(); |
| 226 AuthenticationService* authentication_service = |
| 227 AuthenticationServiceFactory::GetForBrowserState(browser_state); |
| 228 GREYAssert(authentication_service->IsAuthenticated(), |
| 229 @"User is not signed in."); |
| 230 authentication_service->SignOut(signin_metrics::SIGNOUT_TEST, nil); |
| 231 AssertSyncInitialized(false); |
| 232 |
| 233 // Sign the user back in, and verify the guid has changed. |
| 234 SignInIdentity(identity.userEmail); |
| 235 AssertSyncInitialized(true); |
| 236 GREYAssertTrue( |
| 237 chrome_test_util::GetSyncCacheGuid() != original_guid, |
| 238 @"guid didn't change after user signed out and signed back in"); |
| 239 } |
| 240 |
| 241 // Tests that the local cache guid does not change when sync is restarted, if |
| 242 // a user previously signed out and back in. |
| 243 // Test for http://crbug.com/413611 . |
| 244 - (void)testSyncCheckSameCacheGuid_SyncRestartedAfterSignOutAndSignIn { |
| 245 // Sign in a fake idenitty. |
| 246 ChromeIdentity* identity = GetFakeIdentity1(); |
| 247 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 248 identity); |
| 249 SignInIdentity(identity.userEmail); |
| 250 AssertSyncInitialized(true); |
| 251 |
| 252 // Sign out the current user. |
| 253 ios::ChromeBrowserState* browser_state = |
| 254 chrome_test_util::GetOriginalBrowserState(); |
| 255 AuthenticationService* authentication_service = |
| 256 AuthenticationServiceFactory::GetForBrowserState(browser_state); |
| 257 GREYAssert(authentication_service->IsAuthenticated(), |
| 258 @"User is not signed in."); |
| 259 authentication_service->SignOut(signin_metrics::SIGNOUT_TEST, nil); |
| 260 AssertSyncInitialized(false); |
| 261 |
| 262 // Sign the user back in. |
| 263 SignInIdentity(identity.userEmail); |
| 264 AssertSyncInitialized(true); |
| 265 |
| 266 // Record the initial guid, before restarting sync. |
| 267 std::string original_guid = chrome_test_util::GetSyncCacheGuid(); |
| 268 chrome_test_util::StopSync(); |
| 269 AssertSyncInitialized(false); |
| 270 chrome_test_util::StartSync(); |
| 271 |
| 272 // Verify the guid did not change after restarting sync. |
| 273 AssertSyncInitialized(true); |
| 274 GREYAssertEqual(chrome_test_util::GetSyncCacheGuid(), original_guid, |
| 275 @"Stored guid doesn't match current value"); |
| 276 } |
| 277 |
| 278 // Tests that autofill profile injected in FakeServer gets synced to client. |
| 279 - (void)testSyncDownloadAutofillProfile { |
| 280 const std::string kGuid = "2340E83B-5BEE-4560-8F95-5914EF7F539E"; |
| 281 const std::string kFullName = "Peter Pan"; |
| 282 GREYAssertFalse(chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName), |
| 283 @"autofill profile should not exist"); |
| 284 |
| 285 chrome_test_util::InjectAutofillProfileOnFakeSyncServer(kGuid, kFullName); |
| 286 [self setTearDownHandler:^{ |
| 287 chrome_test_util::ClearAutofillProfile(kGuid); |
| 288 }]; |
| 289 |
| 290 // Sign in to sync. |
| 291 ChromeIdentity* identity = GetFakeIdentity1(); |
| 292 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 293 identity); |
| 294 SignInIdentity(identity.userEmail); |
| 295 |
| 296 // Verify that the autofill profile has been downloaded. |
| 297 AssertSyncInitialized(YES); |
| 298 GREYAssertTrue(chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName), |
| 299 @"autofill profile should exist"); |
| 300 } |
| 301 |
| 302 // Test that update to autofill profile injected in FakeServer gets synced to |
| 303 // client. |
| 304 - (void)testSyncUpdateAutofillProfile { |
| 305 const std::string kGuid = "2340E83B-5BEE-4560-8F95-5914EF7F539E"; |
| 306 const std::string kFullName = "Peter Pan"; |
| 307 const std::string kUpdatedFullName = "Roger Rabbit"; |
| 308 GREYAssertFalse(chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName), |
| 309 @"autofill profile should not exist"); |
| 310 |
| 311 chrome_test_util::InjectAutofillProfileOnFakeSyncServer(kGuid, kFullName); |
| 312 [self setTearDownHandler:^{ |
| 313 chrome_test_util::ClearAutofillProfile(kGuid); |
| 314 }]; |
| 315 |
| 316 // Sign in to sync. |
| 317 ChromeIdentity* identity = GetFakeIdentity1(); |
| 318 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 319 identity); |
| 320 SignInIdentity(identity.userEmail); |
| 321 |
| 322 // Verify that the autofill profile has been downloaded. |
| 323 AssertSyncInitialized(YES); |
| 324 GREYAssertTrue(chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName), |
| 325 @"autofill profile should exist"); |
| 326 |
| 327 // Update autofill profile. |
| 328 chrome_test_util::InjectAutofillProfileOnFakeSyncServer(kGuid, |
| 329 kUpdatedFullName); |
| 330 |
| 331 // Trigger sync cycle and wait for update. |
| 332 chrome_test_util::TriggerSyncCycle(syncer::AUTOFILL_PROFILE); |
| 333 NSString* errorMessage = |
| 334 [NSString stringWithFormat: |
| 335 @"Did not find autofill profile for guid: %@, and name: %@", |
| 336 base::SysUTF8ToNSString(kGuid), |
| 337 base::SysUTF8ToNSString(kUpdatedFullName)]; |
| 338 ConditionBlock condition = ^{ |
| 339 return chrome_test_util::IsAutofillProfilePresent(kGuid, kUpdatedFullName); |
| 340 }; |
| 341 GREYAssert( |
| 342 testing::WaitUntilConditionOrTimeout(kSyncOperationTimeout, condition), |
| 343 errorMessage); |
| 344 } |
| 345 |
| 346 // Test that autofill profile deleted from FakeServer gets deleted from client |
| 347 // as well. |
| 348 - (void)testSyncDeleteAutofillProfile { |
| 349 const std::string kGuid = "2340E83B-5BEE-4560-8F95-5914EF7F539E"; |
| 350 const std::string kFullName = "Peter Pan"; |
| 351 GREYAssertFalse(chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName), |
| 352 @"autofill profile should not exist"); |
| 353 chrome_test_util::InjectAutofillProfileOnFakeSyncServer(kGuid, kFullName); |
| 354 [self setTearDownHandler:^{ |
| 355 chrome_test_util::ClearAutofillProfile(kGuid); |
| 356 }]; |
| 357 |
| 358 // Sign in to sync. |
| 359 ChromeIdentity* identity = GetFakeIdentity1(); |
| 360 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 361 identity); |
| 362 SignInIdentity(identity.userEmail); |
| 363 |
| 364 // Verify that the autofill profile has been downloaded |
| 365 AssertSyncInitialized(YES); |
| 366 GREYAssertTrue(chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName), |
| 367 @"autofill profile should exist"); |
| 368 |
| 369 // Delete autofill profile from server, and verify it is removed. |
| 370 chrome_test_util::DeleteAutofillProfileOnFakeSyncServer(kGuid); |
| 371 chrome_test_util::TriggerSyncCycle(syncer::AUTOFILL_PROFILE); |
| 372 ConditionBlock condition = ^{ |
| 373 return !chrome_test_util::IsAutofillProfilePresent(kGuid, kFullName); |
| 374 }; |
| 375 GREYAssert( |
| 376 testing::WaitUntilConditionOrTimeout(kSyncOperationTimeout, condition), |
| 377 @"Autofill profile was not deleted."); |
| 378 } |
| 379 |
| 380 // Tests that tabs opened on this client are committed to the Sync server and |
| 381 // that the created sessions entities are correct. |
| 382 - (void)testSyncUploadOpenTabs { |
| 383 // Create map of canned responses and set up the test HTML server. |
| 384 const GURL URL1 = web::test::HttpServer::MakeUrl("http://page1"); |
| 385 const GURL URL2 = web::test::HttpServer::MakeUrl("http://page2"); |
| 386 std::map<GURL, std::string> responses = { |
| 387 {URL1, std::string("page 1")}, {URL2, std::string("page 2")}, |
| 388 }; |
| 389 web::test::SetUpSimpleHttpServer(responses); |
| 390 |
| 391 // Load both URLs in separate tabs. |
| 392 [ChromeEarlGrey loadURL:URL1]; |
| 393 chrome_test_util::OpenNewTab(); |
| 394 [ChromeEarlGrey loadURL:URL2]; |
| 395 |
| 396 // Sign in to sync, after opening two tabs. |
| 397 ChromeIdentity* identity = GetFakeIdentity1(); |
| 398 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 399 identity); |
| 400 SignInIdentity(identity.userEmail); |
| 401 |
| 402 // Verify the sessions on the sync server. |
| 403 AssertSyncInitialized(true); |
| 404 AssertNumberOfEntities(3, syncer::SESSIONS); |
| 405 |
| 406 NSError* error = nil; |
| 407 BOOL success = chrome_test_util::VerifySessionsOnSyncServer( |
| 408 std::multiset<std::string>{URL1.spec(), URL2.spec()}, &error); |
| 409 |
| 410 DCHECK(success || error); |
| 411 GREYAssertTrue(success, [error localizedDescription]); |
| 412 } |
| 413 |
| 414 // Tests that a typed URL (after Sync is enabled) is uploaded to the Sync |
| 415 // server. |
| 416 - (void)testSyncTypedURLUpload { |
| 417 const GURL mockURL("http://not-a-real-site/"); |
| 418 |
| 419 chrome_test_util::ClearBrowsingHistory(); |
| 420 |
| 421 [self setTearDownHandler:^{ |
| 422 chrome_test_util::ClearBrowsingHistory(); |
| 423 }]; |
| 424 chrome_test_util::AddTypedURLOnClient(mockURL); |
| 425 |
| 426 // Sign in to sync. |
| 427 ChromeIdentity* identity = GetFakeIdentity1(); |
| 428 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 429 identity); |
| 430 SignInIdentity(identity.userEmail); |
| 431 |
| 432 AssertSyncInitialized(YES); |
| 433 |
| 434 // Trigger sync and verify the typed URL is on the fake sync server. |
| 435 chrome_test_util::TriggerSyncCycle(syncer::TYPED_URLS); |
| 436 __block NSError* blockSafeError = nil; |
| 437 GREYCondition* condition = [GREYCondition |
| 438 conditionWithName:@"Wait for typed URL to be uploaded." |
| 439 block:^BOOL { |
| 440 NSError* error = nil; |
| 441 BOOL result = |
| 442 chrome_test_util::VerifyNumberOfSyncEntitiesWithName( |
| 443 syncer::TYPED_URLS, mockURL.spec(), 1, &error); |
| 444 blockSafeError = [error copy]; |
| 445 return result; |
| 446 }]; |
| 447 BOOL success = [condition waitWithTimeout:kSyncOperationTimeout]; |
| 448 DCHECK(success || blockSafeError); |
| 449 if (blockSafeError) { |
| 450 [blockSafeError autorelease]; |
| 451 } |
| 452 GREYAssertTrue(success, [blockSafeError localizedDescription]); |
| 453 } |
| 454 |
| 455 // Tests that typed url is downloaded from sync server. |
| 456 - (void)testSyncTypedUrlDownload { |
| 457 const GURL mockURL("http://not-a-real-site/"); |
| 458 |
| 459 chrome_test_util::ClearBrowsingHistory(); |
| 460 [self setTearDownHandler:^{ |
| 461 chrome_test_util::ClearBrowsingHistory(); |
| 462 }]; |
| 463 |
| 464 // Inject typed url on server. |
| 465 chrome_test_util::InjectTypedURLOnFakeSyncServer(mockURL.spec()); |
| 466 |
| 467 // Sign in to sync. |
| 468 ChromeIdentity* identity = GetFakeIdentity1(); |
| 469 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 470 identity); |
| 471 SignInIdentity(identity.userEmail); |
| 472 |
| 473 AssertSyncInitialized(YES); |
| 474 |
| 475 // Wait for typed url to appear on client. |
| 476 __block NSError* blockSafeError = nil; |
| 477 |
| 478 GREYCondition* condition = [GREYCondition |
| 479 conditionWithName:@"Wait for typed URL to be downloaded." |
| 480 block:^BOOL { |
| 481 return chrome_test_util::IsTypedUrlPresentOnClient( |
| 482 mockURL, YES, &blockSafeError); |
| 483 }]; |
| 484 BOOL success = [condition waitWithTimeout:kSyncOperationTimeout]; |
| 485 DCHECK(success || blockSafeError); |
| 486 GREYAssert(success, [blockSafeError localizedDescription]); |
| 487 } |
| 488 |
| 489 // Tests that when typed url is deleted on the client, sync the change gets |
| 490 // propagated to server. |
| 491 - (void)testSyncTypedURLDeleteFromClient { |
| 492 const GURL mockURL("http://not-a-real-site/"); |
| 493 |
| 494 chrome_test_util::ClearBrowsingHistory(); |
| 495 |
| 496 [self setTearDownHandler:^{ |
| 497 chrome_test_util::ClearBrowsingHistory(); |
| 498 }]; |
| 499 |
| 500 // Inject typed url on server. |
| 501 chrome_test_util::InjectTypedURLOnFakeSyncServer(mockURL.spec()); |
| 502 |
| 503 // Sign in to sync. |
| 504 ChromeIdentity* identity = GetFakeIdentity1(); |
| 505 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 506 identity); |
| 507 SignInIdentity(identity.userEmail); |
| 508 |
| 509 AssertSyncInitialized(YES); |
| 510 |
| 511 // Wait for typed url to appear on client. |
| 512 __block NSError* blockSafeError = nil; |
| 513 |
| 514 GREYCondition* condition = [GREYCondition |
| 515 conditionWithName:@"Wait for typed URL to be downloaded." |
| 516 block:^BOOL { |
| 517 return chrome_test_util::IsTypedUrlPresentOnClient( |
| 518 mockURL, YES, &blockSafeError); |
| 519 }]; |
| 520 BOOL success = [condition waitWithTimeout:kSyncOperationTimeout]; |
| 521 DCHECK(success || blockSafeError); |
| 522 GREYAssert(success, [blockSafeError localizedDescription]); |
| 523 |
| 524 GREYAssert(chrome_test_util::GetNumberOfSyncEntities(syncer::TYPED_URLS) == 1, |
| 525 @"There should be 1 typed URL entity"); |
| 526 |
| 527 // Delete typed URL from client. |
| 528 chrome_test_util::DeleteTypedUrlFromClient(mockURL); |
| 529 |
| 530 // Trigger sync and wait for typed URL to be deleted. |
| 531 chrome_test_util::TriggerSyncCycle(syncer::TYPED_URLS); |
| 532 AssertNumberOfEntities(0, syncer::TYPED_URLS); |
| 533 } |
| 534 |
| 535 // Test that typed url is deleted from client after server sends tombstone for |
| 536 // that typed url. |
| 537 - (void)FLAKY_testSyncTypedURLDeleteFromServer { |
| 538 const GURL mockURL("http://not-a-real-site/"); |
| 539 |
| 540 chrome_test_util::ClearBrowsingHistory(); |
| 541 |
| 542 [self setTearDownHandler:^{ |
| 543 chrome_test_util::ClearBrowsingHistory(); |
| 544 }]; |
| 545 chrome_test_util::AddTypedURLOnClient(mockURL); |
| 546 |
| 547 // Sign in to sync. |
| 548 ChromeIdentity* identity = GetFakeIdentity1(); |
| 549 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()->AddIdentity( |
| 550 identity); |
| 551 SignInIdentity(identity.userEmail); |
| 552 |
| 553 AssertSyncInitialized(YES); |
| 554 chrome_test_util::TriggerSyncCycle(syncer::TYPED_URLS); |
| 555 |
| 556 NSError* error = nil; |
| 557 BOOL success = chrome_test_util::VerifyNumberOfSyncEntitiesWithName( |
| 558 syncer::TYPED_URLS, mockURL.spec(), 1, &error); |
| 559 DCHECK(success || error); |
| 560 GREYAssertTrue(success, [error localizedDescription]); |
| 561 |
| 562 chrome_test_util::DeleteTypedUrlFromClient(mockURL); |
| 563 |
| 564 // Trigger sync and wait for fake server to be updated. |
| 565 chrome_test_util::TriggerSyncCycle(syncer::TYPED_URLS); |
| 566 |
| 567 __block NSError* blockSafeError = nil; |
| 568 GREYCondition* condition = [GREYCondition |
| 569 conditionWithName:@"Wait for typed URL to be downloaded." |
| 570 block:^BOOL { |
| 571 NSError* error = nil; |
| 572 BOOL result = chrome_test_util::IsTypedUrlPresentOnClient( |
| 573 mockURL, NO, &error); |
| 574 blockSafeError = [error copy]; |
| 575 return result; |
| 576 }]; |
| 577 success = [condition waitWithTimeout:kSyncOperationTimeout]; |
| 578 DCHECK(success || blockSafeError); |
| 579 if (blockSafeError) { |
| 580 [blockSafeError autorelease]; |
| 581 } |
| 582 GREYAssert(success, [blockSafeError localizedDescription]); |
| 583 } |
| 584 |
| 585 #pragma mark - Test Utilities |
| 586 |
| 587 // Adds a bookmark with the given |url| and |title| into the Mobile Bookmarks |
| 588 // folder. |
| 589 // TODO(crbug.com/646164): This is copied from bookmarks_egtest.mm and should |
| 590 // move to common location. |
| 591 - (void)addBookmark:(const GURL)url withTitle:(NSString*)title { |
| 592 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 593 testing::kWaitForUIElementTimeout, |
| 594 ^{ |
| 595 return chrome_test_util::BookmarksLoaded(); |
| 596 }), |
| 597 @"Bookmark model did not load"); |
| 598 bookmarks::BookmarkModel* bookmark_model = |
| 599 ios::BookmarkModelFactory::GetForBrowserState( |
| 600 chrome_test_util::GetOriginalBrowserState()); |
| 601 bookmark_model->AddURL(bookmark_model->mobile_node(), 0, |
| 602 base::SysNSStringToUTF16(title), url); |
| 603 } |
| 604 |
| 605 // Asserts that |expectedCount| bookmarks exist with the corresponding |title| |
| 606 // using the BookmarkModel. |
| 607 // TODO(crbug.com/646164): This is copied from bookmarks_egtest.mm and should |
| 608 // move to common location. |
| 609 + (void)assertBookmarksWithTitle:(NSString*)title |
| 610 expectedCount:(NSUInteger)expectedCount { |
| 611 // Get BookmarkModel and wait for it to be loaded. |
| 612 bookmarks::BookmarkModel* bookmarkModel = |
| 613 ios::BookmarkModelFactory::GetForBrowserState( |
| 614 chrome_test_util::GetOriginalBrowserState()); |
| 615 |
| 616 // Verify the correct number of bookmarks exist. |
| 617 base::string16 matchString = base::SysNSStringToUTF16(title); |
| 618 std::vector<bookmarks::TitledUrlMatch> matches; |
| 619 bookmarkModel->GetBookmarksMatching(matchString, 50, &matches); |
| 620 const size_t count = matches.size(); |
| 621 GREYAssertEqual(expectedCount, count, @"Unexpected number of bookmarks"); |
| 622 } |
| 623 |
| 624 @end |
OLD | NEW |