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.
5 #include "sync/internal_api/sync_backup_manager.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "sync/internal_api/public/read_node.h"
9 #include "sync/internal_api/public/read_transaction.h"
10 #include "sync/internal_api/public/test/test_internal_components_factory.h"
11 #include "sync/internal_api/public/write_node.h"
12 #include "sync/internal_api/public/write_transaction.h"
13 #include "sync/syncable/entry.h"
14 #include "sync/test/test_directory_backing_store.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 void OnConfigDone(bool success) {
25 class SyncBackupManagerTest : public testing::Test {
27 virtual void SetUp() OVERRIDE {
28 CHECK(temp_dir_.CreateUniqueTempDir());
31 void InitManager(SyncManager* manager) {
32 TestInternalComponentsFactory factory(InternalComponentsFactory::Switches(),
35 manager->Init(temp_dir_.path(),
36 MakeWeakHandle(base::WeakPtr<JsEventHandler>()),
37 "", 0, true, scoped_ptr<HttpPostProviderFactory>().Pass(),
38 std::vector<scoped_refptr<ModelSafeWorker> >(),
39 NULL, NULL, SyncCredentials(), "", "", "", &factory,
40 NULL, scoped_ptr<UnrecoverableErrorHandler>().Pass(),
42 manager->ConfigureSyncer(
43 CONFIGURE_REASON_NEW_CLIENT,
44 ModelTypeSet(PREFERENCES),
45 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(),
46 ModelSafeRoutingInfo(),
47 base::Bind(&OnConfigDone, true),
48 base::Bind(&OnConfigDone, false));
51 void CreateEntry(UserShare* user_share, ModelType type,
52 const std::string& client_tag) {
53 WriteTransaction trans(FROM_HERE, user_share);
54 ReadNode type_root(&trans);
55 EXPECT_EQ(BaseNode::INIT_OK, type_root.InitTypeRoot(type));
57 WriteNode node(&trans);
58 EXPECT_EQ(WriteNode::INIT_SUCCESS,
59 node.InitUniqueByCreation(type, type_root, client_tag));
62 base::ScopedTempDir temp_dir_;
63 base::MessageLoop loop_; // Needed for WeakHandle
66 TEST_F(SyncBackupManagerTest, NormalizeAndPersist) {
67 scoped_ptr<SyncBackupManager> manager(new SyncBackupManager);
68 InitManager(manager.get());
70 CreateEntry(manager->GetUserShare(), PREFERENCES, "test");
73 // New entry is local and unsynced at first.
74 ReadTransaction trans(FROM_HERE, manager->GetUserShare());
75 ReadNode pref(&trans);
76 EXPECT_EQ(BaseNode::INIT_OK,
77 pref.InitByClientTagLookup(PREFERENCES, "test"));
78 EXPECT_FALSE(pref.GetEntry()->GetId().ServerKnows());
79 EXPECT_TRUE(pref.GetEntry()->GetIsUnsynced());
82 manager->SaveChanges();
85 // New entry has server ID and unsynced bit is cleared after saving.
86 ReadTransaction trans(FROM_HERE, manager->GetUserShare());
87 ReadNode pref(&trans);
88 EXPECT_EQ(BaseNode::INIT_OK,
89 pref.InitByClientTagLookup(PREFERENCES, "test"));
90 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows());
91 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced());
93 manager->ShutdownOnSyncThread();
95 // Reopen db to verify entry is persisted.
96 manager.reset(new SyncBackupManager);
97 InitManager(manager.get());
99 ReadTransaction trans(FROM_HERE, manager->GetUserShare());
100 ReadNode pref(&trans);
101 EXPECT_EQ(BaseNode::INIT_OK,
102 pref.InitByClientTagLookup(PREFERENCES, "test"));
103 EXPECT_TRUE(pref.GetEntry()->GetId().ServerKnows());
104 EXPECT_FALSE(pref.GetEntry()->GetIsUnsynced());
108 } // anonymous namespace
110 } // namespace syncer