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