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