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
3 // found in the LICENSE file.
5 #include "base/auto_reset.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/json/json_reader.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/webui/downloads_dom_handler.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/mock_download_item.h"
17 #include "content/public/test/mock_download_manager.h"
18 #include "content/public/test/test_utils.h"
22 // Reads |right_json| into a ListValue |left_list|; returns true if all
23 // key-value pairs in in all dictionaries in |right_list| are also in the
24 // corresponding dictionary in |left_list|. Ignores keys in dictionaries in
25 // |left_list| that are not in the corresponding dictionary in |right_list|.
26 bool ListMatches(base::ListValue
* left_list
, const std::string
& right_json
) {
27 scoped_ptr
<base::Value
> right_value(base::JSONReader::Read(right_json
));
28 base::ListValue
* right_list
= NULL
;
29 CHECK(right_value
->GetAsList(&right_list
));
30 for (size_t i
= 0; i
< left_list
->GetSize(); ++i
) {
31 base::DictionaryValue
* left_dict
= NULL
;
32 base::DictionaryValue
* right_dict
= NULL
;
33 CHECK(left_list
->GetDictionary(i
, &left_dict
));
34 CHECK(right_list
->GetDictionary(i
, &right_dict
));
35 for (base::DictionaryValue::Iterator
iter(*right_dict
);
36 !iter
.IsAtEnd(); iter
.Advance()) {
37 base::Value
* left_value
= NULL
;
38 if (left_dict
->HasKey(iter
.key()) &&
39 left_dict
->Get(iter
.key(), &left_value
) &&
40 !iter
.value().Equals(left_value
)) {
41 LOG(WARNING
) << "key \"" << iter
.key() << "\" doesn't match ("
42 << iter
.value() << " vs. " << *left_value
<< ")";
50 // A |DownloadsDOMHandler| that doesn't use a real WebUI object, but is real in
51 // all other respects.
52 class MockDownloadsDOMHandler
: public DownloadsDOMHandler
{
54 explicit MockDownloadsDOMHandler(content::DownloadManager
* dlm
)
55 : DownloadsDOMHandler(dlm
),
57 waiting_updated_(false),
60 ~MockDownloadsDOMHandler() override
{}
62 base::ListValue
* downloads_list() { return downloads_list_
.get(); }
63 base::ListValue
* download_updated() { return download_updated_
.get(); }
65 void WaitForDownloadsList() {
66 if (downloads_list_
.get())
68 base::AutoReset
<bool> reset_waiting(&waiting_list_
, true);
69 content::RunMessageLoop();
72 void WaitForDownloadUpdated() {
73 if (download_updated_
.get())
75 base::AutoReset
<bool> reset_waiting(&waiting_updated_
, true);
76 content::RunMessageLoop();
79 void ForceSendCurrentDownloads() {
80 ScheduleSendCurrentDownloads();
83 void reset_downloads_list() { downloads_list_
.reset(); }
84 void reset_download_updated() { download_updated_
.reset(); }
86 void set_manager(content::DownloadManager
* manager
) { manager_
= manager
; }
89 content::WebContents
* GetWebUIWebContents() override
{ return NULL
; }
91 void CallDownloadsList(const base::ListValue
& downloads
) override
{
92 downloads_list_
.reset(downloads
.DeepCopy());
94 content::BrowserThread::PostTask(content::BrowserThread::UI
,
96 base::MessageLoop::QuitClosure());
100 void CallDownloadUpdated(const base::ListValue
& download
) override
{
101 download_updated_
.reset(download
.DeepCopy());
102 if (waiting_updated_
) {
103 content::BrowserThread::PostTask(content::BrowserThread::UI
,
105 base::MessageLoop::QuitClosure());
109 content::DownloadManager
* GetMainNotifierManager() override
{
110 return manager_
? manager_
: DownloadsDOMHandler::GetMainNotifierManager();
114 scoped_ptr
<base::ListValue
> downloads_list_
;
115 scoped_ptr
<base::ListValue
> download_updated_
;
117 bool waiting_updated_
;
118 content::DownloadManager
* manager_
; // weak.
120 DISALLOW_COPY_AND_ASSIGN(MockDownloadsDOMHandler
);
125 class DownloadsDOMHandlerTest
: public InProcessBrowserTest
{
127 DownloadsDOMHandlerTest() {}
129 ~DownloadsDOMHandlerTest() override
{}
131 void SetUpOnMainThread() override
{
132 mock_handler_
.reset(new MockDownloadsDOMHandler(download_manager()));
133 CHECK(downloads_directory_
.CreateUniqueTempDir());
134 browser()->profile()->GetPrefs()->SetFilePath(
135 prefs::kDownloadDefaultDirectory
,
136 downloads_directory_
.path());
137 CHECK(test_server()->Start());
140 content::DownloadManager
* download_manager() {
141 return content::BrowserContext::GetDownloadManager(browser()->profile());
144 void DownloadAnItem() {
145 GURL url
= test_server()->GetURL("files/downloads/image.jpg");
146 std::vector
<GURL
> url_chain
;
147 url_chain
.push_back(url
);
148 base::Time
current(base::Time::Now());
149 download_manager()->CreateDownloadItem(
151 base::FilePath(FILE_PATH_LITERAL("/path/to/file")),
152 base::FilePath(FILE_PATH_LITERAL("/path/to/file")),
155 "application/octet-stream",
156 "application/octet-stream",
163 content::DownloadItem::COMPLETE
,
164 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
,
165 content::DOWNLOAD_INTERRUPT_REASON_NONE
,
168 mock_handler_
->WaitForDownloadsList();
169 ASSERT_EQ(1, static_cast<int>(mock_handler_
->downloads_list()->GetSize()));
170 EXPECT_TRUE(ListMatches(
171 mock_handler_
->downloads_list(),
172 "[{\"file_externally_removed\": false,"
173 " \"file_name\": \"file\","
176 " \"since_string\": \"Today\","
177 " \"state\": \"COMPLETE\","
178 " \"total\": 128}]"));
182 scoped_ptr
<MockDownloadsDOMHandler
> mock_handler_
;
185 base::ScopedTempDir downloads_directory_
;
187 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandlerTest
);
190 // Tests removing all items, both when prohibited and when allowed.
191 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest
, RemoveAll
) {
194 mock_handler_
->reset_downloads_list();
195 browser()->profile()->GetPrefs()->SetBoolean(
196 prefs::kAllowDeletingBrowserHistory
, false);
197 mock_handler_
->HandleClearAll(NULL
);
198 // Attempting to clear all shouldn't do anything when deletion is disabled.
199 mock_handler_
->ForceSendCurrentDownloads();
200 mock_handler_
->WaitForDownloadsList();
201 ASSERT_EQ(1, static_cast<int>(mock_handler_
->downloads_list()->GetSize()));
203 mock_handler_
->reset_downloads_list();
204 browser()->profile()->GetPrefs()->SetBoolean(
205 prefs::kAllowDeletingBrowserHistory
, true);
206 mock_handler_
->HandleClearAll(NULL
);
207 mock_handler_
->WaitForDownloadsList();
208 EXPECT_EQ(0, static_cast<int>(mock_handler_
->downloads_list()->GetSize()));
211 // Tests removing one item, both when prohibited and when allowed.
212 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest
, RemoveOneItem
) {
214 base::ListValue item
;
215 item
.AppendString("1");
217 mock_handler_
->reset_downloads_list();
218 browser()->profile()->GetPrefs()->SetBoolean(
219 prefs::kAllowDeletingBrowserHistory
, false);
220 mock_handler_
->HandleRemove(&item
);
221 // Removing an item only sends the new download list if anything was actually
222 // removed, so force it.
223 mock_handler_
->ForceSendCurrentDownloads();
224 mock_handler_
->WaitForDownloadsList();
225 ASSERT_EQ(1, static_cast<int>(mock_handler_
->downloads_list()->GetSize()));
227 mock_handler_
->reset_downloads_list();
228 browser()->profile()->GetPrefs()->SetBoolean(
229 prefs::kAllowDeletingBrowserHistory
, true);
230 mock_handler_
->HandleRemove(&item
);
231 mock_handler_
->WaitForDownloadsList();
232 EXPECT_EQ(0, static_cast<int>(mock_handler_
->downloads_list()->GetSize()));
235 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest
, ClearAllSkipsInProgress
) {
236 content::MockDownloadManager manager
;
237 mock_handler_
->set_manager(&manager
);
239 content::MockDownloadItem item
;
240 EXPECT_CALL(item
, GetState()).WillRepeatedly(
241 testing::Return(content::DownloadItem::IN_PROGRESS
));
242 EXPECT_CALL(item
, UpdateObservers()).Times(0);
244 std::vector
<content::DownloadItem
*> items
;
245 items
.push_back(&item
);
246 EXPECT_CALL(manager
, GetAllDownloads(testing::_
)).WillOnce(
247 testing::SetArgPointee
<0>(items
));
249 mock_handler_
->HandleClearAll(NULL
);
252 // Tests that DownloadsDOMHandler detects new downloads and relays them to the
254 // crbug.com/159390: This test fails when daylight savings time ends.
255 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest
, DownloadsRelayed
) {
258 mock_handler_
->WaitForDownloadUpdated();
259 ASSERT_EQ(1, static_cast<int>(mock_handler_
->download_updated()->GetSize()));
260 EXPECT_TRUE(ListMatches(
261 mock_handler_
->download_updated(),
262 "[{\"file_externally_removed\": true,"
263 " \"id\": \"1\"}]"));
265 mock_handler_
->reset_downloads_list();
266 browser()->profile()->GetPrefs()->SetBoolean(
267 prefs::kAllowDeletingBrowserHistory
, true);
268 mock_handler_
->HandleClearAll(NULL
);
269 mock_handler_
->WaitForDownloadsList();
270 EXPECT_EQ(0, static_cast<int>(mock_handler_
->downloads_list()->GetSize()));
274 // TODO(benjhayden): Test the extension downloads filter for both
275 // mock_handler_.downloads_list() and mock_handler_.download_updated().
277 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record
278 // calls can't access off-record items.
280 // TODO(benjhayden): Test that bad download ids incoming from the javascript are
281 // dropped on the floor.
283 // TODO(benjhayden): Test that IsTemporary() downloads are not shown.
285 // TODO(benjhayden): Test that RemoveObserver is called on all download items,
286 // including items that crossed IsTemporary() and back.