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/stl_util.h"
7 #include "sync/syncable/deferred_on_disk_directory_backing_store.h"
8 #include "sync/syncable/directory.h"
9 #include "sync/syncable/entry_kernel.h"
10 #include "sync/syncable/on_disk_directory_backing_store.h"
11 #include "sync/syncable/syncable_delete_journal.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 static const base::FilePath::CharType kSyncDataFolderName
[] =
19 FILE_PATH_LITERAL("Sync Data");
21 class DeferredOnDiskDirectoryBackingStoreTest
: public testing::Test
{
23 void SetUp() override
{
24 CHECK(temp_dir_
.CreateUniqueTempDir());
25 db_path_
= temp_dir_
.path().Append(kSyncDataFolderName
);
28 void TearDown() override
{ STLDeleteValues(&handles_map_
); }
30 base::ScopedTempDir temp_dir_
;
31 base::FilePath db_path_
;
32 Directory::MetahandlesMap handles_map_
;
33 JournalIndex delete_journals_
;
34 MetahandleSet metahandles_to_purge_
;
35 Directory::KernelLoadInfo kernel_load_info_
;
38 // Test initialization of root entry when calling Load().
39 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
, Load
) {
40 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
41 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
42 &metahandles_to_purge_
, &kernel_load_info_
));
43 EXPECT_TRUE(delete_journals_
.empty());
44 EXPECT_TRUE(metahandles_to_purge_
.empty());
45 ASSERT_EQ(1u, handles_map_
.size()); // root node
46 ASSERT_TRUE(handles_map_
.count(1));
47 EntryKernel
* root
= handles_map_
[1];
48 EXPECT_TRUE(root
->ref(ID
).IsRoot());
49 EXPECT_EQ(1, root
->ref(META_HANDLE
));
50 EXPECT_TRUE(root
->ref(IS_DIR
));
53 // Test on-disk DB is not created if SaveChanges() is not called.
54 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
,
55 DontPersistIfSavingChangesNotCalled
) {
58 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
59 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
60 &metahandles_to_purge_
, &kernel_load_info_
));
63 EXPECT_FALSE(base::PathExists(db_path_
));
66 // Test on-disk DB is not created when there are no changes.
67 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
,
68 DontPersistWhenSavingNoChanges
) {
71 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
72 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
73 &metahandles_to_purge_
, &kernel_load_info_
));
75 Directory::SaveChangesSnapshot snapshot
;
76 store
.SaveChanges(snapshot
);
79 EXPECT_FALSE(base::PathExists(db_path_
));
82 // Test changes are persisted to disk when SaveChanges() is called.
83 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
, PersistWhenSavingValidChanges
) {
86 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
87 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
88 &metahandles_to_purge_
, &kernel_load_info_
));
90 Directory::SaveChangesSnapshot snapshot
;
91 EntryKernel
* entry
= new EntryKernel();
92 entry
->put(ID
, Id::CreateFromClientString("test_entry"));
93 entry
->put(META_HANDLE
, 2);
94 entry
->mark_dirty(NULL
);
95 snapshot
.dirty_metas
.insert(entry
);
96 store
.SaveChanges(snapshot
);
99 STLDeleteValues(&handles_map_
);
101 ASSERT_TRUE(base::PathExists(db_path_
));
102 OnDiskDirectoryBackingStore
read_store("test", db_path_
);
104 read_store
.Load(&handles_map_
, &delete_journals_
,
105 &metahandles_to_purge_
, &kernel_load_info_
));
106 ASSERT_EQ(2u, handles_map_
.size());
107 ASSERT_TRUE(handles_map_
.count(1)); // root node
108 ASSERT_TRUE(handles_map_
.count(2));
109 EntryKernel
* entry
= handles_map_
[2];
110 EXPECT_EQ(Id::CreateFromClientString("test_entry"), entry
->ref(ID
));
114 } // namespace syncable
115 } // namespace syncer