1 // Copyright 2014 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.
5 package org.chromium.components.devtools_bridge.gcd;
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import junit.framework.Assert;
12 import org.chromium.components.devtools_bridge.commands.Command;
13 import org.chromium.components.devtools_bridge.util.TestSource;
16 * Tests for {@link MessageReader}.
18 public class MessageReaderTest extends InstrumentationTestCase {
19 private static final String DEVICE_ID = "4ac8a0f8-??????????????-192e2727710d";
20 private static final String ROBOT_ACCOUNT_EMAIL =
21 "2a3???????????????????????????87@clouddevices.gserviceaccount.com";
22 private static final String AUTHORIZATION_CODE =
23 "4/6V0jpup-????????????????????????????????????????????_e85kQI";
24 private static final String COMMAND_ID =
25 "a0217abb-????-????-????-?????????????????????????-????-2725-b2332fe99829";
28 public void testReadTicket() throws Exception {
29 TestSource source = new TestSource();
30 source.write().beginObject()
31 .name("kind").value("clouddevices#registrationTicket")
32 .name("id").value("p8hI4")
33 .name("deviceId").value(DEVICE_ID)
34 .name("creationTimeMs").value("1411029429794")
35 .name("expirationTimeMs").value("1411029669794")
37 String result = new MessageReader(source.read()).readTicketId();
38 Assert.assertEquals("p8hI4", result);
42 public void testReadCredential() throws Exception {
43 TestSource source = new TestSource();
44 source.write().beginObject()
45 .name("kind").value("clouddevices#registrationTicket")
46 .name("id").value("p8hI4")
47 .name("deviceId").value(DEVICE_ID)
48 .name("userEmail").value("...@chromium.org")
49 .name("creationTimeMs").value("1411029429794")
50 .name("expirationTimeMs").value("1411029669794")
51 .name("robotAccountEmail").value(ROBOT_ACCOUNT_EMAIL)
52 .name("robotAccountAuthorizationCode").value(AUTHORIZATION_CODE)
54 InstanceCredential result = new MessageReader(source.read()).readInstanceCredential();
55 Assert.assertEquals(DEVICE_ID, result.id);
56 Assert.assertEquals(AUTHORIZATION_CODE, result.secret);
60 public void testReadNotificationCommandCreated() throws Exception {
61 TestSource source = new TestSource();
62 source.write().beginObject()
63 .name("type").value("COMMAND_CREATED")
64 .name("commandId").value(COMMAND_ID)
65 .name("deviceId").value(DEVICE_ID)
66 .name("command").beginObject()
67 .name("kind").value("clouddevices#command")
68 .name("id").value(COMMAND_ID)
69 .name("deviceId").value(DEVICE_ID)
70 .name("name").value("base._startSession")
71 .name("parameters").beginObject()
72 .name("_sessionId").value("SESSION_ID")
73 .name("_config").value("{}")
74 .name("_offer").value("INVALID_OFFER")
75 .endObject() // parameters
76 .name("state").value("queued")
77 .name("error").beginObject()
78 .name("arguments").beginArray().endArray()
79 .name("creationTimeMs").value("1411137527329")
80 .name("expirationTimeMs").value("1411137547329")
82 .endObject() // command
83 .name("expirationTimeMs").value("1411029669794")
85 Notification result = new MessageReader(source.read()).readNotification();
87 Assert.assertEquals(Notification.Type.COMMAND_CREATED, result.type);
88 Assert.assertEquals(DEVICE_ID, result.instanceId);
89 Assert.assertNotNull(result.command);
90 Assert.assertEquals(COMMAND_ID, result.command.id);
91 Assert.assertEquals(Command.Type.START_SESSION, result.command.type);
95 public void testReadNotificationCommandExpired() throws Exception {
96 TestSource source = new TestSource();
97 source.write().beginObject()
98 .name("type").value("COMMAND_EXPIRED")
99 .name("commandId").value(COMMAND_ID)
100 .name("deviceId").value(DEVICE_ID)
101 .endObject().close();
102 Notification result = new MessageReader(source.read()).readNotification();
103 Assert.assertNull(result);
107 public void testReadNotificationInstanceUNregistered() throws Exception {
108 TestSource source = new TestSource();
109 source.write().beginObject()
110 .name("type").value("DEVICE_DELETED")
111 .name("deviceId").value(DEVICE_ID)
112 .endObject().close();
113 Notification result = new MessageReader(source.read()).readNotification();
115 Assert.assertEquals(Notification.Type.INSTANCE_UNREGISTERED, result.type);
116 Assert.assertEquals(DEVICE_ID, result.instanceId);
117 Assert.assertNull(result.command);