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

Side by Side Diff: chrome/browser/extensions/api/file_handlers/mime_util_unittest.cc

Issue 380993002: Upstream RunBlockingPoolTask(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
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 "chrome/browser/extensions/api/file_handlers/mime_util.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "content/public/test/test_file_system_context.h" 17 #include "content/public/test/test_file_system_context.h"
18 #include "content/public/test/test_utils.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 namespace extensions { 21 namespace extensions {
21 namespace app_file_handler_util { 22 namespace app_file_handler_util {
22 namespace { 23 namespace {
23 24
24 const char kOrigin[] = "chrome-extension://cmalghjoncmjoeakimpfhojhpgemgaje"; 25 const char kOrigin[] = "chrome-extension://cmalghjoncmjoeakimpfhojhpgemgaje";
25 const char kJPEGExtensionFilePath[] = "/fake/path/foo.jpg"; 26 const char kJPEGExtensionFilePath[] = "/fake/path/foo.jpg";
26 const char kJPEGExtensionUpperCaseFilePath[] = "/fake/path/FOO.JPG"; 27 const char kJPEGExtensionUpperCaseFilePath[] = "/fake/path/FOO.JPG";
27 28
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 base::FilePath html_mime_file_path_; 70 base::FilePath html_mime_file_path_;
70 }; 71 };
71 72
72 TEST_F(FileHandlersMimeUtilTest, GetMimeTypeForLocalPath) { 73 TEST_F(FileHandlersMimeUtilTest, GetMimeTypeForLocalPath) {
73 { 74 {
74 std::string result; 75 std::string result;
75 GetMimeTypeForLocalPath( 76 GetMimeTypeForLocalPath(
76 &profile_, 77 &profile_,
77 base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath), 78 base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath),
78 base::Bind(&OnMimeTypeResult, &result)); 79 base::Bind(&OnMimeTypeResult, &result));
79 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 80 content::RunAllBlockingPoolTasksUntilIdle();
80 base::RunLoop().RunUntilIdle();
81 EXPECT_EQ("image/jpeg", result); 81 EXPECT_EQ("image/jpeg", result);
82 } 82 }
83 83
84 { 84 {
85 std::string result; 85 std::string result;
86 GetMimeTypeForLocalPath( 86 GetMimeTypeForLocalPath(
87 &profile_, 87 &profile_,
88 base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath), 88 base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath),
89 base::Bind(&OnMimeTypeResult, &result)); 89 base::Bind(&OnMimeTypeResult, &result));
90 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 90 content::RunAllBlockingPoolTasksUntilIdle();
91 base::RunLoop().RunUntilIdle();
92 EXPECT_EQ("image/jpeg", result); 91 EXPECT_EQ("image/jpeg", result);
93 } 92 }
94 93
95 { 94 {
96 std::string result; 95 std::string result;
97 GetMimeTypeForLocalPath(&profile_, 96 GetMimeTypeForLocalPath(&profile_,
98 html_mime_file_path_, 97 html_mime_file_path_,
99 base::Bind(&OnMimeTypeResult, &result)); 98 base::Bind(&OnMimeTypeResult, &result));
100 99 content::RunAllBlockingPoolTasksUntilIdle();
101 // Since there are two calls to the blocking pool, it has to be flushed
102 // twice.
103 content::BrowserThread::GetBlockingPool()->FlushForTesting();
104 base::RunLoop().RunUntilIdle();
105 content::BrowserThread::GetBlockingPool()->FlushForTesting();
106 base::RunLoop().RunUntilIdle();
107
108 EXPECT_EQ("text/html", result); 100 EXPECT_EQ("text/html", result);
109 } 101 }
110 } 102 }
111 103
112 TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForURLs) { 104 TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForURLs) {
113 MimeTypeCollector collector(&profile_); 105 MimeTypeCollector collector(&profile_);
114 106
115 std::vector<fileapi::FileSystemURL> urls; 107 std::vector<fileapi::FileSystemURL> urls;
116 urls.push_back(CreateNativeLocalFileSystemURL( 108 urls.push_back(CreateNativeLocalFileSystemURL(
117 file_system_context_, 109 file_system_context_,
118 base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath))); 110 base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath)));
119 urls.push_back(CreateNativeLocalFileSystemURL( 111 urls.push_back(CreateNativeLocalFileSystemURL(
120 file_system_context_, 112 file_system_context_,
121 base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath))); 113 base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath)));
122 urls.push_back(CreateNativeLocalFileSystemURL(file_system_context_, 114 urls.push_back(CreateNativeLocalFileSystemURL(file_system_context_,
123 html_mime_file_path_)); 115 html_mime_file_path_));
124 116
125 std::vector<std::string> result; 117 std::vector<std::string> result;
126 collector.CollectForURLs(urls, base::Bind(&OnMimeTypesCollected, &result)); 118 collector.CollectForURLs(urls, base::Bind(&OnMimeTypesCollected, &result));
127 119 content::RunAllBlockingPoolTasksUntilIdle();
128 // Each URL may do up to 2 calls to the blocking pool. Hence, we need to
129 // flush it at least 6 times. This is unelegant, but there seem to be no
130 // better way.
131 for (int i = 0; i < 6; ++i) {
132 content::BrowserThread::GetBlockingPool()->FlushForTesting();
133 base::RunLoop().RunUntilIdle();
134 }
135 120
136 ASSERT_EQ(3u, result.size()); 121 ASSERT_EQ(3u, result.size());
137 EXPECT_EQ("image/jpeg", result[0]); 122 EXPECT_EQ("image/jpeg", result[0]);
138 EXPECT_EQ("image/jpeg", result[1]); 123 EXPECT_EQ("image/jpeg", result[1]);
139 EXPECT_EQ("text/html", result[2]); 124 EXPECT_EQ("text/html", result[2]);
140 } 125 }
141 126
142 TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForLocalPaths) { 127 TEST_F(FileHandlersMimeUtilTest, MimeTypeCollector_ForLocalPaths) {
143 MimeTypeCollector collector(&profile_); 128 MimeTypeCollector collector(&profile_);
144 129
145 std::vector<base::FilePath> local_paths; 130 std::vector<base::FilePath> local_paths;
146 local_paths.push_back(base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath)); 131 local_paths.push_back(base::FilePath::FromUTF8Unsafe(kJPEGExtensionFilePath));
147 local_paths.push_back( 132 local_paths.push_back(
148 base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath)); 133 base::FilePath::FromUTF8Unsafe(kJPEGExtensionUpperCaseFilePath));
149 local_paths.push_back(html_mime_file_path_); 134 local_paths.push_back(html_mime_file_path_);
150 135
151 std::vector<std::string> result; 136 std::vector<std::string> result;
152 collector.CollectForLocalPaths(local_paths, 137 collector.CollectForLocalPaths(local_paths,
153 base::Bind(&OnMimeTypesCollected, &result)); 138 base::Bind(&OnMimeTypesCollected, &result));
154 139 content::RunAllBlockingPoolTasksUntilIdle();
155 for (int i = 0; i < 6; ++i) {
156 content::BrowserThread::GetBlockingPool()->FlushForTesting();
157 base::RunLoop().RunUntilIdle();
158 }
159 140
160 ASSERT_EQ(3u, result.size()); 141 ASSERT_EQ(3u, result.size());
161 EXPECT_EQ("image/jpeg", result[0]); 142 EXPECT_EQ("image/jpeg", result[0]);
162 EXPECT_EQ("image/jpeg", result[1]); 143 EXPECT_EQ("image/jpeg", result[1]);
163 EXPECT_EQ("text/html", result[2]); 144 EXPECT_EQ("text/html", result[2]);
164 } 145 }
165 146
166 } // namespace app_file_handler_util 147 } // namespace app_file_handler_util
167 } // namespace extensions 148 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698