Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_system_provider / create_file / test.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
5 'use strict';
6
7 /**
8  * @type {Object}
9  * @const
10  */
11 var TESTING_FILE = Object.freeze({
12   isDirectory: false,
13   name: 'kitty',
14   size: 0,
15   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
16 });
17
18 /**
19  * @type {Object}
20  * @const
21  */
22 var TESTING_NEW_FILE = Object.freeze({
23   isDirectory: false,
24   name: 'puppy',
25   size: 0,
26   modificationTime: new Date(2014, 4, 28, 10, 39, 15)
27 });
28
29 /**
30  * Sets up the tests. Called once per all test cases. In case of a failure,
31  * the callback is not called.
32  *
33  * @param {function()} callback Success callback.
34  */
35 function setUp(callback) {
36   chrome.fileSystemProvider.onGetMetadataRequested.addListener(
37       test_util.onGetMetadataRequestedDefault);
38   chrome.fileSystemProvider.onCreateFileRequested.addListener(
39       test_util.onCreateFileRequested);
40
41   test_util.defaultMetadata['/' + TESTING_FILE.name] = TESTING_FILE;
42
43   test_util.mountFileSystem(callback);
44 }
45
46 /**
47  * Runs all of the test cases, one by one.
48  */
49 function runTests() {
50   chrome.test.runTests([
51     // Create a file which doesn't exist. Should succeed.
52     function createFileSuccessSimple() {
53       test_util.fileSystem.root.getFile(
54           TESTING_NEW_FILE.name, {create: true},
55           chrome.test.callbackPass(function(entry) {
56             chrome.test.assertEq(TESTING_NEW_FILE.name, entry.name);
57             chrome.test.assertFalse(entry.isDirectory);
58           }), function(error) {
59             chrome.test.fail(error.name);
60           });
61     },
62
63     // Create a file which exists, non-exclusively. Should succeed.
64     function createFileOrOpenSuccess() {
65       test_util.fileSystem.root.getFile(
66           TESTING_FILE.name, {create: true, exclusive: false},
67           chrome.test.callbackPass(function(entry) {
68             chrome.test.assertEq(TESTING_FILE.name, entry.name);
69             chrome.test.assertFalse(entry.isDirectory);
70           }), function(error) {
71             chrome.test.fail(error.name);
72           });
73     },
74
75     // Create a file which exists, exclusively. Should fail.
76     function createFileExistsError() {
77       test_util.fileSystem.root.getFile(
78           TESTING_FILE.name, {create: true, exclusive: true},
79           function(entry) {
80             chrome.test.fail('Created a file, but should fail.');
81           }, chrome.test.callbackPass(function(error) {
82             chrome.test.assertEq('InvalidModificationError', error.name);
83           }));
84     }
85   ]);
86 }
87
88 // Setup and run all of the test cases.
89 setUp(runTests);