| 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/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kData[] = "Hello!"; | 20 const char kData[] = "Hello!"; |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 class URLFetcherStringWriterTest : public PlatformTest { | 24 class URLFetcherStringWriterTest : public PlatformTest { |
| 25 protected: | 25 protected: |
| 26 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() override { |
| 27 writer_.reset(new URLFetcherStringWriter); | 27 writer_.reset(new URLFetcherStringWriter); |
| 28 buf_ = new StringIOBuffer(kData); | 28 buf_ = new StringIOBuffer(kData); |
| 29 } | 29 } |
| 30 | 30 |
| 31 scoped_ptr<URLFetcherStringWriter> writer_; | 31 scoped_ptr<URLFetcherStringWriter> writer_; |
| 32 scoped_refptr<StringIOBuffer> buf_; | 32 scoped_refptr<StringIOBuffer> buf_; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 TEST_F(URLFetcherStringWriterTest, Basic) { | 35 TEST_F(URLFetcherStringWriterTest, Basic) { |
| 36 int rv = 0; | 36 int rv = 0; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 47 EXPECT_EQ(kData, writer_->data()); | 47 EXPECT_EQ(kData, writer_->data()); |
| 48 | 48 |
| 49 // Initialize() again to reset. | 49 // Initialize() again to reset. |
| 50 rv = writer_->Initialize(callback.callback()); | 50 rv = writer_->Initialize(callback.callback()); |
| 51 EXPECT_EQ(OK, callback.GetResult(rv)); | 51 EXPECT_EQ(OK, callback.GetResult(rv)); |
| 52 EXPECT_TRUE(writer_->data().empty()); | 52 EXPECT_TRUE(writer_->data().empty()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 class URLFetcherFileWriterTest : public PlatformTest { | 55 class URLFetcherFileWriterTest : public PlatformTest { |
| 56 protected: | 56 protected: |
| 57 virtual void SetUp() OVERRIDE { | 57 virtual void SetUp() override { |
| 58 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 58 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 59 file_path_ = temp_dir_.path().AppendASCII("test.txt"); | 59 file_path_ = temp_dir_.path().AppendASCII("test.txt"); |
| 60 writer_.reset(new URLFetcherFileWriter( | 60 writer_.reset(new URLFetcherFileWriter( |
| 61 base::MessageLoopProxy::current(), file_path_)); | 61 base::MessageLoopProxy::current(), file_path_)); |
| 62 buf_ = new StringIOBuffer(kData); | 62 buf_ = new StringIOBuffer(kData); |
| 63 } | 63 } |
| 64 | 64 |
| 65 base::ScopedTempDir temp_dir_; | 65 base::ScopedTempDir temp_dir_; |
| 66 base::FilePath file_path_; | 66 base::FilePath file_path_; |
| 67 scoped_ptr<URLFetcherFileWriter> writer_; | 67 scoped_ptr<URLFetcherFileWriter> writer_; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 writer_->DisownFile(); | 137 writer_->DisownFile(); |
| 138 | 138 |
| 139 // File is not deleted even after the writer gets destroyed. | 139 // File is not deleted even after the writer gets destroyed. |
| 140 writer_.reset(); | 140 writer_.reset(); |
| 141 base::RunLoop().RunUntilIdle(); | 141 base::RunLoop().RunUntilIdle(); |
| 142 EXPECT_TRUE(base::PathExists(file_path_)); | 142 EXPECT_TRUE(base::PathExists(file_path_)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 class URLFetcherFileWriterTemporaryFileTest : public PlatformTest { | 145 class URLFetcherFileWriterTemporaryFileTest : public PlatformTest { |
| 146 protected: | 146 protected: |
| 147 virtual void SetUp() OVERRIDE { | 147 virtual void SetUp() override { |
| 148 writer_.reset(new URLFetcherFileWriter( | 148 writer_.reset(new URLFetcherFileWriter( |
| 149 base::MessageLoopProxy::current(), base::FilePath())); | 149 base::MessageLoopProxy::current(), base::FilePath())); |
| 150 buf_ = new StringIOBuffer(kData); | 150 buf_ = new StringIOBuffer(kData); |
| 151 } | 151 } |
| 152 | 152 |
| 153 scoped_ptr<URLFetcherFileWriter> writer_; | 153 scoped_ptr<URLFetcherFileWriter> writer_; |
| 154 scoped_refptr<StringIOBuffer> buf_; | 154 scoped_refptr<StringIOBuffer> buf_; |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) { | 157 TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 171 EXPECT_EQ(kData, file_contents); | 171 EXPECT_EQ(kData, file_contents); |
| 172 | 172 |
| 173 // Destroy the writer. File should be deleted. | 173 // Destroy the writer. File should be deleted. |
| 174 const base::FilePath file_path = writer_->file_path(); | 174 const base::FilePath file_path = writer_->file_path(); |
| 175 writer_.reset(); | 175 writer_.reset(); |
| 176 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 177 EXPECT_FALSE(base::PathExists(file_path)); | 177 EXPECT_FALSE(base::PathExists(file_path)); |
| 178 } | 178 } |
| 179 | 179 |
| 180 } // namespace net | 180 } // namespace net |
| OLD | NEW |