OLD | NEW |
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 "chrome/browser/net/file_downloader.h" | 5 #include "chrome/browser/net/file_downloader.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/test/scoped_task_environment.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
12 #include "content/public/test/test_utils.h" | 14 #include "content/public/browser/browser_thread.h" |
13 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 15 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
14 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
15 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
19 | 21 |
20 const char kURL[] = "https://www.url.com/path"; | 22 const char kURL[] = "https://www.url.com/path"; |
21 const char kFilename[] = "filename.ext"; | 23 const char kFilename[] = "filename.ext"; |
22 const char kFileContents1[] = "file contents"; | 24 const char kFileContents1[] = "file contents"; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 } | 61 } |
60 | 62 |
61 void Download(bool overwrite, FileDownloader::Result expected_result) { | 63 void Download(bool overwrite, FileDownloader::Result expected_result) { |
62 FileDownloader downloader( | 64 FileDownloader downloader( |
63 GURL(kURL), path_, overwrite, request_context_.get(), | 65 GURL(kURL), path_, overwrite, request_context_.get(), |
64 base::Bind(&FileDownloaderTest::OnDownloadFinished, | 66 base::Bind(&FileDownloaderTest::OnDownloadFinished, |
65 base::Unretained(this)), | 67 base::Unretained(this)), |
66 TRAFFIC_ANNOTATION_FOR_TESTS); | 68 TRAFFIC_ANNOTATION_FOR_TESTS); |
67 EXPECT_CALL(*this, OnDownloadFinished(expected_result)); | 69 EXPECT_CALL(*this, OnDownloadFinished(expected_result)); |
68 // Wait for the FileExists check to happen if necessary. | 70 // Wait for the FileExists check to happen if necessary. |
69 content::RunAllBlockingPoolTasksUntilIdle(); | 71 if (!overwrite) |
| 72 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 73 // Wait for the actual download to happen. |
| 74 base::RunLoop().RunUntilIdle(); |
| 75 // Wait for the FileMove to happen. |
| 76 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 77 base::RunLoop().RunUntilIdle(); |
70 } | 78 } |
71 | 79 |
72 private: | 80 private: |
73 base::ScopedTempDir dir_; | 81 base::ScopedTempDir dir_; |
74 base::FilePath path_; | 82 base::FilePath path_; |
75 | 83 |
76 base::test::ScopedTaskEnvironment scoped_task_environment_; | 84 base::MessageLoop message_loop_; |
77 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 85 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
78 net::FakeURLFetcherFactory url_fetcher_factory_; | 86 net::FakeURLFetcherFactory url_fetcher_factory_; |
79 }; | 87 }; |
80 | 88 |
81 TEST_F(FileDownloaderTest, Success) { | 89 TEST_F(FileDownloaderTest, Success) { |
82 SetValidResponse(); | 90 SetValidResponse(); |
83 Download(true, FileDownloader::DOWNLOADED); | 91 Download(true, FileDownloader::DOWNLOADED); |
84 EXPECT_TRUE(base::PathExists(path())); | 92 EXPECT_TRUE(base::PathExists(path())); |
85 std::string contents; | 93 std::string contents; |
86 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); | 94 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
(...skipping 30 matching lines...) Expand all Loading... |
117 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); | 125 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
118 EXPECT_EQ(std::string(kFileContents1), contents); | 126 EXPECT_EQ(std::string(kFileContents1), contents); |
119 | 127 |
120 SetValidResponse2(); | 128 SetValidResponse2(); |
121 Download(false, FileDownloader::EXISTS); | 129 Download(false, FileDownloader::EXISTS); |
122 // The file should still have the old contents. | 130 // The file should still have the old contents. |
123 EXPECT_TRUE(base::PathExists(path())); | 131 EXPECT_TRUE(base::PathExists(path())); |
124 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); | 132 ASSERT_TRUE(base::ReadFileToString(path(), &contents)); |
125 EXPECT_EQ(std::string(kFileContents1), contents); | 133 EXPECT_EQ(std::string(kFileContents1), contents); |
126 } | 134 } |
OLD | NEW |