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