Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: net/base/mime_util_unittest.cc

Issue 1129643002: Moved media mime type and codec checks to media/base/mime_util.* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add media dependency for mime_util in .gyp file Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/base/mime_util.cc ('K') | « net/base/mime_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/strings/string_split.h" 6 #include "base/strings/string_split.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "net/base/mime_util.h" 8 #include "net/base/mime_util.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 #if defined(OS_ANDROID)
12 #include "base/android/build_info.h"
13 #endif
14
15 namespace net { 11 namespace net {
16 12
17 TEST(MimeUtilTest, ExtensionTest) { 13 TEST(MimeUtilTest, ExtensionTest) {
18 const struct { 14 const struct {
19 const base::FilePath::CharType* extension; 15 const base::FilePath::CharType* extension;
20 const char* const mime_type; 16 const char* const mime_type;
21 bool valid; 17 bool valid;
22 } tests[] = { 18 } tests[] = {
23 {FILE_PATH_LITERAL("png"), "image/png", true}, 19 {FILE_PATH_LITERAL("png"), "image/png", true},
24 {FILE_PATH_LITERAL("PNG"), "image/png", true}, 20 {FILE_PATH_LITERAL("PNG"), "image/png", true},
25 {FILE_PATH_LITERAL("css"), "text/css", true}, 21 {FILE_PATH_LITERAL("css"), "text/css", true},
26 {FILE_PATH_LITERAL("pjp"), "image/jpeg", true}, 22 {FILE_PATH_LITERAL("pjp"), "image/jpeg", true},
27 {FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true}, 23 {FILE_PATH_LITERAL("pjpeg"), "image/jpeg", true},
28 #if defined(OS_ANDROID) 24 #if defined(OS_ANDROID)
ddorwin 2015/05/08 16:29:19 #include "build/build_config.h"
servolk 2015/05/08 17:33:05 Done.
29 {FILE_PATH_LITERAL("m3u8"), "application/x-mpegurl", true}, 25 {FILE_PATH_LITERAL("m3u8"), "application/x-mpegurl", true},
30 #endif 26 #endif
31 {FILE_PATH_LITERAL("not an extension / for sure"), "", false}, 27 {FILE_PATH_LITERAL("not an extension / for sure"), "", false},
32 }; 28 };
33 29
34 std::string mime_type; 30 std::string mime_type;
35 bool rv; 31 bool rv;
36 32
37 for (size_t i = 0; i < arraysize(tests); ++i) { 33 for (size_t i = 0; i < arraysize(tests); ++i) {
38 rv = GetMimeTypeFromExtension(tests[i].extension, &mime_type); 34 rv = GetMimeTypeFromExtension(tests[i].extension, &mime_type);
(...skipping 23 matching lines...) Expand all
62 58
63 for (size_t i = 0; i < arraysize(tests); ++i) { 59 for (size_t i = 0; i < arraysize(tests); ++i) {
64 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path), 60 rv = GetMimeTypeFromFile(base::FilePath(tests[i].file_path),
65 &mime_type); 61 &mime_type);
66 EXPECT_EQ(tests[i].valid, rv); 62 EXPECT_EQ(tests[i].valid, rv);
67 if (rv) 63 if (rv)
68 EXPECT_EQ(tests[i].mime_type, mime_type); 64 EXPECT_EQ(tests[i].mime_type, mime_type);
69 } 65 }
70 } 66 }
71 67
72 TEST(MimeUtilTest, LookupTypes) {
73 #if defined(OS_ANDROID)
74 EXPECT_TRUE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
75 EXPECT_TRUE(IsSupportedMediaMimeType("application/x-mpegurl"));
76 EXPECT_TRUE(IsSupportedMediaMimeType("Application/X-MPEGURL"));
77 #endif
78 }
79
80 TEST(MimeUtilTest, StrictMediaMimeType) {
81 EXPECT_TRUE(IsStrictMediaMimeType("video/webm"));
82 EXPECT_TRUE(IsStrictMediaMimeType("Video/WEBM"));
83 EXPECT_TRUE(IsStrictMediaMimeType("audio/webm"));
84
85 EXPECT_TRUE(IsStrictMediaMimeType("audio/wav"));
86 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-wav"));
87
88 EXPECT_TRUE(IsStrictMediaMimeType("video/ogg"));
89 EXPECT_TRUE(IsStrictMediaMimeType("audio/ogg"));
90 EXPECT_TRUE(IsStrictMediaMimeType("application/ogg"));
91
92 EXPECT_TRUE(IsStrictMediaMimeType("audio/mpeg"));
93 EXPECT_TRUE(IsStrictMediaMimeType("audio/mp3"));
94 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-mp3"));
95
96 EXPECT_TRUE(IsStrictMediaMimeType("video/mp4"));
97 EXPECT_TRUE(IsStrictMediaMimeType("video/x-m4v"));
98 EXPECT_TRUE(IsStrictMediaMimeType("audio/mp4"));
99 EXPECT_TRUE(IsStrictMediaMimeType("audio/x-m4a"));
100
101 EXPECT_TRUE(IsStrictMediaMimeType("application/x-mpegurl"));
102 EXPECT_TRUE(IsStrictMediaMimeType("application/vnd.apple.mpegurl"));
103
104 EXPECT_FALSE(IsStrictMediaMimeType("video/unknown"));
105 EXPECT_FALSE(IsStrictMediaMimeType("Video/UNKNOWN"));
106 EXPECT_FALSE(IsStrictMediaMimeType("audio/unknown"));
107 EXPECT_FALSE(IsStrictMediaMimeType("application/unknown"));
108 EXPECT_FALSE(IsStrictMediaMimeType("unknown/unknown"));
109 }
110
111 TEST(MimeUtilTest, MatchesMimeType) { 68 TEST(MimeUtilTest, MatchesMimeType) {
112 // MIME types are case insensitive. 69 // MIME types are case insensitive.
113 EXPECT_TRUE(MatchesMimeType("VIDEO/*", "video/x-mpeg")); 70 EXPECT_TRUE(MatchesMimeType("VIDEO/*", "video/x-mpeg"));
114 EXPECT_TRUE(MatchesMimeType("video/*", "VIDEO/X-MPEG")); 71 EXPECT_TRUE(MatchesMimeType("video/*", "VIDEO/X-MPEG"));
115 72
116 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg")); 73 EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg"));
117 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg")); 74 EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg"));
118 EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); 75 EXPECT_TRUE(MatchesMimeType("video/*", "video/*"));
119 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg")); 76 EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg"));
120 EXPECT_TRUE(MatchesMimeType("application/*+xml", 77 EXPECT_TRUE(MatchesMimeType("application/*+xml",
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 EXPECT_FALSE(MatchesMimeType("video/*", "*/*")); 137 EXPECT_FALSE(MatchesMimeType("video/*", "*/*"));
181 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*")); 138 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*"));
182 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val")); 139 EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val"));
183 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2")); 140 EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2"));
184 141
185 EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd")); 142 EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd"));
186 EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd")); 143 EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd"));
187 EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd")); 144 EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd"));
188 } 145 }
189 146
190 TEST(MimeUtilTest, CommonMediaMimeType) {
191 #if defined(OS_ANDROID)
192 bool HLSSupported;
193 if (base::android::BuildInfo::GetInstance()->sdk_int() < 14)
194 HLSSupported = false;
195 else
196 HLSSupported = true;
197 #endif
198
199 EXPECT_TRUE(IsSupportedMediaMimeType("audio/webm"));
200 EXPECT_TRUE(IsSupportedMediaMimeType("video/webm"));
201
202 EXPECT_TRUE(IsSupportedMediaMimeType("audio/wav"));
203 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-wav"));
204
205 EXPECT_TRUE(IsSupportedMediaMimeType("audio/ogg"));
206 EXPECT_TRUE(IsSupportedMediaMimeType("application/ogg"));
207 #if defined(OS_ANDROID)
208 EXPECT_FALSE(IsSupportedMediaMimeType("video/ogg"));
209 EXPECT_EQ(HLSSupported, IsSupportedMediaMimeType("application/x-mpegurl"));
210 EXPECT_EQ(HLSSupported,
211 IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
212 #else
213 EXPECT_TRUE(IsSupportedMediaMimeType("video/ogg"));
214 EXPECT_FALSE(IsSupportedMediaMimeType("application/x-mpegurl"));
215 EXPECT_FALSE(IsSupportedMediaMimeType("application/vnd.apple.mpegurl"));
216 #endif // OS_ANDROID
217
218 #if defined(USE_PROPRIETARY_CODECS)
219 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp4"));
220 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-m4a"));
221 EXPECT_TRUE(IsSupportedMediaMimeType("video/mp4"));
222 EXPECT_TRUE(IsSupportedMediaMimeType("video/x-m4v"));
223
224 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mp3"));
225 EXPECT_TRUE(IsSupportedMediaMimeType("audio/x-mp3"));
226 EXPECT_TRUE(IsSupportedMediaMimeType("audio/mpeg"));
227 EXPECT_TRUE(IsSupportedMediaMimeType("audio/aac"));
228
229 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
230 EXPECT_TRUE(IsSupportedMediaMimeType("video/mp2t"));
231 #else
232 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp2t"));
233 #endif
234 #else
235 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp4"));
236 EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-m4a"));
237 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp4"));
238 EXPECT_FALSE(IsSupportedMediaMimeType("video/x-m4v"));
239
240 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mp3"));
241 EXPECT_FALSE(IsSupportedMediaMimeType("audio/x-mp3"));
242 EXPECT_FALSE(IsSupportedMediaMimeType("audio/mpeg"));
243 EXPECT_FALSE(IsSupportedMediaMimeType("audio/aac"));
244 #endif // USE_PROPRIETARY_CODECS
245 EXPECT_FALSE(IsSupportedMediaMimeType("video/mp3"));
246
247 EXPECT_FALSE(IsSupportedMediaMimeType("video/unknown"));
248 EXPECT_FALSE(IsSupportedMediaMimeType("audio/unknown"));
249 EXPECT_FALSE(IsSupportedMediaMimeType("unknown/unknown"));
250 }
251
252 // Note: codecs should only be a list of 2 or fewer; hence the restriction of
253 // results' length to 2.
254 TEST(MimeUtilTest, ParseCodecString) {
255 const struct {
256 const char* const original;
257 size_t expected_size;
258 const char* const results[2];
259 } tests[] = {
260 { "\"bogus\"", 1, { "bogus" } },
261 { "0", 1, { "0" } },
262 { "avc1.42E01E, mp4a.40.2", 2, { "avc1", "mp4a" } },
263 { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v", "mp4a" } },
264 { "mp4v.20.8, samr", 2, { "mp4v", "samr" } },
265 { "\"theora, vorbis\"", 2, { "theora", "vorbis" } },
266 { "", 0, { } },
267 { "\"\"", 0, { } },
268 { "\" \"", 0, { } },
269 { ",", 2, { "", "" } },
270 };
271
272 for (size_t i = 0; i < arraysize(tests); ++i) {
273 std::vector<std::string> codecs_out;
274 ParseCodecString(tests[i].original, &codecs_out, true);
275 ASSERT_EQ(tests[i].expected_size, codecs_out.size());
276 for (size_t j = 0; j < tests[i].expected_size; ++j)
277 EXPECT_EQ(tests[i].results[j], codecs_out[j]);
278 }
279
280 // Test without stripping the codec type.
281 std::vector<std::string> codecs_out;
282 ParseCodecString("avc1.42E01E, mp4a.40.2", &codecs_out, false);
283 ASSERT_EQ(2u, codecs_out.size());
284 EXPECT_EQ("avc1.42E01E", codecs_out[0]);
285 EXPECT_EQ("mp4a.40.2", codecs_out[1]);
286 }
287
288 TEST(MimeUtilTest, TestParseMimeTypeWithoutParameter) { 147 TEST(MimeUtilTest, TestParseMimeTypeWithoutParameter) {
289 std::string nonAscii("application/nonutf8"); 148 std::string nonAscii("application/nonutf8");
290 EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL)); 149 EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL));
291 #if defined(OS_WIN) 150 #if defined(OS_WIN)
292 nonAscii.append(base::WideToUTF8(L"\u2603")); 151 nonAscii.append(base::WideToUTF8(L"\u2603"));
293 #else 152 #else
294 nonAscii.append("\u2603"); // unicode snowman 153 nonAscii.append("\u2603"); // unicode snowman
295 #endif 154 #endif
296 EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL)); 155 EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii, NULL, NULL));
297 156
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 std::string post_data; 273 std::string post_data;
415 AddMultipartValueForUpload("value name", "value", "boundary", 274 AddMultipartValueForUpload("value name", "value", "boundary",
416 "content type", &post_data); 275 "content type", &post_data);
417 AddMultipartValueForUpload("value name", "value", "boundary", 276 AddMultipartValueForUpload("value name", "value", "boundary",
418 "", &post_data); 277 "", &post_data);
419 AddMultipartFinalDelimiterForUpload("boundary", &post_data); 278 AddMultipartFinalDelimiterForUpload("boundary", &post_data);
420 EXPECT_STREQ(ref_output, post_data.c_str()); 279 EXPECT_STREQ(ref_output, post_data.c_str());
421 } 280 }
422 281
423 } // namespace net 282 } // namespace net
OLDNEW
« net/base/mime_util.cc ('K') | « net/base/mime_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698