| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.testing.fs.integrationTest'); |
| 16 |
goog.setTestOnly('goog.testing.fs.integrationTest'); |
| 17 |
|
| 18 |
goog.require('goog.Promise'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.fs'); |
| 21 |
goog.require('goog.fs.DirectoryEntry'); |
| 22 |
goog.require('goog.fs.Error'); |
| 23 |
goog.require('goog.fs.FileSaver'); |
| 24 |
goog.require('goog.testing.PropertyReplacer'); |
| 25 |
goog.require('goog.testing.fs'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
|
| 28 |
var TEST_DIR = 'goog-fs-test-dir'; |
| 29 |
|
| 30 |
var Behavior = goog.fs.DirectoryEntry.Behavior; |
| 31 |
var EventType = goog.fs.FileSaver.EventType; |
| 32 |
var ReadyState = goog.fs.FileSaver.ReadyState; |
| 33 |
|
| 34 |
var deferredFs = goog.testing.fs.getTemporary(); |
| 35 |
|
| 36 |
function setUpPage() { |
| 37 |
goog.testing.fs.install(new goog.testing.PropertyReplacer()); |
| 38 |
} |
| 39 |
|
| 40 |
function tearDown() { |
| 41 |
return loadTestDir().then(function(dir) { return dir.removeRecursively(); }); |
| 42 |
} |
| 43 |
|
| 44 |
function testWriteFile() { |
| 45 |
return loadFile('test', Behavior.CREATE) |
| 46 |
.then(goog.partial(writeToFile, 'test content')) |
| 47 |
.then(goog.partial(checkFileContent, 'test content')); |
| 48 |
} |
| 49 |
|
| 50 |
function testRemoveFile() { |
| 51 |
return loadFile('test', Behavior.CREATE) |
| 52 |
.then(goog.partial(writeToFile, 'test content')) |
| 53 |
.then(function(fileEntry) { return fileEntry.remove(); }) |
| 54 |
.then(goog.partial(checkFileRemoved, 'test')); |
| 55 |
} |
| 56 |
|
| 57 |
function testMoveFile() { |
| 58 |
var subdir = loadDirectory('subdir', Behavior.CREATE); |
| 59 |
var writtenFile = loadFile('test', Behavior.CREATE) |
| 60 |
.then(goog.partial(writeToFile, 'test content')); |
| 61 |
|
| 62 |
return goog.Promise.all([subdir, writtenFile]) |
| 63 |
.then(function(results) { |
| 64 |
var dir = results[0]; |
| 65 |
var fileEntry = results[1]; |
| 66 |
return fileEntry.moveTo(dir); |
| 67 |
}) |
| 68 |
.then(goog.partial(checkFileContent, 'test content')) |
| 69 |
.then(goog.partial(checkFileRemoved, 'test')); |
| 70 |
} |
| 71 |
|
| 72 |
function testCopyFile() { |
| 73 |
var file = loadFile('test', Behavior.CREATE); |
| 74 |
var subdir = loadDirectory('subdir', Behavior.CREATE); |
| 75 |
var writtenFile = file.then(goog.partial(writeToFile, 'test content')); |
| 76 |
|
| 77 |
return goog.Promise.all([subdir, writtenFile]) |
| 78 |
.then(function(results) { |
| 79 |
var dir = results[0]; |
| 80 |
var fileEntry = results[1]; |
| 81 |
return fileEntry.copyTo(dir); |
| 82 |
}) |
| 83 |
.then(goog.partial(checkFileContent, 'test content')) |
| 84 |
.then(function() { return file; }) |
| 85 |
.then(goog.partial(checkFileContent, 'test content')); |
| 86 |
} |
| 87 |
|
| 88 |
function testAbortWrite() { |
| 89 |
var file = loadFile('test', Behavior.CREATE); |
| 90 |
|
| 91 |
file.then(goog.partial(startWrite, 'test content')) |
| 92 |
.then(function(writer) { |
| 93 |
writer.abort(); |
| 94 |
return writer; |
| 95 |
}) |
| 96 |
.then(goog.partial(waitForEvent, EventType.ABORT)); |
| 97 |
|
| 98 |
return file.then(goog.partial(checkFileContent, '')); |
| 99 |
} |
| 100 |
|
| 101 |
function testSeek() { |
| 102 |
var file = loadFile('test', Behavior.CREATE); |
| 103 |
|
| 104 |
return file.then(goog.partial(writeToFile, 'test content')) |
| 105 |
.then(function(fileEntry) { return fileEntry.createWriter(); }) |
| 106 |
.then(goog.partial(checkReadyState, ReadyState.INIT)) |
| 107 |
.then(function(writer) { |
| 108 |
writer.seek(5); |
| 109 |
writer.write(goog.fs.getBlob('stuff and things')); |
| 110 |
return writer; |
| 111 |
}) |
| 112 |
.then(goog.partial(checkReadyState, ReadyState.WRITING)) |
| 113 |
.then(goog.partial(waitForEvent, EventType.WRITE)) |
| 114 |
.then(function() { return file; }) |
| 115 |
.then(goog.partial(checkFileContent, 'test stuff and things')); |
| 116 |
} |
| 117 |
|
| 118 |
function testTruncate() { |
| 119 |
var file = loadFile('test', Behavior.CREATE); |
| 120 |
|
| 121 |
return file.then(goog.partial(writeToFile, 'test content')) |
| 122 |
.then(function(fileEntry) { return fileEntry.createWriter(); }) |
| 123 |
.then(goog.partial(checkReadyState, ReadyState.INIT)) |
| 124 |
.then(function(writer) { |
| 125 |
writer.truncate(4); |
| 126 |
return writer; |
| 127 |
}) |
| 128 |
.then(goog.partial(checkReadyState, ReadyState.WRITING)) |
| 129 |
.then(goog.partial(waitForEvent, EventType.WRITE)) |
| 130 |
.then(function() { return file; }) |
| 131 |
.then(goog.partial(checkFileContent, 'test')); |
| 132 |
} |
| 133 |
|
| 134 |
function loadTestDir() { |
| 135 |
return deferredFs.then(function(fs) { |
| 136 |
return fs.getRoot().getDirectory(TEST_DIR, Behavior.CREATE); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
function loadFile(filename, behavior) { |
| 141 |
return loadTestDir().then(function(dir) { |
| 142 |
return dir.getFile(filename, behavior); |
| 143 |
}); |
| 144 |
} |
| 145 |
|
| 146 |
function loadDirectory(filename, behavior) { |
| 147 |
return loadTestDir().then(function(dir) { |
| 148 |
return dir.getDirectory(filename, behavior); |
| 149 |
}); |
| 150 |
} |
| 151 |
|
| 152 |
function startWrite(content, fileEntry) { |
| 153 |
return fileEntry.createWriter() |
| 154 |
.then(goog.partial(checkReadyState, ReadyState.INIT)) |
| 155 |
.then(function(writer) { |
| 156 |
writer.write(goog.fs.getBlob(content)); |
| 157 |
return writer; |
| 158 |
}) |
| 159 |
.then(goog.partial(checkReadyState, ReadyState.WRITING)); |
| 160 |
} |
| 161 |
|
| 162 |
function waitForEvent(type, target) { |
| 163 |
return new goog.Promise(function(resolve, reject) { |
| 164 |
goog.events.listenOnce(target, type, resolve); |
| 165 |
}); |
| 166 |
} |
| 167 |
|
| 168 |
function writeToFile(content, fileEntry) { |
| 169 |
return startWrite(content, fileEntry) |
| 170 |
.then(goog.partial(waitForEvent, EventType.WRITE)) |
| 171 |
.then(function() { return fileEntry; }); |
| 172 |
} |
| 173 |
|
| 174 |
function checkFileContent(content, fileEntry) { |
| 175 |
return fileEntry.file() |
| 176 |
.then(function(blob) { return goog.fs.blobToString(blob); }) |
| 177 |
.then(goog.partial(assertEquals, content)); |
| 178 |
} |
| 179 |
|
| 180 |
function checkFileRemoved(filename) { |
| 181 |
return loadFile(filename) |
| 182 |
.then(goog.partial(fail, 'expected file to be removed')) |
| 183 |
.thenCatch(function(err) { |
| 184 |
assertEquals(err.code, goog.fs.Error.ErrorCode.NOT_FOUND); |
| 185 |
return true; // Go back to the non-rejected path. |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
function checkReadyState(expectedState, writer) { |
| 190 |
assertEquals(expectedState, writer.getReadyState()); |
| 191 |
return writer; |
| 192 |
} |