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 "content/child/web_data_consumer_handle_impl.h" | 5 #include "content/child/web_data_consumer_handle_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 | 186 |
| 187 class WebDataConsumerHandleImplTest : public ::testing::Test { | 187 class WebDataConsumerHandleImplTest : public ::testing::Test { |
| 188 public: | 188 public: |
| 189 typedef WebDataConsumerHandle::Result Result; | 189 typedef WebDataConsumerHandle::Result Result; |
| 190 | 190 |
| 191 void SetUp() override { | 191 void SetUp() override { |
| 192 MojoCreateDataPipeOptions options; | 192 MojoCreateDataPipeOptions options; |
| 193 options.struct_size = sizeof(MojoCreateDataPipeOptions); | 193 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 194 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 194 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 195 options.element_num_bytes = 1; | 195 options.element_num_bytes = 1; |
| 196 options.capacity_num_bytes = 4; | 196 options.capacity_num_bytes = kDataPipeCapacity; |
| 197 | 197 |
| 198 MojoResult result = mojo::CreateDataPipe(&options, &producer_, &consumer_); | 198 MojoResult result = mojo::CreateDataPipe(&options, &producer_, &consumer_); |
| 199 ASSERT_EQ(MOJO_RESULT_OK, result); | 199 ASSERT_EQ(MOJO_RESULT_OK, result); |
| 200 } | 200 } |
| 201 | 201 |
| 202 protected: | |
| 203 static int kDataPipeCapacity; | |
|
yhirano
2016/09/23 06:55:55
+ constexpr
horo
2016/09/23 07:38:37
Done.
| |
| 202 // This function can be blocked if the associated consumer doesn't consume | 204 // This function can be blocked if the associated consumer doesn't consume |
| 203 // the data. | 205 // the data. |
| 204 std::string ProduceData(size_t total_size) { | 206 std::string ProduceData(size_t total_size) { |
| 205 int index = 0; | 207 int index = 0; |
| 206 std::string expected; | 208 std::string expected; |
| 207 for (size_t i = 0; i < total_size; ++i) { | 209 for (size_t i = 0; i < total_size; ++i) { |
| 208 expected += static_cast<char>(index + 'a'); | 210 expected += static_cast<char>(index + 'a'); |
| 209 index = (37 * index + 11) % 26; | 211 index = (37 * index + 11) % 26; |
| 210 } | 212 } |
| 211 | 213 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 227 } | 229 } |
| 228 return expected; | 230 return expected; |
| 229 } | 231 } |
| 230 | 232 |
| 231 base::MessageLoop message_loop_; | 233 base::MessageLoop message_loop_; |
| 232 | 234 |
| 233 mojo::ScopedDataPipeProducerHandle producer_; | 235 mojo::ScopedDataPipeProducerHandle producer_; |
| 234 mojo::ScopedDataPipeConsumerHandle consumer_; | 236 mojo::ScopedDataPipeConsumerHandle consumer_; |
| 235 }; | 237 }; |
| 236 | 238 |
| 239 int WebDataConsumerHandleImplTest::kDataPipeCapacity = 4; | |
| 240 | |
| 237 TEST_F(WebDataConsumerHandleImplTest, ReadData) { | 241 TEST_F(WebDataConsumerHandleImplTest, ReadData) { |
| 238 base::RunLoop run_loop; | 242 base::RunLoop run_loop; |
| 239 auto operation = base::MakeUnique<ReadDataOperation>( | 243 auto operation = base::MakeUnique<ReadDataOperation>( |
| 240 std::move(consumer_), &message_loop_, run_loop.QuitClosure()); | 244 std::move(consumer_), &message_loop_, run_loop.QuitClosure()); |
| 241 | 245 |
| 242 base::Thread t("DataConsumerHandle test thread"); | 246 base::Thread t("DataConsumerHandle test thread"); |
| 243 ASSERT_TRUE(t.Start()); | 247 ASSERT_TRUE(t.Start()); |
| 244 | 248 |
| 245 t.task_runner()->PostTask(FROM_HERE, | 249 t.task_runner()->PostTask(FROM_HERE, |
| 246 base::Bind(&ReadDataOperation::ReadData, | 250 base::Bind(&ReadDataOperation::ReadData, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 269 | 273 |
| 270 std::string expected = ProduceData(24 * 1024); | 274 std::string expected = ProduceData(24 * 1024); |
| 271 producer_.reset(); | 275 producer_.reset(); |
| 272 | 276 |
| 273 run_loop.Run(); | 277 run_loop.Run(); |
| 274 t.Stop(); | 278 t.Stop(); |
| 275 | 279 |
| 276 EXPECT_EQ(expected, operation->result()); | 280 EXPECT_EQ(expected, operation->result()); |
| 277 } | 281 } |
| 278 | 282 |
| 283 TEST_F(WebDataConsumerHandleImplTest, ZeroSizeRead) { | |
| 284 ASSERT_GT(kDataPipeCapacity - 1, 0); | |
| 285 const size_t data_size = kDataPipeCapacity - 1; | |
|
yhirano
2016/09/23 06:55:55
constexpr
horo
2016/09/23 07:38:37
Done.
| |
| 286 std::string expected = ProduceData(data_size); | |
| 287 producer_.reset(); | |
| 288 std::unique_ptr<WebDataConsumerHandleImpl> handle( | |
| 289 new WebDataConsumerHandleImpl(std::move(consumer_))); | |
| 290 std::unique_ptr<WebDataConsumerHandle::Reader> reader( | |
| 291 handle->obtainReader(nullptr)); | |
| 292 | |
| 293 size_t read_size; | |
| 294 WebDataConsumerHandle::Result rv = | |
| 295 reader->read(nullptr, 0, WebDataConsumerHandle::FlagNone, &read_size); | |
| 296 EXPECT_EQ(WebDataConsumerHandle::Result::Ok, rv); | |
| 297 | |
| 298 char buffer[16]; | |
| 299 rv = reader->read(&buffer, sizeof(buffer), WebDataConsumerHandle::FlagNone, | |
| 300 &read_size); | |
| 301 EXPECT_EQ(WebDataConsumerHandle::Result::Ok, rv); | |
| 302 EXPECT_EQ(data_size, read_size); | |
| 303 EXPECT_EQ(expected, std::string(buffer, read_size)); | |
| 304 } | |
| 305 | |
| 279 } // namespace | 306 } // namespace |
| 280 | 307 |
| 281 } // namespace content | 308 } // namespace content |
| OLD | NEW |