Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / test / integration / single_client_backup_rollback_test.cc
index bf74892..59ef9c8 100644 (file)
@@ -3,8 +3,10 @@
 // found in the LICENSE file.
 
 #include "base/command_line.h"
+#include "base/file_util.h"
 #include "base/message_loop/message_loop.h"
 #include "base/prefs/pref_service.h"
+#include "base/run_loop.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/sync/profile_sync_service.h"
 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
@@ -14,7 +16,9 @@
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/pref_names.h"
 #include "components/bookmarks/browser/bookmark_model.h"
+#include "sync/internal_api/public/util/sync_db_util.h"
 #include "sync/test/fake_server/fake_server_verifier.h"
+#include "sync/util/time.h"
 
 using bookmarks_helper::AddFolder;
 using bookmarks_helper::AddURL;
@@ -40,19 +44,38 @@ class SingleClientBackupRollbackTest : public SyncTest {
           switches::kSyncDisableBackup);
   }
 
-  void EnableRollback() {
+  void DisableRollback() {
     CommandLine::ForCurrentProcess()->AppendSwitch(
-          switches::kSyncEnableRollback);
+          switches::kSyncDisableRollback);
+  }
+
+  base::Time GetBackupDbLastModified() {
+    base::RunLoop run_loop;
+
+    base::Time backup_time;
+    syncer::CheckSyncDbLastModifiedTime(
+        GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup")),
+        base::MessageLoopProxy::current(),
+        base::Bind(&SingleClientBackupRollbackTest::CheckDbCallback,
+                   base::Unretained(this), &backup_time));
+    base::MessageLoopProxy::current()->PostTask(
+        FROM_HERE, run_loop.QuitClosure());
+    run_loop.Run();
+    return backup_time;
   }
 
  private:
+  void CheckDbCallback(base::Time* time_out, base::Time time_in) {
+    *time_out = syncer::ProtoTimeToTime(syncer::TimeToProtoTime(time_in));
+  }
+
   DISALLOW_COPY_AND_ASSIGN(SingleClientBackupRollbackTest);
 };
 
