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