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 "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"
21 using ::testing::Invoke
;
22 using ::testing::WithArgs
;
28 void OnConfigDone(bool success
) {
32 class SyncBackupManagerTest
: public syncer::SyncManager::Observer
,
33 public testing::Test
{
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
>&,
47 virtual void SetUp() OVERRIDE
{
48 CHECK(temp_dir_
.CreateUniqueTempDir());
51 void InitManager(SyncManager
* manager
, StorageOption storage_option
) {
53 EXPECT_CALL(*this, OnInitializationComplete(_
, _
, _
, _
))
54 .WillOnce(WithArgs
<2>(Invoke(this,
55 &SyncBackupManagerTest::HandleInit
)));
57 TestInternalComponentsFactory
factory(InternalComponentsFactory::Switches(),
59 manager
->AddObserver(this);
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(),
69 loop_
.PostTask(FROM_HERE
, run_loop
.QuitClosure());
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
));
79 WriteNode
node(&trans
);
80 EXPECT_EQ(WriteNode::INIT_SUCCESS
,
81 node
.InitUniqueByCreation(type
, type_root
, client_tag
));
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));
94 void HandleInit(bool success
) {
96 loop_
.PostTask(FROM_HERE
,
97 base::Bind(&SyncBackupManagerTest::ConfigureSyncer
,
98 base::Unretained(this)));
100 manager_
->ShutdownOnSyncThread();
104 base::ScopedTempDir temp_dir_
;
105 base::MessageLoop loop_
; // Needed for WeakHandle
106 SyncManager
* manager_
;
109 TEST_F(SyncBackupManagerTest
, NormalizeAndPersist
) {
110 scoped_ptr
<SyncBackupManager
> manager(new SyncBackupManager
);
111 InitManager(manager
.get(), STORAGE_ON_DISK
);
113 CreateEntry(manager
->GetUserShare(), SEARCH_ENGINES
, "test");
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());
125 manager
->SaveChanges();
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());
136 manager
->ShutdownOnSyncThread();
138 // Reopen db to verify entry is persisted.
139 manager
.reset(new SyncBackupManager
);
140 InitManager(manager
.get(), STORAGE_ON_DISK
);
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());
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
);
157 } // anonymous namespace
159 } // namespace syncer