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
,
56 type_root
.InitByTagLookup(ModelTypeToRootTag(type
)));
58 WriteNode
node(&trans
);
59 EXPECT_EQ(WriteNode::INIT_SUCCESS
,
60 node
.InitUniqueByCreation(type
, type_root
, client_tag
));
63 base::ScopedTempDir temp_dir_
;
64 base::MessageLoop loop_
; // Needed for WeakHandle
67 TEST_F(SyncBackupManagerTest
, NormalizeAndPersist
) {
68 scoped_ptr
<SyncBackupManager
> manager(new SyncBackupManager
);
69 InitManager(manager
.get());
71 CreateEntry(manager
->GetUserShare(), PREFERENCES
, "test");
74 // New entry is local and unsynced at first.
75 ReadTransaction
trans(FROM_HERE
, manager
->GetUserShare());
76 ReadNode
pref(&trans
);
77 EXPECT_EQ(BaseNode::INIT_OK
,
78 pref
.InitByClientTagLookup(PREFERENCES
, "test"));
79 EXPECT_FALSE(pref
.GetEntry()->GetId().ServerKnows());
80 EXPECT_TRUE(pref
.GetEntry()->GetIsUnsynced());
83 manager
->SaveChanges();
86 // New entry has server ID and unsynced bit is cleared after saving.
87 ReadTransaction
trans(FROM_HERE
, manager
->GetUserShare());
88 ReadNode
pref(&trans
);
89 EXPECT_EQ(BaseNode::INIT_OK
,
90 pref
.InitByClientTagLookup(PREFERENCES
, "test"));
91 EXPECT_TRUE(pref
.GetEntry()->GetId().ServerKnows());
92 EXPECT_FALSE(pref
.GetEntry()->GetIsUnsynced());
94 manager
->ShutdownOnSyncThread();
96 // Reopen db to verify entry is persisted.
97 manager
.reset(new SyncBackupManager
);
98 InitManager(manager
.get());
100 ReadTransaction
trans(FROM_HERE
, manager
->GetUserShare());
101 ReadNode
pref(&trans
);
102 EXPECT_EQ(BaseNode::INIT_OK
,
103 pref
.InitByClientTagLookup(PREFERENCES
, "test"));
104 EXPECT_TRUE(pref
.GetEntry()->GetId().ServerKnows());
105 EXPECT_FALSE(pref
.GetEntry()->GetIsUnsynced());
109 } // anonymous namespace
111 } // namespace syncer