Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(943)

Side by Side Diff: chrome/browser/net/file_downloader_unittest.cc

Issue 1851803002: [NTP Popular Sites] Store country/version in prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
(...skipping 16 matching lines...) Expand all
27 : request_context_(new net::TestURLRequestContextGetter( 27 : request_context_(new net::TestURLRequestContextGetter(
28 base::ThreadTaskRunnerHandle::Get())), 28 base::ThreadTaskRunnerHandle::Get())),
29 url_fetcher_factory_(nullptr) {} 29 url_fetcher_factory_(nullptr) {}
30 30
31 void SetUp() override { 31 void SetUp() override {
32 ASSERT_TRUE(dir_.CreateUniqueTempDir()); 32 ASSERT_TRUE(dir_.CreateUniqueTempDir());
33 path_ = dir_.path().AppendASCII(kFilename); 33 path_ = dir_.path().AppendASCII(kFilename);
34 ASSERT_FALSE(base::PathExists(path_)); 34 ASSERT_FALSE(base::PathExists(path_));
35 } 35 }
36 36
37 MOCK_METHOD1(OnDownloadFinished, void(bool success)); 37 MOCK_METHOD1(OnDownloadFinished, void(FileDownloader::Result));
38 38
39 protected: 39 protected:
40 const base::FilePath& path() const { return path_; } 40 const base::FilePath& path() const { return path_; }
41 41
42 void SetValidResponse() { 42 void SetValidResponse() {
43 url_fetcher_factory_.SetFakeResponse( 43 url_fetcher_factory_.SetFakeResponse(
44 GURL(kURL), kFileContents1, net::HTTP_OK, 44 GURL(kURL), kFileContents1, net::HTTP_OK,
45 net::URLRequestStatus::SUCCESS); 45 net::URLRequestStatus::SUCCESS);
46 } 46 }
47 47
48 void SetValidResponse2() { 48 void SetValidResponse2() {
49 url_fetcher_factory_.SetFakeResponse( 49 url_fetcher_factory_.SetFakeResponse(
50 GURL(kURL), kFileContents2, net::HTTP_OK, 50 GURL(kURL), kFileContents2, net::HTTP_OK,
51 net::URLRequestStatus::SUCCESS); 51 net::URLRequestStatus::SUCCESS);
52 } 52 }
53 53
54 void SetFailedResponse() { 54 void SetFailedResponse() {
55 url_fetcher_factory_.SetFakeResponse( 55 url_fetcher_factory_.SetFakeResponse(
56 GURL(kURL), std::string(), net::HTTP_NOT_FOUND, 56 GURL(kURL), std::string(), net::HTTP_NOT_FOUND,
57 net::URLRequestStatus::SUCCESS); 57 net::URLRequestStatus::SUCCESS);
58 } 58 }
59 59
60 void Download(bool overwrite, bool expect_success) { 60 void Download(bool overwrite, FileDownloader::Result expected_result) {
61 FileDownloader downloader( 61 FileDownloader downloader(
62 GURL(kURL), path_, overwrite, request_context_.get(), 62 GURL(kURL), path_, overwrite, request_context_.get(),
63 base::Bind(&FileDownloaderTest::OnDownloadFinished, 63 base::Bind(&FileDownloaderTest::OnDownloadFinished,
64 base::Unretained(this))); 64 base::Unretained(this)));
65 EXPECT_CALL(*this, OnDownloadFinished(expect_success)); 65 EXPECT_CALL(*this, OnDownloadFinished(expected_result));
66 // Wait for the FileExists check to happen if necessary. 66 // Wait for the FileExists check to happen if necessary.
67 if (!overwrite) 67 if (!overwrite)
68 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 68 content::BrowserThread::GetBlockingPool()->FlushForTesting();
69 // Wait for the actual download to happen. 69 // Wait for the actual download to happen.
70 base::RunLoop().RunUntilIdle(); 70 base::RunLoop().RunUntilIdle();
71 // Wait for the FileMove to happen. 71 // Wait for the FileMove to happen.
72 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 72 content::BrowserThread::GetBlockingPool()->FlushForTesting();
73 base::RunLoop().RunUntilIdle(); 73 base::RunLoop().RunUntilIdle();
74 } 74 }
75 75
76 private: 76 private:
77 base::ScopedTempDir dir_; 77 base::ScopedTempDir dir_;
78 base::FilePath path_; 78 base::FilePath path_;
79 79
80 base::MessageLoop message_loop_; 80 base::MessageLoop message_loop_;
81 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 81 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
82 net::FakeURLFetcherFactory url_fetcher_factory_; 82 net::FakeURLFetcherFactory url_fetcher_factory_;
83 }; 83 };
84 84
85 TEST_F(FileDownloaderTest, Success) { 85 TEST_F(FileDownloaderTest, Success) {
86 SetValidResponse(); 86 SetValidResponse();
87 Download(true, true); 87 Download(true, FileDownloader::DOWNLOADED);
88 EXPECT_TRUE(base::PathExists(path())); 88 EXPECT_TRUE(base::PathExists(path()));
89 std::string contents; 89 std::string contents;
90 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); 90 ASSERT_TRUE(base::ReadFileToString(path(), &contents));
91 EXPECT_EQ(std::string(kFileContents1), contents); 91 EXPECT_EQ(std::string(kFileContents1), contents);
92 } 92 }
93 93
94 TEST_F(FileDownloaderTest, Failure) { 94 TEST_F(FileDownloaderTest, Failure) {
95 SetFailedResponse(); 95 SetFailedResponse();
96 Download(true, false); 96 Download(true, FileDownloader::FAILED);
97 EXPECT_FALSE(base::PathExists(path())); 97 EXPECT_FALSE(base::PathExists(path()));
98 } 98 }
99 99
100 TEST_F(FileDownloaderTest, Overwrite) { 100 TEST_F(FileDownloaderTest, Overwrite) {
101 SetValidResponse(); 101 SetValidResponse();
102 Download(true, true); 102 Download(true, FileDownloader::DOWNLOADED);
103 ASSERT_TRUE(base::PathExists(path())); 103 ASSERT_TRUE(base::PathExists(path()));
104 std::string contents; 104 std::string contents;
105 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); 105 ASSERT_TRUE(base::ReadFileToString(path(), &contents));
106 ASSERT_EQ(std::string(kFileContents1), contents); 106 ASSERT_EQ(std::string(kFileContents1), contents);
107 107
108 SetValidResponse2(); 108 SetValidResponse2();
109 Download(true, true); 109 Download(true, FileDownloader::DOWNLOADED);
110 // The file should have been overwritten with the new contents. 110 // The file should have been overwritten with the new contents.
111 EXPECT_TRUE(base::PathExists(path())); 111 EXPECT_TRUE(base::PathExists(path()));
112 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); 112 ASSERT_TRUE(base::ReadFileToString(path(), &contents));
113 EXPECT_EQ(std::string(kFileContents2), contents); 113 EXPECT_EQ(std::string(kFileContents2), contents);
114 } 114 }
115 115
116 TEST_F(FileDownloaderTest, DontOverwrite) { 116 TEST_F(FileDownloaderTest, DontOverwrite) {
117 SetValidResponse(); 117 SetValidResponse();
118 Download(true, true); 118 Download(true, FileDownloader::DOWNLOADED);
119 ASSERT_TRUE(base::PathExists(path())); 119 ASSERT_TRUE(base::PathExists(path()));
120 std::string contents; 120 std::string contents;
121 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); 121 ASSERT_TRUE(base::ReadFileToString(path(), &contents));
122 EXPECT_EQ(std::string(kFileContents1), contents); 122 EXPECT_EQ(std::string(kFileContents1), contents);
123 123
124 SetValidResponse2(); 124 SetValidResponse2();
125 Download(false, true); 125 Download(false, FileDownloader::EXISTS);
126 // The file should still have the old contents. 126 // The file should still have the old contents.
127 EXPECT_TRUE(base::PathExists(path())); 127 EXPECT_TRUE(base::PathExists(path()));
128 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); 128 ASSERT_TRUE(base::ReadFileToString(path(), &contents));
129 EXPECT_EQ(std::string(kFileContents1), contents); 129 EXPECT_EQ(std::string(kFileContents1), contents);
130 } 130 }
OLDNEW
« no previous file with comments | « chrome/browser/net/file_downloader.cc ('k') | chrome/browser/resources/popular_sites_internals.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698