- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend_v1 / local_sync_operation_resolver_unittest.cc
1 // Copyright 2013 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 <string>
6 #include <vector>
7
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "chrome/browser/sync_file_system/drive_backend_v1/local_sync_operation_resolver.h"
11 #include "chrome/browser/sync_file_system/file_change.h"
12 #include "chrome/browser/sync_file_system/sync_file_type.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace sync_file_system {
16
17 namespace {
18
19 struct Input {
20   scoped_ptr<FileChange> remote_file_change;
21   SyncFileType remote_file_type_in_metadata;
22
23   std::string DebugString() const {
24     std::string change_type =
25         (remote_file_change == NULL) ? "none"
26                                      : remote_file_change->DebugString();
27     std::ostringstream ss;
28     ss << "RemoteFileChange: " << change_type
29        << ", RemoteFileTypeInMetadata: " << remote_file_type_in_metadata;
30     return ss.str();
31   }
32
33   Input(FileChange* remote_file_change,
34         SyncFileType remote_file_type_in_metadata)
35       : remote_file_change(remote_file_change),
36         remote_file_type_in_metadata(remote_file_type_in_metadata) {
37   }
38 };
39
40 template <typename type, size_t array_size>
41 std::vector<type> CreateList(const type (&inputs)[array_size]) {
42   return std::vector<type>(inputs, inputs + array_size);
43 }
44
45 ScopedVector<Input> CreateInput() {
46   SyncFileType dummy_file_type = SYNC_FILE_TYPE_UNKNOWN;
47
48   ScopedVector<Input> vector;
49   vector.push_back(new Input(NULL, SYNC_FILE_TYPE_UNKNOWN));
50   vector.push_back(new Input(NULL, SYNC_FILE_TYPE_FILE));
51   vector.push_back(new Input(NULL, SYNC_FILE_TYPE_DIRECTORY));
52
53   // When remote_file_change exists, the resolver does not take care of
54   // remote_file_type_in_metadata.
55   vector.push_back(new Input(
56       new FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
57                      SYNC_FILE_TYPE_FILE),
58       dummy_file_type));
59   vector.push_back(new Input(
60       new FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
61                      SYNC_FILE_TYPE_DIRECTORY),
62       dummy_file_type));
63   vector.push_back(new Input(
64       new FileChange(FileChange::FILE_CHANGE_DELETE,
65                      SYNC_FILE_TYPE_FILE),
66       dummy_file_type));
67   vector.push_back(new Input(
68       new FileChange(FileChange::FILE_CHANGE_DELETE,
69                      SYNC_FILE_TYPE_DIRECTORY),
70       dummy_file_type));
71
72   return vector.Pass();
73 }
74
75 std::string DebugString(const ScopedVector<Input>& inputs, int number) {
76   std::ostringstream ss;
77   ss << "Case " << number << ": (" << inputs[number]->DebugString() << ")";
78   return ss.str();
79 }
80
81 }  // namespace
82
83 class LocalSyncOperationResolverTest : public testing::Test {
84  public:
85   LocalSyncOperationResolverTest() {}
86
87  protected:
88   typedef LocalSyncOperationResolver Resolver;
89   typedef std::vector<SyncOperationType> ExpectedTypes;
90
91  private:
92   DISALLOW_COPY_AND_ASSIGN(LocalSyncOperationResolverTest);
93 };
94
95 TEST_F(LocalSyncOperationResolverTest, ResolveForAddOrUpdateFile) {
96   const SyncOperationType kExpectedTypes[] = {
97     SYNC_OPERATION_ADD_FILE,
98     SYNC_OPERATION_UPDATE_FILE,
99     SYNC_OPERATION_RESOLVE_TO_REMOTE,
100
101     SYNC_OPERATION_CONFLICT,
102     SYNC_OPERATION_RESOLVE_TO_REMOTE,
103     SYNC_OPERATION_RESOLVE_TO_LOCAL,
104     SYNC_OPERATION_RESOLVE_TO_LOCAL,
105   };
106
107   ExpectedTypes expected_types = CreateList(kExpectedTypes);
108   ScopedVector<Input> inputs = CreateInput();
109
110   ASSERT_EQ(expected_types.size(), inputs.size());
111   for (ExpectedTypes::size_type i = 0; i < expected_types.size(); ++i) {
112     EXPECT_EQ(expected_types[i],
113               Resolver::ResolveForAddOrUpdateFile(
114                   inputs[i]->remote_file_change.get(),
115                   inputs[i]->remote_file_type_in_metadata))
116         << DebugString(inputs, i);
117   }
118 }
119
120 TEST_F(LocalSyncOperationResolverTest, ResolveForAddOrUpdateFileInConflict) {
121   const SyncOperationType kExpectedTypes[] = {
122     SYNC_OPERATION_CONFLICT,
123     SYNC_OPERATION_CONFLICT,
124     SYNC_OPERATION_CONFLICT,
125
126     SYNC_OPERATION_CONFLICT,
127     SYNC_OPERATION_RESOLVE_TO_REMOTE,
128     SYNC_OPERATION_RESOLVE_TO_LOCAL,
129     SYNC_OPERATION_RESOLVE_TO_LOCAL,
130   };
131
132   ExpectedTypes expected_types = CreateList(kExpectedTypes);
133   ScopedVector<Input> inputs = CreateInput();
134
135   ASSERT_EQ(expected_types.size(), inputs.size());
136   for (ExpectedTypes::size_type i = 0; i < expected_types.size(); ++i) {
137     EXPECT_EQ(expected_types[i],
138               Resolver::ResolveForAddOrUpdateFileInConflict(
139                   inputs[i]->remote_file_change.get()))
140         << DebugString(inputs, i);
141   }
142 }
143
144 TEST_F(LocalSyncOperationResolverTest, ResolveForAddDirectory) {
145   const SyncOperationType kExpectedTypes[] = {
146     SYNC_OPERATION_ADD_DIRECTORY,
147     SYNC_OPERATION_RESOLVE_TO_LOCAL,
148     SYNC_OPERATION_NONE,
149
150     SYNC_OPERATION_RESOLVE_TO_LOCAL,
151     SYNC_OPERATION_NONE,
152     SYNC_OPERATION_ADD_DIRECTORY,
153     SYNC_OPERATION_RESOLVE_TO_LOCAL,
154   };
155
156   ExpectedTypes expected_types = CreateList(kExpectedTypes);
157   ScopedVector<Input> inputs = CreateInput();
158
159   ASSERT_EQ(expected_types.size(), inputs.size());
160   for (ExpectedTypes::size_type i = 0; i < expected_types.size(); ++i) {
161     EXPECT_EQ(expected_types[i],
162               Resolver::ResolveForAddDirectory(
163                   inputs[i]->remote_file_change.get(),
164                   inputs[i]->remote_file_type_in_metadata))
165         << DebugString(inputs, i);
166   }
167 }
168
169 TEST_F(LocalSyncOperationResolverTest, ResolveForAddDirectoryInConflict) {
170   EXPECT_EQ(SYNC_OPERATION_RESOLVE_TO_LOCAL,
171             Resolver::ResolveForAddDirectoryInConflict());
172 }
173
174 TEST_F(LocalSyncOperationResolverTest, ResolveForDelete) {
175   const SyncOperationType kExpectedTypes[] = {
176     SYNC_OPERATION_DELETE,
177     SYNC_OPERATION_DELETE,
178     SYNC_OPERATION_DELETE,
179
180     SYNC_OPERATION_RESOLVE_TO_REMOTE,
181     SYNC_OPERATION_RESOLVE_TO_REMOTE,
182     SYNC_OPERATION_DELETE_METADATA,
183     SYNC_OPERATION_DELETE_METADATA,
184   };
185
186   ExpectedTypes expected_types = CreateList(kExpectedTypes);
187   ScopedVector<Input> inputs = CreateInput();
188
189   ASSERT_EQ(expected_types.size(), inputs.size());
190   for (ExpectedTypes::size_type i = 0; i < expected_types.size(); ++i) {
191     EXPECT_EQ(expected_types[i],
192               Resolver::ResolveForDelete(
193                   inputs[i]->remote_file_change.get(),
194                   inputs[i]->remote_file_type_in_metadata))
195         << DebugString(inputs, i);
196   }
197 }
198
199 TEST_F(LocalSyncOperationResolverTest, ResolveForDeleteInConflict) {
200   const SyncOperationType kExpectedTypes[] = {
201     SYNC_OPERATION_RESOLVE_TO_REMOTE,
202     SYNC_OPERATION_RESOLVE_TO_REMOTE,
203     SYNC_OPERATION_RESOLVE_TO_REMOTE,
204
205     SYNC_OPERATION_RESOLVE_TO_REMOTE,
206     SYNC_OPERATION_RESOLVE_TO_REMOTE,
207     SYNC_OPERATION_DELETE_METADATA,
208     SYNC_OPERATION_DELETE_METADATA,
209   };
210
211   ExpectedTypes expected_types = CreateList(kExpectedTypes);
212   ScopedVector<Input> inputs = CreateInput();
213
214   ASSERT_EQ(expected_types.size(), inputs.size());
215   for (ExpectedTypes::size_type i = 0; i < expected_types.size(); ++i) {
216     EXPECT_EQ(expected_types[i],
217               Resolver::ResolveForDeleteInConflict(
218                   inputs[i]->remote_file_change.get()))
219         << DebugString(inputs, i);
220   }
221 }
222
223 }  // namespace sync_file_system