treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 1 | // Copyright 2015 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 "base/files/file_path.h" |
| 6 | #include "base/files/file_util.h" |
| 7 | #include "base/files/scoped_temp_dir.h" |
| 8 | #include "base/message_loop/message_loop.h" |
| 9 | #include "base/run_loop.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 10 | #include "base/threading/thread_task_runner_handle.h" |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 11 | #include "chrome/browser/net/file_downloader.h" |
| 12 | #include "content/public/browser/browser_thread.h" |
| 13 | #include "net/url_request/test_url_fetcher_factory.h" |
| 14 | #include "net/url_request/url_request_test_util.h" |
| 15 | #include "testing/gmock/include/gmock/gmock.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | #include "url/gurl.h" |
| 18 | |
| 19 | const char kURL[] = "https://www.url.com/path"; |
| 20 | const char kFilename[] = "filename.ext"; |
| 21 | const char kFileContents1[] = "file contents"; |
| 22 | const char kFileContents2[] = "different contents"; |
| 23 | |
| 24 | class FileDownloaderTest : public testing::Test { |
| 25 | public: |
| 26 | FileDownloaderTest() |
| 27 | : request_context_(new net::TestURLRequestContextGetter( |
| 28 | base::ThreadTaskRunnerHandle::Get())), |
| 29 | url_fetcher_factory_(nullptr) {} |
| 30 | |
| 31 | void SetUp() override { |
| 32 | ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
vabr | b858232 | 2016-09-09 08:05:37 | [diff] [blame] | 33 | path_ = dir_.GetPath().AppendASCII(kFilename); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 34 | ASSERT_FALSE(base::PathExists(path_)); |
| 35 | } |
| 36 | |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 37 | MOCK_METHOD1(OnDownloadFinished, void(FileDownloader::Result)); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 38 | |
| 39 | protected: |
| 40 | const base::FilePath& path() const { return path_; } |
| 41 | |
| 42 | void SetValidResponse() { |
| 43 | url_fetcher_factory_.SetFakeResponse( |
| 44 | GURL(kURL), kFileContents1, net::HTTP_OK, |
| 45 | net::URLRequestStatus::SUCCESS); |
| 46 | } |
| 47 | |
| 48 | void SetValidResponse2() { |
| 49 | url_fetcher_factory_.SetFakeResponse( |
| 50 | GURL(kURL), kFileContents2, net::HTTP_OK, |
| 51 | net::URLRequestStatus::SUCCESS); |
| 52 | } |
| 53 | |
| 54 | void SetFailedResponse() { |
| 55 | url_fetcher_factory_.SetFakeResponse( |
| 56 | GURL(kURL), std::string(), net::HTTP_NOT_FOUND, |
| 57 | net::URLRequestStatus::SUCCESS); |
| 58 | } |
| 59 | |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 60 | void Download(bool overwrite, FileDownloader::Result expected_result) { |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 61 | FileDownloader downloader( |
| 62 | GURL(kURL), path_, overwrite, request_context_.get(), |
| 63 | base::Bind(&FileDownloaderTest::OnDownloadFinished, |
| 64 | base::Unretained(this))); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 65 | EXPECT_CALL(*this, OnDownloadFinished(expected_result)); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 66 | // Wait for the FileExists check to happen if necessary. |
| 67 | if (!overwrite) |
| 68 | content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 69 | // Wait for the actual download to happen. |
| 70 | base::RunLoop().RunUntilIdle(); |
| 71 | // Wait for the FileMove to happen. |
| 72 | content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 73 | base::RunLoop().RunUntilIdle(); |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | base::ScopedTempDir dir_; |
| 78 | base::FilePath path_; |
| 79 | |
| 80 | base::MessageLoop message_loop_; |
| 81 | scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 82 | net::FakeURLFetcherFactory url_fetcher_factory_; |
| 83 | }; |
| 84 | |
| 85 | TEST_F(FileDownloaderTest, Success) { |
| 86 | SetValidResponse(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 87 | Download(true, FileDownloader::DOWNLOADED); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 88 | EXPECT_TRUE(base::PathExists(path())); |
| 89 | std::string contents; |
| 90 | ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
| 91 | EXPECT_EQ(std::string(kFileContents1), contents); |
| 92 | } |
| 93 | |
| 94 | TEST_F(FileDownloaderTest, Failure) { |
| 95 | SetFailedResponse(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 96 | Download(true, FileDownloader::FAILED); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 97 | EXPECT_FALSE(base::PathExists(path())); |
| 98 | } |
| 99 | |
| 100 | TEST_F(FileDownloaderTest, Overwrite) { |
| 101 | SetValidResponse(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 102 | Download(true, FileDownloader::DOWNLOADED); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 103 | ASSERT_TRUE(base::PathExists(path())); |
| 104 | std::string contents; |
| 105 | ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
| 106 | ASSERT_EQ(std::string(kFileContents1), contents); |
| 107 | |
| 108 | SetValidResponse2(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 109 | Download(true, FileDownloader::DOWNLOADED); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 110 | // The file should have been overwritten with the new contents. |
| 111 | EXPECT_TRUE(base::PathExists(path())); |
| 112 | ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
| 113 | EXPECT_EQ(std::string(kFileContents2), contents); |
| 114 | } |
| 115 | |
| 116 | TEST_F(FileDownloaderTest, DontOverwrite) { |
| 117 | SetValidResponse(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 118 | Download(true, FileDownloader::DOWNLOADED); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 119 | ASSERT_TRUE(base::PathExists(path())); |
| 120 | std::string contents; |
| 121 | ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
| 122 | EXPECT_EQ(std::string(kFileContents1), contents); |
| 123 | |
| 124 | SetValidResponse2(); |
treib | f38cc25 | 2016-04-07 14:44:11 | [diff] [blame] | 125 | Download(false, FileDownloader::EXISTS); |
treib | 0e1ad00d | 2015-10-14 13:14:32 | [diff] [blame] | 126 | // The file should still have the old contents. |
| 127 | EXPECT_TRUE(base::PathExists(path())); |
| 128 | ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
| 129 | EXPECT_EQ(std::string(kFileContents1), contents); |
| 130 | } |