task_dependency_manager_unittest.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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 "chrome/browser/sync_file_system/drive_backend/task_dependency_manager.h"
6
7#include "base/files/file_path.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10#define FPL(path) FILE_PATH_LITERAL(path)
11
12namespace sync_file_system {
13namespace drive_backend {
14
15namespace {
16
17base::FilePath MakePath(const base::FilePath::StringType& path) {
18  return base::FilePath(path).NormalizePathSeparators();
19}
20
21bool InsertPath(TaskDependencyManager* manager,
22                const std::string& app_id,
23                const base::FilePath::StringType& path) {
24  BlockingFactor blocker;
25  blocker.app_id = app_id;
26  blocker.paths.push_back(MakePath(path));
27  return manager->Insert(&blocker);
28}
29
30void ErasePath(TaskDependencyManager* manager,
31               const std::string& app_id,
32               const base::FilePath::StringType& path) {
33  BlockingFactor blocker;
34  blocker.app_id = app_id;
35  blocker.paths.push_back(MakePath(path));
36  return manager->Erase(&blocker);
37}
38
39bool InsertExclusiveTask(TaskDependencyManager* manager) {
40  BlockingFactor blocker;
41  blocker.exclusive = true;
42  return manager->Insert(&blocker);
43}
44
45void EraseExclusiveTask(TaskDependencyManager* manager) {
46  BlockingFactor blocker;
47  blocker.exclusive = true;
48  manager->Erase(&blocker);
49}
50
51}  // namespace
52
53TEST(TaskDependencyManagerTest, BasicTest) {
54  TaskDependencyManager manager;
55  BlockingFactor blocker;
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);
60
61  EXPECT_TRUE(manager.Insert(&blocker));
62  EXPECT_FALSE(manager.Insert(&blocker));
63
64  manager.Erase(&blocker);
65
66  EXPECT_TRUE(manager.Insert(&blocker));
67
68  manager.Erase(&blocker);
69}
70
71TEST(TaskDependencyManagerTest, BlocksAncestorAndDescendant) {
72  TaskDependencyManager manager;
73
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")));
83
84  EXPECT_TRUE(InsertPath(
85      &manager, "another_app_id", FPL("/ancestor/parent/self")));
86  ErasePath(&manager, "another_app_id", FPL("/ancestor/parent/self"));
87
88  EXPECT_TRUE(InsertPath(&manager, "app_id", FPL("/file")));
89  ErasePath(&manager, "app_id", FPL("/file"));
90
91  ErasePath(&manager, "app_id", FPL("/ancestor/parent/self/child/descendant"));
92}
93
94TEST(TaskDependencyManagerTest, ExclusiveTask) {
95  TaskDependencyManager manager;
96
97  EXPECT_TRUE(InsertPath(&manager, "app_id", FPL("/foo/bar")));
98  EXPECT_FALSE(InsertExclusiveTask(&manager));
99  ErasePath(&manager, "app_id", FPL("/foo/bar"));
100
101  EXPECT_TRUE(InsertExclusiveTask(&manager));
102  EXPECT_FALSE(InsertPath(&manager, "app_id", FPL("/foo/bar")));
103  EraseExclusiveTask(&manager);
104
105  EXPECT_TRUE(InsertPath(&manager, "app_id", FPL("/foo/bar")));
106  ErasePath(&manager, "app_id", FPL("/foo/bar"));
107}
108
109TEST(TaskDependencyManagerTest, PermissiveTask) {
110  TaskDependencyManager manager;
111
112  EXPECT_TRUE(manager.Insert(NULL));
113  EXPECT_TRUE(InsertPath(&manager, "app_id", FPL("/foo/bar")));
114  EXPECT_FALSE(InsertExclusiveTask(&manager));
115  ErasePath(&manager, "app_id", FPL("/foo/bar"));
116
117  EXPECT_FALSE(InsertExclusiveTask(&manager));
118  manager.Erase(NULL);
119  EXPECT_TRUE(InsertExclusiveTask(&manager));
120
121  EXPECT_FALSE(manager.Insert(NULL));
122
123  EraseExclusiveTask(&manager);
124}
125
126}  // namespace drive_backend
127}  // namespace sync_file_system
128