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 // This file contains download browser tests that are known to be runnable |
| 6 // in a pure content context. Over time tests should be migrated here. |
| 7 |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/scoped_temp_dir.h" |
| 11 #include "content/browser/download/download_item_impl.h" |
| 12 #include "content/browser/download/download_manager_impl.h" |
| 13 #include "content/browser/web_contents/web_contents_impl.h" |
| 14 #include "content/public/test/download_test_observer.h" |
| 15 #include "content/shell/shell.h" |
| 16 #include "content/shell/shell_browser_context.h" |
| 17 #include "content/shell/shell_download_manager_delegate.h" |
| 18 #include "content/test/content_browser_test.h" |
| 19 #include "content/test/content_browser_test_utils.h" |
| 20 #include "content/test/net/url_request_mock_http_job.h" |
| 21 #include "content/test/net/url_request_slow_download_job.h" |
| 22 #include "googleurl/src/gurl.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
| 28 static DownloadManager* DownloadManagerForShell(Shell* shell) { |
| 29 return BrowserContext::GetDownloadManager( |
| 30 shell->web_contents()->GetBrowserContext()); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 class DownloadContentTest : public ContentBrowserTest { |
| 36 protected: |
| 37 virtual void SetUpOnMainThread() OVERRIDE { |
| 38 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
| 39 |
| 40 ShellDownloadManagerDelegate* delegate = |
| 41 static_cast<ShellDownloadManagerDelegate*>( |
| 42 shell()->web_contents()->GetBrowserContext() |
| 43 ->GetDownloadManagerDelegate()); |
| 44 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path()); |
| 45 |
| 46 BrowserThread::PostTask( |
| 47 BrowserThread::IO, FROM_HERE, |
| 48 base::Bind(&URLRequestSlowDownloadJob::AddUrlHandler)); |
| 49 FilePath mock_base(GetTestFilePath("download", "")); |
| 50 BrowserThread::PostTask( |
| 51 BrowserThread::IO, FROM_HERE, |
| 52 base::Bind(&URLRequestMockHTTPJob::AddUrlHandler, mock_base)); |
| 53 } |
| 54 |
| 55 // Create a DownloadTestObserverTerminal that will wait for the |
| 56 // specified number of downloads to finish. |
| 57 DownloadTestObserver* CreateWaiter( |
| 58 Shell* shell, int num_downloads) { |
| 59 DownloadManager* download_manager = DownloadManagerForShell(shell); |
| 60 return new DownloadTestObserverTerminal(download_manager, num_downloads, |
| 61 DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); |
| 62 } |
| 63 |
| 64 // Create a DownloadTestObserverInProgress that will wait for the |
| 65 // specified number of downloads to start. |
| 66 DownloadTestObserver* CreateInProgressWaiter( |
| 67 Shell* shell, int num_downloads) { |
| 68 DownloadManager* download_manager = DownloadManagerForShell(shell); |
| 69 return new DownloadTestObserverInProgress(download_manager, num_downloads); |
| 70 } |
| 71 |
| 72 bool EnsureNoPendingDownloads() { |
| 73 bool result = true; |
| 74 BrowserThread::PostTask( |
| 75 BrowserThread::IO, FROM_HERE, |
| 76 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
| 77 MessageLoop::current()->Run(); |
| 78 return result && DownloadManager::EnsureNoPendingDownloadsForTesting(); |
| 79 } |
| 80 |
| 81 void DownloadAndWait(Shell* shell, const GURL& url) { |
| 82 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); |
| 83 NavigateToURL(shell, url); |
| 84 observer->WaitForFinished(); |
| 85 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
| 86 } |
| 87 |
| 88 // Checks that |path| is has |file_size| bytes, and matches the |value| |
| 89 // string. |
| 90 bool VerifyFile(const FilePath& path, |
| 91 const std::string& value, |
| 92 const int64 file_size) { |
| 93 std::string file_contents; |
| 94 |
| 95 bool read = file_util::ReadFileToString(path, &file_contents); |
| 96 EXPECT_TRUE(read) << "Failed reading file: " << path.value() << std::endl; |
| 97 if (!read) |
| 98 return false; // Couldn't read the file. |
| 99 |
| 100 // Note: we don't handle really large files (more than size_t can hold) |
| 101 // so we will fail in that case. |
| 102 size_t expected_size = static_cast<size_t>(file_size); |
| 103 |
| 104 // Check the size. |
| 105 EXPECT_EQ(expected_size, file_contents.size()); |
| 106 if (expected_size != file_contents.size()) |
| 107 return false; |
| 108 |
| 109 // Check the contents. |
| 110 EXPECT_EQ(value, file_contents); |
| 111 if (memcmp(file_contents.c_str(), value.c_str(), expected_size) != 0) |
| 112 return false; |
| 113 |
| 114 return true; |
| 115 } |
| 116 |
| 117 private: |
| 118 static void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 119 if (URLRequestSlowDownloadJob::NumberOutstandingRequests()) |
| 120 *result = false; |
| 121 BrowserThread::PostTask( |
| 122 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 123 } |
| 124 |
| 125 // Location of the downloads directory for these tests |
| 126 ScopedTempDir downloads_directory_; |
| 127 }; |
| 128 |
| 129 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { |
| 130 // TODO(rdsmith): Fragile code warning! The code below relies on the |
| 131 // DownloadTestObserverInProgress only finishing when the new download |
| 132 // has reached the state of being entered into the history and being |
| 133 // user-visible (that's what's required for the Remove to be valid and |
| 134 // for the download shelf to be visible). By the pure semantics of |
| 135 // DownloadTestObserverInProgress, that's not guaranteed; DownloadItems |
| 136 // are created in the IN_PROGRESS state and made known to the DownloadManager |
| 137 // immediately, so any ModelChanged event on the DownloadManager after |
| 138 // navigation would allow the observer to return. However, the only |
| 139 // ModelChanged() event the code will currently fire is in |
| 140 // OnCreateDownloadEntryComplete, at which point the download item will |
| 141 // be in the state we need. |
| 142 // The right way to fix this is to create finer grained states on the |
| 143 // DownloadItem, and wait for the state that indicates the item has been |
| 144 // entered in the history and made visible in the UI. |
| 145 |
| 146 // Create a download, wait until it's started, and confirm |
| 147 // we're in the expected state. |
| 148 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
| 149 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
| 150 observer->WaitForFinished(); |
| 151 |
| 152 std::vector<DownloadItem*> downloads; |
| 153 DownloadManagerForShell(shell())->SearchDownloads(string16(), &downloads); |
| 154 ASSERT_EQ(1u, downloads.size()); |
| 155 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); |
| 156 |
| 157 // Cancel the download and wait for download system quiesce. |
| 158 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 159 scoped_refptr<DownloadTestFlushObserver> flush_observer( |
| 160 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); |
| 161 flush_observer->WaitForFlush(); |
| 162 |
| 163 // Get the important info from other threads and check it. |
| 164 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 165 } |
| 166 |
| 167 // Check that downloading multiple (in this case, 2) files does not result in |
| 168 // corrupted files. |
| 169 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { |
| 170 // Create a download, wait until it's started, and confirm |
| 171 // we're in the expected state. |
| 172 scoped_ptr<DownloadTestObserver> observer1( |
| 173 CreateInProgressWaiter(shell(), 1)); |
| 174 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
| 175 observer1->WaitForFinished(); |
| 176 |
| 177 std::vector<DownloadItem*> downloads; |
| 178 DownloadManagerForShell(shell())->SearchDownloads(string16(), &downloads); |
| 179 ASSERT_EQ(1u, downloads.size()); |
| 180 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); |
| 181 DownloadItem* download1 = downloads[0]; // The only download. |
| 182 |
| 183 // Start the second download and wait until it's done. |
| 184 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
| 185 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); |
| 186 // Download the file and wait. |
| 187 DownloadAndWait(shell(), url); |
| 188 |
| 189 // Should now have 2 items on the manager. |
| 190 downloads.clear(); |
| 191 DownloadManagerForShell(shell())->SearchDownloads(string16(), &downloads); |
| 192 ASSERT_EQ(2u, downloads.size()); |
| 193 // We don't know the order of the downloads. |
| 194 DownloadItem* download2 = downloads[(download1 == downloads[0]) ? 1 : 0]; |
| 195 |
| 196 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); |
| 197 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState()); |
| 198 |
| 199 // Allow the first request to finish. |
| 200 scoped_ptr<DownloadTestObserver> observer2(CreateWaiter(shell(), 1)); |
| 201 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kFinishDownloadUrl)); |
| 202 observer2->WaitForFinished(); // Wait for the third request. |
| 203 EXPECT_EQ(1u, observer2->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
| 204 |
| 205 // Get the important info from other threads and check it. |
| 206 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 207 |
| 208 // The |DownloadItem|s should now be done and have the final file names. |
| 209 // Verify that the files have the expected data and size. |
| 210 // |file1| should be full of '*'s, and |file2| should be the same as the |
| 211 // source file. |
| 212 FilePath file1(download1->GetFullPath()); |
| 213 size_t file_size1 = URLRequestSlowDownloadJob::kFirstDownloadSize + |
| 214 URLRequestSlowDownloadJob::kSecondDownloadSize; |
| 215 std::string expected_contents(file_size1, '*'); |
| 216 ASSERT_TRUE(VerifyFile(file1, expected_contents, file_size1)); |
| 217 |
| 218 FilePath file2(download2->GetFullPath()); |
| 219 ASSERT_TRUE(file_util::ContentsEqual( |
| 220 file2, GetTestFilePath("download", "download-test.lib"))); |
| 221 } |
| 222 |
| 223 } // namespace content |
OLD | NEW |