OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/download/download_buffer.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "net/base/io_buffer.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace content { | |
13 | |
14 namespace { | |
15 | |
16 const char* kTestData[] = { | |
17 "Four score and twenty years ago", | |
18 "Our four (or five) fathers", | |
19 "Danced and sang in the woods" | |
20 }; | |
21 const size_t kTestDataQty = arraysize(kTestData); | |
22 | |
23 class DownloadBufferTest : public testing::Test { | |
24 public: | |
25 DownloadBufferTest() { | |
26 } | |
27 ~DownloadBufferTest() { | |
28 } | |
29 | |
30 void CreateBuffer() { | |
31 EXPECT_EQ(0u, content_buffer_.size()); | |
32 | |
33 for (size_t i = 0; i < kTestDataQty; ++i) { | |
34 net::StringIOBuffer* io_buffer = new net::StringIOBuffer(kTestData[i]); | |
35 content_buffer_.push_back(std::make_pair(io_buffer, io_buffer->size())); | |
36 EXPECT_EQ(i + 1, content_buffer_.size()); | |
37 } | |
38 } | |
39 | |
40 ContentVector* content_buffer() { | |
41 return &content_buffer_; | |
42 } | |
43 | |
44 private: | |
45 ContentVector content_buffer_; | |
46 }; | |
47 | |
48 TEST_F(DownloadBufferTest, CreateContentVector) { | |
49 CreateBuffer(); | |
50 | |
51 ContentVector* contents = content_buffer(); | |
52 | |
53 EXPECT_EQ(kTestDataQty, content_buffer()->size()); | |
54 EXPECT_EQ(kTestDataQty, contents->size()); | |
55 | |
56 for (size_t i = 0; i < kTestDataQty; ++i) { | |
57 size_t io_buffer_size = strlen(kTestData[i]); | |
58 EXPECT_STREQ(kTestData[i], (*contents)[i].first->data()); | |
59 EXPECT_EQ(io_buffer_size, (*contents)[i].second); | |
60 } | |
61 } | |
62 | |
63 TEST_F(DownloadBufferTest, CreateDownloadBuffer) { | |
64 scoped_refptr<DownloadBuffer> content_buffer(new DownloadBuffer); | |
65 EXPECT_EQ(0u, content_buffer->size()); | |
66 | |
67 for (size_t i = 0; i < kTestDataQty; ++i) { | |
68 net::StringIOBuffer* io_buffer = new net::StringIOBuffer(kTestData[i]); | |
69 EXPECT_EQ(i + 1, content_buffer->AddData(io_buffer, io_buffer->size())); | |
70 } | |
71 scoped_ptr<ContentVector> contents(content_buffer->ReleaseContents()); | |
72 | |
73 EXPECT_EQ(0u, content_buffer->size()); | |
74 EXPECT_EQ(kTestDataQty, contents->size()); | |
75 | |
76 for (size_t i = 0; i < kTestDataQty; ++i) { | |
77 size_t io_buffer_size = strlen(kTestData[i]); | |
78 EXPECT_STREQ(kTestData[i], (*contents)[i].first->data()); | |
79 EXPECT_EQ(io_buffer_size, (*contents)[i].second); | |
80 } | |
81 } | |
82 | |
83 TEST_F(DownloadBufferTest, AssembleData) { | |
84 CreateBuffer(); | |
85 | |
86 size_t assembled_bytes = 0; | |
87 scoped_refptr<net::IOBuffer> assembled_buffer = | |
88 AssembleData(*content_buffer(), &assembled_bytes); | |
89 | |
90 // Did not change the content buffer. | |
91 EXPECT_EQ(kTestDataQty, content_buffer()->size()); | |
92 | |
93 // Verify the data. | |
94 size_t total_size = 0; | |
95 for (size_t i = 0; i < kTestDataQty; ++i) { | |
96 size_t len = strlen(kTestData[i]); | |
97 // assembled_buffer->data() may not be NULL-terminated, so we can't use | |
98 // EXPECT_STREQ(). | |
99 EXPECT_EQ( | |
100 0, memcmp(kTestData[i], assembled_buffer->data() + total_size, len)); | |
101 total_size += len; | |
102 } | |
103 // Verify the data length. | |
104 EXPECT_EQ(total_size, assembled_bytes); | |
105 } | |
106 | |
107 TEST_F(DownloadBufferTest, AssembleDataWithEmptyBuffer) { | |
108 ContentVector buffer; | |
109 EXPECT_EQ(0u, buffer.size()); | |
110 | |
111 size_t assembled_bytes = 0; | |
112 scoped_refptr<net::IOBuffer> assembled_buffer = | |
113 AssembleData(buffer, &assembled_bytes); | |
114 | |
115 // Did not change the content buffer. | |
116 EXPECT_EQ(0u, buffer.size()); | |
117 EXPECT_EQ(0u, assembled_bytes); | |
118 EXPECT_TRUE(NULL == assembled_buffer.get()); | |
119 } | |
120 | |
121 } // namespace | |
122 | |
123 } // namespace content | |
OLD | NEW |