Chromium Code Reviews| 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 test_file = test_file.Append(FILE_PATH_LITERAL("media")); | |
|
Lei Zhang
2013/08/08 00:08:02
test_file =
test_file.AppendASCII("media")
vandebo (ex-Chrome)
2013/08/08 17:34:01
Done.
| |
| 63 test_file = test_file.Append(FILE_PATH_LITERAL("test")); | |
| 64 test_file = test_file.Append(FILE_PATH_LITERAL("data")); | |
| 65 } | |
| 66 return test_file; | |
| 67 } | |
| 68 | |
| 58 } // namespace | 69 } // namespace |
| 59 | 70 |
| 60 namespace chrome { | 71 namespace chrome { |
| 61 | 72 |
| 62 class MediaFileValidatorTest : public InProcessBrowserTest { | 73 class MediaFileValidatorTest : public InProcessBrowserTest { |
| 63 public: | 74 public: |
| 64 MediaFileValidatorTest() : test_file_size_(0) {} | 75 MediaFileValidatorTest() : test_file_size_(0) {} |
| 65 | 76 |
| 66 virtual ~MediaFileValidatorTest() {} | 77 virtual ~MediaFileValidatorTest() {} |
| 67 | 78 |
| 68 // Write |content| into |filename| in a test file system and try to move | 79 // 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|. | 80 // it into a media file system. The result is compared to |expected_result|. |
| 70 void MoveTest(const std::string& filename, const std::string& content, | 81 void MoveTest(const std::string& filename, const std::string& content, |
| 71 bool expected_result) { | 82 bool expected_result) { |
| 72 content::BrowserThread::PostTask( | 83 content::BrowserThread::PostTask( |
| 73 content::BrowserThread::FILE, | 84 content::BrowserThread::FILE, |
| 74 FROM_HERE, | 85 FROM_HERE, |
| 75 base::Bind(&MediaFileValidatorTest::SetupOnFileThread, | 86 base::Bind(&MediaFileValidatorTest::SetupOnFileThread, |
| 76 base::Unretained(this), filename, content, expected_result)); | 87 base::Unretained(this), filename, content, expected_result)); |
| 77 loop_runner_ = new content::MessageLoopRunner; | 88 loop_runner_ = new content::MessageLoopRunner; |
| 78 loop_runner_->Run(); | 89 loop_runner_->Run(); |
| 79 } | 90 } |
| 80 | 91 |
| 92 void MoveTestFromFile(const std::string& filename, | |
|
Lei Zhang
2013/08/08 00:08:02
Add a comment.
vandebo (ex-Chrome)
2013/08/08 17:34:01
Done.
| |
| 93 const base::FilePath& source, bool expected_result) { | |
| 94 content::BrowserThread::PostTask( | |
| 95 content::BrowserThread::FILE, | |
| 96 FROM_HERE, | |
| 97 base::Bind(&MediaFileValidatorTest::SetupFromFileOnFileThread, | |
| 98 base::Unretained(this), filename, source, expected_result)); | |
| 99 loop_runner_ = new content::MessageLoopRunner; | |
| 100 loop_runner_->Run(); | |
| 101 } | |
| 102 | |
| 81 private: | 103 private: |
| 82 // Create the test files, filesystem objects, etc. | 104 // Create the test files, filesystem objects, etc. |
| 83 void SetupOnFileThread(const std::string& filename, | 105 void SetupOnFileThread(const std::string& filename, |
| 84 const std::string& content, | 106 const std::string& content, |
| 85 bool expected_result) { | 107 bool expected_result) { |
| 86 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | 108 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| 87 base::FilePath base = base_dir_.path(); | 109 base::FilePath base = base_dir_.path(); |
| 88 base::FilePath src_path = base.AppendASCII("src_fs"); | 110 base::FilePath src_path = base.AppendASCII("src_fs"); |
| 89 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 111 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
| 90 | 112 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 | 144 |
| 123 content::BrowserThread::PostTask( | 145 content::BrowserThread::PostTask( |
| 124 content::BrowserThread::IO, | 146 content::BrowserThread::IO, |
| 125 FROM_HERE, | 147 FROM_HERE, |
| 126 base::Bind(&MediaFileValidatorTest::CheckFiles, | 148 base::Bind(&MediaFileValidatorTest::CheckFiles, |
| 127 base::Unretained(this), true, | 149 base::Unretained(this), true, |
| 128 base::Bind(&MediaFileValidatorTest::OnTestFilesReady, | 150 base::Bind(&MediaFileValidatorTest::OnTestFilesReady, |
| 129 base::Unretained(this), expected_result))); | 151 base::Unretained(this), expected_result))); |
| 130 } | 152 } |
| 131 | 153 |
| 154 void SetupFromFileOnFileThread(const std::string& filename, | |
| 155 const base::FilePath& source, | |
| 156 bool expected_result) { | |
| 157 std::string content; | |
| 158 ASSERT_TRUE(file_util::ReadFileToString(source, &content)); | |
| 159 SetupOnFileThread(filename, content, expected_result); | |
| 160 } | |
| 161 | |
| 132 // Check that exactly one of |move_src_| and |move_dest_| exists. | 162 // Check that exactly one of |move_src_| and |move_dest_| exists. |
| 133 // |src_expected| indicates which one should exist. When complete, | 163 // |src_expected| indicates which one should exist. When complete, |
| 134 // |callback| is called with success/failure. | 164 // |callback| is called with success/failure. |
| 135 void CheckFiles(bool src_expected, | 165 void CheckFiles(bool src_expected, |
| 136 const base::Callback<void(bool success)>& callback) { | 166 const base::Callback<void(bool success)>& callback) { |
| 137 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize, | 167 CheckFile(move_src_, src_expected ? test_file_size_ : kNoFileSize, |
| 138 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult, | 168 base::Bind(&MediaFileValidatorTest::OnCheckFilesFirstResult, |
| 139 base::Unretained(this), !src_expected, callback)); | 169 base::Unretained(this), !src_expected, callback)); |
| 140 } | 170 } |
| 141 | 171 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 int test_file_size_; | 235 int test_file_size_; |
| 206 | 236 |
| 207 fileapi::FileSystemURL move_src_; | 237 fileapi::FileSystemURL move_src_; |
| 208 fileapi::FileSystemURL move_dest_; | 238 fileapi::FileSystemURL move_dest_; |
| 209 | 239 |
| 210 scoped_refptr<content::MessageLoopRunner> loop_runner_; | 240 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
| 211 | 241 |
| 212 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest); | 242 DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTest); |
| 213 }; | 243 }; |
| 214 | 244 |
| 245 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) { | |
| 246 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false); | |
| 247 } | |
| 248 | |
| 215 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) { | 249 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidImage) { |
| 216 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true); | 250 MoveTest("a.webp", std::string(kValidImage, arraysize(kValidImage)), true); |
| 217 } | 251 } |
| 218 | 252 |
| 219 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) { | 253 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidImage) { |
| 220 MoveTest("a.webp", std::string(kInvalidImage, arraysize(kInvalidImage)), | 254 MoveTest("a.webp", std::string(kInvalidMediaFile, |
| 221 false); | 255 arraysize(kInvalidMediaFile)), false); |
| 222 } | 256 } |
| 223 | 257 |
| 224 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, UnsupportedExtension) { | 258 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidAudio) { |
| 225 MoveTest("a.txt", std::string(kValidImage, arraysize(kValidImage)), false); | 259 MoveTest("a.ogg", std::string(kInvalidMediaFile, |
| 260 arraysize(kInvalidMediaFile)), false); | |
| 261 } | |
| 262 | |
| 263 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidAudio) { | |
| 264 base::FilePath test_file = GetMediaTestDir().Append( | |
|
Lei Zhang
2013/08/08 00:08:02
AppendASCII() is shorter than APPEND(FPL()).
vandebo (ex-Chrome)
2013/08/08 17:34:01
Done.
| |
| 265 FILE_PATH_LITERAL("sfx.ogg")); | |
| 266 ASSERT_FALSE(test_file.empty()); | |
|
Lei Zhang
2013/08/08 00:08:02
This will never fail because you always do Append(
vandebo (ex-Chrome)
2013/08/08 17:34:01
Done.
| |
| 267 MoveTestFromFile("sfx.ogg", test_file, true); | |
| 268 } | |
| 269 | |
| 270 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, InvalidVideo) { | |
| 271 base::FilePath test_file = GetMediaTestDir().Append( | |
| 272 FILE_PATH_LITERAL("no_streams.webm")); | |
| 273 ASSERT_FALSE(test_file.empty()); | |
| 274 MoveTestFromFile("no_streams.webm", test_file, false); | |
| 275 } | |
| 276 | |
| 277 IN_PROC_BROWSER_TEST_F(MediaFileValidatorTest, ValidVideo) { | |
| 278 base::FilePath test_file = GetMediaTestDir().Append( | |
| 279 FILE_PATH_LITERAL("bear-320x240-multitrack.webm")); | |
| 280 ASSERT_FALSE(test_file.empty()); | |
| 281 MoveTestFromFile("multitrack.webm", test_file, true); | |
| 226 } | 282 } |
| 227 | 283 |
| 228 } // namespace chrome | 284 } // namespace chrome |
| OLD | NEW |