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