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