1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// 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 "chrome/browser/extensions/api/file_handlers/mime_util.h"
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_file_system_context.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace extensions {
namespace app_file_handler_util {
namespace {
const char kOrigin[] = "chrome-extension://cmalghjoncmjoeakimpfhojhpgemgaje";
const char kJPEGExtensionFilePath[] = "/fake/path/foo.jpg";
const char kJPEGExtensionUpperCaseFilePath[] = "/fake/path/FOO.JPG";
// Saves the returned mime type to a variable.
void OnMimeTypeResult(std::string* output, const std::string& mime_type) {
*output = mime_type;
}
// Saves returned mime types to a vector.
void OnMimeTypesCollected(std::vector<std::string>* output,
scoped_ptr<std::vector<std::string> > mime_types) {
*output = *mime_types;
}
// Creates a native local file system URL for a local path.
storage::FileSystemURL CreateNativeLocalFileSystemURL(
storage::FileSystemContext* context,
const base::FilePath local_path) {
return context->CreateCrackedFileSystemURL(
GURL(kOrigin), storage::kFileSystemTypeNativeLocal, local_path);
}
} // namespace
class FileHandlersMimeUtilTest : public testing::Test {
protected:
FileHandlersMimeUtilTest() {}
~FileHandlersMimeUtilTest() override {}
void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
file_system_context_ =
content::CreateFileSystemContextForTesting(NULL, data_dir_.path());
EXPECT_TRUE(base::CreateTemporaryFile(&html_mime_file_path_));
const std::string kSampleContent = "<html><body></body></html>";
EXPECT_TRUE(base::WriteFile(
html_mime_file_path_, kSampleContent.c_str(), kSampleContent.size()));
}
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
base::ScopedTempDir data_dir_;
base::FilePath html_mime_file_path_;
};
TEST_F(FileHandlersMimeUtilTest, GetMimeTypeForLocalPath) {
{
std::string result;
GetMimeTypeForLocalPath(
&profile_,
base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath),
base::Bind(&OnMimeTypeResult, &result));
content::RunAllBlockingPoolTasksUntilIdle();
EXPECT_EQ("image/jpeg", result);
}
{
std::string result;
GetMimeTypeForLocalPath(
&profile_,
base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath),
base::Bind(&OnMimeTypeResult, &result));
content::RunAllBlockingPoolTasksUntilIdle();
EXPECT_EQ("image/jpeg", result);
}
{
std::string result;
GetMimeTypeForLocalPath(&profile_,
html_mime_file_path_,
base::Bind(&OnMimeTypeResult, &result));
content::RunAllBlockingPoolTasksUntilIdle();
EXPECT_EQ("text/html", result);
}
}
TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForURLs) {
MimeTypeCollector collector(&profile_);
std::vector<storage::FileSystemURL> urls;
urls.push_back(CreateNativeLocalFileSystemURL(
file_system_context_.get(),
base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath)));
urls.push_back(CreateNativeLocalFileSystemURL(
file_system_context_.get(),
base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath)));
urls.push_back(CreateNativeLocalFileSystemURL(file_system_context_.get(),
html_mime_file_path_));
std::vector<std::string> result;
collector.CollectForURLs(urls, base::Bind(&OnMimeTypesCollected, &result));
content::RunAllBlockingPoolTasksUntilIdle();
ASSERT_EQ(3u, result.size());
EXPECT_EQ("image/jpeg", result[0]);
EXPECT_EQ("image/jpeg", result[1]);
EXPECT_EQ("text/html", result[2]);
}
TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForLocalPaths) {
MimeTypeCollector collector(&profile_);
std::vector<base::FilePath> local_paths;
local_paths.push_back(base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath));
local_paths.push_back(
base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath));
local_paths.push_back(html_mime_file_path_);
std::vector<std::string> result;
collector.CollectForLocalPaths(local_paths,
base::Bind(&OnMimeTypesCollected, &result));
content::RunAllBlockingPoolTasksUntilIdle();
ASSERT_EQ(3u, result.size());
EXPECT_EQ("image/jpeg", result[0]);
EXPECT_EQ("image/jpeg", result[1]);
EXPECT_EQ("text/html", result[2]);
}
} // namespace app_file_handler_util
} // namespace extensions
|