blob: ba14f96b4d8a1e15df265a97b164e753fb4a995e [file] [log] [blame]
treib0e1ad00d2015-10-14 13:14:321// 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"
gabb15e19072016-05-11 20:45:4110#include "base/threading/thread_task_runner_handle.h"
treib0e1ad00d2015-10-14 13:14:3211#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
19const char kURL[] = "https://www.url.com/path";
20const char kFilename[] = "filename.ext";
21const char kFileContents1[] = "file contents";
22const char kFileContents2[] = "different contents";
23
24class 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());
vabrb8582322016-09-09 08:05:3733 path_ = dir_.GetPath().AppendASCII(kFilename);
treib0e1ad00d2015-10-14 13:14:3234 ASSERT_FALSE(base::PathExists(path_));
35 }
36
treibf38cc252016-04-07 14:44:1137 MOCK_METHOD1(OnDownloadFinished, void(FileDownloader::Result));
treib0e1ad00d2015-10-14 13:14:3238
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
treibf38cc252016-04-07 14:44:1160 void Download(bool overwrite, FileDownloader::Result expected_result) {
treib0e1ad00d2015-10-14 13:14:3261 FileDownloader downloader(
62 GURL(kURL), path_, overwrite, request_context_.get(),
63 base::Bind(&FileDownloaderTest::OnDownloadFinished,
64 base::Unretained(this)));
treibf38cc252016-04-07 14:44:1165 EXPECT_CALL(*this, OnDownloadFinished(expected_result));
treib0e1ad00d2015-10-14 13:14:3266 // 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
85TEST_F(FileDownloaderTest, Success) {
86 SetValidResponse();
treibf38cc252016-04-07 14:44:1187 Download(true, FileDownloader::DOWNLOADED);
treib0e1ad00d2015-10-14 13:14:3288 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
94TEST_F(FileDownloaderTest, Failure) {
95 SetFailedResponse();
treibf38cc252016-04-07 14:44:1196 Download(true, FileDownloader::FAILED);
treib0e1ad00d2015-10-14 13:14:3297 EXPECT_FALSE(base::PathExists(path()));
98}
99
100TEST_F(FileDownloaderTest, Overwrite) {
101 SetValidResponse();
treibf38cc252016-04-07 14:44:11102 Download(true, FileDownloader::DOWNLOADED);
treib0e1ad00d2015-10-14 13:14:32103 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();
treibf38cc252016-04-07 14:44:11109 Download(true, FileDownloader::DOWNLOADED);
treib0e1ad00d2015-10-14 13:14:32110 // 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
116TEST_F(FileDownloaderTest, DontOverwrite) {
117 SetValidResponse();
treibf38cc252016-04-07 14:44:11118 Download(true, FileDownloader::DOWNLOADED);
treib0e1ad00d2015-10-14 13:14:32119 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();
treibf38cc252016-04-07 14:44:11125 Download(false, FileDownloader::EXISTS);
treib0e1ad00d2015-10-14 13:14:32126 // 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}