-class BackupModeChecker {
+class SyncBackendStoppedChecker {
  public:
-  explicit BackupModeChecker(ProfileSyncService* service,
-                             base::TimeDelta timeout)
+  explicit SyncBackendStoppedChecker(ProfileSyncService* service,
+                                     base::TimeDelta timeout)
       : pss_(service),
         timeout_(timeout) {}
 
@@ -60,42 +83,79 @@ class BackupModeChecker {
     expiration_ = base::TimeTicks::Now() + timeout_;
     base::MessageLoop::current()->PostDelayedTask(
         FROM_HERE,
-        base::Bind(&BackupModeChecker::PeriodicCheck, base::Unretained(this)),
+        base::Bind(&SyncBackendStoppedChecker::PeriodicCheck,
+                   base::Unretained(this)),
         base::TimeDelta::FromSeconds(1));
     base::MessageLoop::current()->Run();
-    return IsBackupComplete();
+    return ProfileSyncService::IDLE == pss_->backend_mode();
   }
 
  private:
   void PeriodicCheck() {
-    if (IsBackupComplete() || base::TimeTicks::Now() > expiration_) {
+    if (ProfileSyncService::IDLE == pss_->backend_mode() ||
+        base::TimeTicks::Now() > expiration_) {
       base::MessageLoop::current()->Quit();
     } else {
       base::MessageLoop::current()->PostDelayedTask(
           FROM_HERE,
-          base::Bind(&BackupModeChecker::PeriodicCheck, base::Unretained(this)),
+          base::Bind(&SyncBackendStoppedChecker::PeriodicCheck,
+                     base::Unretained(this)),
           base::TimeDelta::FromSeconds(1));
     }
   }
 
-  bool IsBackupComplete() {
-    return pss_->backend_mode() == ProfileSyncService::BACKUP &&
-        pss_->ShouldPushChanges();
-  }
-
   ProfileSyncService* pss_;
   base::TimeDelta timeout_;
   base::TimeTicks expiration_;
 };
 
 #if defined(ENABLE_PRE_SYNC_BACKUP)
-#define MAYBE_TestBackupRollback TestBackupRollback
+#define MAYBE_TestBackup TestBackup
 #else
-#define MAYBE_TestBackupRollback DISABLED_TestBackupRollback
+#define MAYBE_TestBackup DISABLED_TestBackup
 #endif
 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
-                       MAYBE_TestBackupRollback) {
-  EnableRollback();
+                       MAYBE_TestBackup) {
+  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
+
+  // Setup sync, wait for its completion, and make sure changes were synced.
+  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
+
+  // Verify backup DB is created and backup time is set on device info.
+  base::Time backup_time = GetBackupDbLastModified();
+  ASSERT_FALSE(backup_time.is_null());
+  ASSERT_EQ(backup_time, GetSyncService(0)->GetDeviceBackupTimeForTesting());
+}
+
+#if defined(ENABLE_PRE_SYNC_BACKUP)
+#define MAYBE_TestBackupDisabled TestBackupDisabled
+#else
+#define MAYBE_TestBackupDisabled DISABLED_TestBackupDisabled
+#endif
+IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
+                       MAYBE_TestBackupDisabled) {
+  DisableBackup();
+
+  // Setup sync, wait for its completion, and make sure changes were synced.
+  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
+
+  // Verify backup DB is not created and backup time is not set on device info.
+  ASSERT_FALSE(base::PathExists(
+      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
+  ASSERT_TRUE(GetSyncService(0)->GetDeviceBackupTimeForTesting().is_null());
+}
+
+#if defined(ENABLE_PRE_SYNC_BACKUP)
+#define MAYBE_TestRollback TestRollback
+#else
+#define MAYBE_TestRollback DISABLED_TestRollback
+#endif
+IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
+                       MAYBE_TestRollback) {
   ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
 
   // Starting state:
@@ -113,20 +173,16 @@ IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
   ASSERT_TRUE(AddURL(0, tier1_b, 0, "tier1_b_url0",
                      GURL("http://www.nhl.com")));
 
-  BackupModeChecker checker(GetSyncService(0),
-                            base::TimeDelta::FromSeconds(15));
-  ASSERT_TRUE(checker.Wait());
-
   // Setup sync, wait for its completion, and make sure changes were synced.
   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
-  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
   ASSERT_TRUE(ModelMatchesVerifier(0));
 
   // Made bookmark changes while sync is on.
   Move(0, tier1_a->GetChild(0), tier1_b, 1);
   Remove(0, tier1_b, 0);
   ASSERT_TRUE(AddFolder(0, tier1_b, 1, "tier2_c"));
-  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
   ASSERT_TRUE(ModelMatchesVerifier(0));
 
   // Let server to return rollback command on next sync request.
@@ -135,7 +191,9 @@ IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
   // Make another change to trigger downloading of rollback command.
   Remove(0, tier1_b, 0);
 
-  // Wait for sync to switch to backup mode after finishing rollback.
+  // Wait for rollback to finish and sync backend is completely shut down.
+  SyncBackendStoppedChecker checker(GetSyncService(0),
+                                    base::TimeDelta::FromSeconds(5));
   ASSERT_TRUE(checker.Wait());
 
   // Verify bookmarks are restored.
@@ -146,55 +204,115 @@ IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
   ASSERT_EQ(1, tier1_b->child_count());
   const BookmarkNode* url2 = tier1_b->GetChild(0);
   ASSERT_EQ(GURL("http://www.nhl.com"), url2->url());
+
+  // Backup DB should be deleted after rollback is done.
+  ASSERT_FALSE(base::PathExists(
+      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
 }
 
 #if defined(ENABLE_PRE_SYNC_BACKUP)
-#define MAYBE_TestPrefBackupRollback TestPrefBackupRollback
+#define MAYBE_TestRollbackDisabled TestRollbackDisabled
 #else
-#define MAYBE_TestPrefBackupRollback DISABLED_TestPrefBackupRollback
+#define MAYBE_TestRollbackDisabled DISABLED_TestRollbackDisabled
 #endif
-// Verify local preferences are not affected by preferences in backup DB under
-// backup mode.
 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
-                       MAYBE_TestPrefBackupRollback) {
-  EnableRollback();
+                       MAYBE_TestRollbackDisabled) {
+  DisableRollback();
 
   ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
 
-  preferences_helper::ChangeStringPref(0, prefs::kHomePage, kUrl1);
+  // Starting state:
+  // other_node
+  //    -> http://mail.google.com  "url0"
+  //    -> http://www.nhl.com "url1"
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 0, "url0",
+                     GURL("http://mail.google.com")));
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
+                     GURL("http://www.nhl.com")));
 
