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 Directory::KernelLoadInfo kernel_load_info_
;
37 // Test initialization of root entry when calling Load().
38 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
, Load
) {
39 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
40 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
42 EXPECT_TRUE(delete_journals_
.empty());
43 ASSERT_EQ(1u, handles_map_
.size()); // root node
44 ASSERT_TRUE(handles_map_
.count(1));
45 EntryKernel
* root
= handles_map_
[1];
46 EXPECT_TRUE(root
->ref(ID
).IsRoot());
47 EXPECT_EQ(1, root
->ref(META_HANDLE
));
48 EXPECT_TRUE(root
->ref(IS_DIR
));
51 // Test on-disk DB is not created if SaveChanges() is not called.
52 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
,
53 DontPersistIfSavingChangesNotCalled
) {
56 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
57 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
61 EXPECT_FALSE(base::PathExists(db_path_
));
64 // Test on-disk DB is not created when there are no changes.
65 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
,
66 DontPersistWhenSavingNoChanges
) {
69 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
70 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
73 Directory::SaveChangesSnapshot snapshot
;
74 store
.SaveChanges(snapshot
);
77 EXPECT_FALSE(base::PathExists(db_path_
));
80 // Test changes are persisted to disk when SaveChanges() is called.
81 TEST_F(DeferredOnDiskDirectoryBackingStoreTest
, PersistWhenSavingValidChanges
) {
84 DeferredOnDiskDirectoryBackingStore
store("test", db_path_
);
85 EXPECT_EQ(OPENED
, store
.Load(&handles_map_
, &delete_journals_
,
88 Directory::SaveChangesSnapshot snapshot
;
89 EntryKernel
* entry
= new EntryKernel();
90 entry
->put(ID
, Id::CreateFromClientString("test_entry"));
91 entry
->put(META_HANDLE
, 2);
92 entry
->mark_dirty(NULL
);
93 snapshot
.dirty_metas
.insert(entry
);
94 store
.SaveChanges(snapshot
);
97 STLDeleteValues(&handles_map_
);
99 ASSERT_TRUE(base::PathExists(db_path_
));
100 OnDiskDirectoryBackingStore
read_store("test", db_path_
);
101 EXPECT_EQ(OPENED
, read_store
.Load(&handles_map_
, &delete_journals_
,
102 &kernel_load_info_
));
103 ASSERT_EQ(2u, handles_map_
.size());
104 ASSERT_TRUE(handles_map_
.count(1)); // root node
105 ASSERT_TRUE(handles_map_
.count(2));
106 EntryKernel
* entry
= handles_map_
[2];
107 EXPECT_EQ(Id::CreateFromClientString("test_entry"), entry
->ref(ID
));
111 } // namespace syncable
112 } // namespace syncer