OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "base/basictypes.h" |
| 6 #include "base/bind.h" |
| 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_p
rovider.h" |
| 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webkit/fileapi/async_file_test_helper.h" |
| 15 #include "webkit/fileapi/copy_or_move_file_validator.h" |
| 16 #include "webkit/fileapi/external_mount_points.h" |
| 17 #include "webkit/fileapi/file_system_context.h" |
| 18 #include "webkit/fileapi/file_system_mount_point_provider.h" |
| 19 #include "webkit/fileapi/file_system_types.h" |
| 20 #include "webkit/fileapi/file_system_url.h" |
| 21 #include "webkit/fileapi/file_system_util.h" |
| 22 #include "webkit/fileapi/isolated_context.h" |
| 23 #include "webkit/fileapi/mock_file_system_context.h" |
| 24 #include "webkit/fileapi/test_mount_point_provider.h" |
| 25 #include "webkit/quota/mock_special_storage_policy.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kOrigin[] = "http://foo"; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 namespace chrome { |
| 34 |
| 35 class MediaFileValidatorTestHelper { |
| 36 public: |
| 37 MediaFileValidatorTestHelper() |
| 38 : ui_thread_(content::BrowserThread::UI, MessageLoop::current()), |
| 39 file_thread_(content::BrowserThread::FILE, MessageLoop::current()), |
| 40 io_thread_(content::BrowserThread::IO, MessageLoop::current()), |
| 41 test_file_size_(0) { |
| 42 } |
| 43 |
| 44 ~MediaFileValidatorTestHelper() { |
| 45 file_system_context_ = NULL; |
| 46 base::MessageLoop::current()->RunUntilIdle(); |
| 47 } |
| 48 |
| 49 void SetUp(const std::string& filename, const std::string& content) { |
| 50 ASSERT_TRUE(base_.CreateUniqueTempDir()); |
| 51 base::FilePath base_dir = base_.path(); |
| 52 base::FilePath src_path = base_dir.Append(FILE_PATH_LITERAL("src_fs")); |
| 53 |
| 54 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; |
| 55 additional_providers.push_back(new fileapi::TestMountPointProvider( |
| 56 base::MessageLoopProxy::current(), src_path)); |
| 57 additional_providers.push_back( |
| 58 new MediaFileSystemMountPointProvider(base_dir)); |
| 59 file_system_context_ = |
| 60 fileapi::CreateFileSystemContextWithAdditionalProvidersForTesting( |
| 61 NULL, additional_providers.Pass(), base_dir); |
| 62 |
| 63 fileapi::FileSystemMountPointProvider* mount_point_provider = |
| 64 file_system_context_->GetMountPointProvider( |
| 65 fileapi::kFileSystemTypeTest); |
| 66 mount_point_provider->GetFileSystemRootPathOnFileThread( |
| 67 SourceURL(std::string()), true /* create */); |
| 68 |
| 69 base::FilePath dest_path = base_dir.Append(FILE_PATH_LITERAL("dest_fs")); |
| 70 file_util::CreateDirectory(dest_path); |
| 71 std::string dest_fsid = |
| 72 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 73 fileapi::kFileSystemTypeNativeMedia, dest_path, NULL); |
| 74 |
| 75 test_file_size_ = content.size(); |
| 76 base::FilePath src_file = src_path.AppendASCII(filename.c_str()); |
| 77 ASSERT_EQ(test_file_size_, |
| 78 file_util::WriteFile(src_file, content.data(), test_file_size_)); |
| 79 |
| 80 move_src_ = SourceURL(filename.c_str()); |
| 81 ASSERT_EQ('.', filename[filename.size() - 4]); |
| 82 std::string extension = filename.substr(filename.size() - 4); |
| 83 std::string dest_root_fs_url = fileapi::GetIsolatedFileSystemRootURIString( |
| 84 GURL(kOrigin), dest_fsid, "dest_fs/"); |
| 85 move_dest_ = file_system_context_->CrackURL(GURL( |
| 86 dest_root_fs_url + "move_dest" + extension)); |
| 87 } |
| 88 |
| 89 void MoveTest(base::PlatformFileError expected) { |
| 90 ASSERT_TRUE(FileExists(move_src_, test_file_size_)); |
| 91 ASSERT_FALSE(FileExists(move_dest_, test_file_size_)); |
| 92 |
| 93 printf("Doing move\n"); |
| 94 EXPECT_EQ(expected, |
| 95 fileapi::AsyncFileTestHelper::Move(file_system_context_, |
| 96 move_src_, move_dest_)); |
| 97 printf("Move done\n"); |
| 98 |
| 99 if (expected == base::PLATFORM_FILE_OK) { |
| 100 EXPECT_FALSE(FileExists(move_src_, test_file_size_)); |
| 101 EXPECT_TRUE(FileExists(move_dest_, test_file_size_)); |
| 102 } else { |
| 103 EXPECT_TRUE(FileExists(move_src_, test_file_size_)); |
| 104 EXPECT_FALSE(FileExists(move_dest_, test_file_size_)); |
| 105 } |
| 106 }; |
| 107 |
| 108 private: |
| 109 fileapi::FileSystemURL SourceURL(const std::string& path) { |
| 110 return file_system_context_->CreateCrackedFileSystemURL( |
| 111 GURL(kOrigin), fileapi::kFileSystemTypeTest, |
| 112 base::FilePath::FromUTF8Unsafe(path)); |
| 113 } |
| 114 |
| 115 bool FileExists(const fileapi::FileSystemURL& url, int64 expected_size) { |
| 116 return fileapi::AsyncFileTestHelper::FileExists( |
| 117 file_system_context_, url, expected_size); |
| 118 } |
| 119 |
| 120 base::ScopedTempDir base_; |
| 121 |
| 122 base::MessageLoop message_loop_; |
| 123 content::TestBrowserThread ui_thread_; |
| 124 content::TestBrowserThread file_thread_; |
| 125 content::TestBrowserThread io_thread_; |
| 126 |
| 127 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 128 |
| 129 int test_file_size_; |
| 130 |
| 131 fileapi::FileSystemURL move_src_; |
| 132 fileapi::FileSystemURL move_dest_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTestHelper); |
| 135 }; |
| 136 |
| 137 TEST(MediaFileValidatorTest, ValidGif) { |
| 138 // Within a file system type, validation is not expected, so it should |
| 139 // work for kFileSystemTypeNativeMedia without a validator set. |
| 140 MediaFileValidatorTestHelper helper; |
| 141 helper.SetUp("a.gif", ""); |
| 142 helper.MoveTest(base::PLATFORM_FILE_OK); |
| 143 } |
| 144 |
| 145 } // namespace chrome |
OLD | NEW |