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

Side by Side Diff: net/base/upload_data_unittest.cc

Issue 10878082: net: Move file operation code from UploadData to UploadDataStream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/base/upload_data.h"
6
7 #include "base/bind.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"
12 #include "base/memory/ref_counted.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "base/time.h"
15 #include "googleurl/src/gurl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
18
19 namespace net {
20
21 // Simplified version of TestCompletionCallback for ContentLengthCallback,
22 // that handles uint64 rather than int.
23 class TestContentLengthCallback {
24 public:
25 TestContentLengthCallback()
26 : result_(0),
27 callback_(ALLOW_THIS_IN_INITIALIZER_LIST(
28 base::Bind(&TestContentLengthCallback::SetResult,
29 base::Unretained(this)))) {
30 }
31
32 ~TestContentLengthCallback() {}
33
34 const UploadData::ContentLengthCallback& callback() const {
35 return callback_;
36 }
37
38 // Waits for the result and returns it.
39 uint64 WaitForResult() {
40 MessageLoop::current()->Run();
41 return result_;
42 }
43
44 private:
45 // Sets the result and stops the message loop.
46 void SetResult(uint64 result) {
47 result_ = result;
48 MessageLoop::current()->Quit();
49 }
50
51 uint64 result_;
52 const UploadData::ContentLengthCallback callback_;
53
54 DISALLOW_COPY_AND_ASSIGN(TestContentLengthCallback);
55 };
56
57 class UploadDataTest : public PlatformTest {
58 protected:
59 virtual void SetUp() {
60 upload_data_= new UploadData;
61 // To ensure that file IO is not performed on the main thread in the
62 // test (i.e. GetContentLengthSync() will fail if file IO is performed).
63 base::ThreadRestrictions::SetIOAllowed(false);
64 }
65
66 virtual void TearDown() {
67 base::ThreadRestrictions::SetIOAllowed(true);
68 }
69
70 // Creates a temporary file with the given data. The temporary file is
71 // deleted by temp_dir_. Returns true on success.
72 bool CreateTemporaryFile(const std::string& data,
73 FilePath* temp_file_path) {
74 base::ThreadRestrictions::ScopedAllowIO allow_io;
75 if (!temp_dir_.CreateUniqueTempDir())
76 return false;
77 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), temp_file_path))
78 return false;
79 if (static_cast<int>(data.size()) !=
80 file_util::WriteFile(*temp_file_path, data.data(), data.size()))
81 return false;
82
83 return true;
84 }
85
86 ScopedTempDir temp_dir_;
87 scoped_refptr<UploadData> upload_data_;
88 };
89
90 TEST_F(UploadDataTest, IsInMemory_Empty) {
91 ASSERT_TRUE(upload_data_->IsInMemory());
92 }
93
94 TEST_F(UploadDataTest, IsInMemory_Bytes) {
95 upload_data_->AppendBytes("123", 3);
96 ASSERT_TRUE(upload_data_->IsInMemory());
97 }
98
99 TEST_F(UploadDataTest, IsInMemory_File) {
100 upload_data_->AppendFileRange(
101 FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
102 0, 0, base::Time());
103 ASSERT_FALSE(upload_data_->IsInMemory());
104 }
105
106 TEST_F(UploadDataTest, IsInMemory_Chunk) {
107 upload_data_->set_is_chunked(true);
108 ASSERT_FALSE(upload_data_->IsInMemory());
109 }
110
111 TEST_F(UploadDataTest, IsInMemory_Mixed) {
112 ASSERT_TRUE(upload_data_->IsInMemory());
113
114 upload_data_->AppendBytes("123", 3);
115 upload_data_->AppendBytes("abc", 3);
116 ASSERT_TRUE(upload_data_->IsInMemory());
117
118 upload_data_->AppendFileRange(
119 FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
120 0, 0, base::Time());
121 ASSERT_FALSE(upload_data_->IsInMemory());
122 }
123
124 TEST_F(UploadDataTest, GetContentLength_Empty) {
125 ASSERT_EQ(0U, upload_data_->GetContentLengthSync());
126 }
127
128 TEST_F(UploadDataTest, GetContentLength_Bytes) {
129 upload_data_->AppendBytes("123", 3);
130 ASSERT_EQ(3U, upload_data_->GetContentLengthSync());
131 }
132
133 TEST_F(UploadDataTest, GetContentLength_File) {
134 // Create a temporary file with some data.
135 const std::string kData = "hello";
136 FilePath temp_file_path;
137 ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path));
138 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time());
139
140 // The length is returned asynchronously.
141 TestContentLengthCallback callback;
142 upload_data_->GetContentLength(callback.callback());
143 ASSERT_EQ(kData.size(), callback.WaitForResult());
144 }
145
146 TEST_F(UploadDataTest, GetContentLength_Chunk) {
147 upload_data_->set_is_chunked(true);
148 ASSERT_EQ(0U, upload_data_->GetContentLengthSync());
149 }
150
151 TEST_F(UploadDataTest, GetContentLength_Mixed) {
152 upload_data_->AppendBytes("123", 3);
153 upload_data_->AppendBytes("abc", 3);
154
155 const uint64 content_length = upload_data_->GetContentLengthSync();
156 ASSERT_EQ(6U, content_length);
157
158 // Append a file.
159 const std::string kData = "hello";
160 FilePath temp_file_path;
161 ASSERT_TRUE(CreateTemporaryFile(kData, &temp_file_path));
162 upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time());
163
164 TestContentLengthCallback callback;
165 upload_data_->GetContentLength(callback.callback());
166 ASSERT_EQ(content_length + kData.size(), callback.WaitForResult());
167 }
168
169 } // namespace net
OLDNEW
« no previous file with comments | « net/base/upload_data_stream_unittest.cc ('k') | net/http/http_network_transaction_spdy2_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698