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

Side by Side Diff: chrome/test/data/extensions/api_test/media_remoting/test_rpc.js

Issue 2724173002: Media Remoting: Add browser tests. (Closed)
Patch Set: Created 3 years, 9 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
OLDNEW
(Empty)
1 // Copyright 2017 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 'use strict';
6
7 // The RPCs used in this test. These should keep identical with those defined in
8 // src/media/remoting/rpc.proto.
9 mrTest.RpcProc = {
10 RPC_ACQUIRE_RENDERER: 1,
11 RPC_ACQUIRE_RENDERER_DONE: 2,
12 RPC_R_INITIALIZE: 1000,
13 RPC_R_STARTPLAYINGFROM: 1002,
14 RPC_R_INITIALIZE_CALLBACK: 1100,
15 RPC_DS_INITIALIZE: 3000,
16 RPC_DS_INITIALIZE_CALLBACK: 3100
17 };
18
19 mrTest.WireType = {
20 INVALID: -1,
21 VARINT: 0,
22 FIXED64: 1,
23 DELIMITED: 2,
24 START_GROUP: 3,
25 END_GROUP: 4,
26 FIXED32: 5
27 };
28
29 mrTest.RpcMessage = class {
30 constructor() {
31 this.handle = null;
32 this.proc = null;
33 this.integerValue = null;
34 this.boolValue = null;
35 this.rendererInitializeRpc = null;
36 }
37
38 setHandle(handle) { this.handle = handle; }
39 setProc(proc) { this.proc = proc; }
40 setIntegerValue(value) { this.integerValue = value; }
41 setBool(boolValue) { this.boolValue = boolValue; }
42 setRendererInitializeRpc(msg) { this.rendererInitializeRpc = msg; }
43
44 getHandle() { return this.handle; }
45 getProc() { return this.proc; }
46 getIntegerValue() { return this.integerValue; }
47 getBool() { return this.boolValue; }
48 getRendererInitializeRpc() { return this.rendererInitializeRpc; }
49 };
50
51 mrTest.RendererInitializeRpc = class {
52 constructor() {
53 this.handle = null;
54 this.audioDemuxerHandle = null;
55 this.videoDemuxerHandle = null;
56 this.callbackHandle = null;
57 }
58
59 setHandle(handle) { this.handle = handle; }
60 setAudioDemuxerHandle(handle) { this.audioDemuxerHandle = handle; }
61 setVideoDemuxerHandle(handle) { this.videoDemuxerHandle = handle; }
62 setCallbackHandle(handle) { this.callbackHandle = handle; }
63
64 getHandle() { return this.handle; }
65 getAudioDemuxerHandle() { return this.audioDemuxerHandle; }
66 getVideoDemuxerHandle() { return this.videoDemuxerHandle; }
67 getCallbackHandle() { return this.callbackHandle; }
68 }
69
70 // A partial RPC message deserializer.
71 mrTest.reader = class {
72 constructor(message) {
73 this.buf = message;
74 this.nextField = null;
75 this.nextWireType = null;
76 this.readPos = 0;
77 this.end = this.buf.length;
78 }
79
80 // Returns false when reaches the end.
81 readNextField() {
82 if (this.readPos >= this.end) {
83 this.nextField = null;
84 this.nextWireType = null;
85 return false;
86 }
87
88 let header = this.readInt32();
89 this.nextField = header >>> 3;
90 this.nextWireType = header & 0x7;
91 return true;
92 }
93
94 readInt32() {
95 var tmp;
96 let x = 0;
97 for (var i = 0; i < 4; i++) {
98 tmp = this.buf[this.readPos++];
99 x |= (tmp & 0x7F) << (i * 7);
100 if (tmp < 128)
101 return x;
102 }
103 tmp = this.buf[this.readPos++];
104 x |= (tmp & 0x0F) << 28;
105 if (tmp < 128)
106 return x >>> 0;
107
108 chrome.test.assertEq(tmp & 0xF0, 0xF0);
109 return x;
110 }
111
112 deserializeRendererInitializeRpc(msg) {
113 let len = this.readInt32();
114 let oldEnd = this.end;
115 this.end = this.readPos + len;
116 while (this.readNextField()) {
117 if (this.nextWireType == mrTest.WireType.END_GROUP)
118 break;
119 switch (this.nextField) {
120 case 1:
121 msg.setHandle(this.readInt32());
122 break;
123 case 2:
124 msg.setAudioDemuxerHandle(this.readInt32());
125 break;
126 case 3:
127 msg.setVideoDemuxerHandle(this.readInt32());
128 break;
129 case 4:
130 msg.setCallbackHandle(this.readInt32());
131 break;
132 default:
133 // Ignore other fields.
134 this.skipField();
135 }
136 }
137 this.readPos = this.end;
138 this.end = oldEnd;
139 }
140
141 deserializeBinary(msg) {
142 while (this.readNextField()) {
143 if (this.nextWireType == mrTest.WireType.END_GROUP)
144 break;
145 switch (this.nextField) {
146 case 1:
147 chrome.test.assertEq(this.nextWireType, mrTest.WireType.VARINT);
148 msg.setHandle(this.readInt32());
149 break;
150 case 2:
151 chrome.test.assertEq(this.nextWireType, mrTest.WireType.VARINT);
152 msg.setProc(this.readInt32());
153 break;
154 case 3:
155 chrome.test.assertEq(this.nextWireType, mrTest.WireType.VARINT);
156 msg.setIntegerValue(this.readInt32());
157 break;
158 case 6:
159 chrome.test.assertEq(this.nextWireType, mrTest.WireType.VARINT);
160 let value = this.readInt32();
161 msg.setBool(!!value);
162 break;
163 case 100:
164 let rendererInitializeMsg = new mrTest.RendererInitializeRpc();
165 this.deserializeRendererInitializeRpc(rendererInitializeMsg);
166 msg.setRendererInitializeRpc(rendererInitializeMsg);
167 break;
168 default:
169 this.skipField();
170 }
171 }
172 }
173
174 skipField() {
175 switch (this.nextWireType) {
176 case mrTest.WireType.VARINT:
177 while (this.buf[this.readPos++] & 0x80) {}
178 break;
179 case mrTest.WireType.FIXED64:
180 this.readPos += 8;
181 break;
182 case mrTest.WireType.DELIMITED:
183 let len = this.readInt32();
184 this.readPos += len;
185 break;
186 case mrTest.WireType.FIXED32:
187 this.readPos += 4;
188 break;
189 case mrTest.WireType.START_GROUP:
190 this.skipGroup();
191 break;
192 default:
193 chrome.test.fail('wrong nextWireType_.' + this.nextWireType);
194 }
195 }
196
197 skipGroup() {
198 let nestedGroups = [this.nextField];
199 do {
200 chrome.test.assertTrue(this.readNextField());
201 if (this.nextWireType == mrTest.WireType.START_GROUP) {
202 nestedGroups.push(this.nextField);
203 } else if (this.nextWireType == mrTest.WireType.END_GROUP) {
204 chrome.test.assertEq(this.nextField, nestedGroups.pop());
205 }
206 } while (nestedGroups.length > 0);
207 }
208
209 };
210
211 // A partial RPC message serializer.
212 mrTest.Writer = class {
213 constructor() {
214 this.buf = [];
215 }
216
217 serializeMessage(msg) {
218 if (msg.getHandle()) {
219 this.writeFieldHeader(1, mrTest.WireType.VARINT);
220 this.writeSignedVarint32(msg.getHandle());
221 }
222 if (msg.getProc()) {
223 this.writeFieldHeader(2, mrTest.WireType.VARINT);
224 this.writeSignedVarint32(msg.getProc());
225 }
226 if (msg.getIntegerValue()) {
227 this.writeFieldHeader(3, mrTest.WireType.VARINT);
228 this.writeSignedVarint32(msg.getIntegerValue());
229 }
230 if (msg.getBool() != null) {
231 this.writeFieldHeader(6, mrTest.WireType.VARINT);
232 this.writeSignedVarint32(msg.getBool() ? 1 : 0);
233 }
234 }
235
236 getResultBuffer() {
237 let flat = new Uint8Array(this.buf.length);
238 flat.set(this.buf, 0);
239 return flat;
240 }
241
242 writeUnsignedVarint32(value) {
243 while (value > 127) {
244 this.buf.push((value & 0x7f) | 0x80);
245 value = value >>> 7;
246 }
247 this.buf.push(value);
248 }
249
250 writeSignedVarint32(value) {
251 if (value >= 0) {
252 this.writeUnsignedVarint32(value);
253 return;
254 }
255
256 for (var i=0; i < 9; i++) {
257 this.buf.push((value & 0x7f) | 0x80);
258 value = value >> 7;
259 }
260 this.buf.push(1);
261 }
262
263 writeFieldHeader(field, wireType) {
264 this.writeUnsignedVarint32((field << 3) + wireType);
265 }
266 }
267
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698