Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base/upload_file_element_reader.h" | 5 #include "net/base/upload_file_element_reader.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/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 "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 class UploadFileElementReaderTest : public PlatformTest { | 19 class UploadFileElementReaderTest : public PlatformTest { |
| 19 protected: | 20 protected: |
| 20 virtual void SetUp() OVERRIDE { | 21 virtual void SetUp() OVERRIDE { |
| 22 PlatformTest::SetUp(); | |
| 23 | |
| 21 // Some tests (*.ReadPartially) rely on bytes_.size() being even. | 24 // Some tests (*.ReadPartially) rely on bytes_.size() being even. |
| 22 const char kData[] = "123456789abcdefghi"; | 25 const char kData[] = "123456789abcdefghi"; |
| 23 bytes_.assign(kData, kData + arraysize(kData) - 1); | 26 bytes_.assign(kData, kData + arraysize(kData) - 1); |
| 24 | 27 |
| 25 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 26 | 29 |
| 27 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 30 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 28 &temp_file_path_)); | 31 &temp_file_path_)); |
| 29 ASSERT_EQ( | 32 ASSERT_EQ( |
| 30 static_cast<int>(bytes_.size()), | 33 static_cast<int>(bytes_.size()), |
| 31 file_util::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); | 34 file_util::WriteFile(temp_file_path_, &bytes_[0], bytes_.size())); |
| 32 | 35 |
| 33 reader_.reset( | 36 reader_.reset( |
| 34 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | 37 new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 35 temp_file_path_, | 38 temp_file_path_, |
| 36 0, | 39 0, |
| 37 kuint64max, | 40 kuint64max, |
| 38 base::Time())); | 41 base::Time())); |
| 39 TestCompletionCallback callback; | 42 TestCompletionCallback callback; |
| 40 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); | 43 ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback())); |
| 41 EXPECT_EQ(OK, callback.WaitForResult()); | 44 EXPECT_EQ(OK, callback.WaitForResult()); |
| 42 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); | 45 EXPECT_EQ(bytes_.size(), reader_->GetContentLength()); |
| 43 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); | 46 EXPECT_EQ(bytes_.size(), reader_->BytesRemaining()); |
| 44 EXPECT_FALSE(reader_->IsInMemory()); | 47 EXPECT_FALSE(reader_->IsInMemory()); |
| 45 } | 48 } |
| 46 | 49 |
| 50 virtual void TearDown() OVERRIDE { | |
| 51 reader_.reset(); | |
| 52 base::RunLoop().RunUntilIdle(); | |
| 53 PlatformTest::TearDown(); | |
| 54 } | |
|
Ryan Sleevi
2013/09/13 18:47:10
Ditto here - it should be possible to update these
| |
| 55 | |
| 47 std::vector<char> bytes_; | 56 std::vector<char> bytes_; |
| 48 scoped_ptr<UploadElementReader> reader_; | 57 scoped_ptr<UploadElementReader> reader_; |
| 49 base::ScopedTempDir temp_dir_; | 58 base::ScopedTempDir temp_dir_; |
| 50 base::FilePath temp_file_path_; | 59 base::FilePath temp_file_path_; |
| 51 }; | 60 }; |
| 52 | 61 |
| 53 TEST_F(UploadFileElementReaderTest, ReadPartially) { | 62 TEST_F(UploadFileElementReaderTest, ReadPartially) { |
| 54 const size_t kHalfSize = bytes_.size() / 2; | 63 const size_t kHalfSize = bytes_.size() / 2; |
| 55 ASSERT_EQ(bytes_.size(), kHalfSize * 2); | 64 ASSERT_EQ(bytes_.size(), kHalfSize * 2); |
| 56 std::vector<char> buf(kHalfSize); | 65 std::vector<char> buf(kHalfSize); |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 TEST_F(UploadFileElementReaderSyncTest, WrongPath) { | 367 TEST_F(UploadFileElementReaderSyncTest, WrongPath) { |
| 359 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); | 368 const base::FilePath wrong_path(FILE_PATH_LITERAL("wrong_path")); |
| 360 reader_.reset(new UploadFileElementReaderSync( | 369 reader_.reset(new UploadFileElementReaderSync( |
| 361 wrong_path, 0, kuint64max, base::Time())); | 370 wrong_path, 0, kuint64max, base::Time())); |
| 362 ASSERT_EQ(OK, reader_->Init(CompletionCallback())); | 371 ASSERT_EQ(OK, reader_->Init(CompletionCallback())); |
| 363 EXPECT_EQ(0U, reader_->GetContentLength()); | 372 EXPECT_EQ(0U, reader_->GetContentLength()); |
| 364 EXPECT_EQ(0U, reader_->BytesRemaining()); | 373 EXPECT_EQ(0U, reader_->BytesRemaining()); |
| 365 } | 374 } |
| 366 | 375 |
| 367 } // namespace net | 376 } // namespace net |
| OLD | NEW |