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.
5 #include "chrome/browser/sync_file_system/file_change.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace sync_file_system
{
15 FileChange
AddOrUpdateFile() {
16 return FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE
, SYNC_FILE_TYPE_FILE
);
19 FileChange
DeleteFile() {
20 return FileChange(FileChange::FILE_CHANGE_DELETE
, SYNC_FILE_TYPE_FILE
);
23 FileChange
AddDirectory() {
24 return FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE
,
25 SYNC_FILE_TYPE_DIRECTORY
);
28 FileChange
DeleteDirectory() {
29 return FileChange(FileChange::FILE_CHANGE_DELETE
, SYNC_FILE_TYPE_DIRECTORY
);
32 template <size_t INPUT_SIZE
>
33 void CreateList(FileChangeList
* list
, const FileChange (&inputs
)[INPUT_SIZE
]) {
35 for (size_t i
= 0; i
< INPUT_SIZE
; ++i
)
36 list
->Update(inputs
[i
]);
39 template <size_t EXPECTED_SIZE
>
40 void VerifyList(const FileChangeList
& list
,
41 const FileChange (&expected
)[EXPECTED_SIZE
]) {
42 SCOPED_TRACE(testing::Message() << "actual:" << list
.DebugString());
43 ASSERT_EQ(EXPECTED_SIZE
, list
.size());
44 for (size_t i
= 0; i
< list
.size(); ++i
) {
45 SCOPED_TRACE(testing::Message() << i
<< ": "
46 << " expected:" << expected
[i
].DebugString()
47 << " actual:" << list
.list().at(i
).DebugString());
48 EXPECT_EQ(expected
[i
], list
.list().at(i
));
54 TEST(FileChangeListTest
, UpdateSimple
) {
56 const FileChange kInput1
[] = { AddOrUpdateFile() };
57 const FileChange kExpected1
[] = { AddOrUpdateFile() };
58 CreateList(&list
, kInput1
);
59 VerifyList(list
, kExpected1
);
61 // AddOrUpdate + Delete -> Delete.
62 const FileChange kInput2
[] = { AddOrUpdateFile(), DeleteFile() };
63 const FileChange kExpected2
[] = { DeleteFile() };
64 CreateList(&list
, kInput2
);
65 VerifyList(list
, kExpected2
);
67 // Add + Delete -> empty (directory).
68 const FileChange kInput3
[] = { AddDirectory(), DeleteDirectory() };
69 CreateList(&list
, kInput3
);
70 ASSERT_TRUE(list
.empty());
72 // Delete + AddOrUpdate -> AddOrUpdate.
73 const FileChange kInput4
[] = { DeleteFile(), AddOrUpdateFile() };
74 const FileChange kExpected4
[] = { AddOrUpdateFile() };
75 CreateList(&list
, kInput4
);
76 VerifyList(list
, kExpected4
);
78 // Delete + Add -> Add (directory).
79 const FileChange kInput5
[] = { DeleteDirectory(), AddDirectory() };
80 const FileChange kExpected5
[] = { AddDirectory() };
81 CreateList(&list
, kInput5
);
82 VerifyList(list
, kExpected5
);
85 TEST(FileChangeListTest
, UpdateCombined
) {
89 const FileChange kInput1
[] = {
97 const FileChange kExpected1
[] = { DeleteFile(), AddDirectory() };
98 CreateList(&list
, kInput1
);
99 VerifyList(list
, kExpected1
);
101 const FileChange kInput2
[] = {
108 const FileChange kExpected2
[] = { AddOrUpdateFile() };
109 CreateList(&list
, kInput2
);
110 VerifyList(list
, kExpected2
);
112 const FileChange kInput3
[] = {
119 const FileChange kExpected3
[] = { AddOrUpdateFile() };
120 CreateList(&list
, kInput3
);
121 VerifyList(list
, kExpected3
);
123 const FileChange kInput4
[] = {
131 const FileChange kExpected4
[] = { DeleteFile() };
132 CreateList(&list
, kInput4
);
133 VerifyList(list
, kExpected4
);
136 } // namespace sync_file_system