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

Unified Diff: net/base/upload_data_unittest.cc

Issue 9321003: net: Make UploadData::GetContentLength() asynchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 8 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/upload_data_stream_unittest.cc ('k') | net/http/http_network_transaction_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/upload_data_unittest.cc
diff --git a/net/base/upload_data_unittest.cc b/net/base/upload_data_unittest.cc
index 5d56523ff41bbbb1f9432b53a70adb5a7fa9bf5b..ac690c76d507411894de553d938d01e88c9715a4 100644
--- a/net/base/upload_data_unittest.cc
+++ b/net/base/upload_data_unittest.cc
@@ -4,58 +4,177 @@
#include "net/base/upload_data.h"
+#include "base/bind.h"
#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/message_loop.h"
+#include "base/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
+#include "base/threading/thread_restrictions.h"
#include "base/time.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
namespace net {
-TEST(UploadDataTest, IsInMemory_Empty) {
- scoped_refptr<UploadData> upload_data = new UploadData;
- ASSERT_TRUE(upload_data->IsInMemory());
+// Simplified version of TestCompletionCallback for ContentLengthCallback,
+// that handles uint64 rather than int.
+class TestContentLengthCallback {
+ public:
+ TestContentLengthCallback()
+ : result_(0),
+ callback_(ALLOW_THIS_IN_INITIALIZER_LIST(
+ base::Bind(&TestContentLengthCallback::SetResult,
+ base::Unretained(this)))) {
+ }
+
+ ~TestContentLengthCallback() {}
+
+ const UploadData::ContentLengthCallback& callback() const {
+ return callback_;
+ }
+
+ // Waits for the result and returns it.
+ uint64 WaitForResult() {
+ MessageLoop::current()->Run();
+ return result_;
+ }
+
+ private:
+ // Sets the result and stops the message loop.
+ void SetResult(uint64 result) {
+ result_ = result;
+ MessageLoop::current()->Quit();
+ }
+
+ uint64 result_;
+ const UploadData::ContentLengthCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestContentLengthCallback);
+};
+
+class UploadDataTest : public PlatformTest {
willchan no longer on Chromium 2012/02/09 19:15:04 You should probably move the protected keyword up
satorux1 2012/02/09 19:25:22 Done.
+ virtual void SetUp() {
+ upload_data_= new UploadData;
+ // To ensure that file IO is not performed on the main thread in the
+ // test (i.e. GetContentLengthSync() will fail if file IO is performed).
+ base::ThreadRestrictions::SetIOAllowed(false);
+ }
+
+ virtual void TearDown() {
+ base::ThreadRestrictions::SetIOAllowed(true);
+ }
+
+ protected:
+ // Creates a temporary file with the given data. The temporary file is
+ // deleted by temp_dir_. Returns true on success.
+ bool CreateTemporaryFile(const std::string& data,
+ FilePath* temp_file_path) {
+ base::ThreadRestrictions::ScopedAllowIO allow_io;
+ if (!temp_dir_.CreateUniqueTempDir())
+ return false;
+ if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), temp_file_path))
+ return false;
+ if (static_cast<int>(data.size()) !=
+ file_util::WriteFile(*temp_file_path, data.data(), data.size()))
+ return false;
+
+ return true;
+ }
+
+ ScopedTempDir temp_dir_;
+ scoped_refptr<UploadData> upload_data_;
+};
+
+TEST_F(UploadDataTest, IsInMemory_Empty) {
+ ASSERT_TRUE(upload_data_->IsInMemory());
}
-TEST(UploadDataTest, IsInMemory_Bytes) {
- scoped_refptr<UploadData> upload_data = new UploadData;
- upload_data->AppendBytes("123", 3);
- ASSERT_TRUE(upload_data->IsInMemory());
+TEST_F(UploadDataTest, IsInMemory_Bytes) {
+ upload_data_->AppendBytes("123", 3);
+ ASSERT_TRUE(upload_data_->IsInMemory());
}
-TEST(UploadDataTest, IsInMemory_File) {
- scoped_refptr<UploadData> upload_data = new UploadData;
- upload_data->AppendFileRange(
+TEST_F(UploadDataTest, IsInMemory_File) {
+ upload_data_->AppendFileRange(
FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
0, 0, base::Time());
- ASSERT_FALSE(upload_data->IsInMemory());
+ ASSERT_FALSE(upload_data_->IsInMemory());
}
-TEST(UploadDataTest, IsInMemory_Blob) {
- scoped_refptr<UploadData> upload_data = new UploadData;
- upload_data->AppendBlob(GURL("blog:internal:12345"));
+TEST_F(UploadDataTest, IsInMemory_Blob) {
+ upload_data_->AppendBlob(GURL("blog:internal:12345"));
// Until it's resolved, we don't know what blob contains.
- ASSERT_FALSE(upload_data->IsInMemory());
+ ASSERT_FALSE(upload_data_->IsInMemory());
}
-TEST(UploadDataTest, IsInMemory_Chunk) {
- scoped_refptr<UploadData> upload_data = new UploadData;
- upload_data->set_is_chunked(true);
- ASSERT_FALSE(upload_data->IsInMemory());
+TEST_F(UploadDataTest, IsInMemory_Chunk) {
+ upload_data_->set_is_chunked(true);
+ ASSERT_FALSE(upload_data_->IsInMemory());
}
-TEST(UploadDataTest, IsInMemory_Mixed) {
- scoped_refptr<UploadData> upload_data = new UploadData;
- ASSERT_TRUE(upload_data->IsInMemory());
+TEST_F(UploadDataTest, IsInMemory_Mixed) {
+ ASSERT_TRUE(upload_data_->IsInMemory());
- upload_data->AppendBytes("123", 3);
- upload_data->AppendBytes("abc", 3);
- ASSERT_TRUE(upload_data->IsInMemory());
+ upload_data_->AppendBytes("123", 3);
+ upload_data_->AppendBytes("abc", 3);
+ ASSERT_TRUE(upload_data_->IsInMemory());
- upload_data->AppendFileRange(
+ upload_data_->AppendFileRange(
FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
0, 0, base::Time());
- ASSERT_FALSE(upload_data->IsInMemory());
+ ASSERT_FALSE(upload_data_->IsInMemory());
+}
+
+TEST_F(UploadDataTest, GetContentLength_Empty) {
+ ASSERT_EQ(0U, upload_data_->GetContentLengthSync());
+}
+
+TEST_F(UploadDataTest, GetContentLength_Bytes) {
+ upload_data_->AppendBytes("123", 3);
+ ASSERT_EQ(3U, upload_data_->GetContentLengthSync());
+}
+
+TEST_F(UploadDataTest, GetContentLength_File) {
+ // Create a temporary file with some data.
+ const std::string kData = "hello";
+ FilePath temp_file_path;
+ ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path));
+ upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time());
+
+ // The lengh is returned asynchronously.
willchan no longer on Chromium 2012/02/09 19:15:04 length
satorux1 2012/02/09 19:25:22 Done.
+ TestContentLengthCallback callback;
+ upload_data_->GetContentLength(callback.callback());
+ ASSERT_EQ(kData.size(), callback.WaitForResult());
+}
+
+TEST_F(UploadDataTest, GetContentLength_Blob) {
+ upload_data_->AppendBlob(GURL("blog:internal:12345"));
+ ASSERT_EQ(0U, upload_data_->GetContentLengthSync());
+}
+
+TEST_F(UploadDataTest, GetContentLength_Chunk) {
+ upload_data_->set_is_chunked(true);
+ ASSERT_EQ(0U, upload_data_->GetContentLengthSync());
+}
+
+TEST_F(UploadDataTest, GetContentLength_Mixed) {
+ upload_data_->AppendBytes("123", 3);
+ upload_data_->AppendBytes("abc", 3);
+
+ const uint64 content_length = upload_data_->GetContentLengthSync();
+ ASSERT_EQ(6U, content_length);
+
+ // Append a file.
+ const std::string kData = "hello";
+ FilePath temp_file_path;
+ ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path));
+ upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time());
+
+ TestContentLengthCallback callback;
+ upload_data_->GetContentLength(callback.callback());
+ ASSERT_EQ(content_length + kData.size(), callback.WaitForResult());
}
} // namespace net
« no previous file with comments | « net/base/upload_data_stream_unittest.cc ('k') | net/http/http_network_transaction_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698