-  BackupModeChecker checker(GetSyncService(0),
-                            base::TimeDelta::FromSeconds(15));
-  ASSERT_TRUE(checker.Wait());
+  // Setup sync, wait for its completion, and make sure changes were synced.
+  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
 
-  // Shut down backup, then change preference.
-  GetSyncService(0)->StartStopBackupForTesting();
-  preferences_helper::ChangeStringPref(0, prefs::kHomePage, kUrl2);
+  // Made bookmark changes while sync is on.
+  Remove(0, GetOtherNode(0), 1);
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url2",
+                     GURL("http://www.yahoo.com")));
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
+
+  // Let server to return rollback command on next sync request.
+  GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
+
+  // Make another change to trigger downloading of rollback command.
+  Remove(0, GetOtherNode(0), 0);
 
-  // Restart backup. Preference shouldn't change after backup starts.
-  GetSyncService(0)->StartStopBackupForTesting();
+  // Wait for rollback to finish and sync backend is completely shut down.
+  SyncBackendStoppedChecker checker(GetSyncService(0),
+                                    base::TimeDelta::FromSeconds(5));
   ASSERT_TRUE(checker.Wait());
-  ASSERT_EQ(kUrl2,
-            preferences_helper::GetPrefs(0)->GetString(prefs::kHomePage));
 
-  // Start sync and change preference.
+  // With rollback disabled, bookmarks in backup DB should not be restored.
+  // Only bookmark added during sync is present.
+  ASSERT_EQ(1, GetOtherNode(0)->child_count());
+  ASSERT_EQ(GURL("http://www.yahoo.com"),
+            GetOtherNode(0)->GetChild(0)->url());
+
+  // Backup DB should be deleted after.
+  ASSERT_FALSE(base::PathExists(
+      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
+}
+
+#if defined(ENABLE_PRE_SYNC_BACKUP)
+#define MAYBE_TestSyncDisabled TestSyncDisabled
+#else
+#define MAYBE_TestSyncDisabled DISABLED_TestSyncDisabled
+#endif
+IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
+                       MAYBE_TestSyncDisabled) {
+  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
+
+  // Starting state:
+  // other_node
+  //    -> http://mail.google.com  "url0"
+  //    -> http://www.nhl.com "url1"
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 0, "url0",
+                     GURL("http://mail.google.com")));
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
+                     GURL("http://www.nhl.com")));
+
+  // Setup sync, wait for its completion, and make sure changes were synced.
   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
-  preferences_helper::ChangeStringPref(0, prefs::kHomePage, kUrl3);
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
+
+  // Made bookmark changes while sync is on.
+  Remove(0, GetOtherNode(0), 1);
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url2",
+                     GURL("http://www.yahoo.com")));
   ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
   ASSERT_TRUE(ModelMatchesVerifier(0));
 
-  // Let server return rollback command on next sync request.
-  GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
+  // Let server to return birthday error on next sync request.
+  GetFakeServer()->TriggerError(sync_pb::SyncEnums::NOT_MY_BIRTHDAY);
 
   // Make another change to trigger downloading of rollback command.
-  preferences_helper::ChangeStringPref(0, prefs::kHomePage, "");
+  Remove(0, GetOtherNode(0), 0);
 
-  // Wait for sync to switch to backup mode after finishing rollback.
+  // Wait for rollback to finish and sync backend is completely shut down.
+  SyncBackendStoppedChecker checker(GetSyncService(0),
+                                    base::TimeDelta::FromSeconds(5));
   ASSERT_TRUE(checker.Wait());
 
