- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / identity_internals_ui_browsertest.js
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.
4
5 GEN('#include "chrome/browser/ui/webui/identity_internals_ui_browsertest.h"');
6
7 /**
8  * Test C++ fixture for downloads WebUI testing.
9  * @constructor
10  * @extends {testing.Test}
11  */
12 function IdentityInternalsUIBrowserTest() {}
13
14 /**
15  * Base fixture for Downloads WebUI testing.
16  * @extends {testing.Test}
17  * @constructor
18  */
19 function BaseIdentityInternalsWebUITest() {}
20
21 BaseIdentityInternalsWebUITest.prototype = {
22   __proto__: testing.Test.prototype,
23
24   /**
25    * Browse to the downloads page & call our preLoad().
26    */
27   browsePreload: 'chrome://identity-internals',
28
29   /** @override */
30   typedefCppFixture: 'IdentityInternalsUIBrowserTest',
31
32   /**
33    * Gets all of the token entries on the page.
34    * return {Element[]} Elements displaying token information.
35    */
36   getTokens: function() {
37     return document.querySelectorAll('#token-list > div');
38   },
39
40   /**
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.
44    */
45   getExpirationTime: function(tokenEntry) {
46     return Date.parse(tokenEntry.querySelector('.expiration-time')
47         .innerText);
48   },
49
50   /**
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.
54    */
55   getExtensionId: function(tokenEntry) {
56     return tokenEntry.querySelector('.extension-id').innerText;
57   },
58
59   /**
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.
63    */
64   getExtensionName: function(tokenEntry) {
65     return tokenEntry.querySelector('.extension-name').innerText;
66   },
67
68   /**
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.
72    */
73   getRevokeButton: function(tokenEntry) {
74     return tokenEntry.querySelector('.revoke-button');
75   },
76
77   /**
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.
81    */
82   getAccessToken: function(tokenEntry) {
83     return tokenEntry.querySelector('.access-token').innerText;
84   },
85
86   /**
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.
90    */
91   getTokenStatus: function(tokenEntry) {
92     return tokenEntry.querySelector('.token-status').innerText;
93   },
94
95   /**
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.
99    */
100   getScopes: function(tokenEntry) {
101     return tokenEntry.querySelector('.scope-list')
102         .innerHTML.split('<br>');
103   },
104 };
105
106 // Test verifying chrome://identity-internals Web UI when the token cache is
107 // empty.
108 TEST_F('BaseIdentityInternalsWebUITest', 'emptyTokenCache', function() {
109   var tokenListEntries = this.getTokens();
110   expectEquals(0, tokenListEntries.length);
111 });
112
113 /**
114  * Fixture for Identity Internals Web UI testing with a single token in the
115  * Identity API token cache.
116  * @extends {BaseIdentityInternalsWebUITest}
117  * @constructor
118  */
119 function IdentityInternalsSingleTokenWebUITest() {}
120
121 IdentityInternalsSingleTokenWebUITest.prototype = {
122   __proto__: BaseIdentityInternalsWebUITest.prototype,
123
124   /** @override */
125   testGenPreamble: function() {
126     GEN('  SetupTokenCacheWithStoreApp();');
127   },
128 };
129
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(),
141            3600 * 1000);
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]);
147 });
148
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')
167           .innerText));
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]);
174   }
175 });
176
177 /**
178  * Fixture for Identity Internals Web UI testing with multiple tokens in the
179  * Identity API token cache.
180  * @extends {BaseIdentityInternalsWebUITest}
181  * @constructor
182  */
183 function IdentityInternalsMultipleTokensWebUITest() {}
184
185 IdentityInternalsMultipleTokensWebUITest.prototype = {
186   __proto__: BaseIdentityInternalsWebUITest.prototype,
187
188   /** @override */
189   testGenPreamble: function() {
190     GEN('  SetupTokenCache(2);');
191   },
192 };
193
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
196 // service.
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(),
206            3600 * 1000);
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(),
218            3600 * 1000);
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]);
224 });
225
226 /**
227  * Fixture for asynchronous testing of Identity Internals Web UI with multiple
228  * tokens in Identity API token cache.
229  * @extends {IdentityInternalsMultipleTokensWebUITest}
230  * @constructor
231  */
232 function IdentityInternalsWebUITestAsync() {}
233
234 IdentityInternalsWebUITestAsync.prototype = {
235   __proto__: IdentityInternalsMultipleTokensWebUITest.prototype,
236
237   /** @inhritDoc */
238   isAsync: true,
239 };
240
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]));
253       }.bind(this));
254   this.getRevokeButton(tokenListBefore[1]).click();
255 });
256