Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/url_request/url_fetcher_response_writer.h" | 5 #include "net/url_request/url_fetcher_response_writer.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 TEST_F(URLFetcherStringWriterTest, Basic) { | 39 TEST_F(URLFetcherStringWriterTest, Basic) { |
| 40 int rv = 0; | 40 int rv = 0; |
| 41 // Initialize(), Write() and Finish(). | 41 // Initialize(), Write() and Finish(). |
| 42 TestCompletionCallback callback; | 42 TestCompletionCallback callback; |
| 43 rv = writer_->Initialize(callback.callback()); | 43 rv = writer_->Initialize(callback.callback()); |
| 44 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 44 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 45 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); | 45 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); |
| 46 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); | 46 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); |
| 47 rv = writer_->Finish(callback.callback()); | 47 rv = writer_->Finish(OK, callback.callback()); |
| 48 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 48 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 49 | 49 |
| 50 // Verify the result. | 50 // Verify the result. |
| 51 EXPECT_EQ(kData, writer_->data()); | 51 EXPECT_EQ(kData, writer_->data()); |
| 52 | 52 |
| 53 // Initialize() again to reset. | 53 // Initialize() again to reset. |
| 54 rv = writer_->Initialize(callback.callback()); | 54 rv = writer_->Initialize(callback.callback()); |
| 55 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 55 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 56 EXPECT_TRUE(writer_->data().empty()); | 56 EXPECT_TRUE(writer_->data().empty()); |
| 57 } | 57 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 TEST_F(URLFetcherFileWriterTest, WriteToFile) { | 75 TEST_F(URLFetcherFileWriterTest, WriteToFile) { |
| 76 int rv = 0; | 76 int rv = 0; |
| 77 // Initialize(), Write() and Finish(). | 77 // Initialize(), Write() and Finish(). |
| 78 TestCompletionCallback callback; | 78 TestCompletionCallback callback; |
| 79 rv = writer_->Initialize(callback.callback()); | 79 rv = writer_->Initialize(callback.callback()); |
| 80 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 80 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 81 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); | 81 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); |
| 82 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); | 82 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); |
| 83 rv = writer_->Finish(callback.callback()); | 83 rv = writer_->Finish(OK, callback.callback()); |
| 84 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 84 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 85 | 85 |
| 86 // Verify the result. | 86 // Verify the result. |
| 87 EXPECT_EQ(file_path_.value(), writer_->file_path().value()); | 87 EXPECT_EQ(file_path_.value(), writer_->file_path().value()); |
| 88 std::string file_contents; | 88 std::string file_contents; |
| 89 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); | 89 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); |
| 90 EXPECT_EQ(kData, file_contents); | 90 EXPECT_EQ(kData, file_contents); |
| 91 | 91 |
| 92 // Destroy the writer. File should be deleted. | 92 // Destroy the writer. File should be deleted. |
| 93 writer_.reset(); | 93 writer_.reset(); |
| 94 base::RunLoop().RunUntilIdle(); | 94 base::RunLoop().RunUntilIdle(); |
| 95 EXPECT_FALSE(base::PathExists(file_path_)); | 95 EXPECT_FALSE(base::PathExists(file_path_)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 TEST_F(URLFetcherFileWriterTest, InitializeAgain) { | 98 TEST_F(URLFetcherFileWriterTest, InitializeAgain) { |
| 99 int rv = 0; | 99 int rv = 0; |
| 100 // Initialize(), Write() and Finish(). | 100 // Initialize(), Write() and Finish(). |
| 101 TestCompletionCallback callback; | 101 TestCompletionCallback callback; |
| 102 rv = writer_->Initialize(callback.callback()); | 102 rv = writer_->Initialize(callback.callback()); |
| 103 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 103 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 104 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); | 104 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); |
| 105 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); | 105 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); |
| 106 rv = writer_->Finish(callback.callback()); | 106 rv = writer_->Finish(OK, callback.callback()); |
| 107 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 107 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 108 | 108 |
| 109 // Verify the result. | 109 // Verify the result. |
| 110 std::string file_contents; | 110 std::string file_contents; |
| 111 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); | 111 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); |
| 112 EXPECT_EQ(kData, file_contents); | 112 EXPECT_EQ(kData, file_contents); |
| 113 | 113 |
| 114 // Initialize() again to reset. Write different data. | 114 // Initialize() again to reset. Write different data. |
| 115 const std::string data2 = "Bye!"; | 115 const std::string data2 = "Bye!"; |
| 116 scoped_refptr<StringIOBuffer> buf2(new StringIOBuffer(data2)); | 116 scoped_refptr<StringIOBuffer> buf2(new StringIOBuffer(data2)); |
| 117 | 117 |
| 118 rv = writer_->Initialize(callback.callback()); | 118 rv = writer_->Initialize(callback.callback()); |
| 119 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 119 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 120 rv = writer_->Write(buf2.get(), buf2->size(), callback.callback()); | 120 rv = writer_->Write(buf2.get(), buf2->size(), callback.callback()); |
| 121 EXPECT_EQ(buf2->size(), callback.GetResult(rv)); | 121 EXPECT_EQ(buf2->size(), callback.GetResult(rv)); |
| 122 rv = writer_->Finish(callback.callback()); | 122 rv = writer_->Finish(OK, callback.callback()); |
| 123 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 123 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 124 | 124 |
| 125 // Verify the result. | 125 // Verify the result. |
| 126 file_contents.clear(); | 126 file_contents.clear(); |
| 127 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); | 127 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); |
| 128 EXPECT_EQ(data2, file_contents); | 128 EXPECT_EQ(data2, file_contents); |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(URLFetcherFileWriterTest, FinishWhileOperationPending) { | |
| 132 int rv = 0; | |
| 133 // Initialize(), Write() and Finish(). | |
| 134 TestCompletionCallback callback; | |
| 135 rv = writer_->Initialize(callback.callback()); | |
|
mmenke
2016/10/18 20:20:02
Maybe a test where Initialize is pending, too? Do
xunjieli
2016/10/18 21:18:07
Done. Ah I should have thought about that.
The ne
| |
| 136 EXPECT_THAT(callback.GetResult(rv), IsOk()); | |
| 137 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); | |
| 138 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 139 rv = writer_->Finish(ERR_FAILED, callback.callback()); | |
| 140 EXPECT_EQ(OK, rv); | |
| 141 | |
| 142 // Verify the result. | |
| 143 base::RunLoop().RunUntilIdle(); | |
| 144 EXPECT_FALSE(base::PathExists(file_path_)); | |
|
mmenke
2016/10/18 20:20:01
Is it worth having a test where we Initialize() th
xunjieli
2016/10/18 21:18:07
Done.
| |
| 145 } | |
| 146 | |
| 131 TEST_F(URLFetcherFileWriterTest, DisownFile) { | 147 TEST_F(URLFetcherFileWriterTest, DisownFile) { |
| 132 int rv = 0; | 148 int rv = 0; |
| 133 // Initialize() and Finish() to create a file. | 149 // Initialize() and Finish() to create a file. |
| 134 TestCompletionCallback callback; | 150 TestCompletionCallback callback; |
| 135 rv = writer_->Initialize(callback.callback()); | 151 rv = writer_->Initialize(callback.callback()); |
| 136 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 152 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 137 rv = writer_->Finish(callback.callback()); | 153 rv = writer_->Finish(OK, callback.callback()); |
| 138 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 154 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 139 | 155 |
| 140 // Disown file. | 156 // Disown file. |
| 141 writer_->DisownFile(); | 157 writer_->DisownFile(); |
| 142 | 158 |
| 143 // File is not deleted even after the writer gets destroyed. | 159 // File is not deleted even after the writer gets destroyed. |
| 144 writer_.reset(); | 160 writer_.reset(); |
| 145 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 146 EXPECT_TRUE(base::PathExists(file_path_)); | 162 EXPECT_TRUE(base::PathExists(file_path_)); |
| 147 } | 163 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 159 }; | 175 }; |
| 160 | 176 |
| 161 TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) { | 177 TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) { |
| 162 int rv = 0; | 178 int rv = 0; |
| 163 // Initialize(), Write() and Finish(). | 179 // Initialize(), Write() and Finish(). |
| 164 TestCompletionCallback callback; | 180 TestCompletionCallback callback; |
| 165 rv = writer_->Initialize(callback.callback()); | 181 rv = writer_->Initialize(callback.callback()); |
| 166 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 182 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 167 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); | 183 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); |
| 168 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); | 184 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); |
| 169 rv = writer_->Finish(callback.callback()); | 185 rv = writer_->Finish(OK, callback.callback()); |
| 170 EXPECT_THAT(callback.GetResult(rv), IsOk()); | 186 EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 171 | 187 |
| 172 // Verify the result. | 188 // Verify the result. |
| 173 std::string file_contents; | 189 std::string file_contents; |
| 174 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); | 190 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); |
| 175 EXPECT_EQ(kData, file_contents); | 191 EXPECT_EQ(kData, file_contents); |
| 176 | 192 |
| 177 // Destroy the writer. File should be deleted. | 193 // Destroy the writer. File should be deleted. |
| 178 const base::FilePath file_path = writer_->file_path(); | 194 const base::FilePath file_path = writer_->file_path(); |
| 179 writer_.reset(); | 195 writer_.reset(); |
| 180 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
| 181 EXPECT_FALSE(base::PathExists(file_path)); | 197 EXPECT_FALSE(base::PathExists(file_path)); |
| 182 } | 198 } |
| 183 | 199 |
| 184 } // namespace net | 200 } // namespace net |
| OLD | NEW |