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

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: _ 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
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/bind.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/scoped_temp_dir.h" 11 #include "base/scoped_temp_dir.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h" 17 #include "testing/platform_test.h"
18 18
19 namespace net { 19 namespace net {
20 20
21 // Simplified version of TestCompletionCallback for ContentLengthCallback, 21 class UploadDataTest : public PlatformTest {
22 // that handles uint64 rather than int. 22 protected:
23 class TestContentLengthCallback { 23 virtual void SetUp() OVERRIDE {
24 public: 24 upload_data_= new UploadData;
25 TestContentLengthCallback()
26 : result_(0),
27 callback_(ALLOW_THIS_IN_INITIALIZER_LIST(
28 base::Bind(&TestContentLengthCallback::SetResult,
29 base::Unretained(this)))) {
30 } 25 }
31 26
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_; 27 scoped_refptr<UploadData> upload_data_;
88 }; 28 };
89 29
90 TEST_F(UploadDataTest, IsInMemory_Empty) { 30 TEST_F(UploadDataTest, IsInMemory_Empty) {
91 ASSERT_TRUE(upload_data_->IsInMemory()); 31 ASSERT_TRUE(upload_data_->IsInMemory());
92 } 32 }
93 33
94 TEST_F(UploadDataTest, IsInMemory_Bytes) { 34 TEST_F(UploadDataTest, IsInMemory_Bytes) {
95 upload_data_->AppendBytes("123", 3); 35 upload_data_->AppendBytes("123", 3);
96 ASSERT_TRUE(upload_data_->IsInMemory()); 36 ASSERT_TRUE(upload_data_->IsInMemory());
(...skipping 17 matching lines...) Expand all
114 upload_data_->AppendBytes("123", 3); 54 upload_data_->AppendBytes("123", 3);
115 upload_data_->AppendBytes("abc", 3); 55 upload_data_->AppendBytes("abc", 3);
116 ASSERT_TRUE(upload_data_->IsInMemory()); 56 ASSERT_TRUE(upload_data_->IsInMemory());
117 57
118 upload_data_->AppendFileRange( 58 upload_data_->AppendFileRange(
119 FilePath(FILE_PATH_LITERAL("random_file_name.txt")), 59 FilePath(FILE_PATH_LITERAL("random_file_name.txt")),
120 0, 0, base::Time()); 60 0, 0, base::Time());
121 ASSERT_FALSE(upload_data_->IsInMemory()); 61 ASSERT_FALSE(upload_data_->IsInMemory());
122 } 62 }
123 63
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 64 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698