| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/callback_old.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 10 #include "remoting/base/compound_buffer.h" | 11 #include "remoting/base/compound_buffer.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 using net::IOBuffer; | 14 using net::IOBuffer; |
| 14 | 15 |
| 15 namespace remoting { | 16 namespace remoting { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 virtual void SetUp() { | 76 virtual void SetUp() { |
| 76 data_ = new IOBuffer(kDataSize); | 77 data_ = new IOBuffer(kDataSize); |
| 77 for (int i = 0; i < kDataSize; ++i) { | 78 for (int i = 0; i < kDataSize; ++i) { |
| 78 data_->data()[i] = i; | 79 data_->data()[i] = i; |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 82 // Iterate over chunks of data with sizes specified in |sizes| in the | 83 // Iterate over chunks of data with sizes specified in |sizes| in the |
| 83 // interval [0..kDataSize]. |function| is called for each chunk. | 84 // interval [0..kDataSize]. |function| is called for each chunk. |
| 84 void IterateOverPieces(const int sizes[], | 85 void IterateOverPieces(const int sizes[], |
| 85 Callback2<int, int>::Type* function) { | 86 const base::Callback<void(int, int)>& function) { |
| 86 DCHECK_GT(sizes[0], 0); | 87 DCHECK_GT(sizes[0], 0); |
| 87 | 88 |
| 88 int pos = 0; | 89 int pos = 0; |
| 89 int index = 0; | 90 int index = 0; |
| 90 while (pos < kDataSize) { | 91 while (pos < kDataSize) { |
| 91 int size = std::min(sizes[index], kDataSize - pos); | 92 int size = std::min(sizes[index], kDataSize - pos); |
| 92 ++index; | 93 ++index; |
| 93 if (sizes[index] <= 0) | 94 if (sizes[index] <= 0) |
| 94 index = 0; | 95 index = 0; |
| 95 | 96 |
| 96 function->Run(pos, size); | 97 function.Run(pos, size); |
| 97 | 98 |
| 98 pos += size; | 99 pos += size; |
| 99 } | 100 } |
| 100 delete function; | |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool CompareData(const CompoundBuffer& buffer, char* data, int size) { | 103 bool CompareData(const CompoundBuffer& buffer, char* data, int size) { |
| 104 scoped_refptr<IOBuffer> buffer_data = buffer.ToIOBufferWithSize(); | 104 scoped_refptr<IOBuffer> buffer_data = buffer.ToIOBufferWithSize(); |
| 105 return buffer.total_bytes() == size && | 105 return buffer.total_bytes() == size && |
| 106 memcmp(buffer_data->data(), data, size) == 0; | 106 memcmp(buffer_data->data(), data, size) == 0; |
| 107 } | 107 } |
| 108 | 108 |
| 109 static size_t ReadFromInput(CompoundBufferInputStream* input, | 109 static size_t ReadFromInput(CompoundBufferInputStream* input, |
| 110 void* data, size_t size) { | 110 void* data, size_t size) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 result->Lock(); | 172 result->Lock(); |
| 173 buffer->reset(result); | 173 buffer->reset(result); |
| 174 } | 174 } |
| 175 | 175 |
| 176 CompoundBuffer target_; | 176 CompoundBuffer target_; |
| 177 scoped_refptr<IOBuffer> data_; | 177 scoped_refptr<IOBuffer> data_; |
| 178 }; | 178 }; |
| 179 | 179 |
| 180 TEST_F(CompoundBufferTest, Append) { | 180 TEST_F(CompoundBufferTest, Append) { |
| 181 target_.Clear(); | 181 target_.Clear(); |
| 182 IterateOverPieces(kChunkSizes0, NewCallback( | 182 IterateOverPieces(kChunkSizes0, base::Bind( |
| 183 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); | 183 &CompoundBufferTest::Append, base::Unretained(this))); |
| 184 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 184 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 185 | 185 |
| 186 target_.Clear(); | 186 target_.Clear(); |
| 187 IterateOverPieces(kChunkSizes1, NewCallback( | 187 IterateOverPieces(kChunkSizes1, base::Bind( |
| 188 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); | 188 &CompoundBufferTest::Append, base::Unretained(this))); |
| 189 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 189 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 190 } | 190 } |
| 191 | 191 |
| 192 TEST_F(CompoundBufferTest, AppendCopyOf) { | 192 TEST_F(CompoundBufferTest, AppendCopyOf) { |
| 193 target_.Clear(); | 193 target_.Clear(); |
| 194 IterateOverPieces(kChunkSizes0, NewCallback( | 194 IterateOverPieces(kChunkSizes0, base::Bind( |
| 195 static_cast<CompoundBufferTest*>(this), | 195 &CompoundBufferTest::AppendCopyOf, base::Unretained(this))); |
| 196 &CompoundBufferTest::AppendCopyOf)); | |
| 197 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 196 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 198 | 197 |
| 199 target_.Clear(); | 198 target_.Clear(); |
| 200 IterateOverPieces(kChunkSizes1, NewCallback( | 199 IterateOverPieces(kChunkSizes1, base::Bind( |
| 201 static_cast<CompoundBufferTest*>(this), | 200 &CompoundBufferTest::AppendCopyOf, base::Unretained(this))); |
| 202 &CompoundBufferTest::AppendCopyOf)); | |
| 203 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 201 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 204 } | 202 } |
| 205 | 203 |
| 206 TEST_F(CompoundBufferTest, Prepend) { | 204 TEST_F(CompoundBufferTest, Prepend) { |
| 207 target_.Clear(); | 205 target_.Clear(); |
| 208 IterateOverPieces(kChunkSizes0, NewCallback( | 206 IterateOverPieces(kChunkSizes0, base::Bind( |
| 209 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Prepend)); | 207 &CompoundBufferTest::Prepend, base::Unretained(this))); |
| 210 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 208 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 211 | 209 |
| 212 target_.Clear(); | 210 target_.Clear(); |
| 213 IterateOverPieces(kChunkSizes1, NewCallback( | 211 IterateOverPieces(kChunkSizes1, base::Bind( |
| 214 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Prepend)); | 212 &CompoundBufferTest::Prepend, base::Unretained(this))); |
| 215 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 213 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 216 } | 214 } |
| 217 | 215 |
| 218 TEST_F(CompoundBufferTest, PrependCopyOf) { | 216 TEST_F(CompoundBufferTest, PrependCopyOf) { |
| 219 target_.Clear(); | 217 target_.Clear(); |
| 220 IterateOverPieces(kChunkSizes0, NewCallback( | 218 IterateOverPieces(kChunkSizes0, base::Bind( |
| 221 static_cast<CompoundBufferTest*>(this), | 219 &CompoundBufferTest::PrependCopyOf, base::Unretained(this))); |
| 222 &CompoundBufferTest::PrependCopyOf)); | |
| 223 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 220 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 224 | 221 |
| 225 target_.Clear(); | 222 target_.Clear(); |
| 226 IterateOverPieces(kChunkSizes1, NewCallback( | 223 IterateOverPieces(kChunkSizes1, base::Bind( |
| 227 static_cast<CompoundBufferTest*>(this), | 224 &CompoundBufferTest::PrependCopyOf, base::Unretained(this))); |
| 228 &CompoundBufferTest::PrependCopyOf)); | |
| 229 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); | 225 EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); |
| 230 } | 226 } |
| 231 | 227 |
| 232 TEST_F(CompoundBufferTest, CropFront) { | 228 TEST_F(CompoundBufferTest, CropFront) { |
| 233 target_.Clear(); | 229 target_.Clear(); |
| 234 IterateOverPieces(kChunkSizes1, NewCallback( | 230 IterateOverPieces(kChunkSizes1, base::Bind( |
| 235 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); | 231 &CompoundBufferTest::Append, base::Unretained(this))); |
| 236 IterateOverPieces(kCropSizes, NewCallback( | 232 IterateOverPieces(kCropSizes, base::Bind( |
| 237 static_cast<CompoundBufferTest*>(this), | 233 &CompoundBufferTest::TestCropFront, base::Unretained(this))); |
| 238 &CompoundBufferTest::TestCropFront)); | |
| 239 } | 234 } |
| 240 | 235 |
| 241 TEST_F(CompoundBufferTest, CropBack) { | 236 TEST_F(CompoundBufferTest, CropBack) { |
| 242 target_.Clear(); | 237 target_.Clear(); |
| 243 IterateOverPieces(kChunkSizes1, NewCallback( | 238 IterateOverPieces(kChunkSizes1, base::Bind( |
| 244 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); | 239 &CompoundBufferTest::Append, base::Unretained(this))); |
| 245 IterateOverPieces(kCropSizes, NewCallback( | 240 IterateOverPieces(kCropSizes, base::Bind( |
| 246 static_cast<CompoundBufferTest*>(this), | 241 &CompoundBufferTest::TestCropBack, base::Unretained(this))); |
| 247 &CompoundBufferTest::TestCropBack)); | |
| 248 } | 242 } |
| 249 | 243 |
| 250 TEST_F(CompoundBufferTest, CopyFrom) { | 244 TEST_F(CompoundBufferTest, CopyFrom) { |
| 251 target_.Clear(); | 245 target_.Clear(); |
| 252 IterateOverPieces(kChunkSizes1, NewCallback( | 246 IterateOverPieces(kChunkSizes1, base::Bind( |
| 253 static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); | 247 &CompoundBufferTest::Append, base::Unretained(this))); |
| 254 { | 248 { |
| 255 SCOPED_TRACE("CopyFrom.kCopySizes0"); | 249 SCOPED_TRACE("CopyFrom.kCopySizes0"); |
| 256 IterateOverPieces(kCopySizes0, NewCallback( | 250 IterateOverPieces(kCopySizes0, base::Bind( |
| 257 static_cast<CompoundBufferTest*>(this), | 251 &CompoundBufferTest::TestCopyFrom, base::Unretained(this))); |
| 258 &CompoundBufferTest::TestCopyFrom)); | |
| 259 } | 252 } |
| 260 { | 253 { |
| 261 SCOPED_TRACE("CopyFrom.kCopySizes1"); | 254 SCOPED_TRACE("CopyFrom.kCopySizes1"); |
| 262 IterateOverPieces(kCopySizes1, NewCallback( | 255 IterateOverPieces(kCopySizes1, base::Bind( |
| 263 static_cast<CompoundBufferTest*>(this), | 256 &CompoundBufferTest::TestCopyFrom, base::Unretained(this))); |
| 264 &CompoundBufferTest::TestCopyFrom)); | |
| 265 } | 257 } |
| 266 } | 258 } |
| 267 | 259 |
| 268 TEST_F(CompoundBufferTest, InputStream) { | 260 TEST_F(CompoundBufferTest, InputStream) { |
| 269 scoped_ptr<CompoundBuffer> buffer; | 261 scoped_ptr<CompoundBuffer> buffer; |
| 270 PrepareData(&buffer); | 262 PrepareData(&buffer); |
| 271 CompoundBufferInputStream stream(buffer.get()); | 263 CompoundBufferInputStream stream(buffer.get()); |
| 272 | 264 |
| 273 ReadString(&stream, "Hello world!"); | 265 ReadString(&stream, "Hello world!"); |
| 274 ReadString(&stream, "This "); | 266 ReadString(&stream, "This "); |
| 275 ReadString(&stream, "is test"); | 267 ReadString(&stream, "is test"); |
| 276 EXPECT_TRUE(stream.Skip(3)); | 268 EXPECT_TRUE(stream.Skip(3)); |
| 277 ReadString(&stream, "MultipleArrayInput"); | 269 ReadString(&stream, "MultipleArrayInput"); |
| 278 EXPECT_TRUE(stream.Skip(6)); | 270 EXPECT_TRUE(stream.Skip(6)); |
| 279 ReadString(&stream, "f"); | 271 ReadString(&stream, "f"); |
| 280 ReadString(&stream, "o"); | 272 ReadString(&stream, "o"); |
| 281 ReadString(&stream, "r"); | 273 ReadString(&stream, "r"); |
| 282 ReadString(&stream, " "); | 274 ReadString(&stream, " "); |
| 283 ReadString(&stream, "Chromoting"); | 275 ReadString(&stream, "Chromoting"); |
| 284 } | 276 } |
| 285 | 277 |
| 286 } // namespace remoting | 278 } // namespace remoting |
| OLD | NEW |