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