OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "webkit/fileapi/syncable/syncable_context.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "webkit/fileapi/file_system_context.h" |
| 14 #include "webkit/fileapi/isolated_context.h" |
| 15 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" |
| 16 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
| 17 #include "webkit/fileapi/syncable/sync_status_code.h" |
| 18 #include "webkit/fileapi/syncable/syncable_context.h" |
| 19 |
| 20 namespace fileapi { |
| 21 |
| 22 namespace { |
| 23 const char kOrigin1[] = "http://example.com"; |
| 24 const char kOrigin2[] = "http://chromium.org"; |
| 25 const char kServiceName[] = "test"; |
| 26 const FileSystemType kSyncableType = kFileSystemTypeSyncable; |
| 27 } |
| 28 |
| 29 class SyncableContextTest : public testing::Test { |
| 30 protected: |
| 31 SyncableContextTest() |
| 32 : status_(SYNC_FILE_ERROR_FAILED) {} |
| 33 // : external_file_system_(kServiceName, kSyncableType, FilePath()) {} |
| 34 |
| 35 virtual void SetUp() OVERRIDE { |
| 36 io_thread_.reset(new base::Thread("Thread_IO")); |
| 37 file_thread_.reset(new base::Thread("Thread_File")); |
| 38 io_thread_->Start(); |
| 39 file_thread_->Start(); |
| 40 |
| 41 ui_task_runner_ = MessageLoop::current()->message_loop_proxy(); |
| 42 io_task_runner_ = io_thread_->message_loop_proxy(); |
| 43 file_task_runner_ = file_thread_->message_loop_proxy(); |
| 44 } |
| 45 |
| 46 virtual void TearDown() OVERRIDE { |
| 47 io_thread_->Stop(); |
| 48 file_thread_->Stop(); |
| 49 } |
| 50 |
| 51 SyncStatusCode MaybeInitializeFileSystemContext( |
| 52 const GURL& origin, |
| 53 FileSystemContext* file_system_context, |
| 54 SyncableContext* syncable_context) { |
| 55 DCHECK(syncable_context); |
| 56 status_ = SYNC_FILE_ERROR_FAILED; |
| 57 syncable_context->MaybeInitializeFileSystemContext( |
| 58 origin, |
| 59 file_system_context, |
| 60 base::Bind(&SyncableContextTest::DidInitializeFileSystemContext, |
| 61 base::Unretained(this))); |
| 62 MessageLoop::current()->Run(); |
| 63 return status_; |
| 64 } |
| 65 |
| 66 void DidInitializeFileSystemContext(SyncStatusCode status) { |
| 67 status_ = status; |
| 68 MessageLoop::current()->Quit(); |
| 69 } |
| 70 |
| 71 // These need to remain until the very end. |
| 72 scoped_ptr<base::Thread> io_thread_; |
| 73 scoped_ptr<base::Thread> file_thread_; |
| 74 MessageLoop loop_; |
| 75 |
| 76 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 77 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 78 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 79 |
| 80 // Make sure we revoke the file system at the end of each test. |
| 81 // ScopedExternalFileSystem external_file_system_; |
| 82 |
| 83 scoped_refptr<SyncableContext> syncable_context_; |
| 84 |
| 85 SyncStatusCode status_; |
| 86 }; |
| 87 |
| 88 TEST_F(SyncableContextTest, ConstructAndDestruct) { |
| 89 syncable_context_ = new SyncableContext(ui_task_runner_, io_task_runner_); |
| 90 syncable_context_->ShutdownOnUIThread(); |
| 91 } |
| 92 |
| 93 TEST_F(SyncableContextTest, InitializeFileSystemContext) { |
| 94 CannedSyncableFileSystem file_system(GURL(kOrigin1), kServiceName); |
| 95 file_system.SetUp(); |
| 96 |
| 97 syncable_context_ = new SyncableContext(ui_task_runner_, io_task_runner_); |
| 98 |
| 99 // Initializes file_system using |syncable_context_|. |
| 100 EXPECT_EQ(SYNC_STATUS_OK, |
| 101 MaybeInitializeFileSystemContext(GURL(kOrigin1), |
| 102 file_system.file_system_context(), |
| 103 syncable_context_)); |
| 104 |
| 105 // Make sure everything's set up for file_system to be able to handle |
| 106 // syncable file system operations. |
| 107 EXPECT_TRUE(file_system.file_system_context()->syncable_context() != NULL); |
| 108 EXPECT_TRUE(file_system.file_system_context()->change_tracker() != NULL); |
| 109 EXPECT_EQ(syncable_context_.get(), |
| 110 file_system.file_system_context()->syncable_context()); |
| 111 |
| 112 // Calling MaybeInitialize for the same context multiple times must be ok. |
| 113 EXPECT_EQ(SYNC_STATUS_OK, |
| 114 MaybeInitializeFileSystemContext(GURL(kOrigin1), |
| 115 file_system.file_system_context(), |
| 116 syncable_context_)); |
| 117 EXPECT_EQ(syncable_context_.get(), |
| 118 file_system.file_system_context()->syncable_context()); |
| 119 |
| 120 // Opens the file_system, perform some operation and see if the change tracker |
| 121 // correctly captures the change. |
| 122 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system.OpenFileSystem()); |
| 123 |
| 124 const FileSystemURL kURL(file_system.URL("foo")); |
| 125 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system.CreateFile(kURL)); |
| 126 |
| 127 std::vector<FileSystemURL> urls; |
| 128 file_system.file_system_context()->change_tracker()->GetChangedURLs(&urls); |
| 129 ASSERT_EQ(1U, urls.size()); |
| 130 EXPECT_EQ(kURL, urls[0]); |
| 131 |
| 132 // Finishing the test. |
| 133 syncable_context_->ShutdownOnUIThread(); |
| 134 file_system.TearDown(); |
| 135 } |
| 136 |
| 137 TEST_F(SyncableContextTest, MultipleFileSystemContexts) { |
| 138 CannedSyncableFileSystem file_system1(GURL(kOrigin1), kServiceName); |
| 139 CannedSyncableFileSystem file_system2(GURL(kOrigin2), kServiceName); |
| 140 file_system1.SetUp(); |
| 141 file_system2.SetUp(); |
| 142 |
| 143 syncable_context_ = new SyncableContext(ui_task_runner_, io_task_runner_); |
| 144 |
| 145 // Initializes file_system1 and file_system2. |
| 146 EXPECT_EQ(SYNC_STATUS_OK, |
| 147 MaybeInitializeFileSystemContext(GURL(kOrigin1), |
| 148 file_system1.file_system_context(), |
| 149 syncable_context_)); |
| 150 EXPECT_EQ(SYNC_STATUS_OK, |
| 151 MaybeInitializeFileSystemContext(GURL(kOrigin2), |
| 152 file_system2.file_system_context(), |
| 153 syncable_context_)); |
| 154 |
| 155 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system1.OpenFileSystem()); |
| 156 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system2.OpenFileSystem()); |
| 157 |
| 158 const FileSystemURL kURL1(file_system1.URL("foo")); |
| 159 const FileSystemURL kURL2(file_system2.URL("bar")); |
| 160 |
| 161 // Creates a file in file_system1. |
| 162 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system1.CreateFile(kURL1)); |
| 163 |
| 164 // file_system1's tracker must have recorded the change. |
| 165 std::vector<FileSystemURL> urls; |
| 166 file_system1.file_system_context()->change_tracker()->GetChangedURLs(&urls); |
| 167 ASSERT_EQ(1U, urls.size()); |
| 168 EXPECT_EQ(kURL1, urls[0]); |
| 169 |
| 170 // file_system1's tracker must have no change. |
| 171 urls.clear(); |
| 172 file_system2.file_system_context()->change_tracker()->GetChangedURLs(&urls); |
| 173 ASSERT_TRUE(urls.empty()); |
| 174 |
| 175 // Creates a directory in file_system2. |
| 176 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system2.CreateDirectory(kURL2)); |
| 177 |
| 178 // file_system1's tracker must have the change for kURL1 as before. |
| 179 urls.clear(); |
| 180 file_system1.file_system_context()->change_tracker()->GetChangedURLs(&urls); |
| 181 ASSERT_EQ(1U, urls.size()); |
| 182 EXPECT_EQ(kURL1, urls[0]); |
| 183 |
| 184 // file_system2's tracker now must have the change for kURL2. |
| 185 urls.clear(); |
| 186 file_system2.file_system_context()->change_tracker()->GetChangedURLs(&urls); |
| 187 ASSERT_EQ(1U, urls.size()); |
| 188 EXPECT_EQ(kURL2, urls[0]); |
| 189 |
| 190 syncable_context_->ShutdownOnUIThread(); |
| 191 |
| 192 file_system1.TearDown(); |
| 193 file_system2.TearDown(); |
| 194 } |
| 195 |
| 196 } // namespace fileapi |
OLD | NEW |