1 // Copyright 2015 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.
7 * Unit tests for combined_host_list_api.js.
14 /** @type {!remoting.MockHostListApi} */
17 /** @type {!remoting.MockHostListApi} */
20 /** @type {!remoting.CombinedHostListApi} */
23 /** @type {sinon.TestStub} */
24 var registerWithHostIdStub
;
26 /** @type {!remoting.Host} */
29 /** @type {!remoting.Host} */
32 QUnit
.module('CombinedHostListApi', {
33 beforeEach: function(/** QUnit.Assert */ assert
) {
34 remoting
.settings
= new remoting
.Settings();
35 remoting
.settings
['USE_GCD'] = true;
36 remoting
.mockIdentity
.setAccessToken(
37 remoting
.MockIdentity
.AccessToken
.VALID
);
38 mockGcdApi
= new remoting
.MockHostListApi();
39 mockGcdApi
.addMockHost('gcd-host');
40 commonHostGcd
= mockGcdApi
.addMockHost('common-host');
41 commonHostGcd
.hostName
= 'common-host-gcd';
42 mockLegacyApi
= new remoting
.MockHostListApi();
43 mockLegacyApi
.addMockHost('legacy-host');
44 commonHostLegacy
= mockLegacyApi
.addMockHost('common-host');
45 commonHostLegacy
.hostName
= 'common-host-legacy';
46 combinedApi
= new remoting
.CombinedHostListApi(mockLegacyApi
, mockGcdApi
);
47 registerWithHostIdStub
=
48 sinon
.stub(remoting
.LegacyHostListApi
, 'registerWithHostId');
50 afterEach: function(/** QUnit.Assert */ assert
) {
51 remoting
.settings
= null;
52 registerWithHostIdStub
.restore();
56 QUnit
.test('register', function(/** QUnit.Assert */ assert
) {
57 registerWithHostIdStub
.returns(Promise
.resolve());
59 mockGcdApi
.authCodeFromRegister
= '<fake_auth_code>';
60 mockGcdApi
.emailFromRegister
= '<fake_email>';
61 mockGcdApi
.hostIdFromRegister
= '<fake_host_id>';
62 mockLegacyApi
.authCodeFromRegister
= '<wrong_fake_auth_code>';
63 mockLegacyApi
.emailFromRegister
= '<wrong_fake_email>';
64 mockLegacyApi
.hostIdFromRegister
= '<wrong_fake_host_id>';
65 return combinedApi
.register('', '', '').then(function(regResult
) {
66 assert
.equal(regResult
.authCode
, '<fake_auth_code>');
67 assert
.equal(regResult
.email
, '<fake_email>');
68 assert
.equal(regResult
.hostId
, '<fake_host_id>');
72 QUnit
.test('get', function(/** QUnit.Assert */ assert
) {
73 return combinedApi
.get().then(function(hosts
) {
74 assert
.equal(hosts
.length
, 3);
75 var hostIds
= new Set();
76 hosts
.forEach(function(host
) {
77 hostIds
.add(host
.hostId
);
78 if (host
.hostId
== 'common-host') {
79 assert
.equal(host
.hostName
, 'common-host-gcd');
82 assert
.ok(hostIds
.has('gcd-host'));
83 assert
.ok(hostIds
.has('legacy-host'));
84 assert
.ok(hostIds
.has('common-host'));
88 QUnit
.test('get w/ GCD newer', function(/** QUnit.Assert */ assert
) {
89 commonHostGcd
.updatedTime
= '1970-01-02';
90 commonHostLegacy
.updatedTime
= '1970-01-01';
91 return combinedApi
.get().then(function(hosts
) {
92 hosts
.forEach(function(host
) {
93 if (host
.hostId
== 'common-host') {
94 assert
.equal(host
.hostName
, 'common-host-gcd');
100 QUnit
.test('get w/ legacy newer', function(/** QUnit.Assert */ assert
) {
101 commonHostGcd
.updatedTime
= '1970-01-01';
102 commonHostLegacy
.updatedTime
= '1970-01-02';
103 return combinedApi
.get().then(function(hosts
) {
104 hosts
.forEach(function(host
) {
105 if (host
.hostId
== 'common-host') {
106 assert
.equal(host
.hostName
, 'common-host-legacy');
112 QUnit
.test('put to legacy', function(/** QUnit.Assert */ assert
) {
113 return combinedApi
.get().then(function() {
114 return combinedApi
.put('legacy-host', 'new host name', '').then(
116 assert
.equal(mockLegacyApi
.hosts
[0].hostName
,
122 QUnit
.test('put to GCD', function(/** QUnit.Assert */ assert
) {
123 return combinedApi
.get().then(function() {
124 return combinedApi
.put('gcd-host', 'new host name', '').then(
126 assert
.equal(mockGcdApi
.hosts
[0].hostName
,
133 QUnit
.test('put to both', function(/** QUnit.Assert */ assert
) {
134 return combinedApi
.get().then(function() {
135 return combinedApi
.put('common-host', 'new host name', '').then(
137 assert
.equal(mockGcdApi
.hosts
[1].hostName
,
139 assert
.equal(mockLegacyApi
.hosts
[1].hostName
,
145 QUnit
.test('remove from legacy', function(/** QUnit.Assert */ assert
) {
146 return combinedApi
.get().then(function() {
147 return combinedApi
.remove('legacy-host').then(function() {
148 assert
.equal(mockGcdApi
.hosts
.length
, 2);
149 assert
.equal(mockLegacyApi
.hosts
.length
, 1);
150 assert
.notEqual(mockLegacyApi
.hosts
[0].hostId
, 'legacy-host');
155 QUnit
.test('remove from gcd', function(/** QUnit.Assert */ assert
) {
156 return combinedApi
.get().then(function() {
157 return combinedApi
.remove('gcd-host').then(function() {
158 assert
.equal(mockLegacyApi
.hosts
.length
, 2);
159 assert
.equal(mockGcdApi
.hosts
.length
, 1);
160 assert
.notEqual(mockGcdApi
.hosts
[0].hostId
, 'gcd-host');
165 QUnit
.test('remove from both', function(/** QUnit.Assert */ assert
) {
166 return combinedApi
.get().then(function() {
167 return combinedApi
.remove('common-host').then(function() {
168 assert
.equal(mockGcdApi
.hosts
.length
, 1);
169 assert
.equal(mockLegacyApi
.hosts
.length
, 1);
170 assert
.notEqual(mockGcdApi
.hosts
[0].hostId
, 'common-host');
171 assert
.notEqual(mockLegacyApi
.hosts
[0].hostId
, 'common-host');