| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <stddef.h> | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/json/json_reader.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/values.h" | |
| 12 #include "chrome/browser/download/download_item_model.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/browser/ui/webui/downloads_dom_handler.h" | |
| 16 #include "chrome/common/pref_names.h" | |
| 17 #include "chrome/test/base/in_process_browser_test.h" | |
| 18 #include "components/prefs/pref_service.h" | |
| 19 #include "content/public/browser/web_contents.h" | |
| 20 #include "content/public/test/mock_download_item.h" | |
| 21 #include "content/public/test/mock_download_manager.h" | |
| 22 #include "content/public/test/test_utils.h" | |
| 23 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Reads |right_json| into a ListValue |left_list|; returns true if all | |
| 28 // key-value pairs in in all dictionaries in |right_list| are also in the | |
| 29 // corresponding dictionary in |left_list|. Ignores keys in dictionaries in | |
| 30 // |left_list| that are not in the corresponding dictionary in |right_list|. | |
| 31 bool ListMatches(base::ListValue* left_list, const std::string& right_json) { | |
| 32 scoped_ptr<base::Value> right_value = base::JSONReader::Read(right_json); | |
| 33 base::ListValue* right_list = NULL; | |
| 34 CHECK(right_value->GetAsList(&right_list)); | |
| 35 for (size_t i = 0; i < left_list->GetSize(); ++i) { | |
| 36 base::DictionaryValue* left_dict = NULL; | |
| 37 base::DictionaryValue* right_dict = NULL; | |
| 38 CHECK(left_list->GetDictionary(i, &left_dict)); | |
| 39 CHECK(right_list->GetDictionary(i, &right_dict)); | |
| 40 for (base::DictionaryValue::Iterator iter(*right_dict); | |
| 41 !iter.IsAtEnd(); iter.Advance()) { | |
| 42 base::Value* left_value = NULL; | |
| 43 if (left_dict->HasKey(iter.key()) && | |
| 44 left_dict->Get(iter.key(), &left_value) && | |
| 45 !iter.value().Equals(left_value)) { | |
| 46 LOG(WARNING) << "key \"" << iter.key() << "\" doesn't match (" | |
| 47 << iter.value() << " vs. " << *left_value << ")"; | |
| 48 return false; | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 // A |DownloadsDOMHandler| that doesn't use a real WebUI object, but is real in | |
| 56 // all other respects. | |
| 57 class MockDownloadsDOMHandler : public DownloadsDOMHandler { | |
| 58 public: | |
| 59 explicit MockDownloadsDOMHandler(content::DownloadManager* download_manager) | |
| 60 : DownloadsDOMHandler(download_manager), | |
| 61 waiting_list_(false), | |
| 62 waiting_updated_(false) { | |
| 63 } | |
| 64 ~MockDownloadsDOMHandler() override {} | |
| 65 | |
| 66 base::ListValue* downloads_list() { return downloads_list_.get(); } | |
| 67 base::DictionaryValue* download_updated() { return download_updated_.get(); } | |
| 68 | |
| 69 void WaitForDownloadsList() { | |
| 70 if (downloads_list_) | |
| 71 return; | |
| 72 base::AutoReset<bool> reset_waiting(&waiting_list_, true); | |
| 73 content::RunMessageLoop(); | |
| 74 } | |
| 75 | |
| 76 void WaitForDownloadUpdated() { | |
| 77 if (download_updated_) | |
| 78 return; | |
| 79 base::AutoReset<bool> reset_waiting(&waiting_updated_, true); | |
| 80 content::RunMessageLoop(); | |
| 81 } | |
| 82 | |
| 83 void ForceSendCurrentDownloads() { | |
| 84 ScheduleSendCurrentDownloads(); | |
| 85 } | |
| 86 | |
| 87 void reset_downloads_list() { downloads_list_.reset(); } | |
| 88 void reset_download_updated() { download_updated_.reset(); } | |
| 89 | |
| 90 using DownloadsDOMHandler::FinalizeRemovals; | |
| 91 | |
| 92 protected: | |
| 93 content::WebContents* GetWebUIWebContents() override { return NULL; } | |
| 94 | |
| 95 void CallUpdateAll(const base::ListValue& list) override { | |
| 96 downloads_list_.reset(list.DeepCopy()); | |
| 97 if (waiting_list_) { | |
| 98 content::BrowserThread::PostTask( | |
| 99 content::BrowserThread::UI, FROM_HERE, | |
| 100 base::MessageLoop::QuitWhenIdleClosure()); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void CallUpdateItem(const base::DictionaryValue& item) override { | |
| 105 download_updated_.reset(item.DeepCopy()); | |
| 106 if (waiting_updated_) { | |
| 107 content::BrowserThread::PostTask( | |
| 108 content::BrowserThread::UI, FROM_HERE, | |
| 109 base::MessageLoop::QuitWhenIdleClosure()); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 scoped_ptr<base::ListValue> downloads_list_; | |
| 115 scoped_ptr<base::DictionaryValue> download_updated_; | |
| 116 bool waiting_list_; | |
| 117 bool waiting_updated_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(MockDownloadsDOMHandler); | |
| 120 }; | |
| 121 | |
| 122 } // namespace | |
| 123 | |
| 124 class DownloadsDOMHandlerTest : public InProcessBrowserTest { | |
| 125 public: | |
| 126 DownloadsDOMHandlerTest() {} | |
| 127 | |
| 128 ~DownloadsDOMHandlerTest() override {} | |
| 129 | |
| 130 void SetUpOnMainThread() override { | |
| 131 mock_handler_.reset(new MockDownloadsDOMHandler(download_manager())); | |
| 132 CHECK(downloads_directory_.CreateUniqueTempDir()); | |
| 133 browser()->profile()->GetPrefs()->SetFilePath( | |
| 134 prefs::kDownloadDefaultDirectory, | |
| 135 downloads_directory_.path()); | |
| 136 CHECK(embedded_test_server()->Start()); | |
| 137 mock_handler_->HandleGetDownloads(nullptr); | |
| 138 } | |
| 139 | |
| 140 content::DownloadManager* download_manager() { | |
| 141 return content::BrowserContext::GetDownloadManager(browser()->profile()); | |
| 142 } | |
| 143 | |
| 144 void DownloadAnItem() { | |
| 145 GURL url = embedded_test_server()->GetURL("/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( | |
| 150 1, // id | |
| 151 base::FilePath(FILE_PATH_LITERAL("/path/to/file")), | |
| 152 base::FilePath(FILE_PATH_LITERAL("/path/to/file")), | |
| 153 url_chain, | |
| 154 GURL(std::string()), | |
| 155 "application/octet-stream", | |
| 156 "application/octet-stream", | |
| 157 current, | |
| 158 current, | |
| 159 std::string(), | |
| 160 std::string(), | |
| 161 128, | |
| 162 128, | |
| 163 content::DownloadItem::COMPLETE, | |
| 164 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 165 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 166 false); | |
| 167 | |
| 168 mock_handler_->ForceSendCurrentDownloads(); | |
| 169 mock_handler_->WaitForDownloadsList(); | |
| 170 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | |
| 171 EXPECT_TRUE(ListMatches( | |
| 172 mock_handler_->downloads_list(), | |
| 173 "[{\"file_externally_removed\": false," | |
| 174 " \"file_name\": \"file\"," | |
| 175 " \"id\": \"1\"," | |
| 176 " \"otr\": false," | |
| 177 " \"since_string\": \"Today\"," | |
| 178 " \"state\": \"COMPLETE\"," | |
| 179 " \"total\": 128}]")); | |
| 180 } | |
| 181 | |
| 182 protected: | |
| 183 scoped_ptr<MockDownloadsDOMHandler> mock_handler_; | |
| 184 | |
| 185 private: | |
| 186 base::ScopedTempDir downloads_directory_; | |
| 187 | |
| 188 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandlerTest); | |
| 189 }; | |
| 190 | |
| 191 // Tests removing all items, both when prohibited and when allowed. | |
| 192 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveAll) { | |
| 193 DownloadAnItem(); | |
| 194 | |
| 195 mock_handler_->reset_downloads_list(); | |
| 196 browser()->profile()->GetPrefs()->SetBoolean( | |
| 197 prefs::kAllowDeletingBrowserHistory, false); | |
| 198 mock_handler_->HandleClearAll(NULL); | |
| 199 // Attempting to clear all shouldn't do anything when deletion is disabled. | |
| 200 mock_handler_->ForceSendCurrentDownloads(); | |
| 201 mock_handler_->WaitForDownloadsList(); | |
| 202 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | |
| 203 | |
| 204 mock_handler_->reset_downloads_list(); | |
| 205 browser()->profile()->GetPrefs()->SetBoolean( | |
| 206 prefs::kAllowDeletingBrowserHistory, true); | |
| 207 mock_handler_->HandleClearAll(NULL); | |
| 208 mock_handler_->WaitForDownloadsList(); | |
| 209 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | |
| 210 } | |
| 211 | |
| 212 // Tests removing one item, both when prohibited and when allowed. | |
| 213 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveOneItem) { | |
| 214 DownloadAnItem(); | |
| 215 base::ListValue item; | |
| 216 item.AppendString("1"); | |
| 217 | |
| 218 mock_handler_->reset_downloads_list(); | |
| 219 browser()->profile()->GetPrefs()->SetBoolean( | |
| 220 prefs::kAllowDeletingBrowserHistory, false); | |
| 221 mock_handler_->HandleRemove(&item); | |
| 222 // Removing an item only sends the new download list if anything was actually | |
| 223 // removed, so force it. | |
| 224 mock_handler_->ForceSendCurrentDownloads(); | |
| 225 mock_handler_->WaitForDownloadsList(); | |
| 226 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | |
| 227 | |
| 228 mock_handler_->reset_downloads_list(); | |
| 229 browser()->profile()->GetPrefs()->SetBoolean( | |
| 230 prefs::kAllowDeletingBrowserHistory, true); | |
| 231 mock_handler_->HandleRemove(&item); | |
| 232 mock_handler_->WaitForDownloadsList(); | |
| 233 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | |
| 234 } | |
| 235 | |
| 236 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, ClearAllSkipsInProgress) { | |
| 237 testing::NiceMock<content::MockDownloadManager> manager; | |
| 238 EXPECT_CALL(manager, GetBrowserContext()).WillRepeatedly( | |
| 239 testing::Return(browser()->profile())); | |
| 240 mock_handler_.reset(new MockDownloadsDOMHandler(&manager)); | |
| 241 mock_handler_->HandleGetDownloads(nullptr); | |
| 242 | |
| 243 content::MockDownloadItem item; | |
| 244 EXPECT_CALL(item, GetState()).WillRepeatedly( | |
| 245 testing::Return(content::DownloadItem::IN_PROGRESS)); | |
| 246 EXPECT_CALL(item, UpdateObservers()).Times(0); | |
| 247 | |
| 248 std::vector<content::DownloadItem*> items; | |
| 249 items.push_back(&item); | |
| 250 EXPECT_CALL(manager, GetAllDownloads(testing::_)).WillOnce( | |
| 251 testing::SetArgPointee<0>(items)); | |
| 252 | |
| 253 mock_handler_->HandleClearAll(NULL); | |
| 254 EXPECT_TRUE(DownloadItemModel(&item).ShouldShowInShelf()); | |
| 255 | |
| 256 mock_handler_.reset(); | |
| 257 } | |
| 258 | |
| 259 // Tests that DownloadsDOMHandler detects new downloads and relays them to the | |
| 260 // renderer. | |
| 261 // crbug.com/159390: This test fails when daylight savings time ends. | |
| 262 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, DownloadsRelayed) { | |
| 263 DownloadAnItem(); | |
| 264 | |
| 265 mock_handler_->WaitForDownloadUpdated(); | |
| 266 const base::DictionaryValue* update = mock_handler_->download_updated(); | |
| 267 ASSERT_TRUE(update); | |
| 268 | |
| 269 bool removed; | |
| 270 ASSERT_TRUE(update->GetBoolean("file_externally_removed", &removed)); | |
| 271 EXPECT_TRUE(removed); | |
| 272 | |
| 273 std::string id; | |
| 274 ASSERT_TRUE(update->GetString("id", &id)); | |
| 275 EXPECT_EQ("1", id); | |
| 276 | |
| 277 mock_handler_->reset_downloads_list(); | |
| 278 browser()->profile()->GetPrefs()->SetBoolean( | |
| 279 prefs::kAllowDeletingBrowserHistory, true); | |
| 280 mock_handler_->HandleClearAll(NULL); | |
| 281 mock_handler_->WaitForDownloadsList(); | |
| 282 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | |
| 283 } | |
| 284 | |
| 285 // Tests that DownloadsDOMHandler actually calls DownloadItem::Remove() when | |
| 286 // it's closed (and removals can no longer be undone). | |
| 287 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveCalledOnPageClose) { | |
| 288 testing::NiceMock<content::MockDownloadManager> manager; | |
| 289 EXPECT_CALL(manager, GetBrowserContext()).WillRepeatedly( | |
| 290 testing::Return(browser()->profile())); | |
| 291 mock_handler_.reset(new MockDownloadsDOMHandler(&manager)); | |
| 292 mock_handler_->HandleGetDownloads(nullptr); | |
| 293 | |
| 294 content::MockDownloadItem item; | |
| 295 EXPECT_CALL(item, GetId()).WillRepeatedly(testing::Return(1)); | |
| 296 EXPECT_CALL(item, GetState()).WillRepeatedly( | |
| 297 testing::Return(content::DownloadItem::COMPLETE)); | |
| 298 | |
| 299 DownloadItemModel model(&item); | |
| 300 EXPECT_TRUE(model.ShouldShowInShelf()); | |
| 301 | |
| 302 EXPECT_CALL(manager, GetDownload(1)).WillRepeatedly(testing::Return(&item)); | |
| 303 | |
| 304 base::ListValue remove; | |
| 305 remove.AppendString("1"); | |
| 306 EXPECT_CALL(item, UpdateObservers()).Times(1); | |
| 307 mock_handler_->HandleRemove(&remove); | |
| 308 EXPECT_FALSE(model.ShouldShowInShelf()); | |
| 309 | |
| 310 EXPECT_CALL(item, Remove()).Times(1); | |
| 311 // Call |mock_handler_->FinalizeRemovals()| instead of |mock_handler_.reset()| | |
| 312 // because the vtable is affected during destruction and the fake manager | |
| 313 // rigging doesn't work. | |
| 314 mock_handler_->FinalizeRemovals(); | |
| 315 mock_handler_.reset(); | |
| 316 } | |
| 317 | |
| 318 // TODO(benjhayden): Test the extension downloads filter for both | |
| 319 // mock_handler_.downloads_list() and mock_handler_.download_updated(). | |
| 320 | |
| 321 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record | |
| 322 // calls can't access off-record items. | |
| 323 | |
| 324 // TODO(benjhayden): Test that bad download ids incoming from the javascript are | |
| 325 // dropped on the floor. | |
| 326 | |
| 327 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. | |
| 328 | |
| 329 // TODO(benjhayden): Test that RemoveObserver is called on all download items, | |
| 330 // including items that crossed IsTemporary() and back. | |
| OLD | NEW |