-  // Verify preference is restored.
-  ASSERT_EQ(kUrl2,
-            preferences_helper::GetPrefs(0)->GetString(prefs::kHomePage));
+  // Shouldn't restore bookmarks with sign-out only.
+  ASSERT_EQ(1, GetOtherNode(0)->child_count());
+  ASSERT_EQ(GURL("http://www.yahoo.com"),
+            GetOtherNode(0)->GetChild(0)->url());
+
+  // Backup DB should be deleted after.
+  ASSERT_FALSE(base::PathExists(
+      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup"))));
 }
 
 #if defined(ENABLE_PRE_SYNC_BACKUP)
@@ -204,37 +322,40 @@ IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
 #endif
 IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
                        MAYBE_RollbackNoBackup) {
-  EnableRollback();
-
   ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
 
-  // Setup sync, wait for its completion, and make sure changes were synced.
-  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
-
   // Starting state:
   // other_node
   //    -> http://mail.google.com  "url0"
   //    -> http://www.nhl.com "url1"
   ASSERT_TRUE(AddURL(0, GetOtherNode(0), 0, "url0",
                      GURL("http://mail.google.com")));
-  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
-                     GURL("http://www.nhl.com")));
 
+  // Setup sync, wait for its completion, and make sure changes were synced.
+  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
   ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
   ASSERT_TRUE(ModelMatchesVerifier(0));
 
+  ASSERT_TRUE(AddURL(0, GetOtherNode(0), 1, "url1",
+                     GURL("http://www.nhl.com")));
+
+  // Delete backup DB.
+  base::DeleteFile(
+      GetProfile(0)->GetPath().Append(FILE_PATH_LITERAL("Sync Data Backup")),
+      true);
+
   // Let server to return rollback command on next sync request.
   GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
 
   // Make another change to trigger downloading of rollback command.
   Remove(0, GetOtherNode(0), 0);
 
-  // Wait for sync to switch to backup mode after finishing rollback.
-  BackupModeChecker checker(GetSyncService(0),
-                            base::TimeDelta::FromSeconds(15));
+  // Wait for backend to completely shut down.
+  SyncBackendStoppedChecker checker(GetSyncService(0),
+                                    base::TimeDelta::FromSeconds(15));
   ASSERT_TRUE(checker.Wait());
 
-  // Without backup DB, bookmarks added during sync shouldn't be removed.
+  // Without backup DB, bookmarks remain at the state when sync stops.
   ASSERT_EQ(1, GetOtherNode(0)->child_count());
   ASSERT_EQ(GURL("http://www.nhl.com"),
             GetOtherNode(0)->GetChild(0)->url());
@@ -254,13 +375,26 @@ IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
   ASSERT_TRUE(AddURL(0, sub_folder, 1, "", GURL(kUrl2)));
   ASSERT_TRUE(AddURL(0, sub_folder, 2, "", GURL(kUrl3)));
 
-  BackupModeChecker checker(GetSyncService(0),
-                            base::TimeDelta::FromSeconds(15));
-  ASSERT_TRUE(checker.Wait());
+  // Setup sync, wait for its completion, and make sure changes were synced.
+  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
+
+  // Made bookmark changes while sync is on.
+  Remove(0, sub_folder, 0);
+  Remove(0, sub_folder, 0);
+  ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0)));
+  ASSERT_TRUE(ModelMatchesVerifier(0));
 
-  // Restart backup.
-  GetSyncService(0)->StartStopBackupForTesting();
-  GetSyncService(0)->StartStopBackupForTesting();
+  // Let server to return rollback command on next sync request.
+  GetFakeServer()->TriggerError(sync_pb::SyncEnums::USER_ROLLBACK);
+
+  // Make another change to trigger downloading of rollback command.
+  Remove(0, sub_folder, 0);
+
+  // Wait for rollback to finish and sync backend is completely shut down.
+  SyncBackendStoppedChecker checker(GetSyncService(0),
+                                    base::TimeDelta::FromSeconds(5));
   ASSERT_TRUE(checker.Wait());
 
   // Verify bookmarks are unchanged.
@@ -269,4 +403,3 @@ IN_PROC_BROWSER_TEST_F(SingleClientBackupRollbackTest,
   ASSERT_EQ(GURL(kUrl2), sub_folder->GetChild(1)->url());
   ASSERT_EQ(GURL(kUrl3), sub_folder->GetChild(2)->url());
 }
-