OLD | NEW |
---|---|
(Empty) | |
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/file_system/entry_watcher_service.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/files/file.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/scoped_temp_dir.h" | |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/run_loop.h" | |
15 #include "chrome/common/extensions/api/file_system.h" | |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "content/public/test/test_browser_thread_bundle.h" | |
18 #include "content/public/test/test_file_system_context.h" | |
19 #include "extensions/browser/event_router.h" | |
20 #include "webkit/browser/fileapi/file_system_url.h" | |
21 #include "webkit/common/fileapi/file_system_types.h" | |
22 | |
23 namespace extensions { | |
24 namespace { | |
25 | |
26 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | |
27 | |
28 void LogStatus(std::vector<base::File::Error>* log, base::File::Error status) { | |
29 log->push_back(status); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 class EntryWatcherServiceTest : public testing::Test { | |
35 protected: | |
36 EntryWatcherServiceTest() {} | |
37 virtual ~EntryWatcherServiceTest() {} | |
38 | |
39 virtual void SetUp() OVERRIDE { | |
40 profile_.reset(new TestingProfile); | |
41 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
42 file_system_context_ = | |
43 content::CreateFileSystemContextForTesting(NULL, data_dir_.path()); | |
44 service_.reset(new EntryWatcherService(profile_.get())); | |
45 service_->SetDispatchEventImplForTesting(base::Bind( | |
46 &EntryWatcherServiceTest::DispatchEventImpl, base::Unretained(this))); | |
47 service_->SetGetFileSystemContextImplForTesting( | |
48 base::Bind(&EntryWatcherServiceTest::GetFileSystemContextImpl, | |
49 base::Unretained(this))); | |
50 testing_url_ = file_system_context_->CreateCrackedFileSystemURL( | |
51 GURL(std::string("chrome-extension://") + kExtensionId), | |
52 fileapi::kFileSystemTypeTest, | |
53 base::FilePath::FromUTF8Unsafe("/x/y/z")); | |
54 } | |
55 | |
56 virtual void TearDown() OVERRIDE { | |
57 dispatch_event_log_targets_.clear(); | |
58 dispatch_event_log_events_.clear(); | |
59 } | |
60 | |
61 bool DispatchEventImpl(const std::string& extension_id, | |
62 scoped_ptr<Event> event) { | |
63 dispatch_event_log_targets_.push_back(extension_id); | |
64 dispatch_event_log_events_.push_back(event.release()); | |
65 return true; | |
66 } | |
67 | |
68 fileapi::FileSystemContext* GetFileSystemContextImpl( | |
69 const std::string& extension_id, | |
70 content::BrowserContext* context) { | |
71 EXPECT_EQ(kExtensionId, extension_id); | |
72 EXPECT_EQ(profile_.get(), context); | |
73 return file_system_context_.get(); | |
74 } | |
75 | |
76 content::TestBrowserThreadBundle thread_bundle_; | |
77 scoped_ptr<TestingProfile> profile_; | |
78 base::ScopedTempDir data_dir_; | |
79 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | |
80 scoped_ptr<EntryWatcherService> service_; | |
81 fileapi::FileSystemURL testing_url_; | |
82 std::vector<std::string> dispatch_event_log_targets_; | |
83 ScopedVector<Event> dispatch_event_log_events_; | |
84 }; | |
85 | |
86 TEST_F(EntryWatcherServiceTest, GetWatchedEntries) { | |
87 std::vector<base::File::Error> log; | |
88 | |
89 const bool recursive = false; | |
90 service_->WatchDirectory( | |
91 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
92 base::RunLoop().RunUntilIdle(); | |
93 | |
94 ASSERT_EQ(1u, log.size()); | |
95 EXPECT_EQ(base::File::FILE_OK, log[0]); | |
96 | |
97 { | |
98 const std::vector<fileapi::FileSystemURL> watched_entries = | |
99 service_->GetWatchedEntries(kExtensionId); | |
100 ASSERT_EQ(1u, watched_entries.size()); | |
101 EXPECT_EQ(testing_url_, watched_entries[0]); | |
102 } | |
103 | |
104 { | |
105 const std::string wrong_extension_id = "abcabcabcabcabcabcabcabcabcabcab"; | |
106 const std::vector<fileapi::FileSystemURL> watched_entries = | |
107 service_->GetWatchedEntries(wrong_extension_id); | |
108 EXPECT_EQ(0u, watched_entries.size()); | |
109 } | |
110 } | |
111 | |
112 TEST_F(EntryWatcherServiceTest, WatchDirectory) { | |
113 std::vector<base::File::Error> log; | |
114 | |
115 const bool recursive = false; | |
116 service_->WatchDirectory( | |
117 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
118 base::RunLoop().RunUntilIdle(); | |
119 | |
120 ASSERT_EQ(1u, log.size()); | |
121 EXPECT_EQ(base::File::FILE_OK, log[0]); | |
122 | |
123 ASSERT_LE(1u, dispatch_event_log_targets_.size()); | |
benwells
2014/08/13 05:50:47
What causes the entrychanged and entryremoved even
mtomasz
2014/08/13 07:02:09
It's part of the testing file system backend imple
benwells
2014/08/14 05:16:32
Right, OK. It seems a bit magical - could you add
mtomasz
2014/08/15 05:35:57
I added a comment. The TestWatcherManager class is
| |
124 ASSERT_LE(1u, dispatch_event_log_events_.size()); | |
125 | |
126 EXPECT_EQ(kExtensionId, dispatch_event_log_targets_[0]); | |
127 EXPECT_EQ(api::file_system::OnEntryChanged::kEventName, | |
128 dispatch_event_log_events_[0]->event_name); | |
129 | |
130 ASSERT_LE(2u, dispatch_event_log_targets_.size()); | |
131 ASSERT_LE(2u, dispatch_event_log_events_.size()); | |
132 EXPECT_EQ(kExtensionId, dispatch_event_log_targets_[1]); | |
133 EXPECT_EQ(api::file_system::OnEntryRemoved::kEventName, | |
134 dispatch_event_log_events_[1]->event_name); | |
135 | |
136 // No unexpected events. | |
137 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
138 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
139 | |
140 const std::vector<fileapi::FileSystemURL> watched_entries = | |
141 service_->GetWatchedEntries(kExtensionId); | |
142 ASSERT_EQ(1u, watched_entries.size()); | |
143 EXPECT_EQ(testing_url_, watched_entries[0]); | |
144 } | |
145 | |
146 TEST_F(EntryWatcherServiceTest, WatchDirectory_AlreadyExists) { | |
147 std::vector<base::File::Error> log; | |
148 | |
149 const bool recursive = false; | |
150 service_->WatchDirectory( | |
151 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
152 base::RunLoop().RunUntilIdle(); | |
153 | |
154 ASSERT_EQ(1u, log.size()); | |
155 EXPECT_EQ(base::File::FILE_OK, log[0]); | |
156 | |
157 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
158 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
159 | |
160 { | |
161 const std::vector<fileapi::FileSystemURL> watched_entries = | |
162 service_->GetWatchedEntries(kExtensionId); | |
163 EXPECT_EQ(1u, watched_entries.size()); | |
164 } | |
165 | |
166 service_->WatchDirectory( | |
167 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
168 base::RunLoop().RunUntilIdle(); | |
169 | |
170 ASSERT_EQ(2u, log.size()); | |
171 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, log[1]); | |
172 | |
173 // No new unexpected events. | |
174 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
175 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
176 | |
177 { | |
178 const std::vector<fileapi::FileSystemURL> watched_entries = | |
179 service_->GetWatchedEntries(kExtensionId); | |
180 EXPECT_EQ(1u, watched_entries.size()); | |
181 } | |
182 } | |
183 | |
184 TEST_F(EntryWatcherServiceTest, WatchDirectory_Recursive) { | |
185 std::vector<base::File::Error> log; | |
186 | |
187 const bool recursive = true; | |
188 service_->WatchDirectory( | |
189 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
190 base::RunLoop().RunUntilIdle(); | |
191 | |
192 // Recursive watchers are not supported yet. | |
193 ASSERT_EQ(1u, log.size()); | |
194 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, log[0]); | |
195 | |
196 // No unexpected events. | |
197 ASSERT_EQ(0u, dispatch_event_log_targets_.size()); | |
198 ASSERT_EQ(0u, dispatch_event_log_events_.size()); | |
199 | |
200 const std::vector<fileapi::FileSystemURL> watched_entries = | |
201 service_->GetWatchedEntries(kExtensionId); | |
202 EXPECT_EQ(0u, watched_entries.size()); | |
203 } | |
204 | |
205 TEST_F(EntryWatcherServiceTest, UnwatchEntry) { | |
206 std::vector<base::File::Error> watch_log; | |
207 | |
208 const bool recursive = false; | |
209 service_->WatchDirectory(kExtensionId, | |
210 testing_url_, | |
211 recursive, | |
212 base::Bind(&LogStatus, &watch_log)); | |
213 base::RunLoop().RunUntilIdle(); | |
214 | |
215 ASSERT_EQ(1u, watch_log.size()); | |
216 EXPECT_EQ(base::File::FILE_OK, watch_log[0]); | |
217 | |
218 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
219 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
220 | |
221 { | |
222 const std::vector<fileapi::FileSystemURL> watched_entries = | |
223 service_->GetWatchedEntries(kExtensionId); | |
224 EXPECT_EQ(1u, watched_entries.size()); | |
225 } | |
226 | |
227 std::vector<base::File::Error> unwatch_log; | |
228 service_->UnwatchEntry( | |
229 kExtensionId, testing_url_, base::Bind(&LogStatus, &unwatch_log)); | |
230 base::RunLoop().RunUntilIdle(); | |
231 | |
232 ASSERT_EQ(1u, unwatch_log.size()); | |
233 EXPECT_EQ(base::File::FILE_OK, unwatch_log[0]); | |
234 | |
235 { | |
236 const std::vector<fileapi::FileSystemURL> watched_entries = | |
237 service_->GetWatchedEntries(kExtensionId); | |
238 EXPECT_EQ(0u, watched_entries.size()); | |
239 } | |
240 } | |
241 | |
242 TEST_F(EntryWatcherServiceTest, UnwatchEntry_NotFound) { | |
243 std::vector<base::File::Error> log; | |
244 service_->UnwatchEntry( | |
245 kExtensionId, testing_url_, base::Bind(&LogStatus, &log)); | |
246 base::RunLoop().RunUntilIdle(); | |
247 | |
248 ASSERT_EQ(1u, log.size()); | |
249 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, log[0]); | |
250 } | |
251 | |
252 } // namespace extensions | |
OLD | NEW |