1 // Copyright 2013 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 GEN('#include "chrome/browser/ui/webui/identity_internals_ui_browsertest.h"');
8 * Test C++ fixture for downloads WebUI testing.
10 * @extends {testing.Test}
12 function IdentityInternalsUIBrowserTest() {}
15 * Base fixture for Downloads WebUI testing.
16 * @extends {testing.Test}
19 function BaseIdentityInternalsWebUITest() {}
21 BaseIdentityInternalsWebUITest.prototype = {
22 __proto__: testing.Test.prototype,
25 * Browse to the downloads page & call our preLoad().
27 browsePreload: 'chrome://identity-internals',
30 typedefCppFixture: 'IdentityInternalsUIBrowserTest',
33 * Gets all of the token entries on the page.
34 * @return {!NodeList} Elements displaying token information.
36 getTokens: function() {
37 return document.querySelectorAll('#token-list > div');
41 * Gets the expiration time displayed on the page for a given entry.
42 * @param {Element} tokenEntry Display element holding token information.
43 * @return {Date} Expiration date of the token.
45 getExpirationTime: function(tokenEntry) {
46 return Date.parse(tokenEntry.querySelector('.expiration-time')
51 * Gets the extension id displayed on the page for a given entry.
52 * @param {Element} tokenEntry Display element holding token information.
53 * @return {string} Extension Id of the token.
55 getExtensionId: function(tokenEntry) {
56 return tokenEntry.querySelector('.extension-id').innerText;
60 * Gets the extension name displayed on the page for a given entry.
61 * @param {Element} tokenEntry Display element holding token information.
62 * @return {string} Extension Name of the token.
64 getExtensionName: function(tokenEntry) {
65 return tokenEntry.querySelector('.extension-name').innerText;
69 * Gets the revoke button of the token entry.
70 * @param {Element} tokenEntry Diplsy element holding token information.
71 * @return {HTMLButtonElement} Revoke button belonging related to the token.
73 getRevokeButton: function(tokenEntry) {
74 return tokenEntry.querySelector('.revoke-button');
78 * Gets the token ID displayed on the page for a given entry.
79 * @param {Element} tokenEntry Display element holding token information.
80 * @return {string} Token ID of the token.
82 getAccessToken: function(tokenEntry) {
83 return tokenEntry.querySelector('.access-token').innerText;
87 * Gets the token status displayed on the page for a given entry.
88 * @param {Element} tokenEntry Display element holding token information.
89 * @return {string} Token status of the token.
91 getTokenStatus: function(tokenEntry) {
92 return tokenEntry.querySelector('.token-status').innerText;
96 * Gets the token scopes displayed on the page for a given entry.
97 * @param {Element} tokenEntry Display element holding token information.
98 * @return {string[]} Token scopes of the token.
100 getScopes: function(tokenEntry) {
101 return tokenEntry.querySelector('.scope-list')
102 .innerHTML.split('<br>');
106 // Test verifying chrome://identity-internals Web UI when the token cache is
108 TEST_F('BaseIdentityInternalsWebUITest', 'emptyTokenCache', function() {
109 var tokenListEntries = this.getTokens();
110 expectEquals(0, tokenListEntries.length);
114 * Fixture for Identity Internals Web UI testing with a single token in the
115 * Identity API token cache.
116 * @extends {BaseIdentityInternalsWebUITest}
119 function IdentityInternalsSingleTokenWebUITest() {}
121 IdentityInternalsSingleTokenWebUITest.prototype = {
122 __proto__: BaseIdentityInternalsWebUITest.prototype,
125 testGenPreamble: function() {
126 GEN(' SetupTokenCacheWithStoreApp();');
130 // Test for listing a token cache with a single token. It uses a known extension
131 // - the Chrome Web Store, in order to check the extension name.
132 TEST_F('IdentityInternalsSingleTokenWebUITest', 'getAllTokens', function() {
133 var tokenListEntries = this.getTokens();
134 expectEquals(1, tokenListEntries.length);
135 expectEquals('Store', this.getExtensionName(tokenListEntries[0]));
136 expectEquals('ahfgeienlihckogmohjhadlkjgocpleb',
137 this.getExtensionId(tokenListEntries[0]));
138 expectEquals('store_token', this.getAccessToken(tokenListEntries[0]));
139 expectEquals('Token Present', this.getTokenStatus(tokenListEntries[0]));
140 expectLT(this.getExpirationTime(tokenListEntries[0]) - new Date(),
142 var scopes = this.getScopes(tokenListEntries[0]);
143 expectEquals(3, scopes.length);
144 expectEquals('store_scope1', scopes[0]);
145 expectEquals('store_scope2', scopes[1]);
146 expectEquals('', scopes[2]);
149 // Test ensuring the getters on the BaseIdentityInternalsWebUITest work
150 // correctly. They are implemented on the child class, because the parent does
151 // not have any tokens to display.
152 TEST_F('IdentityInternalsSingleTokenWebUITest', 'verifyGetters', function() {
153 var tokenListEntries = document.querySelectorAll('#token-list > div');
154 var actualTokens = this.getTokens();
155 expectEquals(tokenListEntries.length, actualTokens.length);
156 expectEquals(tokenListEntries[0], actualTokens[0]);
157 expectEquals(this.getExtensionName(tokenListEntries[0]),
158 tokenListEntries[0].querySelector('.extension-name').innerText);
159 expectEquals(this.getExtensionId(tokenListEntries[0]),
160 tokenListEntries[0].querySelector('.extension-id').innerText);
161 expectEquals(this.getAccessToken(tokenListEntries[0]),
162 tokenListEntries[0].querySelector('.access-token').innerText);
163 expectEquals(this.getTokenStatus(tokenListEntries[0]),
164 tokenListEntries[0].querySelector('.token-status').innerText);
165 expectEquals(this.getExpirationTime(tokenListEntries[0]),
166 Date.parse(tokenListEntries[0].querySelector('.expiration-time')
168 var scopes = tokenListEntries[0].querySelector('.scope-list')
169 .innerHTML.split('<br>');
170 var actualScopes = this.getScopes(tokenListEntries[0]);
171 expectEquals(scopes.length, actualScopes.length);
172 for (var i = 0; i < scopes.length; i++) {
173 expectEquals(scopes[i], actualScopes[i]);
178 * Fixture for Identity Internals Web UI testing with multiple tokens in the
179 * Identity API token cache.
180 * @extends {BaseIdentityInternalsWebUITest}
183 function IdentityInternalsMultipleTokensWebUITest() {}
185 IdentityInternalsMultipleTokensWebUITest.prototype = {
186 __proto__: BaseIdentityInternalsWebUITest.prototype,
189 testGenPreamble: function() {
190 GEN(' SetupTokenCache(2);');
194 // Test for listing a token cache with multiple tokens. Names of the extensions
195 // are empty, because extensions are faked, and not present in the extension
197 TEST_F('IdentityInternalsMultipleTokensWebUITest', 'getAllTokens', function() {
198 var tokenListEntries = this.getTokens();
199 expectEquals(2, tokenListEntries.length);
200 expectEquals('', this.getExtensionName(tokenListEntries[0]));
201 expectEquals('extension0',
202 this.getExtensionId(tokenListEntries[0]));
203 expectEquals('token0', this.getAccessToken(tokenListEntries[0]));
204 expectEquals('Token Present', this.getTokenStatus(tokenListEntries[0]));
205 expectLT(this.getExpirationTime(tokenListEntries[0]) - new Date(),
207 var scopes = this.getScopes(tokenListEntries[0]);
208 expectEquals(3, scopes.length);
209 expectEquals('scope_1_0', scopes[0]);
210 expectEquals('scope_2_0', scopes[1]);
211 expectEquals('', scopes[2]);
212 expectEquals('', this.getExtensionName(tokenListEntries[1]));
213 expectEquals('extension1',
214 this.getExtensionId(tokenListEntries[1]));
215 expectEquals('token1', this.getAccessToken(tokenListEntries[1]));
216 expectEquals('Token Present', this.getTokenStatus(tokenListEntries[1]));
217 expectLT(this.getExpirationTime(tokenListEntries[1]) - new Date(),
219 var scopes = this.getScopes(tokenListEntries[1]);
220 expectEquals(3, scopes.length);
221 expectEquals('scope_1_1', scopes[0]);
222 expectEquals('scope_2_1', scopes[1]);
223 expectEquals('', scopes[2]);
227 * Fixture for asynchronous testing of Identity Internals Web UI with multiple
228 * tokens in Identity API token cache.
229 * @extends {IdentityInternalsMultipleTokensWebUITest}
232 function IdentityInternalsWebUITestAsync() {}
234 IdentityInternalsWebUITestAsync.prototype = {
235 __proto__: IdentityInternalsMultipleTokensWebUITest.prototype,
241 TEST_F('IdentityInternalsWebUITestAsync', 'revokeToken', function() {
242 var tokenListBefore = this.getTokens();
243 expectEquals(2, tokenListBefore.length);
244 var tokenRevokeDone = identity_internals.tokenRevokeDone;
245 identity_internals.tokenRevokeDone = this.continueTest(
246 WhenTestDone.ALWAYS, function(accessTokens) {
247 tokenRevokeDone.call(identity_internals, accessTokens);
248 identity_internals.tokenRevokeDone = tokenRevokeDone;
249 var tokenListAfter = this.getTokens();
250 expectEquals(1, tokenListAfter.length);
251 expectEquals(this.getAccessToken(tokenListBefore[0]),
252 this.getAccessToken(tokenListAfter[0]));
254 this.getRevokeButton(tokenListBefore[1]).click();