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_->SetGetContextImplForTesting(base::Bind( |
| 48 &EntryWatcherServiceTest::GetContextImpl, base::Unretained(this))); |
| 49 testing_url_ = file_system_context_->CreateCrackedFileSystemURL( |
| 50 GURL(std::string("chrome-extension://") + kExtensionId), |
| 51 fileapi::kFileSystemTypeTest, |
| 52 base::FilePath::FromUTF8Unsafe("/x/y/z")); |
| 53 } |
| 54 |
| 55 virtual void TearDown() OVERRIDE { |
| 56 dispatch_event_log_targets_.clear(); |
| 57 dispatch_event_log_events_.clear(); |
| 58 } |
| 59 |
| 60 bool DispatchEventImpl(const std::string& extension_id, |
| 61 scoped_ptr<Event> event) { |
| 62 dispatch_event_log_targets_.push_back(extension_id); |
| 63 dispatch_event_log_events_.push_back(event.release()); |
| 64 return true; |
| 65 } |
| 66 |
| 67 fileapi::FileSystemContext* GetContextImpl(const std::string& extension_id, |
| 68 Profile* profile) { |
| 69 EXPECT_EQ(kExtensionId, extension_id); |
| 70 EXPECT_EQ(profile_.get(), profile); |
| 71 return file_system_context_.get(); |
| 72 } |
| 73 |
| 74 content::TestBrowserThreadBundle thread_bundle_; |
| 75 scoped_ptr<TestingProfile> profile_; |
| 76 base::ScopedTempDir data_dir_; |
| 77 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 78 scoped_ptr<EntryWatcherService> service_; |
| 79 fileapi::FileSystemURL testing_url_; |
| 80 std::vector<std::string> dispatch_event_log_targets_; |
| 81 ScopedVector<Event> dispatch_event_log_events_; |
| 82 }; |
| 83 |
| 84 TEST_F(EntryWatcherServiceTest, GetWatchedEntries) { |
| 85 std::vector<base::File::Error> log; |
| 86 |
| 87 const bool recursive = false; |
| 88 service_->WatchDirectory( |
| 89 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); |
| 90 base::RunLoop().RunUntilIdle(); |
| 91 |
| 92 ASSERT_EQ(1u, log.size()); |
| 93 EXPECT_EQ(base::File::FILE_OK, log[0]); |
| 94 |
| 95 { |
| 96 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 97 service_->GetWatchedEntries(kExtensionId); |
| 98 ASSERT_EQ(1u, watched_entries.size()); |
| 99 EXPECT_EQ(testing_url_, watched_entries[0]); |
| 100 } |
| 101 |
| 102 { |
| 103 const std::string wrong_extension_id = "abcabcabcabcabcabcabcabcabcabcab"; |
| 104 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 105 service_->GetWatchedEntries(wrong_extension_id); |
| 106 EXPECT_EQ(0u, watched_entries.size()); |
| 107 } |
| 108 } |
| 109 |
| 110 TEST_F(EntryWatcherServiceTest, WatchDirectory) { |
| 111 std::vector<base::File::Error> log; |
| 112 |
| 113 const bool recursive = false; |
| 114 service_->WatchDirectory( |
| 115 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); |
| 116 base::RunLoop().RunUntilIdle(); |
| 117 |
| 118 ASSERT_EQ(1u, log.size()); |
| 119 EXPECT_EQ(base::File::FILE_OK, log[0]); |
| 120 |
| 121 ASSERT_LE(1u, dispatch_event_log_targets_.size()); |
| 122 ASSERT_LE(1u, dispatch_event_log_events_.size()); |
| 123 |
| 124 EXPECT_EQ(kExtensionId, dispatch_event_log_targets_[0]); |
| 125 EXPECT_EQ(api::file_system::OnEntryChanged::kEventName, |
| 126 dispatch_event_log_events_[0]->event_name); |
| 127 |
| 128 ASSERT_LE(2u, dispatch_event_log_targets_.size()); |
| 129 ASSERT_LE(2u, dispatch_event_log_events_.size()); |
| 130 EXPECT_EQ(kExtensionId, dispatch_event_log_targets_[1]); |
| 131 EXPECT_EQ(api::file_system::OnEntryRemoved::kEventName, |
| 132 dispatch_event_log_events_[1]->event_name); |
| 133 |
| 134 // No unexpected events. |
| 135 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); |
| 136 ASSERT_EQ(2u, dispatch_event_log_events_.size()); |
| 137 |
| 138 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 139 service_->GetWatchedEntries(kExtensionId); |
| 140 ASSERT_EQ(1u, watched_entries.size()); |
| 141 EXPECT_EQ(testing_url_, watched_entries[0]); |
| 142 } |
| 143 |
| 144 TEST_F(EntryWatcherServiceTest, WatchDirectory_AlreadyExists) { |
| 145 std::vector<base::File::Error> log; |
| 146 |
| 147 const bool recursive = false; |
| 148 service_->WatchDirectory( |
| 149 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); |
| 150 base::RunLoop().RunUntilIdle(); |
| 151 |
| 152 ASSERT_EQ(1u, log.size()); |
| 153 EXPECT_EQ(base::File::FILE_OK, log[0]); |
| 154 |
| 155 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); |
| 156 ASSERT_EQ(2u, dispatch_event_log_events_.size()); |
| 157 |
| 158 { |
| 159 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 160 service_->GetWatchedEntries(kExtensionId); |
| 161 EXPECT_EQ(1u, watched_entries.size()); |
| 162 } |
| 163 |
| 164 service_->WatchDirectory( |
| 165 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); |
| 166 base::RunLoop().RunUntilIdle(); |
| 167 |
| 168 ASSERT_EQ(2u, log.size()); |
| 169 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, log[1]); |
| 170 |
| 171 // No new unexpected events. |
| 172 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); |
| 173 ASSERT_EQ(2u, dispatch_event_log_events_.size()); |
| 174 |
| 175 { |
| 176 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 177 service_->GetWatchedEntries(kExtensionId); |
| 178 EXPECT_EQ(1u, watched_entries.size()); |
| 179 } |
| 180 } |
| 181 |
| 182 TEST_F(EntryWatcherServiceTest, WatchDirectory_Recursive) { |
| 183 std::vector<base::File::Error> log; |
| 184 |
| 185 const bool recursive = true; |
| 186 service_->WatchDirectory( |
| 187 kExtensionId, testing_url_, recursive, base::Bind(&LogStatus, &log)); |
| 188 base::RunLoop().RunUntilIdle(); |
| 189 |
| 190 // Recursive watchers are not supported yet. |
| 191 ASSERT_EQ(1u, log.size()); |
| 192 EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, log[0]); |
| 193 |
| 194 // No unexpected events. |
| 195 ASSERT_EQ(0u, dispatch_event_log_targets_.size()); |
| 196 ASSERT_EQ(0u, dispatch_event_log_events_.size()); |
| 197 |
| 198 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 199 service_->GetWatchedEntries(kExtensionId); |
| 200 EXPECT_EQ(0u, watched_entries.size()); |
| 201 } |
| 202 |
| 203 TEST_F(EntryWatcherServiceTest, UnwatchEntry) { |
| 204 std::vector<base::File::Error> watch_log; |
| 205 |
| 206 const bool recursive = false; |
| 207 service_->WatchDirectory(kExtensionId, |
| 208 testing_url_, |
| 209 recursive, |
| 210 base::Bind(&LogStatus, &watch_log)); |
| 211 base::RunLoop().RunUntilIdle(); |
| 212 |
| 213 ASSERT_EQ(1u, watch_log.size()); |
| 214 EXPECT_EQ(base::File::FILE_OK, watch_log[0]); |
| 215 |
| 216 ASSERT_EQ(2u, dispatch_event_log_targets_.size()); |
| 217 ASSERT_EQ(2u, dispatch_event_log_events_.size()); |
| 218 |
| 219 { |
| 220 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 221 service_->GetWatchedEntries(kExtensionId); |
| 222 EXPECT_EQ(1u, watched_entries.size()); |
| 223 } |
| 224 |
| 225 std::vector<base::File::Error> unwatch_log; |
| 226 service_->UnwatchEntry( |
| 227 kExtensionId, testing_url_, base::Bind(&LogStatus, &unwatch_log)); |
| 228 base::RunLoop().RunUntilIdle(); |
| 229 |
| 230 ASSERT_EQ(1u, unwatch_log.size()); |
| 231 EXPECT_EQ(base::File::FILE_OK, unwatch_log[0]); |
| 232 |
| 233 { |
| 234 const std::vector<fileapi::FileSystemURL> watched_entries = |
| 235 service_->GetWatchedEntries(kExtensionId); |
| 236 EXPECT_EQ(0u, watched_entries.size()); |
| 237 } |
| 238 } |
| 239 |
| 240 TEST_F(EntryWatcherServiceTest, UnwatchEntry_NotFound) { |
| 241 std::vector<base::File::Error> log; |
| 242 service_->UnwatchEntry( |
| 243 kExtensionId, testing_url_, base::Bind(&LogStatus, &log)); |
| 244 base::RunLoop().RunUntilIdle(); |
| 245 |
| 246 ASSERT_EQ(1u, log.size()); |
| 247 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, log[0]); |
| 248 } |
| 249 |
| 250 } // namespace extensions |
OLD | NEW |