summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/remote-debugging/test/xpcshell/test_remote_client_manager.js
blob: e66c38b67d14a96d88b88852627483acddbca4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {
  remoteClientManager,
} = require("resource://devtools/client/shared/remote-debugging/remote-client-manager.js");
const {
  CONNECTION_TYPES,
} = require("resource://devtools/client/shared/remote-debugging/constants.js");

add_task(async function testRemoteClientManager() {
  for (const type of Object.values(CONNECTION_TYPES)) {
    const fakeClient = createFakeClient();
    const runtimeInfo = {};
    const clientId = "clientId";
    const remoteId = remoteClientManager.getRemoteId(clientId, type);

    const connectionType =
      remoteClientManager.getConnectionTypeByRemoteId(remoteId);
    equal(
      connectionType,
      type,
      `[${type}]: Correct connection type was returned by getConnectionTypeByRemoteId`
    );

    equal(
      remoteClientManager.hasClient(clientId, type),
      false,
      `[${type}]: hasClient returns false if no client was set`
    );
    equal(
      remoteClientManager.getClient(clientId, type),
      null,
      `[${type}]: getClient returns null if no client was set`
    );
    equal(
      remoteClientManager.getClientByRemoteId(remoteId),
      null,
      `[${type}]: getClientByRemoteId returns null if no client was set`
    );
    equal(
      remoteClientManager.getRuntimeInfoByRemoteId(remoteId),
      null,
      `[${type}]: getRuntimeInfoByRemoteId returns null if no client was set`
    );

    remoteClientManager.setClient(clientId, type, fakeClient, runtimeInfo);
    equal(
      remoteClientManager.hasClient(clientId, type),
      true,
      `[${type}]: hasClient returns true`
    );
    equal(
      remoteClientManager.getClient(clientId, type),
      fakeClient,
      `[${type}]: getClient returns the correct client`
    );
    equal(
      remoteClientManager.getClientByRemoteId(remoteId),
      fakeClient,
      `[${type}]: getClientByRemoteId returns the correct client`
    );
    equal(
      remoteClientManager.getRuntimeInfoByRemoteId(remoteId),
      runtimeInfo,
      `[${type}]: getRuntimeInfoByRemoteId returns the correct object`
    );

    remoteClientManager.removeClient(clientId, type);
    equal(
      remoteClientManager.hasClient(clientId, type),
      false,
      `[${type}]: hasClient returns false after removing the client`
    );
    equal(
      remoteClientManager.getClient(clientId, type),
      null,
      `[${type}]: getClient returns null after removing the client`
    );
    equal(
      remoteClientManager.getClientByRemoteId(remoteId),
      null,
      `[${type}]: getClientByRemoteId returns null after removing the client`
    );
    equal(
      remoteClientManager.getRuntimeInfoByRemoteId(),
      null,
      `[${type}]: getRuntimeInfoByRemoteId returns null after removing the client`
    );
  }

  // Test various fallback scenarios for APIs relying on remoteId, when called without a
  // remoteId, we expect to get the information for the local this-firefox runtime.
  const { THIS_FIREFOX } = CONNECTION_TYPES;
  const thisFirefoxClient = createFakeClient();
  const thisFirefoxInfo = {};
  remoteClientManager.setClient(
    THIS_FIREFOX,
    THIS_FIREFOX,
    thisFirefoxClient,
    thisFirefoxInfo
  );

  equal(
    remoteClientManager.getClientByRemoteId(),
    thisFirefoxClient,
    `[fallback]: getClientByRemoteId returns this-firefox if remoteId is null`
  );
  equal(
    remoteClientManager.getRuntimeInfoByRemoteId(),
    thisFirefoxInfo,
    `[fallback]: getRuntimeInfoByRemoteId returns this-firefox if remoteId is null`
  );

  const otherRemoteId = remoteClientManager.getRemoteId(
    "clientId",
    CONNECTION_TYPES.USB
  );
  equal(
    remoteClientManager.getClientByRemoteId(otherRemoteId),
    null,
    `[fallback]: getClientByRemoteId does not fallback if remoteId is non-null`
  );
  equal(
    remoteClientManager.getRuntimeInfoByRemoteId(otherRemoteId),
    null,
    `[fallback]: getRuntimeInfoByRemoteId does not fallback if remoteId is non-null`
  );
});

add_task(async function testRemoteClientManagerWithUnknownType() {
  const remoteId = remoteClientManager.getRemoteId(
    "someClientId",
    "NotARealType"
  );
  const connectionType =
    remoteClientManager.getConnectionTypeByRemoteId(remoteId);
  equal(
    connectionType,
    CONNECTION_TYPES.UNKNOWN,
    `Connection type UNKNOWN was returned by getConnectionTypeByRemoteId`
  );
});

function createFakeClient() {
  const EventEmitter = require("resource://devtools/shared/event-emitter.js");

  const client = {};
  EventEmitter.decorate(client);
  return client;
}