aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/fs/entry_test.html
blob: eed649f671b15f02bf5264800f3a3e1b6d20e5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<title>Closure Unit Tests - goog.testing.fs.Entry</title>
<script src="../../base.js"></script>
<script>
goog.require('goog.fs.DirectoryEntry.Behavior');
goog.require('goog.fs.Error.ErrorCode');
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.fs.FileSystem');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
var fs, file, mockClock;

function setUp() {
  mockClock = new goog.testing.MockClock(true);

  fs = new goog.testing.fs.FileSystem();
  file = fs.getRoot().
      getDirectorySync('foo', goog.fs.DirectoryEntry.Behavior.CREATE).
      getFileSync('bar', goog.fs.DirectoryEntry.Behavior.CREATE);
}

function tearDown() {
  mockClock.uninstall();
}

function testGetName() {
  assertEquals('bar', file.getName());
}

function testGetFullPath() {
  assertEquals('/foo/bar', file.getFullPath());
  assertEquals('/', fs.getRoot().getFullPath());
}

function testGetFileSystem() {
  assertEquals(fs, file.getFileSystem());
}

function testMoveTo() {
  file.moveTo(fs.getRoot()).addCallback(function(newFile) {
    assertTrue(file.deleted);
    assertFalse(newFile.deleted);
    assertEquals('/bar', newFile.getFullPath());
    assertEquals(fs.getRoot(), newFile.parent);
    assertEquals(newFile, fs.getRoot().getFileSync('bar'));
    assertFalse(fs.getRoot().getDirectorySync('foo').hasChild('bar'));

    asyncTestCase.continueTesting();
  });
  waitForAsync('waiting for file move');
}

function testMoveToNewName() {
  // Advance the clock to an arbitrary, known time.
  mockClock.tick(71);
  file.moveTo(fs.getRoot(), 'baz').
      addCallback(function(newFile) {
        mockClock.tick();
        assertTrue(file.deleted);
        assertFalse(newFile.deleted);
        assertEquals('/baz', newFile.getFullPath());
        assertEquals(fs.getRoot(), newFile.parent);
        assertEquals(newFile, fs.getRoot().getFileSync('baz'));

        var oldParentDir = fs.getRoot().getDirectorySync('foo');
        assertFalse(oldParentDir.hasChild('bar'));
        assertFalse(oldParentDir.hasChild('baz'));

        return oldParentDir.getLastModified();
      }).
      addCallback(function(lastModifiedDate) {
        assertEquals(71, lastModifiedDate.getTime());
        var oldParentDir = fs.getRoot().getDirectorySync('foo');
        return oldParentDir.getMetadata();
      }).
      addCallback(function(metadata) {
        assertEquals(71, metadata.modificationTime.getTime());
        return fs.getRoot().getLastModified();
      }).
      addCallback(function(rootLastModifiedDate) {
        assertEquals(71, rootLastModifiedDate.getTime());
        return fs.getRoot().getMetadata();
      }).
      addCallback(function(rootMetadata) {
        assertEquals(71, rootMetadata.modificationTime.getTime());
        asyncTestCase.continueTesting();
      });
  waitForAsync('waiting for file move');
}

function testMoveDeletedFile() {
  assertFailsWhenDeleted(function() { return file.moveTo(fs.getRoot()); });
}

function testCopyTo() {
  mockClock.tick(61);
  file.copyTo(fs.getRoot()).
      addCallback(function(newFile) {
        assertFalse(file.deleted);
        assertFalse(newFile.deleted);
        assertEquals('/bar', newFile.getFullPath());
        assertEquals(fs.getRoot(), newFile.parent);
        assertEquals(newFile, fs.getRoot().getFileSync('bar'));

        var oldParentDir = fs.getRoot().getDirectorySync('foo');
        assertEquals(file, oldParentDir.getFileSync('bar'));
        return oldParentDir.getLastModified();
      }).
      addCallback(function(lastModifiedDate) {
        assertEquals('The original parent directory was not modified.',
                     0, lastModifiedDate.getTime());
        var oldParentDir = fs.getRoot().getDirectorySync('foo');
        return oldParentDir.getMetadata();
      }).
      addCallback(function(metadata) {
        assertEquals('The original parent directory was not modified.',
                     0, metadata.modificationTime.getTime());
        return fs.getRoot().getLastModified();
      }).
      addCallback(function(rootLastModifiedDate) {
        assertEquals(61, rootLastModifiedDate.getTime());
        return fs.getRoot().getMetadata();
      }).
      addCallback(function(rootMetadata) {
        assertEquals(61, rootMetadata.modificationTime.getTime());
        asyncTestCase.continueTesting();
      });
  waitForAsync('waiting for file copy');
}

function testCopyToNewName() {
  file.copyTo(fs.getRoot(), 'baz').addCallback(function(newFile) {
    assertFalse(file.deleted);
    assertFalse(newFile.deleted);
    assertEquals('/baz', newFile.getFullPath());
    assertEquals(fs.getRoot(), newFile.parent);
    assertEquals(newFile, fs.getRoot().getFileSync('baz'));
    assertEquals(file, fs.getRoot().getDirectorySync('foo').getFileSync('bar'));
    assertFalse(fs.getRoot().getDirectorySync('foo').hasChild('baz'));

    asyncTestCase.continueTesting();
  });
  waitForAsync('waiting for file copy');
}

function testCopyDeletedFile() {
  assertFailsWhenDeleted(function() { return file.copyTo(fs.getRoot()); });
}

function testRemove() {
  mockClock.tick(57);
  file.remove().
      addCallback(function() {
        mockClock.tick();
        var parentDir = fs.getRoot().getDirectorySync('foo');

        assertTrue(file.deleted);
        assertFalse(parentDir.hasChild('bar'));

        return parentDir.getLastModified();
      }).
      addCallback(function(date) {
        assertEquals(57, date.getTime());
        var parentDir = fs.getRoot().getDirectorySync('foo');
        return parentDir.getMetadata();
      }).
      addCallback(function(metadata) {
        assertEquals(57, metadata.modificationTime.getTime());
        asyncTestCase.continueTesting();
      });
  waitForAsync('waiting for file removal');
}

function testRemoveDeletedFile() {
  assertFailsWhenDeleted(function() { return file.remove(); });
}

function testGetParent() {
  file.getParent().addCallback(function(p) {
    assertEquals(file.parent, p);
    assertEquals(fs.getRoot().getDirectorySync('foo'), p);
    assertEquals('/foo', p.getFullPath());

    asyncTestCase.continueTesting();
  });
  waitForAsync('waiting for file parent');
}

function testGetDeletedFileParent() {
  assertFailsWhenDeleted(function() { return file.getParent(); });
}


function assertFailsWhenDeleted(fn) {
  file.remove().addCallback(fn).
      addCallback(function() { fail('Expected an error'); }).
      addErrback(function(err) {
        assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, err.code);
        asyncTestCase.continueTesting();
      });
  waitForAsync('waiting for file operation');
}

function waitForAsync(msg) {
  asyncTestCase.waitForAsync(msg);
  mockClock.tick();
}

</script>
</body>
</html>