Index: chrome/browser/media_galleries/fileapi/media_file_validator_unittest.cc |
diff --git a/chrome/browser/media_galleries/fileapi/media_file_validator_unittest.cc b/chrome/browser/media_galleries/fileapi/media_file_validator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..73d1ab9adee6bac9239cb38809b2982cfb42a3a6 |
--- /dev/null |
+++ b/chrome/browser/media_galleries/fileapi/media_file_validator_unittest.cc |
@@ -0,0 +1,145 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/basictypes.h" |
+#include "base/bind.h" |
+#include "base/file_util.h" |
+#include "base/files/file_path.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/message_loop.h" |
+#include "chrome/browser/media_galleries/fileapi/media_file_system_mount_point_provider.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "webkit/fileapi/async_file_test_helper.h" |
+#include "webkit/fileapi/copy_or_move_file_validator.h" |
+#include "webkit/fileapi/external_mount_points.h" |
+#include "webkit/fileapi/file_system_context.h" |
+#include "webkit/fileapi/file_system_mount_point_provider.h" |
+#include "webkit/fileapi/file_system_types.h" |
+#include "webkit/fileapi/file_system_url.h" |
+#include "webkit/fileapi/file_system_util.h" |
+#include "webkit/fileapi/isolated_context.h" |
+#include "webkit/fileapi/mock_file_system_context.h" |
+#include "webkit/fileapi/test_mount_point_provider.h" |
+#include "webkit/quota/mock_special_storage_policy.h" |
+ |
+namespace { |
+ |
+const char kOrigin[] = "http://foo"; |
+ |
+} // namespace |
+ |
+namespace chrome { |
+ |
+class MediaFileValidatorTestHelper { |
+ public: |
+ MediaFileValidatorTestHelper() |
+ : ui_thread_(content::BrowserThread::UI, MessageLoop::current()), |
+ file_thread_(content::BrowserThread::FILE, MessageLoop::current()), |
+ io_thread_(content::BrowserThread::IO, MessageLoop::current()), |
+ test_file_size_(0) { |
+ } |
+ |
+ ~MediaFileValidatorTestHelper() { |
+ file_system_context_ = NULL; |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ } |
+ |
+ void SetUp(const std::string& filename, const std::string& content) { |
+ ASSERT_TRUE(base_.CreateUniqueTempDir()); |
+ base::FilePath base_dir = base_.path(); |
+ base::FilePath src_path = base_dir.Append(FILE_PATH_LITERAL("src_fs")); |
+ |
+ ScopedVector<fileapi::FileSystemMountPointProvider> additional_providers; |
+ additional_providers.push_back(new fileapi::TestMountPointProvider( |
+ base::MessageLoopProxy::current(), src_path)); |
+ additional_providers.push_back( |
+ new MediaFileSystemMountPointProvider(base_dir)); |
+ file_system_context_ = |
+ fileapi::CreateFileSystemContextWithAdditionalProvidersForTesting( |
+ NULL, additional_providers.Pass(), base_dir); |
+ |
+ fileapi::FileSystemMountPointProvider* mount_point_provider = |
+ file_system_context_->GetMountPointProvider( |
+ fileapi::kFileSystemTypeTest); |
+ mount_point_provider->GetFileSystemRootPathOnFileThread( |
+ SourceURL(std::string()), true /* create */); |
+ |
+ base::FilePath dest_path = base_dir.Append(FILE_PATH_LITERAL("dest_fs")); |
+ file_util::CreateDirectory(dest_path); |
+ std::string dest_fsid = |
+ fileapi::IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
+ fileapi::kFileSystemTypeNativeMedia, dest_path, NULL); |
+ |
+ test_file_size_ = content.size(); |
+ base::FilePath src_file = src_path.AppendASCII(filename.c_str()); |
+ ASSERT_EQ(test_file_size_, |
+ file_util::WriteFile(src_file, content.data(), test_file_size_)); |
+ |
+ move_src_ = SourceURL(filename.c_str()); |
+ ASSERT_EQ('.', filename[filename.size() - 4]); |
+ std::string extension = filename.substr(filename.size() - 4); |
+ std::string dest_root_fs_url = fileapi::GetIsolatedFileSystemRootURIString( |
+ GURL(kOrigin), dest_fsid, "dest_fs/"); |
+ move_dest_ = file_system_context_->CrackURL(GURL( |
+ dest_root_fs_url + "move_dest" + extension)); |
+ } |
+ |
+ void MoveTest(base::PlatformFileError expected) { |
+ ASSERT_TRUE(FileExists(move_src_, test_file_size_)); |
+ ASSERT_FALSE(FileExists(move_dest_, test_file_size_)); |
+ |
+printf("Doing move\n"); |
+ EXPECT_EQ(expected, |
+ fileapi::AsyncFileTestHelper::Move(file_system_context_, |
+ move_src_, move_dest_)); |
+printf("Move done\n"); |
+ |
+ if (expected == base::PLATFORM_FILE_OK) { |
+ EXPECT_FALSE(FileExists(move_src_, test_file_size_)); |
+ EXPECT_TRUE(FileExists(move_dest_, test_file_size_)); |
+ } else { |
+ EXPECT_TRUE(FileExists(move_src_, test_file_size_)); |
+ EXPECT_FALSE(FileExists(move_dest_, test_file_size_)); |
+ } |
+ }; |
+ |
+ private: |
+ fileapi::FileSystemURL SourceURL(const std::string& path) { |
+ return file_system_context_->CreateCrackedFileSystemURL( |
+ GURL(kOrigin), fileapi::kFileSystemTypeTest, |
+ base::FilePath::FromUTF8Unsafe(path)); |
+ } |
+ |
+ bool FileExists(const fileapi::FileSystemURL& url, int64 expected_size) { |
+ return fileapi::AsyncFileTestHelper::FileExists( |
+ file_system_context_, url, expected_size); |
+ } |
+ |
+ base::ScopedTempDir base_; |
+ |
+ base::MessageLoop message_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ content::TestBrowserThread file_thread_; |
+ content::TestBrowserThread io_thread_; |
+ |
+ scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
+ |
+ int test_file_size_; |
+ |
+ fileapi::FileSystemURL move_src_; |
+ fileapi::FileSystemURL move_dest_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MediaFileValidatorTestHelper); |
+}; |
+ |
+TEST(MediaFileValidatorTest, ValidGif) { |
+ // Within a file system type, validation is not expected, so it should |
+ // work for kFileSystemTypeNativeMedia without a validator set. |
+ MediaFileValidatorTestHelper helper; |
+ helper.SetUp("a.gif", ""); |
+ helper.MoveTest(base::PLATFORM_FILE_OK); |
+} |
+ |
+} // namespace chrome |