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 "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.h"
7 #include "base/files/file_path.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 #define FPL(path) FILE_PATH_LITERAL(path)
12 namespace sync_file_system
{
13 namespace drive_backend
{
17 base::FilePath
MakePath(const base::FilePath::StringType
& path
) {
18 return base::FilePath(path
).NormalizePathSeparators();
21 bool InsertPath(TaskDependencyManager
* manager
,
22 const std::string
& app_id
,
23 const base::FilePath::StringType
& path
) {
25 blocker
.app_id
= app_id
;
26 blocker
.paths
.push_back(MakePath(path
));
27 return manager
->Insert(&blocker
);
30 void ErasePath(TaskDependencyManager
* manager
,
31 const std::string
& app_id
,
32 const base::FilePath::StringType
& path
) {
34 blocker
.app_id
= app_id
;
35 blocker
.paths
.push_back(MakePath(path
));
36 return manager
->Erase(&blocker
);
39 bool InsertExclusiveTask(TaskDependencyManager
* manager
) {
41 blocker
.exclusive
= true;
42 return manager
->Insert(&blocker
);
45 void EraseExclusiveTask(TaskDependencyManager
* manager
) {
47 blocker
.exclusive
= true;
48 manager
->Erase(&blocker
);
53 TEST(TaskDependencyManagerTest
, BasicTest
) {
54 TaskDependencyManager manager
;
56 blocker
.app_id
= "app_id";
57 blocker
.paths
.push_back(MakePath(FPL("/folder/file")));
58 blocker
.file_ids
.push_back("file_id");
59 blocker
.tracker_ids
.push_back(100);
61 EXPECT_TRUE(manager
.Insert(&blocker
));
62 EXPECT_FALSE(manager
.Insert(&blocker
));
64 manager
.Erase(&blocker
);
66 EXPECT_TRUE(manager
.Insert(&blocker
));
68 manager
.Erase(&blocker
);
71 TEST(TaskDependencyManagerTest
, BlocksAncestorAndDescendant
) {
72 TaskDependencyManager manager
;
74 EXPECT_TRUE(InsertPath(
75 &manager
, "app_id", FPL("/ancestor/parent/self/child/descendant")));
76 EXPECT_FALSE(InsertPath(&manager
, "app_id", FPL("/ancestor")));
77 EXPECT_FALSE(InsertPath(&manager
, "app_id", FPL("/ancestor/parent")));
78 EXPECT_FALSE(InsertPath(&manager
, "app_id", FPL("/ancestor/parent/self")));
79 EXPECT_FALSE(InsertPath(
80 &manager
, "app_id", FPL("/ancestor/parent/self/child")));
81 EXPECT_FALSE(InsertPath(
82 &manager
, "app_id", FPL("/ancestor/parent/self/child/descendant")));
84 EXPECT_TRUE(InsertPath(
85 &manager
, "another_app_id", FPL("/ancestor/parent/self")));
86 ErasePath(&manager
, "another_app_id", FPL("/ancestor/parent/self"));
88 EXPECT_TRUE(InsertPath(&manager
, "app_id", FPL("/file")));
89 ErasePath(&manager
, "app_id", FPL("/file"));
91 ErasePath(&manager
, "app_id", FPL("/ancestor/parent/self/child/descendant"));
94 TEST(TaskDependencyManagerTest
, ExclusiveTask
) {
95 TaskDependencyManager manager
;
97 EXPECT_TRUE(InsertPath(&manager
, "app_id", FPL("/foo/bar")));
98 EXPECT_FALSE(InsertExclusiveTask(&manager
));
99 ErasePath(&manager
, "app_id", FPL("/foo/bar"));
101 EXPECT_TRUE(InsertExclusiveTask(&manager
));
102 EXPECT_FALSE(InsertPath(&manager
, "app_id", FPL("/foo/bar")));
103 EraseExclusiveTask(&manager
);
105 EXPECT_TRUE(InsertPath(&manager
, "app_id", FPL("/foo/bar")));
106 ErasePath(&manager
, "app_id", FPL("/foo/bar"));
109 TEST(TaskDependencyManagerTest
, PermissiveTask
) {
110 TaskDependencyManager manager
;
112 EXPECT_TRUE(manager
.Insert(nullptr));
113 EXPECT_TRUE(InsertPath(&manager
, "app_id", FPL("/foo/bar")));
114 EXPECT_FALSE(InsertExclusiveTask(&manager
));
115 ErasePath(&manager
, "app_id", FPL("/foo/bar"));
117 EXPECT_FALSE(InsertExclusiveTask(&manager
));
118 manager
.Erase(nullptr);
119 EXPECT_TRUE(InsertExclusiveTask(&manager
));
121 EXPECT_FALSE(manager
.Insert(nullptr));
123 EraseExclusiveTask(&manager
);
126 } // namespace drive_backend
127 } // namespace sync_file_system