Upstream version 9.38.198.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       var onSuccess = chrome.test.callbackPass();
54       test_util.fileSystem.root.getFile(
55           TESTING_NEW_FILE.name, {create: true},
56           function(entry) {
57             chrome.test.assertEq(TESTING_NEW_FILE.name, entry.name);
58             chrome.test.assertFalse(entry.isDirectory);
59             onSuccess();
60           }, function(error) {
61             chrome.test.fail(error.name);
62           });
63     },
64
65     // Create a file which exists, non-exclusively. Should succeed.
66     function createFileOrOpenSuccess() {
67       var onSuccess = chrome.test.callbackPass();
68       test_util.fileSystem.root.getFile(
69           TESTING_FILE.name, {create: true, exclusive: false},
70           function(entry) {
71             chrome.test.assertEq(TESTING_FILE.name, entry.name);
72             chrome.test.assertFalse(entry.isDirectory);
73             onSuccess();
74           }, function(error) {
75             chrome.test.fail(error.name);
76           });
77     },
78
79     // Create a file which exists, exclusively. Should fail.
80     function createFileExistsError() {
81       var onSuccess = chrome.test.callbackPass();
82       test_util.fileSystem.root.getFile(
83           TESTING_FILE.name, {create: true, exclusive: true},
84           function(entry) {
85             chrome.test.fail('Created a file, but should fail.');
86           }, function(error) {
87             chrome.test.assertEq('InvalidModificationError', error.name);
88             onSuccess();
89           });
90     }
91   ]);
92 }
93
94 // Setup and run all of the test cases.
95 setUp(runTests);