| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "base/path_service.h" |
| 12 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | 13 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "content/public/test/browser_test.h" | 15 #include "content/public/test/browser_test.h" |
| 15 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 18 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 18 #include "webkit/browser/fileapi/file_system_backend.h" | 19 #include "webkit/browser/fileapi/file_system_backend.h" |
| 19 #include "webkit/browser/fileapi/file_system_context.h" | 20 #include "webkit/browser/fileapi/file_system_context.h" |
| 20 #include "webkit/browser/fileapi/file_system_operation_runner.h" | 21 #include "webkit/browser/fileapi/file_system_operation_runner.h" |
| 21 #include "webkit/browser/fileapi/file_system_url.h" | 22 #include "webkit/browser/fileapi/file_system_url.h" |
| 22 #include "webkit/browser/fileapi/isolated_context.h" | 23 #include "webkit/browser/fileapi/isolated_context.h" |
| 23 #include "webkit/browser/fileapi/mock_file_system_context.h" | 24 #include "webkit/browser/fileapi/mock_file_system_context.h" |
| 24 #include "webkit/browser/fileapi/test_file_system_backend.h" | 25 #include "webkit/browser/fileapi/test_file_system_backend.h" |
| 25 #include "webkit/common/fileapi/file_system_types.h" | 26 #include "webkit/common/fileapi/file_system_types.h" |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const char kOrigin[] = "http://foo"; | 30 const char kOrigin[] = "http://foo"; |
| 30 | 31 |
| 31 const char kValidImage[] = "RIFF0\0\0\0WEBPVP8 $\0\0\0\xB2\x02\0\x9D\x01\x2A" | 32 const char kValidImage[] = "RIFF0\0\0\0WEBPVP8 $\0\0\0\xB2\x02\0\x9D\x01\x2A" |
| 32 "\x01\0\x01\0\x2F\x9D\xCE\xE7s\xA8((((\x01\x9CK(\0" | 33 "\x01\0\x01\0\x2F\x9D\xCE\xE7s\xA8((((\x01\x9CK(\0" |
| 33 "\x05\xCE\xB3l\0\0\xFE\xD8\x80\0\0"; | 34 "\x05\xCE\xB3l\0\0\xFE\xD8\x80\0\0"; |
| 34 | 35 |
| 35 const char kInvalidImage[] = "Not an image"; | 36 const char kInvalidMediaFile[] = "Not a media file"; |
| 36 | 37 |
| 37 const int64 kNoFileSize = -1; | 38 const int64 kNoFileSize = -1; |
| 38 | 39 |
| 39 void HandleCheckFileResult(int64 expected_size, | 40 void HandleCheckFileResult(int64 expected_size, |
| 40 const base::Callback<void(bool success)>& callback, | 41 const base::Callback<void(bool success)>& callback, |
| 41 base::PlatformFileError result, | 42 base::PlatformFileError result, |
| 42 const base::PlatformFileInfo& file_info) { | 43 const base::PlatformFileInfo& file_info) { |
| 43 if (result == base::PLATFORM_FILE_OK) { | 44 if (result == base::PLATFORM_FILE_OK) { |
| 44 if (!file_info.is_directory && expected_size != kNoFileSize && | 45 if (!file_info.is_directory && expected_size != kNoFileSize && |
| 45 file_info.size == expected_size) { | 46 file_info.size == expected_size) { |
| 46 callback.Run(true); | 47 callback.Run(true); |
| 47 return; | 48 return; |
| 48 } | 49 } |
| 49 } else { | 50 } else { |
| 50 if (expected_size == kNoFileSize) { | 51 if (expected_size == kNoFileSize) { |
| 51 callback.Run(true); | 52 callback.Run(true); |
| 52 return; | 53 return; |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 callback.Run(false); | 56 callback.Run(false); |
| 56 } | 57 } |
| 57 | 58 |
| 59 base::FilePath GetMediaTestDir() { |
| 60 base::FilePath test_file; |
| 61 if (!PathService::Get(base::DIR_SOURCE_ROOT, &test_file)) |
| 62 return base::FilePath(); |
| 63 return test_file.AppendASCII("media").AppendASCII("test").AppendASCII("data"); |
| 64 } |
| 65 |
| 58 } // namespace | 66 } // namespace |
| 59 | 67 |
| 60 namespace chrome { | 68 namespace chrome { |
| 61 | 69 |
| 62 class MediaFileValidatorTest : public InProcessBrowserTest { | 70 class MediaFileValidatorTest : public InProcessBrowserTest { |
| 63 public: | 71 public: |
| 64 MediaFileValidatorTest() : test_file_size_(0) {} | 72 MediaFileValidatorTest() : test_file_size_(0) {} |
| 65 | 73 |
| 66 virtual ~MediaFileValidatorTest() {} | 74 virtual ~MediaFileValidatorTest() {} |
| 67 | 75 |
| 68 // Write |content| into |filename| in a test file system and try to move | 76 // 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 |expected_result|. | 77 // it into a media file system. The result is compared to |expected_result|. |
| 70 void MoveTest(const std::string& filename, const std::string& content, | 78 void MoveTest(const std::string& filename, const std::string& content, |
| 71 bool expected_result) { | 79 bool expected_result) { |
| 72 content::BrowserThread::PostTask( | 80 content::BrowserThread::PostTask( |
| 73 content::BrowserThread::FILE, | 81 content::BrowserThread::FILE, |
| 74 FROM_HERE, | 82 FROM_HERE, |
| 75 base::Bind(&MediaFileValidatorTest::SetupOnFileThread, | 83 base::Bind(&MediaFileValidatorTest::SetupOnFileThread, |
| 76 base::Unretained(this), filename, content, expected_result)); | 84 base::Unretained(this), filename, content, expected_result)); |
| 77 loop_runner_ = new content::MessageLoopRunner; | 85 loop_runner_ = new content::MessageLoopRunner; |
| 78 loop_runner_->Run(); | 86 loop_runner_->Run(); |
| 79 } | 87 } |
| 80 | 88 |
| 89 // Write |source| into |filename| in a test file system and try to move it |
| 90 // into a media file system. The result is compared to |expected_result|. |
| 91 void MoveTestFromFile(const std::string& filename, |
| 92 const base::FilePath& source, bool expected_result) { |
| 93 content::BrowserThread::PostTask( |
| 94 content::BrowserThread::FILE, |
| 95 FROM_HERE, |
| 96 base::Bind(&MediaFileValidatorTest::SetupFromFileOnFileThread, |
| 97 base::Unretained(this), filename, source, expected_result)); |
| 98 loop_runner_ = new content::MessageLoopRunner; |
| 99 loop_runner_->Run(); |
| 100 } |
| 101 |
| 81 private: | 102 private: |
| 82 // Create the test files, filesystem objects, etc. | 103 // Create the test files, filesystem objects, etc. |
| 83 void SetupOnFileThread(const std::string& filename, | 104 void SetupOnFileThread(const std::string& filename, |
| 84 const std::string& content, | 105 const std::string& content, |
| 85 bool expected_result) { | 106 bool expected_result) { |
| 86 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | 107 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| 87 base::FilePath base = base_dir_.path(); | 108 base::FilePath base = base_dir_.path(); |
| 88 base::FilePath src_path = base.AppendASCII("src_fs"); | 109 base::FilePath src_path = base.AppendASCII("src_fs"); |
| 89 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 110 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 90 | 111 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 143 |
| 123 content::BrowserThread::PostTask( | 144 content::BrowserThread::PostTask( |
| 124 content::BrowserThread::IO, | 145 content::BrowserThread::IO, |
| 125 FROM_HERE, | 146 FROM_HERE, |
| 126 base::Bind(&MediaFileValidatorTest::CheckFiles, | 147 base::Bind(&MediaFileValidatorTest::CheckFiles, |
| 127 base::Unretained(this), true, | 148 base::Unretained(this), true, |
| 128 base::Bind(&MediaFileValidatorTest::OnTestFilesReady, | 149 base::Bind(&MediaFileValidatorTest::OnTestFilesReady, |
| 129 base::Unretained(this), expected_result))); | 150 base::Unretained(this), expected_result))); |
| 130 } | 151 } |
| 131 | 152 |
| 153 void SetupFromFileOnFileThread(const std::string& filename, |
| 154 const base::FilePath& source, |
| 155 bool expected_result) { |
| 156 std::string content; |
| 157 ASSERT_TRUE(file_util::ReadFileToString(source, &content)); |
| 158 SetupOnFileThread(filename, content, expected_result); |
| 159 } |
| 160 |
| 132 // Check that exactly one of |move_src_| and |move_dest_| exists. | 161 // Check that exactly one of |move_src_| and |move_dest_| exists. |
| 133 // |src_expected| indicates which one should exist. When complete, | 162 // |src_expected| indicates which one should exist. When complete, |
| 134 // |callback| is called with success/failure. | 163 // |callback| is called with success/failure. |
| 135 void CheckFiles(bool src_expected, | 164 void CheckFiles(bool src_expected, |
| 136 const base::Callback<void(bool success)>& callback) { | 165 const base::Callback<void(bool success)>& callback) { |
| 137 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize, | 166 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize, |
| 138 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult, | 167 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult, |
| 139 base::Unretained(this), !src_expected, callback)); | 168 base::Unretained(this), !src_expected, callback)); |
| 140 } | 169 } |
| 141 | 170 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 int test_file_size_; | 234 int test_file_size_; |
| 206 | 235 |
| 207 fileapi::FileSystemURL move_src_; | 236 fileapi::FileSystemURL move_src_; |
| 208 fileapi::FileSystemURL move_dest_; | 237 fileapi::FileSystemURL move_dest_; |
| 209 | 238 |
| 210 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 239 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
| 211 | 240 |
| 212 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest); | 241 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest); |
| 213 }; | 242 }; |
| 214 | 243 |
| 244 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) { |
| 245 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false); |
| 246 } |
| 247 |
| 215 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) { | 248 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) { |
| 216 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true); | 249 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true); |
| 217 } | 250 } |
| 218 | 251 |
| 219 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) { | 252 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) { |
| 220 MoveTest("a.webp", std::string(kInvalidImage, arraysize(kInvalidImage)), | 253 MoveTest("a.webp", std::string(kInvalidMediaFile, |
| 221 false); | 254 arraysize(kInvalidMediaFile)), false); |
| 222 } | 255 } |
| 223 | 256 |
| 224 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) { | 257 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidAudio) { |
| 225 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false); | 258 MoveTest("a.ogg", std::string(kInvalidMediaFile, |
| 259 arraysize(kInvalidMediaFile)), false); |
| 260 } |
| 261 |
| 262 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidAudio) { |
| 263 base::FilePath test_file = GetMediaTestDir(); |
| 264 ASSERT_FALSE(test_file.empty()); |
| 265 test_file = test_file.AppendASCII("sfx.ogg"); |
| 266 MoveTestFromFile("sfx.ogg", test_file, true); |
| 267 } |
| 268 |
| 269 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidVideo) { |
| 270 base::FilePath test_file = GetMediaTestDir(); |
| 271 ASSERT_FALSE(test_file.empty()); |
| 272 test_file = test_file.AppendASCII("no_streams.webm"); |
| 273 MoveTestFromFile("no_streams.webm", test_file, false); |
| 274 } |
| 275 |
| 276 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidVideo) { |
| 277 base::FilePath test_file = GetMediaTestDir(); |
| 278 ASSERT_FALSE(test_file.empty()); |
| 279 test_file = test_file.AppendASCII("bear-320x240-multitrack.webm"); |
| 280 MoveTestFromFile("multitrack.webm", test_file, true); |
| 226 } | 281 } |
| 227 | 282 |
| 228 } // namespace chrome | 283 } // namespace chrome |
| OLD | NEW |