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

Side by Side Diff: net/url_request/url_fetcher_response_writer_unittest.cc

Issue 2109503009: Refactor net tests to use GMock matchers for checking net::Error results (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert changes to contents.txt files Created 4 years, 5 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 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"
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 "net/test/gtest_util.h"
15 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/platform_test.h" 16 #include "testing/platform_test.h"
15 17
18 using net::test::IsOk;
19
16 namespace net { 20 namespace net {
17 21
18 namespace { 22 namespace {
19 23
20 const char kData[] = "Hello!"; 24 const char kData[] = "Hello!";
21 25
22 } // namespace 26 } // namespace
23 27
24 class URLFetcherStringWriterTest : public PlatformTest { 28 class URLFetcherStringWriterTest : public PlatformTest {
25 protected: 29 protected:
26 void SetUp() override { 30 void SetUp() override {
27 writer_.reset(new URLFetcherStringWriter); 31 writer_.reset(new URLFetcherStringWriter);
28 buf_ = new StringIOBuffer(kData); 32 buf_ = new StringIOBuffer(kData);
29 } 33 }
30 34
31 std::unique_ptr<URLFetcherStringWriter> writer_; 35 std::unique_ptr<URLFetcherStringWriter> writer_;
32 scoped_refptr<StringIOBuffer> buf_; 36 scoped_refptr<StringIOBuffer> buf_;
33 }; 37 };
34 38
35 TEST_F(URLFetcherStringWriterTest, Basic) { 39 TEST_F(URLFetcherStringWriterTest, Basic) {
36 int rv = 0; 40 int rv = 0;
37 // Initialize(), Write() and Finish(). 41 // Initialize(), Write() and Finish().
38 TestCompletionCallback callback; 42 TestCompletionCallback callback;
39 rv = writer_->Initialize(callback.callback()); 43 rv = writer_->Initialize(callback.callback());
40 EXPECT_EQ(OK, callback.GetResult(rv)); 44 EXPECT_THAT(callback.GetResult(rv), IsOk());
41 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); 45 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
42 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); 46 EXPECT_EQ(buf_->size(), callback.GetResult(rv));
43 rv = writer_->Finish(callback.callback()); 47 rv = writer_->Finish(callback.callback());
44 EXPECT_EQ(OK, callback.GetResult(rv)); 48 EXPECT_THAT(callback.GetResult(rv), IsOk());
45 49
46 // Verify the result. 50 // Verify the result.
47 EXPECT_EQ(kData, writer_->data()); 51 EXPECT_EQ(kData, writer_->data());
48 52
49 // Initialize() again to reset. 53 // Initialize() again to reset.
50 rv = writer_->Initialize(callback.callback()); 54 rv = writer_->Initialize(callback.callback());
51 EXPECT_EQ(OK, callback.GetResult(rv)); 55 EXPECT_THAT(callback.GetResult(rv), IsOk());
52 EXPECT_TRUE(writer_->data().empty()); 56 EXPECT_TRUE(writer_->data().empty());
53 } 57 }
54 58
55 class URLFetcherFileWriterTest : public PlatformTest { 59 class URLFetcherFileWriterTest : public PlatformTest {
56 protected: 60 protected:
57 void SetUp() override { 61 void SetUp() override {
58 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 62 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
59 file_path_ = temp_dir_.path().AppendASCII("test.txt"); 63 file_path_ = temp_dir_.path().AppendASCII("test.txt");
60 writer_.reset(new URLFetcherFileWriter(base::ThreadTaskRunnerHandle::Get(), 64 writer_.reset(new URLFetcherFileWriter(base::ThreadTaskRunnerHandle::Get(),
61 file_path_)); 65 file_path_));
62 buf_ = new StringIOBuffer(kData); 66 buf_ = new StringIOBuffer(kData);
63 } 67 }
64 68
65 base::ScopedTempDir temp_dir_; 69 base::ScopedTempDir temp_dir_;
66 base::FilePath file_path_; 70 base::FilePath file_path_;
67 std::unique_ptr<URLFetcherFileWriter> writer_; 71 std::unique_ptr<URLFetcherFileWriter> writer_;
68 scoped_refptr<StringIOBuffer> buf_; 72 scoped_refptr<StringIOBuffer> buf_;
69 }; 73 };
70 74
71 TEST_F(URLFetcherFileWriterTest, WriteToFile) { 75 TEST_F(URLFetcherFileWriterTest, WriteToFile) {
72 int rv = 0; 76 int rv = 0;
73 // Initialize(), Write() and Finish(). 77 // Initialize(), Write() and Finish().
74 TestCompletionCallback callback; 78 TestCompletionCallback callback;
75 rv = writer_->Initialize(callback.callback()); 79 rv = writer_->Initialize(callback.callback());
76 EXPECT_EQ(OK, callback.GetResult(rv)); 80 EXPECT_THAT(callback.GetResult(rv), IsOk());
77 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); 81 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
78 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); 82 EXPECT_EQ(buf_->size(), callback.GetResult(rv));
79 rv = writer_->Finish(callback.callback()); 83 rv = writer_->Finish(callback.callback());
80 EXPECT_EQ(OK, callback.GetResult(rv)); 84 EXPECT_THAT(callback.GetResult(rv), IsOk());
81 85
82 // Verify the result. 86 // Verify the result.
83 EXPECT_EQ(file_path_.value(), writer_->file_path().value()); 87 EXPECT_EQ(file_path_.value(), writer_->file_path().value());
84 std::string file_contents; 88 std::string file_contents;
85 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); 89 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
86 EXPECT_EQ(kData, file_contents); 90 EXPECT_EQ(kData, file_contents);
87 91
88 // Destroy the writer. File should be deleted. 92 // Destroy the writer. File should be deleted.
89 writer_.reset(); 93 writer_.reset();
90 base::RunLoop().RunUntilIdle(); 94 base::RunLoop().RunUntilIdle();
91 EXPECT_FALSE(base::PathExists(file_path_)); 95 EXPECT_FALSE(base::PathExists(file_path_));
92 } 96 }
93 97
94 TEST_F(URLFetcherFileWriterTest, InitializeAgain) { 98 TEST_F(URLFetcherFileWriterTest, InitializeAgain) {
95 int rv = 0; 99 int rv = 0;
96 // Initialize(), Write() and Finish(). 100 // Initialize(), Write() and Finish().
97 TestCompletionCallback callback; 101 TestCompletionCallback callback;
98 rv = writer_->Initialize(callback.callback()); 102 rv = writer_->Initialize(callback.callback());
99 EXPECT_EQ(OK, callback.GetResult(rv)); 103 EXPECT_THAT(callback.GetResult(rv), IsOk());
100 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); 104 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
101 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); 105 EXPECT_EQ(buf_->size(), callback.GetResult(rv));
102 rv = writer_->Finish(callback.callback()); 106 rv = writer_->Finish(callback.callback());
103 EXPECT_EQ(OK, callback.GetResult(rv)); 107 EXPECT_THAT(callback.GetResult(rv), IsOk());
104 108
105 // Verify the result. 109 // Verify the result.
106 std::string file_contents; 110 std::string file_contents;
107 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); 111 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
108 EXPECT_EQ(kData, file_contents); 112 EXPECT_EQ(kData, file_contents);
109 113
110 // Initialize() again to reset. Write different data. 114 // Initialize() again to reset. Write different data.
111 const std::string data2 = "Bye!"; 115 const std::string data2 = "Bye!";
112 scoped_refptr<StringIOBuffer> buf2(new StringIOBuffer(data2)); 116 scoped_refptr<StringIOBuffer> buf2(new StringIOBuffer(data2));
113 117
114 rv = writer_->Initialize(callback.callback()); 118 rv = writer_->Initialize(callback.callback());
115 EXPECT_EQ(OK, callback.GetResult(rv)); 119 EXPECT_THAT(callback.GetResult(rv), IsOk());
116 rv = writer_->Write(buf2.get(), buf2->size(), callback.callback()); 120 rv = writer_->Write(buf2.get(), buf2->size(), callback.callback());
117 EXPECT_EQ(buf2->size(), callback.GetResult(rv)); 121 EXPECT_EQ(buf2->size(), callback.GetResult(rv));
118 rv = writer_->Finish(callback.callback()); 122 rv = writer_->Finish(callback.callback());
119 EXPECT_EQ(OK, callback.GetResult(rv)); 123 EXPECT_THAT(callback.GetResult(rv), IsOk());
120 124
121 // Verify the result. 125 // Verify the result.
122 file_contents.clear(); 126 file_contents.clear();
123 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); 127 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
124 EXPECT_EQ(data2, file_contents); 128 EXPECT_EQ(data2, file_contents);
125 } 129 }
126 130
127 TEST_F(URLFetcherFileWriterTest, DisownFile) { 131 TEST_F(URLFetcherFileWriterTest, DisownFile) {
128 int rv = 0; 132 int rv = 0;
129 // Initialize() and Finish() to create a file. 133 // Initialize() and Finish() to create a file.
130 TestCompletionCallback callback; 134 TestCompletionCallback callback;
131 rv = writer_->Initialize(callback.callback()); 135 rv = writer_->Initialize(callback.callback());
132 EXPECT_EQ(OK, callback.GetResult(rv)); 136 EXPECT_THAT(callback.GetResult(rv), IsOk());
133 rv = writer_->Finish(callback.callback()); 137 rv = writer_->Finish(callback.callback());
134 EXPECT_EQ(OK, callback.GetResult(rv)); 138 EXPECT_THAT(callback.GetResult(rv), IsOk());
135 139
136 // Disown file. 140 // Disown file.
137 writer_->DisownFile(); 141 writer_->DisownFile();
138 142
139 // File is not deleted even after the writer gets destroyed. 143 // File is not deleted even after the writer gets destroyed.
140 writer_.reset(); 144 writer_.reset();
141 base::RunLoop().RunUntilIdle(); 145 base::RunLoop().RunUntilIdle();
142 EXPECT_TRUE(base::PathExists(file_path_)); 146 EXPECT_TRUE(base::PathExists(file_path_));
143 } 147 }
144 148
145 class URLFetcherFileWriterTemporaryFileTest : public PlatformTest { 149 class URLFetcherFileWriterTemporaryFileTest : public PlatformTest {
146 protected: 150 protected:
147 void SetUp() override { 151 void SetUp() override {
148 writer_.reset(new URLFetcherFileWriter(base::ThreadTaskRunnerHandle::Get(), 152 writer_.reset(new URLFetcherFileWriter(base::ThreadTaskRunnerHandle::Get(),
149 base::FilePath())); 153 base::FilePath()));
150 buf_ = new StringIOBuffer(kData); 154 buf_ = new StringIOBuffer(kData);
151 } 155 }
152 156
153 std::unique_ptr<URLFetcherFileWriter> writer_; 157 std::unique_ptr<URLFetcherFileWriter> writer_;
154 scoped_refptr<StringIOBuffer> buf_; 158 scoped_refptr<StringIOBuffer> buf_;
155 }; 159 };
156 160
157 TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) { 161 TEST_F(URLFetcherFileWriterTemporaryFileTest, WriteToTemporaryFile) {
158 int rv = 0; 162 int rv = 0;
159 // Initialize(), Write() and Finish(). 163 // Initialize(), Write() and Finish().
160 TestCompletionCallback callback; 164 TestCompletionCallback callback;
161 rv = writer_->Initialize(callback.callback()); 165 rv = writer_->Initialize(callback.callback());
162 EXPECT_EQ(OK, callback.GetResult(rv)); 166 EXPECT_THAT(callback.GetResult(rv), IsOk());
163 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback()); 167 rv = writer_->Write(buf_.get(), buf_->size(), callback.callback());
164 EXPECT_EQ(buf_->size(), callback.GetResult(rv)); 168 EXPECT_EQ(buf_->size(), callback.GetResult(rv));
165 rv = writer_->Finish(callback.callback()); 169 rv = writer_->Finish(callback.callback());
166 EXPECT_EQ(OK, callback.GetResult(rv)); 170 EXPECT_THAT(callback.GetResult(rv), IsOk());
167 171
168 // Verify the result. 172 // Verify the result.
169 std::string file_contents; 173 std::string file_contents;
170 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents)); 174 EXPECT_TRUE(base::ReadFileToString(writer_->file_path(), &file_contents));
171 EXPECT_EQ(kData, file_contents); 175 EXPECT_EQ(kData, file_contents);
172 176
173 // Destroy the writer. File should be deleted. 177 // Destroy the writer. File should be deleted.
174 const base::FilePath file_path = writer_->file_path(); 178 const base::FilePath file_path = writer_->file_path();
175 writer_.reset(); 179 writer_.reset();
176 base::RunLoop().RunUntilIdle(); 180 base::RunLoop().RunUntilIdle();
177 EXPECT_FALSE(base::PathExists(file_path)); 181 EXPECT_FALSE(base::PathExists(file_path));
178 } 182 }
179 183
180 } // namespace net 184 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_fetcher_impl_unittest.cc ('k') | net/url_request/url_request_file_dir_job_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698