Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / sync / syncable / deferred_on_disk_directory_backing_store_unittest.cc
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.
4
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"
13
14 namespace syncer {
15 namespace syncable {
16 namespace {
17
18 static const base::FilePath::CharType kSyncDataFolderName[] =
19     FILE_PATH_LITERAL("Sync Data");
20
21 class DeferredOnDiskDirectoryBackingStoreTest : public testing::Test {
22  protected:
23   virtual void SetUp() OVERRIDE {
24     CHECK(temp_dir_.CreateUniqueTempDir());
25     db_path_ = temp_dir_.path().Append(kSyncDataFolderName);
26   }
27
28   virtual void TearDown() OVERRIDE {
29     STLDeleteValues(&handles_map_);
30   }
31
32   base::ScopedTempDir temp_dir_;
33   base::FilePath db_path_;
34   Directory::MetahandlesMap handles_map_;
35   JournalIndex delete_journals_;
36   Directory::KernelLoadInfo kernel_load_info_;
37 };
38
39 // Test initialization of root entry when calling Load().
40 TEST_F(DeferredOnDiskDirectoryBackingStoreTest, Load) {
41   DeferredOnDiskDirectoryBackingStore store("test", db_path_);
42   EXPECT_EQ(OPENED, store.Load(&handles_map_, &delete_journals_,
43                                &kernel_load_info_));
44   EXPECT_TRUE(delete_journals_.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));
51 }
52
53 // Test on-disk DB is not created if SaveChanges() is not called.
54 TEST_F(DeferredOnDiskDirectoryBackingStoreTest,
55        DontPersistIfSavingChangesNotCalled) {
56   {
57     // Open and close.
58     DeferredOnDiskDirectoryBackingStore store("test", db_path_);
59     EXPECT_EQ(OPENED, store.Load(&handles_map_, &delete_journals_,
60                                  &kernel_load_info_));
61   }
62
63   EXPECT_FALSE(base::PathExists(db_path_));
64 }
65
66 // Test on-disk DB is not created when there are no changes.
67 TEST_F(DeferredOnDiskDirectoryBackingStoreTest,
68        DontPersistWhenSavingNoChanges) {
69   {
70     // Open and close.
71     DeferredOnDiskDirectoryBackingStore store("test", db_path_);
72     EXPECT_EQ(OPENED, store.Load(&handles_map_, &delete_journals_,
73                                  &kernel_load_info_));
74
75     Directory::SaveChangesSnapshot snapshot;
76     store.SaveChanges(snapshot);
77   }
78
79   EXPECT_FALSE(base::PathExists(db_path_));
80 }
81
82 // Test changes are persisted to disk when SaveChanges() is called.
83 TEST_F(DeferredOnDiskDirectoryBackingStoreTest, PersistWhenSavingValidChanges) {
84   {
85     // Open and close.
86     DeferredOnDiskDirectoryBackingStore store("test", db_path_);
87     EXPECT_EQ(OPENED, store.Load(&handles_map_, &delete_journals_,
88                                  &kernel_load_info_));
89
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);
97   }
98
99   STLDeleteValues(&handles_map_);
100
101   ASSERT_TRUE(base::PathExists(db_path_));
102   OnDiskDirectoryBackingStore read_store("test", db_path_);
103   EXPECT_EQ(OPENED, read_store.Load(&handles_map_, &delete_journals_,
104                                     &kernel_load_info_));
105   ASSERT_EQ(2u, handles_map_.size());
106   ASSERT_TRUE(handles_map_.count(1));     // root node
107   ASSERT_TRUE(handles_map_.count(2));
108   EntryKernel* entry = handles_map_[2];
109   EXPECT_EQ(Id::CreateFromClientString("test_entry"), entry->ref(ID));
110 }
111
112 }  // namespace
113 }  // namespace syncable
114 }  // namespace syncer