| 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/history/download_row.h" | 10 #include "chrome/browser/history/download_row.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 browser()->profile()->GetPrefs()->SetFilePath( | 125 browser()->profile()->GetPrefs()->SetFilePath( |
| 126 prefs::kDownloadDefaultDirectory, | 126 prefs::kDownloadDefaultDirectory, |
| 127 downloads_directory_.path()); | 127 downloads_directory_.path()); |
| 128 CHECK(test_server()->Start()); | 128 CHECK(test_server()->Start()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 content::DownloadManager* download_manager() { | 131 content::DownloadManager* download_manager() { |
| 132 return content::BrowserContext::GetDownloadManager(browser()->profile()); | 132 return content::BrowserContext::GetDownloadManager(browser()->profile()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void DownloadAnItem() { | 135 void DownloadAnItem(bool in_progress) { |
| 136 GURL url = test_server()->GetURL("files/downloads/image.jpg"); | 136 GURL url = test_server()->GetURL("files/downloads/image.jpg"); |
| 137 std::vector<GURL> url_chain; | 137 std::vector<GURL> url_chain; |
| 138 url_chain.push_back(url); | 138 url_chain.push_back(url); |
| 139 base::Time current(base::Time::Now()); | 139 base::Time current(base::Time::Now()); |
| 140 download_manager()->CreateDownloadItem( | 140 download_manager()->CreateDownloadItem( |
| 141 1, // id | 141 1, // id |
| 142 base::FilePath(FILE_PATH_LITERAL("/path/to/file")), | 142 base::FilePath(FILE_PATH_LITERAL("/path/to/file")), |
| 143 base::FilePath(FILE_PATH_LITERAL("/path/to/file")), | 143 base::FilePath(FILE_PATH_LITERAL("/path/to/file")), |
| 144 url_chain, | 144 url_chain, |
| 145 GURL(std::string()), | 145 GURL(std::string()), |
| 146 "application/octet-stream", | 146 "application/octet-stream", |
| 147 "application/octet-stream", | 147 "application/octet-stream", |
| 148 current, | 148 current, |
| 149 current, | 149 current, |
| 150 std::string(), | 150 std::string(), |
| 151 std::string(), | 151 std::string(), |
| 152 128, | 152 128, |
| 153 128, | 153 128, |
| 154 content::DownloadItem::COMPLETE, | 154 in_progress ? content::DownloadItem::IN_PROGRESS : |
| 155 content::DownloadItem::COMPLETE, |
| 155 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 156 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 156 content::DOWNLOAD_INTERRUPT_REASON_NONE, | 157 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 157 false); | 158 false); |
| 158 | 159 |
| 159 mock_handler_->WaitForDownloadsList(); | 160 mock_handler_->WaitForDownloadsList(); |
| 160 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | 161 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
| 162 std::string state = in_progress ? "IN_PROGRESS" : "COMPLETE"; |
| 161 EXPECT_TRUE(ListMatches( | 163 EXPECT_TRUE(ListMatches( |
| 162 mock_handler_->downloads_list(), | 164 mock_handler_->downloads_list(), |
| 163 "[{\"file_externally_removed\": false," | 165 "[{\"file_externally_removed\": false," |
| 164 " \"file_name\": \"file\"," | 166 " \"file_name\": \"file\"," |
| 165 " \"id\": \"1\"," | 167 " \"id\": \"1\"," |
| 166 " \"otr\": false," | 168 " \"otr\": false," |
| 167 " \"since_string\": \"Today\"," | 169 " \"since_string\": \"Today\"," |
| 168 " \"state\": \"COMPLETE\"," | 170 " \"state\": \"" + state + "\"," |
| 169 " \"total\": 128}]")); | 171 " \"total\": 128}]")); |
| 170 } | 172 } |
| 171 | 173 |
| 172 protected: | 174 protected: |
| 173 scoped_ptr<MockDownloadsDOMHandler> mock_handler_; | 175 scoped_ptr<MockDownloadsDOMHandler> mock_handler_; |
| 174 | 176 |
| 175 private: | 177 private: |
| 176 base::ScopedTempDir downloads_directory_; | 178 base::ScopedTempDir downloads_directory_; |
| 177 | 179 |
| 178 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandlerTest); | 180 DISALLOW_COPY_AND_ASSIGN(DownloadsDOMHandlerTest); |
| 179 }; | 181 }; |
| 180 | 182 |
| 181 // Tests removing all items, both when prohibited and when allowed. | 183 // Tests removing all items, both when prohibited and when allowed. |
| 182 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveAll) { | 184 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveAll) { |
| 183 DownloadAnItem(); | 185 DownloadAnItem(false); |
| 184 | 186 |
| 185 mock_handler_->reset_downloads_list(); | 187 mock_handler_->reset_downloads_list(); |
| 186 browser()->profile()->GetPrefs()->SetBoolean( | 188 browser()->profile()->GetPrefs()->SetBoolean( |
| 187 prefs::kAllowDeletingBrowserHistory, false); | 189 prefs::kAllowDeletingBrowserHistory, false); |
| 188 mock_handler_->HandleClearAll(NULL); | 190 mock_handler_->HandleClearAll(NULL); |
| 189 // Attempting to clear all shouldn't do anything when deletion is disabled. | 191 // Attempting to clear all shouldn't do anything when deletion is disabled. |
| 190 mock_handler_->ForceSendCurrentDownloads(); | 192 mock_handler_->ForceSendCurrentDownloads(); |
| 191 mock_handler_->WaitForDownloadsList(); | 193 mock_handler_->WaitForDownloadsList(); |
| 192 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | 194 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
| 193 | 195 |
| 194 mock_handler_->reset_downloads_list(); | 196 mock_handler_->reset_downloads_list(); |
| 195 browser()->profile()->GetPrefs()->SetBoolean( | 197 browser()->profile()->GetPrefs()->SetBoolean( |
| 196 prefs::kAllowDeletingBrowserHistory, true); | 198 prefs::kAllowDeletingBrowserHistory, true); |
| 197 mock_handler_->HandleClearAll(NULL); | 199 mock_handler_->HandleClearAll(NULL); |
| 198 mock_handler_->WaitForDownloadsList(); | 200 mock_handler_->WaitForDownloadsList(); |
| 199 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | 201 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
| 200 } | 202 } |
| 201 | 203 |
| 204 // Tests removing all items, both when prohibited and when allowed. |
| 205 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveAllIgnoresInProgress) { |
| 206 DownloadAnItem(true); |
| 207 |
| 208 mock_handler_->reset_downloads_list(); |
| 209 mock_handler_->HandleClearAll(NULL); |
| 210 mock_handler_->ForceSendCurrentDownloads(); |
| 211 mock_handler_->WaitForDownloadsList(); |
| 212 EXPECT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
| 213 |
| 214 // Cancel the download so Chrome can shut down. |
| 215 download_manager()->GetDownload(1)->Cancel(false); |
| 216 } |
| 217 |
| 202 // Tests removing one item, both when prohibited and when allowed. | 218 // Tests removing one item, both when prohibited and when allowed. |
| 203 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveOneItem) { | 219 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, RemoveOneItem) { |
| 204 DownloadAnItem(); | 220 DownloadAnItem(false); |
| 205 base::ListValue item; | 221 base::ListValue item; |
| 206 item.AppendString("1"); | 222 item.AppendString("1"); |
| 207 | 223 |
| 208 mock_handler_->reset_downloads_list(); | 224 mock_handler_->reset_downloads_list(); |
| 209 browser()->profile()->GetPrefs()->SetBoolean( | 225 browser()->profile()->GetPrefs()->SetBoolean( |
| 210 prefs::kAllowDeletingBrowserHistory, false); | 226 prefs::kAllowDeletingBrowserHistory, false); |
| 211 mock_handler_->HandleRemove(&item); | 227 mock_handler_->HandleRemove(&item); |
| 212 // Removing an item only sends the new download list if anything was actually | 228 // Removing an item only sends the new download list if anything was actually |
| 213 // removed, so force it. | 229 // removed, so force it. |
| 214 mock_handler_->ForceSendCurrentDownloads(); | 230 mock_handler_->ForceSendCurrentDownloads(); |
| 215 mock_handler_->WaitForDownloadsList(); | 231 mock_handler_->WaitForDownloadsList(); |
| 216 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | 232 ASSERT_EQ(1, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
| 217 | 233 |
| 218 mock_handler_->reset_downloads_list(); | 234 mock_handler_->reset_downloads_list(); |
| 219 browser()->profile()->GetPrefs()->SetBoolean( | 235 browser()->profile()->GetPrefs()->SetBoolean( |
| 220 prefs::kAllowDeletingBrowserHistory, true); | 236 prefs::kAllowDeletingBrowserHistory, true); |
| 221 mock_handler_->HandleRemove(&item); | 237 mock_handler_->HandleRemove(&item); |
| 222 mock_handler_->WaitForDownloadsList(); | 238 mock_handler_->WaitForDownloadsList(); |
| 223 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); | 239 EXPECT_EQ(0, static_cast<int>(mock_handler_->downloads_list()->GetSize())); |
| 224 } | 240 } |
| 225 | 241 |
| 226 // Tests that DownloadsDOMHandler detects new downloads and relays them to the | 242 // Tests that DownloadsDOMHandler detects new downloads and relays them to the |
| 227 // renderer. | 243 // renderer. |
| 228 // crbug.com/159390: This test fails when daylight savings time ends. | 244 // crbug.com/159390: This test fails when daylight savings time ends. |
| 229 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, DownloadsRelayed) { | 245 IN_PROC_BROWSER_TEST_F(DownloadsDOMHandlerTest, DownloadsRelayed) { |
| 230 DownloadAnItem(); | 246 DownloadAnItem(false); |
| 231 | 247 |
| 232 mock_handler_->WaitForDownloadUpdated(); | 248 mock_handler_->WaitForDownloadUpdated(); |
| 233 ASSERT_EQ(1, static_cast<int>(mock_handler_->download_updated()->GetSize())); | 249 ASSERT_EQ(1, static_cast<int>(mock_handler_->download_updated()->GetSize())); |
| 234 EXPECT_TRUE(ListMatches( | 250 EXPECT_TRUE(ListMatches( |
| 235 mock_handler_->download_updated(), | 251 mock_handler_->download_updated(), |
| 236 "[{\"file_externally_removed\": true," | 252 "[{\"file_externally_removed\": true," |
| 237 " \"id\": \"1\"}]")); | 253 " \"id\": \"1\"}]")); |
| 238 | 254 |
| 239 mock_handler_->reset_downloads_list(); | 255 mock_handler_->reset_downloads_list(); |
| 240 browser()->profile()->GetPrefs()->SetBoolean( | 256 browser()->profile()->GetPrefs()->SetBoolean( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 251 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record | 267 // TODO(benjhayden): Test incognito, both downloads_list() and that on-record |
| 252 // calls can't access off-record items. | 268 // calls can't access off-record items. |
| 253 | 269 |
| 254 // TODO(benjhayden): Test that bad download ids incoming from the javascript are | 270 // TODO(benjhayden): Test that bad download ids incoming from the javascript are |
| 255 // dropped on the floor. | 271 // dropped on the floor. |
| 256 | 272 |
| 257 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. | 273 // TODO(benjhayden): Test that IsTemporary() downloads are not shown. |
| 258 | 274 |
| 259 // TODO(benjhayden): Test that RemoveObserver is called on all download items, | 275 // TODO(benjhayden): Test that RemoveObserver is called on all download items, |
| 260 // including items that crossed IsTemporary() and back. | 276 // including items that crossed IsTemporary() and back. |
| OLD | NEW |