OLD | NEW |
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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "chrome/browser/download/download_extension_api.h" | 10 #include "chrome/browser/download/download_extension_api.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 const FilePath::CharType* filename; | 52 const FilePath::CharType* filename; |
53 | 53 |
54 // State for the download. Note that IN_PROGRESS downloads will be created | 54 // State for the download. Note that IN_PROGRESS downloads will be created |
55 // as CANCELLED. | 55 // as CANCELLED. |
56 DownloadItem::DownloadState state; | 56 DownloadItem::DownloadState state; |
57 | 57 |
58 // Danger type for the download. | 58 // Danger type for the download. |
59 content::DownloadDangerType danger_type; | 59 content::DownloadDangerType danger_type; |
60 }; | 60 }; |
61 | 61 |
| 62 virtual Browser* current_browser() { return browser(); } |
| 63 |
62 // InProcessBrowserTest | 64 // InProcessBrowserTest |
63 virtual void SetUpOnMainThread() OVERRIDE { | 65 virtual void SetUpOnMainThread() OVERRIDE { |
64 BrowserThread::PostTask( | 66 BrowserThread::PostTask( |
65 BrowserThread::IO, FROM_HERE, | 67 BrowserThread::IO, FROM_HERE, |
66 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 68 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
67 InProcessBrowserTest::SetUpOnMainThread(); | 69 InProcessBrowserTest::SetUpOnMainThread(); |
68 ASSERT_TRUE(CreateAndSetDownloadsDirectory(browser())); | 70 CreateAndSetDownloadsDirectory(); |
69 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPromptForDownload, | 71 current_browser()->profile()->GetPrefs()->SetBoolean( |
70 false); | 72 prefs::kPromptForDownload, false); |
71 GetDownloadManager()->RemoveAllDownloads(); | 73 GetDownloadManager()->RemoveAllDownloads(); |
72 } | 74 } |
73 | 75 |
74 DownloadManager* GetDownloadManager() { | 76 virtual DownloadManager* GetDownloadManager() { |
75 DownloadService* download_service = | 77 DownloadService* download_service = |
76 DownloadServiceFactory::GetForProfile(browser()->profile()); | 78 DownloadServiceFactory::GetForProfile(current_browser()->profile()); |
77 return download_service->GetDownloadManager(); | 79 return download_service->GetDownloadManager(); |
78 } | 80 } |
79 | 81 |
80 // Creates a set of history downloads based on the provided |history_info| | 82 // Creates a set of history downloads based on the provided |history_info| |
81 // array. |count| is the number of elements in |history_info|. On success, | 83 // array. |count| is the number of elements in |history_info|. On success, |
82 // |items| will contain |count| DownloadItems in the order that they were | 84 // |items| will contain |count| DownloadItems in the order that they were |
83 // specified in |history_info|. Returns true on success and false otherwise. | 85 // specified in |history_info|. Returns true on success and false otherwise. |
84 bool CreateHistoryDownloads(const HistoryDownloadInfo* history_info, | 86 bool CreateHistoryDownloads(const HistoryDownloadInfo* history_info, |
85 size_t count, | 87 size_t count, |
86 DownloadManager::DownloadVector* items) { | 88 DownloadManager::DownloadVector* items) { |
(...skipping 30 matching lines...) Expand all Loading... |
117 return true; | 119 return true; |
118 } | 120 } |
119 | 121 |
120 void CreateSlowTestDownloads( | 122 void CreateSlowTestDownloads( |
121 size_t count, DownloadManager::DownloadVector* items) { | 123 size_t count, DownloadManager::DownloadVector* items) { |
122 for (size_t i = 0; i < count; ++i) { | 124 for (size_t i = 0; i < count; ++i) { |
123 scoped_ptr<DownloadTestObserver> observer( | 125 scoped_ptr<DownloadTestObserver> observer( |
124 CreateInProgressDownloadObserver(1)); | 126 CreateInProgressDownloadObserver(1)); |
125 GURL slow_download_url(URLRequestSlowDownloadJob::kUnknownSizeUrl); | 127 GURL slow_download_url(URLRequestSlowDownloadJob::kUnknownSizeUrl); |
126 ui_test_utils::NavigateToURLWithDisposition( | 128 ui_test_utils::NavigateToURLWithDisposition( |
127 browser(), slow_download_url, CURRENT_TAB, | 129 current_browser(), slow_download_url, CURRENT_TAB, |
128 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 130 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
129 observer->WaitForFinished(); | 131 observer->WaitForFinished(); |
130 EXPECT_EQ( | 132 EXPECT_EQ( |
131 1u, observer->NumDownloadsSeenInState(DownloadItem::IN_PROGRESS)); | 133 1u, observer->NumDownloadsSeenInState(DownloadItem::IN_PROGRESS)); |
132 // We don't expect a select file dialog. | 134 // We don't expect a select file dialog. |
133 CHECK(!observer->select_file_dialog_seen()); | 135 CHECK(!observer->select_file_dialog_seen()); |
134 } | 136 } |
135 GetDownloadManager()->GetAllDownloads(FilePath(), items); | 137 GetDownloadManager()->GetAllDownloads(FilePath(), items); |
136 CHECK_EQ(count, items->size()); | 138 CHECK_EQ(count, items->size()); |
137 } | 139 } |
138 | 140 |
139 DownloadItem* CreateSlowTestDownload() { | 141 DownloadItem* CreateSlowTestDownload() { |
140 scoped_ptr<DownloadTestObserver> observer( | 142 scoped_ptr<DownloadTestObserver> observer( |
141 CreateInProgressDownloadObserver(1)); | 143 CreateInProgressDownloadObserver(1)); |
142 GURL slow_download_url(URLRequestSlowDownloadJob::kUnknownSizeUrl); | 144 GURL slow_download_url(URLRequestSlowDownloadJob::kUnknownSizeUrl); |
143 DownloadManager* manager = GetDownloadManager(); | 145 DownloadManager* manager = GetDownloadManager(); |
144 | 146 |
145 EXPECT_EQ(0, manager->InProgressCount()); | 147 EXPECT_EQ(0, manager->InProgressCount()); |
146 if (manager->InProgressCount() != 0) | 148 if (manager->InProgressCount() != 0) |
147 return NULL; | 149 return NULL; |
148 | 150 |
149 ui_test_utils::NavigateToURLWithDisposition( | 151 ui_test_utils::NavigateToURLWithDisposition( |
150 browser(), slow_download_url, CURRENT_TAB, | 152 current_browser(), slow_download_url, CURRENT_TAB, |
151 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 153 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
152 | 154 |
153 observer->WaitForFinished(); | 155 observer->WaitForFinished(); |
154 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::IN_PROGRESS)); | 156 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::IN_PROGRESS)); |
155 // We don't expect a select file dialog. | 157 // We don't expect a select file dialog. |
156 if (observer->select_file_dialog_seen()) | 158 if (observer->select_file_dialog_seen()) |
157 return NULL; | 159 return NULL; |
158 | 160 |
159 DownloadManager::DownloadVector items; | 161 DownloadManager::DownloadVector items; |
160 manager->GetAllDownloads(FilePath(), &items); | 162 manager->GetAllDownloads(FilePath(), &items); |
161 | 163 |
162 DownloadItem* new_item = NULL; | 164 DownloadItem* new_item = NULL; |
163 for (DownloadManager::DownloadVector::iterator iter = items.begin(); | 165 for (DownloadManager::DownloadVector::iterator iter = items.begin(); |
164 iter != items.end(); ++iter) { | 166 iter != items.end(); ++iter) { |
165 if ((*iter)->GetState() == DownloadItem::IN_PROGRESS) { | 167 if ((*iter)->GetState() == DownloadItem::IN_PROGRESS) { |
166 // There should be only one IN_PROGRESS item. | 168 // There should be only one IN_PROGRESS item. |
167 EXPECT_EQ(NULL, new_item); | 169 EXPECT_EQ(NULL, new_item); |
168 new_item = *iter; | 170 new_item = *iter; |
169 } | 171 } |
170 } | 172 } |
171 return new_item; | 173 return new_item; |
172 } | 174 } |
173 | 175 |
174 void FinishPendingSlowDownloads() { | 176 void FinishPendingSlowDownloads() { |
175 scoped_ptr<DownloadTestObserver> observer( | 177 scoped_ptr<DownloadTestObserver> observer( |
176 CreateDownloadObserver(1)); | 178 CreateDownloadObserver(1)); |
177 GURL finish_url(URLRequestSlowDownloadJob::kFinishDownloadUrl); | 179 GURL finish_url(URLRequestSlowDownloadJob::kFinishDownloadUrl); |
178 ui_test_utils::NavigateToURLWithDisposition( | 180 ui_test_utils::NavigateToURLWithDisposition( |
179 browser(), finish_url, NEW_FOREGROUND_TAB, | 181 current_browser(), finish_url, NEW_FOREGROUND_TAB, |
180 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 182 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
181 observer->WaitForFinished(); | 183 observer->WaitForFinished(); |
182 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); | 184 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
183 } | 185 } |
184 | 186 |
185 DownloadTestObserver* CreateDownloadObserver(size_t download_count) { | 187 DownloadTestObserver* CreateDownloadObserver(size_t download_count) { |
186 return new DownloadTestObserverTerminal( | 188 return new DownloadTestObserverTerminal( |
187 GetDownloadManager(), download_count, true, | 189 GetDownloadManager(), download_count, true, |
188 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); | 190 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
189 } | 191 } |
190 | 192 |
191 DownloadTestObserver* CreateInProgressDownloadObserver( | 193 DownloadTestObserver* CreateInProgressDownloadObserver( |
192 size_t download_count) { | 194 size_t download_count) { |
193 return new DownloadTestObserverInProgress(GetDownloadManager(), | 195 return new DownloadTestObserverInProgress(GetDownloadManager(), |
194 download_count, | 196 download_count, |
195 true); | 197 true); |
196 } | 198 } |
197 | 199 |
198 bool RunFunction(UIThreadExtensionFunction* function, | 200 bool RunFunction(UIThreadExtensionFunction* function, |
199 const std::string& args) { | 201 const std::string& args) { |
200 // extension_function_test_utils::RunFunction() does not take | 202 // extension_function_test_utils::RunFunction() does not take |
201 // ownership of |function|. | 203 // ownership of |function|. |
202 scoped_refptr<ExtensionFunction> function_owner(function); | 204 scoped_refptr<ExtensionFunction> function_owner(function); |
203 return extension_function_test_utils::RunFunction( | 205 return extension_function_test_utils::RunFunction( |
204 function, args, browser(), extension_function_test_utils::NONE); | 206 function, args, current_browser(), extension_function_test_utils::NONE); |
205 } | 207 } |
206 | 208 |
207 base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, | 209 base::Value* RunFunctionAndReturnResult(UIThreadExtensionFunction* function, |
208 const std::string& args) { | 210 const std::string& args) { |
209 return extension_function_test_utils::RunFunctionAndReturnResult( | 211 return extension_function_test_utils::RunFunctionAndReturnResult( |
210 function, args, browser(), extension_function_test_utils::NONE); | 212 function, args, current_browser(), extension_function_test_utils::NONE); |
211 } | 213 } |
212 | 214 |
213 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, | 215 std::string RunFunctionAndReturnError(UIThreadExtensionFunction* function, |
214 const std::string& args) { | 216 const std::string& args) { |
215 return extension_function_test_utils::RunFunctionAndReturnError( | 217 return extension_function_test_utils::RunFunctionAndReturnError( |
216 function, args, browser(), extension_function_test_utils::NONE); | 218 function, args, current_browser(), extension_function_test_utils::NONE); |
217 } | 219 } |
218 | 220 |
219 bool RunFunctionAndReturnString(UIThreadExtensionFunction* function, | 221 bool RunFunctionAndReturnString(UIThreadExtensionFunction* function, |
220 const std::string& args, | 222 const std::string& args, |
221 std::string* result_string) { | 223 std::string* result_string) { |
222 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(function, args)); | 224 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(function, args)); |
223 EXPECT_TRUE(result.get()); | 225 EXPECT_TRUE(result.get()); |
224 return result.get() && result->GetAsString(result_string); | 226 return result.get() && result->GetAsString(result_string); |
225 } | 227 } |
226 | 228 |
(...skipping 22 matching lines...) Expand all Loading... |
249 &width, &height)); | 251 &width, &height)); |
250 EXPECT_EQ(expected_size, width); | 252 EXPECT_EQ(expected_size, width); |
251 EXPECT_EQ(expected_size, height); | 253 EXPECT_EQ(expected_size, height); |
252 } | 254 } |
253 | 255 |
254 const FilePath& downloads_directory() { | 256 const FilePath& downloads_directory() { |
255 return downloads_directory_.path(); | 257 return downloads_directory_.path(); |
256 } | 258 } |
257 | 259 |
258 private: | 260 private: |
259 bool CreateAndSetDownloadsDirectory(Browser* browser) { | 261 void CreateAndSetDownloadsDirectory() { |
260 if (!downloads_directory_.CreateUniqueTempDir()) | 262 CHECK(downloads_directory_.CreateUniqueTempDir()); |
261 return false; | 263 current_browser()->profile()->GetPrefs()->SetFilePath( |
262 browser->profile()->GetPrefs()->SetFilePath( | |
263 prefs::kDownloadDefaultDirectory, | 264 prefs::kDownloadDefaultDirectory, |
264 downloads_directory_.path()); | 265 downloads_directory_.path()); |
265 return true; | |
266 } | 266 } |
267 | 267 |
268 ScopedTempDir downloads_directory_; | 268 ScopedTempDir downloads_directory_; |
269 }; | 269 }; |
270 | 270 |
| 271 class DownloadExtensionTestIncognito : public DownloadExtensionTest { |
| 272 public: |
| 273 virtual Browser* current_browser() OVERRIDE { return incognito_browser_; } |
| 274 |
| 275 virtual void SetUpOnMainThread() OVERRIDE { |
| 276 incognito_browser_ = CreateIncognitoBrowser(); |
| 277 CHECK(current_browser()->profile()->IsOffTheRecord()); |
| 278 DownloadExtensionTest::SetUpOnMainThread(); |
| 279 } |
| 280 |
| 281 private: |
| 282 Browser* incognito_browser_; |
| 283 }; |
| 284 |
271 class MockIconExtractorImpl : public DownloadFileIconExtractor { | 285 class MockIconExtractorImpl : public DownloadFileIconExtractor { |
272 public: | 286 public: |
273 MockIconExtractorImpl(const FilePath& path, IconLoader::IconSize icon_size, | 287 MockIconExtractorImpl(const FilePath& path, IconLoader::IconSize icon_size, |
274 const std::string& response) | 288 const std::string& response) |
275 : expected_path_(path), | 289 : expected_path_(path), |
276 expected_icon_size_(icon_size), | 290 expected_icon_size_(icon_size), |
277 response_(response) { | 291 response_(response) { |
278 } | 292 } |
279 virtual ~MockIconExtractorImpl() {} | 293 virtual ~MockIconExtractorImpl() {} |
280 | 294 |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 ASSERT_TRUE(result.get()); | 793 ASSERT_TRUE(result.get()); |
780 base::ListValue* result_list = NULL; | 794 base::ListValue* result_list = NULL; |
781 ASSERT_TRUE(result->GetAsList(&result_list)); | 795 ASSERT_TRUE(result->GetAsList(&result_list)); |
782 ASSERT_EQ(1UL, result_list->GetSize()); | 796 ASSERT_EQ(1UL, result_list->GetSize()); |
783 base::DictionaryValue* item_value = NULL; | 797 base::DictionaryValue* item_value = NULL; |
784 ASSERT_TRUE(result_list->GetDictionary(0, &item_value)); | 798 ASSERT_TRUE(result_list->GetDictionary(0, &item_value)); |
785 FilePath::StringType item_name; | 799 FilePath::StringType item_name; |
786 ASSERT_TRUE(item_value->GetString("filename", &item_name)); | 800 ASSERT_TRUE(item_value->GetString("filename", &item_name)); |
787 ASSERT_EQ(items[2]->GetFullPath().value(), item_name); | 801 ASSERT_EQ(items[2]->GetFullPath().value(), item_name); |
788 } | 802 } |
| 803 |
| 804 IN_PROC_BROWSER_TEST_F(DownloadExtensionTestIncognito, |
| 805 DownloadsApi_SearchIncognitoOnly) { |
| 806 ScopedCancellingItem item(CreateSlowTestDownload()); |
| 807 ASSERT_TRUE(item.get()); |
| 808 |
| 809 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
| 810 new DownloadsSearchFunction(), "[{\"incognito\": \"only\"}]")); |
| 811 ASSERT_TRUE(result.get()); |
| 812 base::ListValue* result_list = NULL; |
| 813 ASSERT_TRUE(result->GetAsList(&result_list)); |
| 814 ASSERT_EQ(1UL, result_list->GetSize()); |
| 815 } |
| 816 |
| 817 IN_PROC_BROWSER_TEST_F(DownloadExtensionTestIncognito, |
| 818 DownloadsApi_SearchIncognitoNone) { |
| 819 ScopedCancellingItem item(CreateSlowTestDownload()); |
| 820 ASSERT_TRUE(item.get()); |
| 821 |
| 822 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( |
| 823 new DownloadsSearchFunction(), "[{\"incognito\": \"none\"}]")); |
| 824 ASSERT_TRUE(result.get()); |
| 825 base::ListValue* result_list = NULL; |
| 826 ASSERT_TRUE(result->GetAsList(&result_list)); |
| 827 ASSERT_EQ(0UL, result_list->GetSize()); |
| 828 } |
OLD | NEW |