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