Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / sync_backup_manager_unittest.cc
1 // Copyright 2014 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 #include "sync/internal_api/sync_backup_manager.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "sync/internal_api/public/read_node.h"
10 #include "sync/internal_api/public/read_transaction.h"
11 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
12 #include "sync/internal_api/public/test/test_internal_components_factory.h"
13 #include "sync/internal_api/public/write_node.h"
14 #include "sync/internal_api/public/write_transaction.h"
15 #include "sync/syncable/entry.h"
16 #include "sync/test/test_directory_backing_store.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 using ::testing::_;
21 using ::testing::Invoke;
22 using ::testing::WithArgs;
23
24 namespace syncer {
25
26 namespace {
27
28 void OnConfigDone(bool success) {
29   EXPECT_TRUE(success);
30 }
31
32 class SyncBackupManagerTest : public syncer::SyncManager::Observer,
33                               public testing::Test {
34  public:
35   MOCK_METHOD1(OnSyncCycleCompleted,
36                void(const sessions::SyncSessionSnapshot&));
37   MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus));
38   MOCK_METHOD1(OnActionableError, void(const SyncProtocolError&));
39   MOCK_METHOD1(OnMigrationRequested, void(ModelTypeSet));;
40   MOCK_METHOD1(OnProtocolEvent, void(const ProtocolEvent&));
41   MOCK_METHOD4(OnInitializationComplete,
42                void(const WeakHandle<JsBackend>&,
43                     const WeakHandle<DataTypeDebugInfoListener>&,
44                     bool, ModelTypeSet));
45
46  protected:
47   virtual void SetUp() OVERRIDE {
48     CHECK(temp_dir_.CreateUniqueTempDir());
49   }
50
51   void InitManager(SyncManager* manager, StorageOption storage_option) {
52     manager_ = manager;
53     EXPECT_CALL(*this, OnInitializationComplete(_, _, _, _))
54         .WillOnce(WithArgs<2>(Invoke(this,
55                                      &SyncBackupManagerTest::HandleInit)));
56
57     TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(),
58                                           storage_option);
59     manager->AddObserver(this);
60
61     base::RunLoop run_loop;
62     manager->Init(temp_dir_.path(),
63                   MakeWeakHandle(base::WeakPtr<JsEventHandler>()),
64                   "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(),
65                   std::vector<scoped_refptr<ModelSafeWorker> >(),
66                   NULL, NULL, SyncCredentials(), "", "", "", &factory,
67                   NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(),
68                   NULL, NULL);
69     loop_.PostTask(FROM_HERE, run_loop.QuitClosure());
70     run_loop.Run();
71   }
72
73   void CreateEntry(UserShare* user_share, ModelType type,
74                    const std::string& client_tag) {
75     WriteTransaction trans(FROM_HERE, user_share);
76     ReadNode type_root(&trans);
77     EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type));
78
79     WriteNode node(&trans);
80     EXPECT_EQ(WriteNode::INIT_SUCCESS,
81               node.InitUniqueByCreation(type, type_root, client_tag));
82   }
83
84  private:
85   void ConfigureSyncer() {
86     manager_->ConfigureSyncer(CONFIGURE_REASON_NEW_CLIENT,
87                               ModelTypeSet(SEARCH_ENGINES),
88                               ModelTypeSet(), ModelTypeSet(), ModelTypeSet(),
89                               ModelSafeRoutingInfo(),
90                               base::Bind(&OnConfigDone, true),
91                               base::Bind(&OnConfigDone, false));
92   }
93
94   void HandleInit(bool success) {
95     if (success) {
96       loop_.PostTask(FROM_HERE,
97                      base::Bind(&SyncBackupManagerTest::ConfigureSyncer,
98                                 base::Unretained(this)));
99     } else {
100       manager_->ShutdownOnSyncThread();
101     }
102   }
103
104   base::ScopedTempDir temp_dir_;
105   base::MessageLoop loop_;    // Needed for WeakHandle
106   SyncManager* manager_;
107 };
108
109 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) {
110   scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
111   InitManager(manager.get(), STORAGE_ON_DISK);
112
113   CreateEntry(manager->GetUserShare(), SEARCH_ENGINES, "test");
114
115   {
116     // New entry is local and unsynced at first.
117     ReadTransaction trans(FROM_HERE, manager->GetUserShare());
118     ReadNode pref(&trans);
119     EXPECT_EQ(BaseNode::INIT_OK,
120               pref.InitByClientTagLookup(SEARCH_ENGINES, "test"));
121     EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows());
122     EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced());
123   }
124
125   manager->SaveChanges();
126
127   {
128     // New entry has server ID and unsynced bit is cleared after saving.
129     ReadTransaction trans(FROM_HERE, manager->GetUserShare());
130     ReadNode pref(&trans);
131     EXPECT_EQ(BaseNode::INIT_OK,
132               pref.InitByClientTagLookup(SEARCH_ENGINES, "test"));
133     EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows());
134     EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced());
135   }
136   manager->ShutdownOnSyncThread();
137
138   // Reopen db to verify entry is persisted.
139   manager.reset(new SyncBackupManager);
140   InitManager(manager.get(), STORAGE_ON_DISK);
141   {
142     ReadTransaction trans(FROM_HERE, manager->GetUserShare());
143     ReadNode pref(&trans);
144     EXPECT_EQ(BaseNode::INIT_OK,
145               pref.InitByClientTagLookup(SEARCH_ENGINES, "test"));
146     EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows());
147     EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced());
148   }
149 }
150
151 TEST_F(SyncBackupManagerTest, FailToInitialize) {
152   // Test graceful shutdown on initialization failure.
153   scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
154   InitManager(manager.get(), STORAGE_INVALID);
155 }
156
157 }  // anonymous namespace
158
159 }  // namespace syncer
160