Chromium Code Reviews| 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 "chrome/test/base/in_process_browser_test.h" | |
| 13 #include "content/public/test/browser_test.h" | |
| 14 #include "content/public/test/test_utils.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | |
| 17 #include "webkit/browser/fileapi/file_system_context.h" | |
| 18 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" | |
| 19 #include "webkit/browser/fileapi/file_system_operation.h" | |
| 20 #include "webkit/browser/fileapi/file_system_url.h" | |
| 21 #include "webkit/browser/fileapi/isolated_context.h" | |
| 22 #include "webkit/browser/fileapi/mock_file_system_context.h" | |
| 23 #include "webkit/browser/fileapi/test_mount_point_provider.h" | |
| 24 #include "webkit/common/fileapi/file_system_types.h" | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 const char kOrigin[] = "http://foo"; | |
| 29 | |
| 30 const char kValidImage[] = "RIFF0\0\0\0WEBPVP8 $\0\0\0\xB2\x02\0\x9D\x01\x2A" | |
| 31 "\x01\0\x01\0\x2F\x9D\xCE\xE7s\xA8((((\x01\x9CK(\0" | |
| 32 "\x05\xCE\xB3l\0\0\xFE\xD8\x80\0\0"; | |
| 33 | |
| 34 const char kInvalidImage[] = "Not an image"; | |
| 35 | |
| 36 const int64 kNoFileSize = -1; | |
| 37 | |
| 38 void HandleCheckFileResult(int64 expected_size, | |
| 39 const base::Callback<void(bool success)>& callback, | |
| 40 base::PlatformFileError result, | |
| 41 const base::PlatformFileInfo& file_info, | |
| 42 const base::FilePath& /*platform_path*/) { | |
| 43 if (result == base::PLATFORM_FILE_OK) { | |
| 44 if (!file_info.is_directory && expected_size != kNoFileSize && | |
| 45 file_info.size == expected_size) { | |
| 46 callback.Run(true); | |
| 47 return; | |
| 48 } | |
| 49 } else { | |
| 50 if (expected_size == kNoFileSize) { | |
| 51 callback.Run(true); | |
| 52 return; | |
| 53 } | |
| 54 } | |
| 55 callback.Run(false); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 namespace chrome { | |
| 61 | |
| 62 class MediaFileValidatorTest : public InProcessBrowserTest { | |
| 63 public: | |
| 64 MediaFileValidatorTest() : test_file_size_(0) {} | |
| 65 | |
| 66 virtual ~MediaFileValidatorTest() {} | |
| 67 | |
| 68 // Write |content| into |filename| in a test file system and try to move | |
| 69 // it into a media file system. The result is compared to |exepcted_result|. | |
|
Lei Zhang
2013/05/29 09:40:58
typo
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 70 void MoveTest(const std::string& filename, const std::string& content, | |
| 71 bool expected_result) { | |
| 72 content::BrowserThread::PostTask( | |
| 73 content::BrowserThread::FILE, | |
| 74 FROM_HERE, | |
| 75 base::Bind(&MediaFileValidatorTest::SetupOnFileThread, | |
| 76 base::Unretained(this), filename, content, expected_result)); | |
| 77 content::RunMessageLoop(); | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 // Create the test files, filesystem objects, etc. | |
| 82 void SetupOnFileThread(const std::string& filename, | |
| 83 const std::string& content, | |
| 84 bool expected_result) { | |
| 85 ASSERT_TRUE(base_.CreateUniqueTempDir()); | |
| 86 base::FilePath base_dir = base_.path(); | |
| 87 base::FilePath src_path = base_dir.Append(FILE_PATH_LITERAL("src_fs")); | |
|
Lei Zhang
2013/05/29 09:40:58
Just AppendASCII() instead? Ditto below.
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 88 file_util::CreateDirectory(src_path); | |
|
Lei Zhang
2013/05/29 09:40:58
ASSERT_TRUE? Ditto below.
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 89 | |
| 90 ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; | |
| 91 additional_providers.push_back(new fileapi::TestMountPointProvider( | |
| 92 base::MessageLoopProxy::current(), src_path)); | |
| 93 additional_providers.push_back( | |
| 94 new MediaFileSystemMountPointProvider(base_dir)); | |
| 95 file_system_context_ = | |
| 96 fileapi::CreateFileSystemContextWithAdditionalProvidersForTesting( | |
| 97 NULL, additional_providers.Pass(), base_dir); | |
| 98 | |
| 99 move_src_ = file_system_context_->CreateCrackedFileSystemURL( | |
| 100 GURL(kOrigin), fileapi::kFileSystemTypeTest, | |
| 101 base::FilePath::FromUTF8Unsafe(filename.c_str())); | |
|
Lei Zhang
2013/05/29 09:40:58
omit the .c_str()
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 102 | |
| 103 test_file_size_ = content.size(); | |
| 104 base::FilePath test_file = src_path.AppendASCII(filename.c_str()); | |
|
Lei Zhang
2013/05/29 09:40:58
Ditto? I think you can omit it.
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 105 ASSERT_EQ(test_file_size_, | |
| 106 file_util::WriteFile(test_file, content.data(), test_file_size_)); | |
| 107 | |
| 108 base::FilePath dest_path = base_dir.Append(FILE_PATH_LITERAL("dest_fs")); | |
| 109 file_util::CreateDirectory(dest_path); | |
| 110 std::string dest_fsid = | |
| 111 fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( | |
| 112 fileapi::kFileSystemTypeNativeMedia, dest_path, NULL); | |
| 113 | |
| 114 size_t extension_index = filename.find_last_of("."); | |
|
Lei Zhang
2013/05/29 09:40:58
Maybe |filename| should just be a FilePath so you
vandebo (ex-Chrome)
2013/05/29 18:56:05
GURL takes std::string/16, not FilePath::StringTyp
| |
| 115 ASSERT_NE(std::string::npos, extension_index); | |
| 116 std::string extension = filename.substr(extension_index); | |
| 117 std::string dest_root_fs_url = fileapi::GetIsolatedFileSystemRootURIString( | |
| 118 GURL(kOrigin), dest_fsid, "dest_fs/"); | |
| 119 move_dest_ = file_system_context_->CrackURL(GURL( | |
| 120 dest_root_fs_url + "move_dest" + extension)); | |
| 121 | |
| 122 content::BrowserThread::PostTask( | |
| 123 content::BrowserThread::IO, | |
| 124 FROM_HERE, | |
| 125 base::Bind(&MediaFileValidatorTest::CheckFiles, | |
| 126 base::Unretained(this), true, | |
| 127 base::Bind(&MediaFileValidatorTest::OnTestFilesReady, | |
| 128 base::Unretained(this), expected_result))); | |
| 129 } | |
| 130 | |
| 131 // Check that exactly one of |move_src_| and |move_dest_| exists. | |
| 132 // |src_expected| indicates which one should exist. When complete, | |
| 133 // |callback| is called with success/failure. | |
| 134 void CheckFiles(bool src_expected, | |
| 135 const base::Callback<void(bool success)>& callback) { | |
| 136 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize, | |
| 137 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult, | |
| 138 base::Unretained(this), !src_expected, callback)); | |
| 139 } | |
| 140 | |
| 141 // Helper that checks a file has the |expected_size|, which may be | |
| 142 // |kNoFileSize| if the file should not exist. |callback| is called | |
| 143 // with success/failure. | |
| 144 void CheckFile(fileapi::FileSystemURL url, | |
| 145 int64 expected_size, | |
| 146 const base::Callback<void(bool success)>& callback) { | |
| 147 CreateFSOp(url)->GetMetadata(url, | |
| 148 base::Bind(&HandleCheckFileResult, | |
| 149 expected_size, callback)); | |
| 150 } | |
| 151 | |
| 152 // Helper that checks the result of |move_src_| lookup and then checks | |
| 153 // |move_dest_| if all is as expected. | |
| 154 void OnCheckFilesFirstResult(bool dest_expected, | |
| 155 const base::Callback<void(bool)>& callback, | |
| 156 bool src_result) { | |
| 157 EXPECT_TRUE(src_result); | |
| 158 if (!src_result) { | |
| 159 callback.Run(false); | |
| 160 return; | |
| 161 } | |
| 162 CheckFile(move_dest_, dest_expected ? test_file_size_ : kNoFileSize, | |
| 163 callback); | |
| 164 } | |
| 165 | |
| 166 // Assert |test_files_ready| and then do the actual test of moving | |
| 167 // |move_src_| to |move_dest_|. | |
| 168 void OnTestFilesReady(bool expected_result, bool test_files_ready) { | |
| 169 ASSERT_TRUE(test_files_ready); | |
| 170 CreateFSOp(move_dest_)->Move( | |
| 171 move_src_, move_dest_, | |
| 172 base::Bind(&MediaFileValidatorTest::OnMoveResult, | |
| 173 base::Unretained(this), expected_result)); | |
| 174 } | |
| 175 | |
| 176 // Check that the move succeeded/failed based on expectation and then | |
| 177 // check that the right file exists. | |
| 178 void OnMoveResult(bool expected_result, base::PlatformFileError result) { | |
| 179 if (expected_result) | |
| 180 EXPECT_EQ(base::PLATFORM_FILE_OK, result); | |
| 181 else | |
| 182 EXPECT_EQ(base::PLATFORM_FILE_ERROR_SECURITY, result); | |
| 183 CheckFiles(!expected_result, | |
| 184 base::Bind(&MediaFileValidatorTest::OnTestFilesCheckResult, | |
| 185 base::Unretained(this))); | |
| 186 } | |
| 187 | |
| 188 // Check that the correct test file exists and then post the result back | |
| 189 // to the UI thread. | |
| 190 void OnTestFilesCheckResult(bool result) { | |
| 191 EXPECT_TRUE(result); | |
| 192 content::BrowserThread::PostTask( | |
| 193 content::BrowserThread::UI, | |
| 194 FROM_HERE, | |
| 195 base::Bind(&MediaFileValidatorTest::TestCompleteOnUIThread, | |
| 196 base::Unretained(this))); | |
| 197 } | |
| 198 | |
| 199 void TestCompleteOnUIThread(void) { | |
| 200 MessageLoop::current()->Quit(); | |
|
Lei Zhang
2013/05/29 09:40:58
needs base::, the crutch in message_loop.h will go
vandebo (ex-Chrome)
2013/05/29 18:56:05
Fixed up in a cleaner way.
| |
| 201 } | |
| 202 | |
| 203 fileapi::FileSystemOperation* CreateFSOp(fileapi::FileSystemURL url) { | |
|
Lei Zhang
2013/05/29 09:40:58
|url| can be const ref.
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 204 return file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 205 } | |
| 206 | |
| 207 base::ScopedTempDir base_; | |
|
Lei Zhang
2013/05/29 09:40:58
how about base_dir_?
vandebo (ex-Chrome)
2013/05/29 18:56:05
Done.
| |
| 208 | |
| 209 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | |
| 210 | |
| 211 int test_file_size_; | |
| 212 | |
| 213 fileapi::FileSystemURL move_src_; | |
| 214 fileapi::FileSystemURL move_dest_; | |
| 215 | |
| 216 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest); | |
| 217 }; | |
| 218 | |
| 219 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) { | |
| 220 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true); | |
| 221 } | |
| 222 | |
| 223 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) { | |
| 224 MoveTest("a.webp", std::string(kInvalidImage, arraysize(kInvalidImage)), | |
| 225 false); | |
| 226 } | |
| 227 | |
| 228 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) { | |
| 229 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false); | |
| 230 } | |
| 231 | |
| 232 } // namespace chrome | |
| OLD | NEW |