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

Side by Side Diff: remoting/protocol/message_decoder_unittest.cc

Issue 5559011: Remove ChromotingClientMessage (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 10 years 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) 2010 The Chromium Authors. All rights reserved. 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 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/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
8 #include "base/stl_util-inl.h" 8 #include "base/stl_util-inl.h"
9 #include "remoting/proto/internal.pb.h" 9 #include "remoting/proto/event.pb.h"
10 #include "remoting/protocol/message_decoder.h" 10 #include "remoting/protocol/message_decoder.h"
11 #include "remoting/protocol/util.h" 11 #include "remoting/protocol/util.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace remoting { 14 namespace remoting {
15 namespace protocol { 15 namespace protocol {
16 16
17 static const int kTestKey = 142; 17 static const int kTestKey = 142;
18 18
19 static void AppendMessage(const ChromotingClientMessage& msg, 19 static void AppendMessage(const EventMessage& msg,
20 std::string* buffer) { 20 std::string* buffer) {
21 // Contains one encoded message. 21 // Contains one encoded message.
22 scoped_refptr<net::IOBufferWithSize> encoded_msg; 22 scoped_refptr<net::IOBufferWithSize> encoded_msg;
23 encoded_msg = SerializeAndFrameMessage(msg); 23 encoded_msg = SerializeAndFrameMessage(msg);
24 buffer->append(encoded_msg->data(), encoded_msg->size()); 24 buffer->append(encoded_msg->data(), encoded_msg->size());
25 } 25 }
26 26
27 // Construct and prepare data in the |output_stream|. 27 // Construct and prepare data in the |output_stream|.
28 static void PrepareData(uint8** buffer, int* size) { 28 static void PrepareData(uint8** buffer, int* size) {
29 // Contains all encoded messages. 29 // Contains all encoded messages.
30 std::string encoded_data; 30 std::string encoded_data;
31 31
32 ChromotingClientMessage msg; 32 EventMessage msg;
33 33
34 // Then append 10 update sequences to the data. 34 // Then append 10 update sequences to the data.
35 for (int i = 0; i < 10; ++i) { 35 for (int i = 0; i < 10; ++i) {
36 msg.mutable_key_event()->set_key(kTestKey + i); 36 Event* event = msg.add_event();
37 msg.mutable_key_event()->set_pressed((i % 2) != 0); 37 event->set_timestamp(i);
38 event->mutable_key()->set_keycode(kTestKey + i);
39 event->mutable_key()->set_pressed((i % 2) != 0);
38 AppendMessage(msg, &encoded_data); 40 AppendMessage(msg, &encoded_data);
39 msg.Clear(); 41 msg.Clear();
40 } 42 }
41 43
42 *size = encoded_data.length(); 44 *size = encoded_data.length();
43 *buffer = new uint8[*size]; 45 *buffer = new uint8[*size];
44 memcpy(*buffer, encoded_data.c_str(), *size); 46 memcpy(*buffer, encoded_data.c_str(), *size);
45 } 47 }
46 48
47 void SimulateReadSequence(const int read_sequence[], int sequence_size) { 49 void SimulateReadSequence(const int read_sequence[], int sequence_size) {
48 // Prepare encoded data for testing. 50 // Prepare encoded data for testing.
49 int size; 51 int size;
50 uint8* test_data; 52 uint8* test_data;
51 PrepareData(&test_data, &size); 53 PrepareData(&test_data, &size);
52 scoped_array<uint8> memory_deleter(test_data); 54 scoped_array<uint8> memory_deleter(test_data);
53 55
54 // Then simulate using MessageDecoder to decode variable 56 // Then simulate using MessageDecoder to decode variable
55 // size of encoded data. 57 // size of encoded data.
56 // The first thing to do is to generate a variable size of data. This is done 58 // The first thing to do is to generate a variable size of data. This is done
57 // by iterating the following array for read sizes. 59 // by iterating the following array for read sizes.
58 MessageDecoder decoder; 60 MessageDecoder decoder;
59 61
60 // Then feed the protocol decoder using the above generated data and the 62 // Then feed the protocol decoder using the above generated data and the
61 // read pattern. 63 // read pattern.
62 std::list<ChromotingClientMessage*> message_list; 64 std::list<EventMessage*> message_list;
63 for (int i = 0; i < size;) { 65 for (int i = 0; i < size;) {
64 // First generate the amount to feed the decoder. 66 // First generate the amount to feed the decoder.
65 int read = std::min(size - i, read_sequence[i % sequence_size]); 67 int read = std::min(size - i, read_sequence[i % sequence_size]);
66 68
67 // And then prepare an IOBuffer for feeding it. 69 // And then prepare an IOBuffer for feeding it.
68 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read)); 70 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(read));
69 memcpy(buffer->data(), test_data + i, read); 71 memcpy(buffer->data(), test_data + i, read);
70 decoder.ParseMessages(buffer, read, &message_list); 72 decoder.ParseMessages(buffer, read, &message_list);
71 i += read; 73 i += read;
72 } 74 }
73 75
74 // Then verify the decoded messages. 76 // Then verify the decoded messages.
75 EXPECT_EQ(10u, message_list.size()); 77 EXPECT_EQ(10u, message_list.size());
76 78
77 int index = 0; 79 int index = 0;
78 for (std::list<ChromotingClientMessage*>::iterator it = 80 for (std::list<EventMessage*>::iterator it =
79 message_list.begin(); 81 message_list.begin();
80 it != message_list.end(); ++it) { 82 it != message_list.end(); ++it) {
81 ChromotingClientMessage* message = *it; 83 EventMessage* message = *it;
82 // Partial update stream. 84 // Partial update stream.
83 EXPECT_TRUE(message->has_key_event()); 85 EXPECT_EQ(message->event_size(), 1);
86 EXPECT_TRUE(message->event(0).has_key());
84 87
85 // TODO(sergeyu): Don't use index here. Instead store the expected values 88 // TODO(sergeyu): Don't use index here. Instead store the expected values
86 // in an array. 89 // in an array.
87 EXPECT_EQ(kTestKey + index, message->key_event().key()); 90 EXPECT_EQ(kTestKey + index, message->event(0).key().keycode());
88 EXPECT_EQ((index % 2) != 0, message->key_event().pressed()); 91 EXPECT_EQ((index % 2) != 0, message->event(0).key().pressed());
89 ++index; 92 ++index;
90 } 93 }
91 STLDeleteElements(&message_list); 94 STLDeleteElements(&message_list);
92 } 95 }
93 96
94 TEST(MessageDecoderTest, SmallReads) { 97 TEST(MessageDecoderTest, SmallReads) {
95 const int kReads[] = {1, 2, 3, 1}; 98 const int kReads[] = {1, 2, 3, 1};
96 SimulateReadSequence(kReads, arraysize(kReads)); 99 SimulateReadSequence(kReads, arraysize(kReads));
97 } 100 }
98 101
99 TEST(MessageDecoderTest, LargeReads) { 102 TEST(MessageDecoderTest, LargeReads) {
100 const int kReads[] = {50, 50, 5}; 103 const int kReads[] = {50, 50, 5};
101 SimulateReadSequence(kReads, arraysize(kReads)); 104 SimulateReadSequence(kReads, arraysize(kReads));
102 } 105 }
103 106
104 TEST(MessageDecoderTest, EmptyReads) { 107 TEST(MessageDecoderTest, EmptyReads) {
105 const int kReads[] = {4, 0, 50, 0}; 108 const int kReads[] = {4, 0, 50, 0};
106 SimulateReadSequence(kReads, arraysize(kReads)); 109 SimulateReadSequence(kReads, arraysize(kReads));
107 } 110 }
108 111
109 } // namespace protocol 112 } // namespace protocol
110 } // namespace remoting 113 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698