Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / file_manager / unit_tests / file_operation_handler_unittest.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 'use strict';
5
6 // Mock items.
7 var background = null;
8
9 // Test target.
10 var handler = null;
11
12 // Set up the test components.
13 function setUp() {
14   // Set up string assets.
15   loadTimeData.data = {
16     COPY_FILE_NAME: 'Copying $1...',
17     COPY_TARGET_EXISTS_ERROR: '$1 is already exists.',
18     COPY_FILESYSTEM_ERROR: 'Copy filesystem error: $1',
19     FILE_ERROR_GENERIC: 'File error generic.',
20     COPY_UNEXPECTED_ERROR: 'Copy unexpected error: $1'
21   };
22
23   // Make ProgressCenterHandler.
24   background = new MockBackground();
25   handler = new FileOperationHandler(background);
26 }
27
28 // Test for success copy.
29 function testCopySuccess() {
30   // Dispatch an event.
31   background.fileOperationManager.dispatchEvent({
32     type: 'copy-progress',
33     taskId: 'TASK_ID',
34     reason: 'BEGIN',
35     status: {
36       operationType: 'COPY',
37       numRemainingItems: 1,
38       processingEntryName: 'sample.txt',
39       totalBytes: 200,
40       processedBytes: 0
41     }
42   });
43
44   // Check the updated item.
45   var item = background.progressCenter.items['TASK_ID'];
46   assertEquals(ProgressItemState.PROGRESSING, item.state);
47   assertEquals('TASK_ID', item.id);
48   assertEquals('Copying sample.txt...', item.message);
49   assertEquals('copy', item.type);
50   assertEquals(true, item.single);
51   assertEquals(0, item.progressRateInPercent);
52
53   // Dispatch an event.
54   background.fileOperationManager.dispatchEvent({
55     type: 'copy-progress',
56     taskId: 'TASK_ID',
57     reason: 'SUCCESS',
58     status: {
59       operationType: 'COPY'
60     }
61   });
62
63   // Check the updated item.
64   item = background.progressCenter.items['TASK_ID'];
65   assertEquals(ProgressItemState.COMPLETED, item.state);
66   assertEquals('TASK_ID', item.id);
67   assertEquals('', item.message);
68   assertEquals('copy', item.type);
69   assertEquals(true, item.single);
70   assertEquals(100, item.progressRateInPercent);
71   assertEquals(1, background.closeRequestCount);
72 }
73
74 // Test for copy cancel.
75 function testCopyCancel() {
76   // Dispatch an event.
77   background.fileOperationManager.dispatchEvent({
78     type: 'copy-progress',
79     taskId: 'TASK_ID',
80     reason: 'BEGIN',
81     status: {
82       operationType: 'COPY',
83       numRemainingItems: 1,
84       processingEntryName: 'sample.txt',
85       totalBytes: 200,
86       processedBytes: 0
87     }
88   });
89
90   // Check the updated item.
91   var item = background.progressCenter.items['TASK_ID'];
92   assertEquals(ProgressItemState.PROGRESSING, item.state);
93   assertEquals('Copying sample.txt...', item.message);
94   assertEquals('copy', item.type);
95   assertEquals(true, item.single);
96   assertEquals(0, item.progressRateInPercent);
97
98   // Dispatch an event.
99   background.fileOperationManager.cancelEvent = {
100     type: 'copy-progress',
101     taskId: 'TASK_ID',
102     reason: 'CANCELED',
103     status: {
104       operationType: 'COPY'
105     }
106   };
107   item.cancelCallback();
108
109   // Check the updated item.
110   item = background.progressCenter.items['TASK_ID'];
111   assertEquals(ProgressItemState.CANCELED, item.state);
112   assertEquals('', item.message);
113   assertEquals('copy', item.type);
114   assertEquals(true, item.single);
115   assertEquals(0, item.progressRateInPercent);
116   assertEquals(1, background.closeRequestCount);
117 }
118
119 // Test for copy target exists error.
120 function testCopyTargetExistsError() {
121   // Dispatch an event.
122   background.fileOperationManager.dispatchEvent({
123     type: 'copy-progress',
124     taskId: 'TASK_ID',
125     reason: 'ERROR',
126     status: {
127       operationType: 'COPY'
128     },
129     error: {
130       code: util.FileOperationErrorType.TARGET_EXISTS,
131       data: {name: 'sample.txt'}
132     }
133   });
134
135   // Check the updated item.
136   var item = background.progressCenter.items['TASK_ID'];
137   assertEquals(ProgressItemState.ERROR, item.state);
138   assertEquals('sample.txt is already exists.', item.message);
139   assertEquals('copy', item.type);
140   assertEquals(true, item.single);
141   assertEquals(0, item.progressRateInPercent);
142   assertEquals(1, background.closeRequestCount);
143 }
144
145 // Test for copy file system error.
146 function testCopyFileSystemError() {
147   // Dispatch an event.
148   background.fileOperationManager.dispatchEvent({
149     type: 'copy-progress',
150     taskId: 'TASK_ID',
151     reason: 'ERROR',
152     status: {
153       operationType: 'COPY'
154     },
155     error: {
156       code: util.FileOperationErrorType.FILESYSTEM_ERROR,
157       data: {code: ''}
158     }
159   });
160
161   // Check the updated item.
162   var item = background.progressCenter.items['TASK_ID'];
163   assertEquals(ProgressItemState.ERROR, item.state);
164   assertEquals('Copy filesystem error: File error generic.', item.message);
165   assertEquals('copy', item.type);
166   assertEquals(true, item.single);
167   assertEquals(0, item.progressRateInPercent);
168   assertEquals(1, background.closeRequestCount);
169 }
170
171 // Test for copy unexpected error.
172 function testCopyUnexpectedError() {
173   // Dispatch an event.
174   background.fileOperationManager.dispatchEvent({
175     type: 'copy-progress',
176     taskId: 'TASK_ID',
177     reason: 'ERROR',
178     status: {
179       operationType: 'COPY'
180     },
181     error: {
182       code: 'Unexpected',
183       data: {name: 'sample.txt'}
184     }
185   });
186
187   // Check the updated item.
188   var item = background.progressCenter.items['TASK_ID'];
189   assertEquals(ProgressItemState.ERROR, item.state);
190   assertEquals('Copy unexpected error: Unexpected', item.message);
191   assertEquals('copy', item.type);
192   assertEquals(true, item.single);
193   assertEquals(0, item.progressRateInPercent);
194   assertEquals(1, background.closeRequestCount);
195 }