Bug 1545482 - THIS_FIREFOX connection type is not recognized;r=ladybenko
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 26 Apr 2019 15:25:12 +0000
changeset 471521 b0f3d584835c25ce778177d8f9395a974d431185
parent 471520 0c0d1b6e9055020892dcd6aab6d8792a08b3e2c6
child 471522 d441eb4ab6012d75af70ff90f3d40fe205c862a3
push id35920
push useraiakab@mozilla.com
push dateFri, 26 Apr 2019 22:02:33 +0000
treeherdermozilla-central@b13f2b24ae62 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersladybenko
bugs1545482
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1545482 - THIS_FIREFOX connection type is not recognized;r=ladybenko Using split("-") was clashing with the connection type "this-firefox". Added unit test for remote-client-manager. Differential Revision: https://phabricator.services.mozilla.com/D28066
devtools/client/shared/remote-debugging/remote-client-manager.js
devtools/client/shared/remote-debugging/test/unit/test_remote_client_manager.js
devtools/client/shared/remote-debugging/test/unit/xpcshell.ini
--- a/devtools/client/shared/remote-debugging/remote-client-manager.js
+++ b/devtools/client/shared/remote-debugging/remote-client-manager.js
@@ -75,31 +75,28 @@ class RemoteClientManager {
    * using getRemoteId.
    */
   getConnectionTypeByRemoteId(remoteId) {
     if (!remoteId) {
       return CONNECTION_TYPES.THIS_FIREFOX;
     }
 
     const key = decodeURIComponent(remoteId);
-    const type = this._getType(key);
-    return Object.values(CONNECTION_TYPES).includes(type)
-      ? type
-      : CONNECTION_TYPES.UNKNOWN;
+    for (const type of Object.values(CONNECTION_TYPES)) {
+      if (key.endsWith(type)) {
+        return type;
+      }
+    }
+    return CONNECTION_TYPES.UNKNOWN;
   }
 
   _getKey(id, type) {
     return id + "-" + type;
   }
 
-  _getType(key) {
-    const chunks = key.split("-");
-    return chunks[chunks.length - 1];
-  }
-
   _removeClientByKey(key) {
     const client = this._clients.get(key);
     if (client) {
       client.removeListener("closed", this._onClientClosed);
       this._clients.delete(key);
     }
   }
 
new file mode 100644
--- /dev/null
+++ b/devtools/client/shared/remote-debugging/test/unit/test_remote_client_manager.js
@@ -0,0 +1,71 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const { remoteClientManager } =
+  require("devtools/client/shared/remote-debugging/remote-client-manager");
+const { CONNECTION_TYPES } =
+  require("devtools/client/shared/remote-debugging/constants");
+
+add_task(async function testRemoteClientManager() {
+  for (const type of Object.values(CONNECTION_TYPES)) {
+    const fakeClient = createFakeClient();
+    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`);
+
+    remoteClientManager.setClient(clientId, type, fakeClient);
+    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`);
+
+    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`);
+  }
+});
+
+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("devtools/shared/event-emitter");
+
+  const client = {};
+  EventEmitter.decorate(client);
+
+  // Define aliases expected by the remote-client-manager.
+  client.addOneTimeListener = (evt, listener) => {
+    return client.once(evt, listener);
+  };
+  client.addListener = (evt, listener) => {
+    return client.on(evt, listener);
+  };
+  client.removeListener = (evt, listener) => {
+    return client.off(evt, listener);
+  };
+
+  return client;
+}
--- a/devtools/client/shared/remote-debugging/test/unit/xpcshell.ini
+++ b/devtools/client/shared/remote-debugging/test/unit/xpcshell.ini
@@ -1,7 +1,8 @@
 [DEFAULT]
 tags = devtools
 head = xpcshell-head.js
 firefox-appdir = browser
 skip-if = toolkit == 'android'
 
+[test_remote_client_manager.js]
 [test_version_checker.js]