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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_data.h" 5 #include "net/base/upload_data.h"
6 6
7 #include "base/bind.h"
7 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/message_loop.h"
11 #include "base/scoped_temp_dir.h"
8 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
9 #include "base/time.h" 13 #include "base/time.h"
10 #include "googleurl/src/gurl.h" 14 #include "googleurl/src/gurl.h"
11 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
12 17
13 namespace net { 18 namespace net {
14 19
15 TEST(UploadDataTest, IsInMemory_Empty) { 20 // Simplified version of TestCompletionCallback for ContentLengthCallback,
16 scoped_refptr<UploadData> upload_data = new UploadData; 21 // that handles uint64 rather than int.
17 ASSERT_TRUE(upload_data->IsInMemory()); 22 class TestContentLengthCallback {
23 public:
24 TestContentLengthCallback()
25 : result_(0),
26 callback_(ALLOW_THIS_IN_INITIALIZER_LIST(
27 base::Bind(&TestContentLengthCallback::SetResult,
28 base::Unretained(this)))) {
29 }
30
31 ~TestContentLengthCallback() {}
32
33 const UploadData::ContentLengthCallback& callback() const {
34 return callback_;
35 }
36
37 // Waits for the result and returns it.
38 uint64 WaitForResult() {
39 MessageLoop::current()->Run();
40 return result_;
41 }
42
43 private:
44 // Sets the result and stops the message loop.
45 void SetResult(uint64 result) {
46 result_ = result;
47 MessageLoop::current()->Quit();
48 }
49
50 uint64 result_;
51 const UploadData::ContentLengthCallback callback_;
52
53 DISALLOW_COPY_AND_ASSIGN(TestContentLengthCallback);
54 };
55
56 class UploadDataTest : public PlatformTest {
57 virtual void SetUp() {
58 upload_data_= new UploadData;
59 }
60
61 protected:
62 // Creates a temporary file with the given data. The temporary file is
63 // deleted by temp_dir_. Returns true on success.
64 bool CreateTemporaryFile(const std::string& data,
65 FilePath* temp_file_path) {
66 if (!temp_dir_.CreateUniqueTempDir())
67 return false;
68 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), temp_file_path))
69 return false;
70 if (static_cast<int>(data.size()) !=
71 file_util::WriteFile(*temp_file_path, data.data(), data.size()))
72 return false;
73
74 return true;
75 }
76
77 ScopedTempDir temp_dir_;
78 scoped_refptr<UploadData> upload_data_;
79 };
80
81 TEST_F(UploadDataTest, IsInMemory_Empty) {
82 ASSERT_TRUE(upload_data_->IsInMemory());
18 } 83 }
19 84
20 TEST(UploadDataTest, IsInMemory_Bytes) { 85 TEST_F(UploadDataTest, IsInMemory_Bytes) {
21 scoped_refptr<UploadData> upload_data = new UploadData; 86 upload_data_->AppendBytes("123", 3);
22 upload_data->AppendBytes("123", 3); 87 ASSERT_TRUE(upload_data_->IsInMemory());
23 ASSERT_TRUE(upload_data->IsInMemory());
24 } 88 }
25 89
26 TEST(UploadDataTest, IsInMemory_File) { 90 TEST_F(UploadDataTest, IsInMemory_File) {
27 scoped_refptr<UploadData> upload_data = new UploadData; 91 upload_data_->AppendFileRange(
28 upload_data->AppendFileRange(
29 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), 92 FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
30 0, 0, base::Time()); 93 0, 0, base::Time());
31 ASSERT_FALSE(upload_data->IsInMemory()); 94 ASSERT_FALSE(upload_data_->IsInMemory());
32 } 95 }
33 96
34 TEST(UploadDataTest, IsInMemory_Blob) { 97 TEST_F(UploadDataTest, IsInMemory_Blob) {
35 scoped_refptr<UploadData> upload_data = new UploadData; 98 upload_data_->AppendBlob(GURL("blog:internal:12345"));
36 upload_data->AppendBlob(GURL("blog:internal:12345"));
37 // Until it's resolved, we don't know what blob contains. 99 // Until it's resolved, we don't know what blob contains.
38 ASSERT_FALSE(upload_data->IsInMemory()); 100 ASSERT_FALSE(upload_data_->IsInMemory());
39 } 101 }
40 102
41 TEST(UploadDataTest, IsInMemory_Chunk) { 103 TEST_F(UploadDataTest, IsInMemory_Chunk) {
42 scoped_refptr<UploadData> upload_data = new UploadData; 104 upload_data_->set_is_chunked(true);
43 upload_data->set_is_chunked(true); 105 ASSERT_FALSE(upload_data_->IsInMemory());
44 ASSERT_FALSE(upload_data->IsInMemory());
45 } 106 }
46 107
47 TEST(UploadDataTest, IsInMemory_Mixed) { 108 TEST_F(UploadDataTest, IsInMemory_Mixed) {
48 scoped_refptr<UploadData> upload_data = new UploadData; 109 ASSERT_TRUE(upload_data_->IsInMemory());
49 ASSERT_TRUE(upload_data->IsInMemory());
50 110
51 upload_data->AppendBytes("123", 3); 111 upload_data_->AppendBytes("123", 3);
52 upload_data->AppendBytes("abc", 3); 112 upload_data_->AppendBytes("abc", 3);
53 ASSERT_TRUE(upload_data->IsInMemory()); 113 ASSERT_TRUE(upload_data_->IsInMemory());
54 114
55 upload_data->AppendFileRange( 115 upload_data_->AppendFileRange(
56 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), 116 FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
57 0, 0, base::Time()); 117 0, 0, base::Time());
58 ASSERT_FALSE(upload_data->IsInMemory()); 118 ASSERT_FALSE(upload_data_->IsInMemory());
119 }
120
121 TEST_F(UploadDataTest, GetContentLength_Empty) {
122 TestContentLengthCallback callback;
123 upload_data_->GetContentLength(callback.callback());
124 ASSERT_EQ(0U, callback.WaitForResult());
125 }
126
127 TEST_F(UploadDataTest, GetContentLength_Bytes) {
128 upload_data_->AppendBytes("123", 3);
129
130 TestContentLengthCallback callback;
131 upload_data_->GetContentLength(callback.callback());
132 ASSERT_EQ(3U, callback.WaitForResult());
133 }
134
135 TEST_F(UploadDataTest, GetContentLength_File) {
136 // Create a temporary file with some data.
137 const std::string kData = "hello";
138 FilePath temp_file_path;
139 ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path));
140 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time());
141
142 // The lengh is returned asynchronously.
143 TestContentLengthCallback callback;
144 upload_data_->GetContentLength(callback.callback());
145 ASSERT_EQ(kData.size(), callback.WaitForResult());
146 }
147
148 TEST_F(UploadDataTest, GetContentLength_Blob) {
149 upload_data_->AppendBlob(GURL("blog:internal:12345"));
150
151 TestContentLengthCallback callback;
152 upload_data_->GetContentLength(callback.callback());
153 ASSERT_EQ(0U, callback.WaitForResult());
154 }
155
156 TEST_F(UploadDataTest, GetContentLength_Chunk) {
157 upload_data_->set_is_chunked(true);
158
159 TestContentLengthCallback callback;
160 upload_data_->GetContentLength(callback.callback());
161 ASSERT_EQ(0U, callback.WaitForResult());
162 }
163
164 TEST_F(UploadDataTest, GetContentLength_Mixed) {
165 upload_data_->AppendBytes("123", 3);
166 upload_data_->AppendBytes("abc", 3);
167
168 TestContentLengthCallback callback;
169 upload_data_->GetContentLength(callback.callback());
170 const uint64 content_length = callback.WaitForResult();
171 ASSERT_EQ(6U, content_length);
172
173 // Append a file.
174 const std::string kData = "hello";
175 FilePath temp_file_path;
176 ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path));
177 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time());
178
179 TestContentLengthCallback callback1;
180 upload_data_->GetContentLength(callback1.callback());
181 ASSERT_EQ(content_length + kData.size(), callback1.WaitForResult());
59 } 182 }
60 183
61 } // namespace net 184 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698