OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 <string> |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "media/base/data_buffer.h" |
| 9 #include "remoting/base/protocol_decoder.h" |
| 10 #include "remoting/base/protocol_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace remoting { |
| 14 |
| 15 static const int kWidth = 640; |
| 16 static const int kHeight = 480; |
| 17 static const std::string kTestData = "Chromoting rockz"; |
| 18 |
| 19 static void AppendMessage(const chromotocol_pb::HostMessage& msg, |
| 20 std::string* buffer) { |
| 21 // Contains one encoded message. |
| 22 scoped_refptr<media::DataBuffer> encoded_msg; |
| 23 encoded_msg = SerializeAndFrameMessage(msg); |
| 24 buffer->append(reinterpret_cast<const char*>(encoded_msg->GetData()), |
| 25 encoded_msg->GetDataSize()); |
| 26 } |
| 27 |
| 28 // Construct and prepare data in the |output_stream|. |
| 29 static void PrepareData(uint8** buffer, int* size) { |
| 30 // Contains all encoded messages. |
| 31 std::string encoded_data; |
| 32 |
| 33 // The first message is InitClient. |
| 34 chromotocol_pb::HostMessage msg; |
| 35 msg.mutable_init_client()->set_width(kWidth); |
| 36 msg.mutable_init_client()->set_height(kHeight); |
| 37 AppendMessage(msg, &encoded_data); |
| 38 msg.Clear(); |
| 39 |
| 40 // Then append 10 update sequences to the data. |
| 41 for (int i = 0; i < 10; ++i) { |
| 42 msg.mutable_begin_update_stream(); |
| 43 AppendMessage(msg, &encoded_data); |
| 44 msg.Clear(); |
| 45 |
| 46 msg.mutable_update_stream_packet()->set_data(kTestData); |
| 47 AppendMessage(msg, &encoded_data); |
| 48 msg.Clear(); |
| 49 |
| 50 msg.mutable_end_update_stream(); |
| 51 AppendMessage(msg, &encoded_data); |
| 52 msg.Clear(); |
| 53 } |
| 54 |
| 55 *size = encoded_data.length(); |
| 56 *buffer = new uint8[*size]; |
| 57 memcpy(*buffer, encoded_data.c_str(), *size); |
| 58 } |
| 59 |
| 60 TEST(ProtocolDecoderTest, BasicOperations) { |
| 61 // Prepare encoded data for testing. |
| 62 int size; |
| 63 uint8* test_data; |
| 64 PrepareData(&test_data, &size); |
| 65 scoped_array<uint8> memory_deleter(test_data); |
| 66 |
| 67 // Then simulate using ProtocolDecoder to decode variable |
| 68 // size of encoded data. |
| 69 // The first thing to do is to generate a variable size of data. This is done |
| 70 // by iterating the following array for read sizes. |
| 71 const int kReadSizes[] = {1, 2, 3, 1}; |
| 72 |
| 73 ProtocolDecoder decoder; |
| 74 |
| 75 // Then feed the protocol decoder using the above generated data and the |
| 76 // read pattern. |
| 77 HostMessageList message_list; |
| 78 for (int i = 0; i < size;) { |
| 79 // First generate the amount to feed the decoder. |
| 80 int read = std::min(size - i, kReadSizes[i % arraysize(kReadSizes)]); |
| 81 |
| 82 // And then prepare a DataBuffer for feeding it. |
| 83 scoped_refptr<media::DataBuffer> buffer = new media::DataBuffer(read); |
| 84 memcpy(buffer->GetWritableData(), test_data + i, read); |
| 85 buffer->SetDataSize(read); |
| 86 decoder.ParseHostMessages(buffer, &message_list); |
| 87 i += read; |
| 88 } |
| 89 |
| 90 // Then verify the decoded messages. |
| 91 EXPECT_EQ(31u, message_list.size()); |
| 92 ASSERT_TRUE(message_list.size() > 0); |
| 93 EXPECT_TRUE(message_list[0]->has_init_client()); |
| 94 delete message_list[0]; |
| 95 |
| 96 for (size_t i = 1; i < message_list.size(); ++i) { |
| 97 int type = (i - 1) % 3; |
| 98 if (type == 0) { |
| 99 // Begin update stream. |
| 100 EXPECT_TRUE(message_list[i]->has_begin_update_stream()); |
| 101 } else if (type == 1) { |
| 102 // Partial update stream. |
| 103 EXPECT_TRUE(message_list[i]->has_update_stream_packet()); |
| 104 EXPECT_EQ(kTestData, message_list[i]->update_stream_packet().data()); |
| 105 } else if (type == 2) { |
| 106 // End update stream. |
| 107 EXPECT_TRUE(message_list[i]->has_end_update_stream()); |
| 108 } |
| 109 delete message_list[i]; |
| 110 } |
| 111 } |
| 112 |
| 113 } // namespace remoting |
OLD | NEW |