Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / file_manager / unit_tests / device_handler_unittest.js
1 // Copyright 2014 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 'use strict';
5
6 /**
7  * Test target.
8  * @type {DeviceHandler}
9  */
10 var handler;
11
12 /**
13  * Dummy private APIs.
14  */
15 var chrome;
16
17 /**
18  * Callbacks registered by setTimeout.
19  * @type {Array.<function>}
20  */
21 var timeoutCallbacks;
22
23 // Set up the test components.
24 function setUp() {
25   // Set up string assets.
26   loadTimeData.data = {
27     REMOVABLE_DEVICE_DETECTION_TITLE: 'Device detected',
28     REMOVABLE_DEVICE_SCANNING_MESSAGE: 'Scanning...',
29     DEVICE_UNKNOWN_MESSAGE: 'DEVICE_UNKNOWN: $1',
30     DEVICE_UNSUPPORTED_MESSAGE: 'DEVICE_UNSUPPORTED: $1',
31     MULTIPART_DEVICE_UNSUPPORTED_MESSAGE: 'MULTIPART_DEVICE_UNSUPPORTED: $1',
32     EXTERNAL_STORAGE_DISABLED_MESSAGE: 'EXTERNAL_STORAGE_DISABLED',
33     FORMATTING_OF_DEVICE_PENDING_TITLE: 'FORMATTING_OF_DEVICE_PENDING_TITLE',
34     FORMATTING_OF_DEVICE_PENDING_MESSAGE: 'FORMATTING_OF_DEVICE_PENDING',
35     FORMATTING_OF_DEVICE_FINISHED_TITLE: 'FORMATTING_OF_DEVICE_FINISHED_TITLE',
36     FORMATTING_FINISHED_SUCCESS_MESSAGE: 'FORMATTING_FINISHED_SUCCESS',
37     FORMATTING_OF_DEVICE_FAILED_TITLE: 'FORMATTING_OF_DEVICE_FAILED_TITLE',
38     FORMATTING_FINISHED_FAILURE_MESSAGE: 'FORMATTING_FINISHED_FAILURE'
39   };
40
41   // Make dummy APIs.
42   chrome = {
43     fileBrowserPrivate: {
44       onDeviceChanged: {
45         addListener: function(listener) {
46           this.dispatch = listener;
47         }
48       },
49       onMountCompleted: {
50         addListener: function(listener) {
51           this.dispatch = listener;
52         }
53       }
54     },
55     notifications: {
56       create: function(id, params, callback) {
57         assertFalse(!!this.items[id]);
58         this.items[id] = params;
59         callback();
60       },
61       clear: function(id, callback) { delete this.items[id]; callback(); },
62       items: {}
63     },
64     runtime: {
65       getURL: function(path) { return path; }
66     }
67   };
68
69   // Reset timeout callbacks.
70   timeoutCallbacks = [];
71
72   // Make a device handler.
73   handler = new DeviceHandler();
74 }
75
76 /**
77  * Overrided setTimoeut funciton.
78  */
79 window.setTimeout = function(func) {
80   timeoutCallbacks.push(func);
81 };
82
83 /**
84  * Call all pending timeout functions.
85  */
86 function callTimeoutCallbacks() {
87   while (timeoutCallbacks.length) {
88     timeoutCallbacks.shift()();
89   }
90 }
91
92 function registerTypicalDevice() {
93   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
94     type: 'added',
95     devicePath: '/device/path'
96   });
97   assertFalse('device:/device/path' in chrome.notifications.items);
98   callTimeoutCallbacks();
99   assertEquals('Scanning...',
100                chrome.notifications.items['device:/device/path'].message);
101 }
102
103 function testGoodDevice() {
104   registerTypicalDevice();
105   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
106     status: 'success',
107     volumeMetadata: {
108       isParentDevice: true,
109       deviceType: 'usb',
110       devicePath: '/device/path',
111       deviceLabel: 'label'
112     }
113   });
114   assertEquals(0, Object.keys(chrome.notifications.items).length);
115 }
116
117 function testGoodDeviceWithBadParent() {
118   registerTypicalDevice();
119
120   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
121     status: 'error_internal',
122     volumeMetadata: {
123       isParentDevice: true,
124       deviceType: 'usb',
125       devicePath: '/device/path',
126       deviceLabel: 'label'
127     }
128   });
129   assertFalse(!!chrome.notifications.items['device:/device/path']);
130   assertEquals(
131       'DEVICE_UNKNOWN: label',
132       chrome.notifications.items['deviceFail:/device/path'].message);
133
134   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
135     status: 'success',
136     volumeMetadata: {
137       isParentDevice: false,
138       deviceType: 'usb',
139       devicePath: '/device/path',
140       deviceLabel: 'label'
141     }
142   });
143   assertEquals(0, Object.keys(chrome.notifications.items).length);
144
145   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
146     status: 'success',
147     volumeMetadata: {
148       isParentDevice: false,
149       deviceType: 'usb',
150       devicePath: '/device/path',
151       deviceLabel: 'label'
152     }
153   });
154   // Should do nothing this time.
155   assertEquals(0, Object.keys(chrome.notifications.items).length);
156 }
157
158 function testUnsupportedDevice() {
159   registerTypicalDevice();
160
161   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
162     status: 'error_unsuported_filesystem',
163     volumeMetadata: {
164       isParentDevice: false,
165       deviceType: 'usb',
166       devicePath: '/device/path',
167       deviceLabel: 'label'
168     }
169   });
170   assertFalse(!!chrome.notifications.items['device:/device/path']);
171   assertEquals(
172       'DEVICE_UNSUPPORTED: label',
173       chrome.notifications.items['deviceFail:/device/path'].message);
174 }
175
176 function testUnsupportedWithUnknownParent() {
177   registerTypicalDevice();
178
179   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
180     status: 'error_internal',
181     volumeMetadata: {
182       isParentDevice: true,
183       deviceType: 'usb',
184       devicePath: '/device/path',
185       deviceLabel: 'label'
186     }
187   });
188   assertEquals(
189       'DEVICE_UNKNOWN: label',
190       chrome.notifications.items['deviceFail:/device/path'].message);
191
192   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
193     status: 'error_unsuported_filesystem',
194     volumeMetadata: {
195       isParentDevice: false,
196       deviceType: 'usb',
197       devicePath: '/device/path',
198       deviceLabel: 'label'
199     }
200   });
201   assertEquals(1, Object.keys(chrome.notifications.items).length);
202   assertEquals(
203       'DEVICE_UNSUPPORTED: label',
204       chrome.notifications.items['deviceFail:/device/path'].message);
205 }
206
207 function testMountPartialSuccess() {
208   registerTypicalDevice();
209
210   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
211     status: 'success',
212     volumeMetadata: {
213       isParentDevice: false,
214       deviceType: 'usb',
215       devicePath: '/device/path',
216       deviceLabel: 'label'
217     }
218   });
219   assertEquals(0, Object.keys(chrome.notifications.items).length);
220
221   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
222     status: 'error_unsuported_filesystem',
223     volumeMetadata: {
224       isParentDevice: false,
225       deviceType: 'usb',
226       devicePath: '/device/path',
227       deviceLabel: 'label'
228     }
229   });
230   assertEquals(1, Object.keys(chrome.notifications.items).length);
231   assertEquals(
232       'MULTIPART_DEVICE_UNSUPPORTED: label',
233       chrome.notifications.items['deviceFail:/device/path'].message);
234 }
235
236 function testUnknown() {
237   registerTypicalDevice();
238
239   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
240     status: 'error_unknown',
241     volumeMetadata: {
242       isParentDevice: false,
243       deviceType: 'usb',
244       devicePath: '/device/path',
245       deviceLabel: 'label'
246     }
247   });
248   assertEquals(1, Object.keys(chrome.notifications.items).length);
249   assertEquals(
250       'DEVICE_UNKNOWN: label',
251       chrome.notifications.items['deviceFail:/device/path'].message);
252 }
253
254 function testNonASCIILabel() {
255   registerTypicalDevice();
256
257   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
258     status: 'error_internal',
259     volumeMetadata: {
260       isParentDevice: false,
261       deviceType: 'usb',
262       devicePath: '/device/path',
263       // "RA (U+30E9) BE (U+30D9) RU (U+30EB)" in Katakana letters.
264       deviceLabel: '\u30E9\u30D9\u30EB'
265     }
266   });
267   assertEquals(1, Object.keys(chrome.notifications.items).length);
268   assertEquals(
269       'DEVICE_UNKNOWN: \u30E9\u30D9\u30EB',
270       chrome.notifications.items['deviceFail:/device/path'].message);
271 }
272
273 function testMulitpleFail() {
274   registerTypicalDevice();
275
276   // The first parent error.
277   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
278     status: 'error_internal',
279     volumeMetadata: {
280       isParentDevice: true,
281       deviceType: 'usb',
282       devicePath: '/device/path',
283       deviceLabel: 'label'
284     }
285   });
286   assertEquals(1, Object.keys(chrome.notifications.items).length);
287   assertEquals(
288       'DEVICE_UNKNOWN: label',
289       chrome.notifications.items['deviceFail:/device/path'].message);
290
291   // The first child error that replaces the parent error.
292   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
293     status: 'error_internal',
294     volumeMetadata: {
295       isParentDevice: false,
296       deviceType: 'usb',
297       devicePath: '/device/path',
298       deviceLabel: 'label'
299     }
300   });
301   assertEquals(1, Object.keys(chrome.notifications.items).length);
302   assertEquals(
303       'DEVICE_UNKNOWN: label',
304       chrome.notifications.items['deviceFail:/device/path'].message);
305
306   // The second child error that turns to a multi-partition error.
307   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
308     status: 'error_internal',
309     volumeMetadata: {
310       isParentDevice: false,
311       deviceType: 'usb',
312       devicePath: '/device/path',
313       deviceLabel: 'label'
314     }
315   });
316   assertEquals(1, Object.keys(chrome.notifications.items).length);
317   assertEquals(
318       'MULTIPART_DEVICE_UNSUPPORTED: label',
319       chrome.notifications.items['deviceFail:/device/path'].message);
320
321   // The third child error that should be ignored because the error message does
322   // not changed.
323   chrome.fileBrowserPrivate.onMountCompleted.dispatch({
324     status: 'error_internal',
325     volumeMetadata: {
326       isParentDevice: false,
327       deviceType: 'usb',
328       devicePath: '/device/path',
329       deviceLabel: 'label'
330     }
331   });
332   assertEquals(1, Object.keys(chrome.notifications.items).length);
333   assertEquals(
334       'MULTIPART_DEVICE_UNSUPPORTED: label',
335       chrome.notifications.items['deviceFail:/device/path'].message);
336 }
337
338 function testScanCanceled() {
339   registerTypicalDevice();
340
341   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
342     type: 'scan_canceled',
343     devicePath: '/device/path'
344   });
345   assertEquals(0, Object.keys(chrome.notifications.items).length);
346
347   // Nothing happened.
348   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
349     type: 'removed',
350     devicePath: '/device/path'
351   });
352   assertEquals(0, Object.keys(chrome.notifications.items).length);
353 }
354
355 function testDisabledDevice() {
356   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
357     type: 'disabled',
358     devicePath: '/device/path'
359   });
360   assertEquals(1, Object.keys(chrome.notifications.items).length);
361   assertEquals('EXTERNAL_STORAGE_DISABLED',
362                chrome.notifications.items['deviceFail:/device/path'].message);
363
364   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
365     type: 'removed',
366     devicePath: '/device/path'
367   });
368   assertEquals(0, Object.keys(chrome.notifications.items).length);
369 }
370
371 function testFormatSucceeded() {
372   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
373     type: 'format_start',
374     devicePath: '/device/path'
375   });
376   assertEquals(1, Object.keys(chrome.notifications.items).length);
377   assertEquals('FORMATTING_OF_DEVICE_PENDING',
378                chrome.notifications.items['formatStart:/device/path'].message);
379
380   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
381     type: 'format_success',
382     devicePath: '/device/path'
383   });
384   assertEquals(1, Object.keys(chrome.notifications.items).length);
385   assertEquals('FORMATTING_FINISHED_SUCCESS',
386                chrome.notifications.items[
387                    'formatSuccess:/device/path'].message);
388 }
389
390 function testFormatFailed() {
391   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
392     type: 'format_start',
393     devicePath: '/device/path'
394   });
395   assertEquals(1, Object.keys(chrome.notifications.items).length);
396   assertEquals('FORMATTING_OF_DEVICE_PENDING',
397                chrome.notifications.items['formatStart:/device/path'].message);
398
399   chrome.fileBrowserPrivate.onDeviceChanged.dispatch({
400     type: 'format_fail',
401     devicePath: '/device/path'
402   });
403   assertEquals(1, Object.keys(chrome.notifications.items).length);
404   assertEquals('FORMATTING_FINISHED_FAILURE',
405                chrome.notifications.items['formatFail:/device/path'].message);
406 }