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 "base/files/scoped_temp_dir.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/stl_util.h"
8 #include "sync/syncable/deferred_on_disk_directory_backing_store.h"
9 #include "sync/syncable/directory.h"
10 #include "sync/syncable/entry_kernel.h"
11 #include "sync/syncable/on_disk_directory_backing_store.h"
12 #include "sync/syncable/syncable_delete_journal.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 static const base::FilePath::CharType kSyncDataFolderName
[] =
20 FILE_PATH_LITERAL("Sync Data");
22 class DeferredOnDiskDirectoryBackingStoreTest
: public testing::Test
{
24 void SetUp() override
{
25 CHECK(temp_dir_
.CreateUniqueTempDir());
26 db_path_
= temp_dir_
.path().Append(kSyncDataFolderName
);
29 void TearDown() override
{ STLDeleteValues(&handles_map_
); }
31 base::MessageLoop message_loop_
;
32 base::ScopedTempDir temp_dir_
;
33 base::FilePath db_path_
;
34 Directory::MetahandlesMap handles_map_
;
35 JournalIndex delete_journals_
;
36 MetahandleSet metahandles_to_purge_
;
37 Directory::KernelLoadInfo kernel_load_info_
;
40 // Test initialization of root entry when calling Load().
41 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
, Load
) {
42 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
43 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
44 &metahandles_to_purge_
, &kernel_load_info_
));
45 EXPECT_TRUE(delete_journals_
.empty());
46 EXPECT_TRUE(metahandles_to_purge_
.empty());
47 ASSERT_EQ(1u, handles_map_
.size()); // root node
48 ASSERT_TRUE(handles_map_
.count(1));
49 EntryKernel
* root
= handles_map_
[1];
50 EXPECT_TRUE(root
->ref(ID
).IsRoot());
51 EXPECT_EQ(1, root
->ref(META_HANDLE
));
52 EXPECT_TRUE(root
->ref(IS_DIR
));
55 // Test on-disk DB is not created if SaveChanges() is not called.
56 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
,
57 DontPersistIfSavingChangesNotCalled
) {
60 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
61 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
62 &metahandles_to_purge_
, &kernel_load_info_
));
65 EXPECT_FALSE(base::PathExists(db_path_
));
68 // Test on-disk DB is not created when there are no changes.
69 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
,
70 DontPersistWhenSavingNoChanges
) {
73 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
74 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
75 &metahandles_to_purge_
, &kernel_load_info_
));
77 Directory::SaveChangesSnapshot snapshot
;
78 store
.SaveChanges(snapshot
);
81 EXPECT_FALSE(base::PathExists(db_path_
));
84 // Test changes are persisted to disk when SaveChanges() is called.
85 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
, PersistWhenSavingValidChanges
) {
88 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
89 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
90 &metahandles_to_purge_
, &kernel_load_info_
));
92 Directory::SaveChangesSnapshot snapshot
;
93 EntryKernel
* entry
= new EntryKernel();
94 entry
->put(ID
, Id::CreateFromClientString("test_entry"));
95 entry
->put(META_HANDLE
, 2);
96 entry
->mark_dirty(NULL
);
97 snapshot
.dirty_metas
.insert(entry
);
98 store
.SaveChanges(snapshot
);
101 STLDeleteValues(&handles_map_
);
103 ASSERT_TRUE(base::PathExists(db_path_
));
104 OnDiskDirectoryBackingStore
read_store("test", db_path_
);
106 read_store
.Load(&handles_map_
, &delete_journals_
,
107 &metahandles_to_purge_
, &kernel_load_info_
));
108 ASSERT_EQ(2u, handles_map_
.size());
109 ASSERT_TRUE(handles_map_
.count(1)); // root node
110 ASSERT_TRUE(handles_map_
.count(2));
111 EntryKernel
* entry
= handles_map_
[2];
112 EXPECT_EQ(Id::CreateFromClientString("test_entry"), entry
->ref(ID
));
116 } // namespace syncable
117 } // namespace syncer