Chromium Code Reviews| 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/extensions/api/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 void 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 } | |
| 66 | |
| 67 fileapi::FileSystemContext* GetFileSystemContextImpl( | |
| 68 const std::string& extension_id, | |
| 69 content::BrowserContext* context) { | |
| 70 EXPECT_EQ(kExtensionId, extension_id); | |
| 71 EXPECT_EQ(profile_.get(), context); | |
| 72 return file_system_context_.get(); | |
| 73 } | |
| 74 | |
| 75 content::TestBrowserThreadBundle thread_bundle_; | |
| 76 scoped_ptr<TestingProfile> profile_; | |
| 77 base::ScopedTempDir data_dir_; | |
| 78 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | |
| 79 scoped_ptr<EntryWatcherService> service_; | |
| 80 fileapi::FileSystemURL testing_url_; | |
| 81 std::vector<std::string> dispatch_event_log_targets_; | |
| 82 ScopedVector<Event> dispatch_event_log_events_; | |
| 83 }; | |
| 84 | |
| 85 TEST_F(EntryWatcherServiceTest, GetWatchedEntries) { | |
| 86 std::vector<base::File::Error> log; | |
| 87 | |
| 88 const bool recursive = false; | |
| 89 service_->WatchDirectory( | |
| 90 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
| 91 base::RunLoop().RunUntilIdle(); | |
| 92 | |
| 93 ASSERT_EQ(1u, log.size()); | |
| 94 EXPECT_EQ(base::File::FILE_OK, log[0]); | |
| 95 | |
| 96 { | |
| 97 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 98 service_->GetWatchedEntries(kExtensionId); | |
| 99 ASSERT_EQ(1u, watched_entries.size()); | |
| 100 EXPECT_EQ(testing_url_, watched_entries[0]); | |
| 101 } | |
| 102 | |
| 103 { | |
| 104 const std::string wrong_extension_id = "abcabcabcabcabcabcabcabcabcabcab"; | |
| 105 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 106 service_->GetWatchedEntries(wrong_extension_id); | |
| 107 EXPECT_EQ(0u, watched_entries.size()); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 TEST_F(EntryWatcherServiceTest, WatchDirectory) { | |
| 112 std::vector<base::File::Error> log; | |
| 113 | |
| 114 const bool recursive = false; | |
| 115 service_->WatchDirectory( | |
| 116 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
| 117 base::RunLoop().RunUntilIdle(); | |
| 118 | |
| 119 ASSERT_EQ(1u, log.size()); | |
| 120 EXPECT_EQ(base::File::FILE_OK, log[0]); | |
| 121 | |
| 122 // The testing WatcherManager implementation emits a fake notification as soon | |
|
benwells
2014/08/18 22:58:32
I think it it emits two. A comment line "The testi
mtomasz
2014/08/19 06:50:36
Done.
| |
| 123 // as the watcher is set properly. | |
| 124 ASSERT_LE(1u, dispatch_event_log_targets_.size()); | |
| 125 ASSERT_LE(1u, dispatch_event_log_events_.size()); | |
| 126 | |
| 127 EXPECT_EQ(kExtensionId, dispatch_event_log_targets_[0]); | |
| 128 EXPECT_EQ(api::file_system::OnEntryChanged::kEventName, | |
| 129 dispatch_event_log_events_[0]->event_name); | |
| 130 | |
| 131 ASSERT_LE(2u, dispatch_event_log_targets_.size()); | |
| 132 ASSERT_LE(2u, dispatch_event_log_events_.size()); | |
| 133 EXPECT_EQ(kExtensionId, dispatch_event_log_targets_[1]); | |
| 134 EXPECT_EQ(api::file_system::OnEntryRemoved::kEventName, | |
| 135 dispatch_event_log_events_[1]->event_name); | |
| 136 | |
| 137 // No unexpected events. | |
| 138 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
| 139 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
| 140 | |
| 141 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 142 service_->GetWatchedEntries(kExtensionId); | |
| 143 ASSERT_EQ(1u, watched_entries.size()); | |
| 144 EXPECT_EQ(testing_url_, watched_entries[0]); | |
| 145 } | |
| 146 | |
| 147 TEST_F(EntryWatcherServiceTest, WatchDirectory_AlreadyExists) { | |
| 148 std::vector<base::File::Error> log; | |
| 149 | |
| 150 const bool recursive = false; | |
| 151 service_->WatchDirectory( | |
| 152 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
| 153 base::RunLoop().RunUntilIdle(); | |
| 154 | |
| 155 ASSERT_EQ(1u, log.size()); | |
| 156 EXPECT_EQ(base::File::FILE_OK, log[0]); | |
| 157 | |
| 158 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
| 159 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
| 160 | |
| 161 { | |
| 162 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 163 service_->GetWatchedEntries(kExtensionId); | |
| 164 EXPECT_EQ(1u, watched_entries.size()); | |
| 165 } | |
| 166 | |
| 167 service_->WatchDirectory( | |
| 168 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
| 169 base::RunLoop().RunUntilIdle(); | |
| 170 | |
| 171 ASSERT_EQ(2u, log.size()); | |
| 172 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, log[1]); | |
| 173 | |
| 174 // No new unexpected events. | |
| 175 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
| 176 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
| 177 | |
| 178 { | |
| 179 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 180 service_->GetWatchedEntries(kExtensionId); | |
| 181 EXPECT_EQ(1u, watched_entries.size()); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 TEST_F(EntryWatcherServiceTest, WatchDirectory_Recursive) { | |
| 186 std::vector<base::File::Error> log; | |
| 187 | |
| 188 const bool recursive = true; | |
| 189 service_->WatchDirectory( | |
| 190 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); | |
| 191 base::RunLoop().RunUntilIdle(); | |
| 192 | |
| 193 // Recursive watchers are not supported yet. | |
| 194 ASSERT_EQ(1u, log.size()); | |
| 195 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, log[0]); | |
| 196 | |
| 197 // No unexpected events. | |
| 198 ASSERT_EQ(0u, dispatch_event_log_targets_.size()); | |
| 199 ASSERT_EQ(0u, dispatch_event_log_events_.size()); | |
| 200 | |
| 201 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 202 service_->GetWatchedEntries(kExtensionId); | |
| 203 EXPECT_EQ(0u, watched_entries.size()); | |
| 204 } | |
| 205 | |
| 206 TEST_F(EntryWatcherServiceTest, UnwatchEntry) { | |
| 207 std::vector<base::File::Error> watch_log; | |
| 208 | |
| 209 const bool recursive = false; | |
| 210 service_->WatchDirectory(kExtensionId, | |
| 211 testing_url_, | |
| 212 recursive, | |
| 213 base::Bind(&LogStatus, &watch_log)); | |
| 214 base::RunLoop().RunUntilIdle(); | |
| 215 | |
| 216 ASSERT_EQ(1u, watch_log.size()); | |
| 217 EXPECT_EQ(base::File::FILE_OK, watch_log[0]); | |
| 218 | |
| 219 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); | |
| 220 ASSERT_EQ(2u, dispatch_event_log_events_.size()); | |
| 221 | |
| 222 { | |
| 223 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 224 service_->GetWatchedEntries(kExtensionId); | |
| 225 EXPECT_EQ(1u, watched_entries.size()); | |
| 226 } | |
| 227 | |
| 228 std::vector<base::File::Error> unwatch_log; | |
| 229 service_->UnwatchEntry( | |
| 230 kExtensionId, testing_url_, base::Bind(&LogStatus, &unwatch_log)); | |
| 231 base::RunLoop().RunUntilIdle(); | |
| 232 | |
| 233 ASSERT_EQ(1u, unwatch_log.size()); | |
| 234 EXPECT_EQ(base::File::FILE_OK, unwatch_log[0]); | |
| 235 | |
| 236 { | |
| 237 const std::vector<fileapi::FileSystemURL> watched_entries = | |
| 238 service_->GetWatchedEntries(kExtensionId); | |
| 239 EXPECT_EQ(0u, watched_entries.size()); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 TEST_F(EntryWatcherServiceTest, UnwatchEntry_NotFound) { | |
| 244 std::vector<base::File::Error> log; | |
| 245 service_->UnwatchEntry( | |
| 246 kExtensionId, testing_url_, base::Bind(&LogStatus, &log)); | |
| 247 base::RunLoop().RunUntilIdle(); | |
| 248 | |
| 249 ASSERT_EQ(1u, log.size()); | |
| 250 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, log[0]); | |
| 251 } | |
| 252 | |
| 253 } // namespace extensions | |
| OLD | NEW |