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 "base/auto_reset.h" | 5 #include "base/auto_reset.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/download/download_item_model.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/webui/downloads_dom_handler.h" | 13 #include "chrome/browser/ui/webui/downloads_dom_handler.h" |
13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
16 #include "content/public/test/mock_download_item.h" | 17 #include "content/public/test/mock_download_item.h" |
17 #include "content/public/test/mock_download_manager.h" | 18 #include "content/public/test/mock_download_manager.h" |
18 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
19 | 20 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 79 |
79 void ForceSendCurrentDownloads() { | 80 void ForceSendCurrentDownloads() { |
80 ScheduleSendCurrentDownloads(); | 81 ScheduleSendCurrentDownloads(); |
81 } | 82 } |
82 | 83 |
83 void reset_downloads_list() { downloads_list_.reset(); } | 84 void reset_downloads_list() { downloads_list_.reset(); } |
84 void reset_download_updated() { download_updated_.reset(); } | 85 void reset_download_updated() { download_updated_.reset(); } |
85 | 86 |
86 void set_manager(content::DownloadManager* manager) { manager_ = manager; } | 87 void set_manager(content::DownloadManager* manager) { manager_ = manager; } |
87 | 88 |
| 89 using DownloadsDOMHandler::FinalizeRemovals; |
| 90 |
88 protected: | 91 protected: |
89 content::WebContents* GetWebUIWebContents() override { return NULL; } | 92 content::WebContents* GetWebUIWebContents() override { return NULL; } |
90 | 93 |
91 void CallDownloadsList(const base::ListValue& downloads) override { | 94 void CallDownloadsList(const base::ListValue& downloads) override { |
92 downloads_list_.reset(downloads.DeepCopy()); | 95 downloads_list_.reset(downloads.DeepCopy()); |
93 if (waiting_list_) { | 96 if (waiting_list_) { |
94 content::BrowserThread::PostTask(content::BrowserThread::UI, | 97 content::BrowserThread::PostTask(content::BrowserThread::UI, |
95 FROM_HERE, | 98 FROM_HERE, |
96 base::MessageLoop::QuitClosure()); | 99 base::MessageLoop::QuitClosure()); |
97 } | 100 } |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 EXPECT_CALL(item, GetState()).WillRepeatedly( | 243 EXPECT_CALL(item, GetState()).WillRepeatedly( |
241 testing::Return(content::DownloadItem::IN_PROGRESS)); | 244 testing::Return(content::DownloadItem::IN_PROGRESS)); |
242 EXPECT_CALL(item, UpdateObservers()).Times(0); | 245 EXPECT_CALL(item, UpdateObservers()).Times(0); |
243 | 246 |
244 std::vector<content::DownloadItem*> items; | 247 std::vector<content::DownloadItem*> items; |
245 items.push_back(&item); | 248 items.push_back(&item); |
246 EXPECT_CALL(manager, GetAllDownloads(testing::_)).WillOnce( | 249 EXPECT_CALL(manager, GetAllDownloads(testing::_)).WillOnce( |
247 testing::SetArgPointee<0>(items)); | 250 testing::SetArgPointee<0>(items)); |
248 | 251 |
249 mock_handler_->HandleClearAll(NULL); | 252 mock_handler_->HandleClearAll(NULL); |
| 253 EXPECT_TRUE(DownloadItemModel(&item).ShouldShowInShelf()); |
250 } | 254 } |
251 | 255 |
252 // Tests that DownloadsDOMHandler detects new downloads and relays them to the | 256 // Tests that DownloadsDOMHandler detects new downloads and relays them to the |
253 // renderer. | 257 // renderer. |
254 // crbug.com/159390: This test fails when daylight savings time ends. | 258 // crbug.com/159390: This test fails when daylight savings time ends. |
255 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, DownloadsRelayed) { | 259 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, DownloadsRelayed) { |
256 DownloadAnItem(); | 260 DownloadAnItem(); |
257 | 261 |
258 mock_handler_->WaitForDownloadUpdated(); | 262 mock_handler_->WaitForDownloadUpdated(); |
259 ASSERT_EQ(1, static_cast<int>(mock_handler_->download_updated()->GetSize())); | 263 ASSERT_EQ(1, static_cast<int>(mock_handler_->download_updated()->GetSize())); |
260 EXPECT_TRUE(ListMatches( | 264 EXPECT_TRUE(ListMatches( |
261 mock_handler_->download_updated(), | 265 mock_handler_->download_updated(), |
262 "[{\"file_externally_removed\": true," | 266 "[{\"file_externally_removed\": true," |
263 " \"id\": \"1\"}]")); | 267 " \"id\": \"1\"}]")); |
264 | 268 |
265 mock_handler_->reset_downloads_list(); | 269 mock_handler_->reset_downloads_list(); |
266 browser()->profile()->GetPrefs()->SetBoolean( | 270 browser()->profile()->GetPrefs()->SetBoolean( |
267 prefs::kAllowDeletingBrowserHistory, true); | 271 prefs::kAllowDeletingBrowserHistory, true); |
268 mock_handler_->HandleClearAll(NULL); | 272 mock_handler_->HandleClearAll(NULL); |
269 mock_handler_->WaitForDownloadsList(); | 273 mock_handler_->WaitForDownloadsList(); |
270 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | 274 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
271 } | 275 } |
272 | 276 |
| 277 // Tests that DownloadsDOMHandler actually calls DownloadItem::Remove() when |
| 278 // it's closed (and removals can no longer be undone). |
| 279 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveCalledOnPageClose) { |
| 280 content::MockDownloadManager manager; |
| 281 mock_handler_->set_manager(&manager); |
| 282 |
| 283 content::MockDownloadItem item; |
| 284 EXPECT_CALL(item, GetId()).WillRepeatedly(testing::Return(1)); |
| 285 EXPECT_CALL(item, GetState()).WillRepeatedly( |
| 286 testing::Return(content::DownloadItem::COMPLETE)); |
| 287 |
| 288 DownloadItemModel model(&item); |
| 289 EXPECT_TRUE(model.ShouldShowInShelf()); |
| 290 |
| 291 EXPECT_CALL(manager, GetDownload(1)).WillRepeatedly(testing::Return(&item)); |
| 292 |
| 293 base::ListValue remove; |
| 294 remove.AppendString("1"); |
| 295 EXPECT_CALL(item, UpdateObservers()).Times(1); |
| 296 mock_handler_->HandleRemove(&remove); |
| 297 EXPECT_FALSE(model.ShouldShowInShelf()); |
| 298 |
| 299 EXPECT_CALL(item, Remove()).Times(1); |
| 300 // Call |mock_handler_->FinalizeRemovals()| instead of |mock_handler_.reset()| |
| 301 // because the vtable is affected during destruction and the fake manager |
| 302 // rigging doesn't work. |
| 303 mock_handler_->FinalizeRemovals(); |
| 304 } |
273 | 305 |
274 // TODO(benjhayden): Test the extension downloads filter for both | 306 // TODO(benjhayden): Test the extension downloads filter for both |
275 // mock_handler_.downloads_list() and mock_handler_.download_updated(). | 307 // mock_handler_.downloads_list() and mock_handler_.download_updated(). |
276 | 308 |
277 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record | 309 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record |
278 // calls can't access off-record items. | 310 // calls can't access off-record items. |
279 | 311 |
280 // TODO(benjhayden): Test that bad download ids incoming from the javascript are | 312 // TODO(benjhayden): Test that bad download ids incoming from the javascript are |
281 // dropped on the floor. | 313 // dropped on the floor. |
282 | 314 |
283 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. | 315 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. |
284 | 316 |
285 // TODO(benjhayden): Test that RemoveObserver is called on all download items, | 317 // TODO(benjhayden): Test that RemoveObserver is called on all download items, |
286 // including items that crossed IsTemporary() and back. | 318 // including items that crossed IsTemporary() and back. |
OLD | NEW |