- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / sync_file_system / get_file_statuses / test.js
1 // Copyright (c) 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
5 var fileSystem;
6 var testFiles = ['Test1', 'Test2', 'Conflicting'];
7
8 var testStep = [
9   function() {
10     chrome.syncFileSystem.requestFileSystem(testStep.shift());
11   },
12   // Create empty files.
13   function(fs) {
14     fileSystem = fs;
15     createFiles(fileSystem, testFiles.slice(0), testStep.shift());
16   },
17   // Read entries in root directory.
18   function() {
19     var reader = fileSystem.root.createReader();
20     reader.readEntries(testStep.shift(), errorHandler);
21   },
22   // Query file statuses for the returned entries.
23   function(entries) {
24     chrome.test.assertEq(testFiles.length, entries.length);
25     chrome.syncFileSystem.getFileStatuses(
26       entries, chrome.test.callbackPass(testStep.shift()));
27   },
28   // Verify the returned statuses.
29   function(fileStatuses) {
30     // Sort the input and results array so that their orders match.
31     testFiles.sort();
32     fileStatuses.sort(sortByFilePath);
33
34     chrome.test.assertEq(testFiles.length, fileStatuses.length);
35     for (var i = 0; i < testFiles.length; ++i) {
36       chrome.test.assertEq(testFiles[i], fileStatuses[i].fileEntry.name);
37       chrome.test.assertEq('/' + testFiles[i],
38                            fileStatuses[i].fileEntry.fullPath);
39       chrome.test.assertTrue(fileStatuses[i].fileEntry.isFile);
40       chrome.test.assertTrue(!fileStatuses[i].error);
41       var expectedStatus =
42           (testFiles[i] == 'Conflicting') ? 'conflicting' : 'pending';
43       chrome.test.assertEq(expectedStatus, fileStatuses[i].status);
44     }
45     chrome.test.succeed();
46   }
47 ];
48
49 function createFiles(fileSystem, fileNames, callback) {
50   if (!fileNames.length) {
51     callback();
52     return;
53   }
54   fileSystem.root.getFile(
55     fileNames.shift(), {create:true},
56     createFiles.bind(null, fileSystem, fileNames, callback),
57     errorHandler);
58 }
59
60 function sortByFilePath(a, b) {
61   if (a.fileEntry.fullPath < b.fileEntry.fullPath)
62     return -1;
63   if (a.fileEntry.fullPath > b.fileEntry.fullPath)
64     return 1;
65   return 0;
66 }
67
68 function errorHandler(e) {
69   console.log("Failed test with error" + e);
70   chrome.test.fail();
71 }
72
73 chrome.test.runTests([
74   testStep.shift()
75 ]);