OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * importer.MediaScanner and importer.ScanResult test double. | 6 * importer.MediaScanner and importer.ScanResult test double. |
7 * | 7 * |
8 * @constructor | 8 * @constructor |
9 * @struct | 9 * @struct |
10 * @implements {importer.MediaScanner} | 10 * @implements {importer.MediaScanner} |
11 * @implements {importer.ScanResult} | |
12 */ | 11 */ |
13 function TestMediaScanner() { | 12 function TestMediaScanner() { |
| 13 |
| 14 /** @private {!importer.ScanResult} */ |
| 15 this.scans_ = []; |
| 16 |
14 /** | 17 /** |
15 * List of file entries found while scanning. | 18 * List of file entries found while scanning. |
16 * @type {!Array.<!FileEntry>} | 19 * @type {!Array.<!FileEntry>} |
17 */ | 20 */ |
18 this.fileEntries = []; | 21 this.fileEntries = []; |
19 | 22 |
20 /** @type {number} */ | 23 /** @type {number} */ |
21 this.totalBytes = 100; | 24 this.totalBytes = 100; |
22 | 25 |
23 /** @type {number} */ | 26 /** @type {number} */ |
24 this.scanDuration = 100; | 27 this.scanDuration = 100; |
25 | 28 |
26 /** @type {!Promise.<!importer.ScanResult>} */ | 29 /** @private {!Array.<!importer.ScanObserver>} */ |
27 this.whenFinished; | 30 this.observers = []; |
28 } | 31 } |
29 | 32 |
30 /** @override */ | 33 /** @override */ |
| 34 TestMediaScanner.prototype.addObserver = function(observer) { |
| 35 this.observers.push(observer); |
| 36 }; |
| 37 |
| 38 /** @override */ |
| 39 TestMediaScanner.prototype.removeObserver = function(observer) { |
| 40 var index = this.observers.indexOf(observer); |
| 41 if (index > -1) { |
| 42 this.observers.splice(index, 1); |
| 43 } else { |
| 44 console.warn('Ignoring request to remove observer that is not registered.'); |
| 45 } |
| 46 }; |
| 47 |
| 48 /** @override */ |
31 TestMediaScanner.prototype.scan = function(entries) { | 49 TestMediaScanner.prototype.scan = function(entries) { |
32 var result = new TestScanResult(this); | 50 var result = new TestScanResult(this.fileEntries); |
33 this.whenFinished = Promise.resolve(result); | 51 result.totalBytes = this.totalBytes; |
| 52 result.scanDuration = this.scanDuration; |
| 53 this.scans_.push(result); |
34 return result; | 54 return result; |
35 }; | 55 }; |
36 | 56 |
37 /** | 57 /** |
| 58 * Finalizes all previously started scans. |
| 59 */ |
| 60 TestMediaScanner.prototype.finalizeScans = function() { |
| 61 this.scans_.forEach(this.finalize.bind(this)); |
| 62 }; |
| 63 |
| 64 /** |
| 65 * Notifies observers that a scan has finished. |
| 66 * @param {!importer.ScanResult} result |
| 67 */ |
| 68 TestMediaScanner.prototype.finalize = function(result) { |
| 69 result.finalize(); |
| 70 this.observers.forEach( |
| 71 function(observer) { |
| 72 observer(importer.ScanEvent.FINALIZED, result); |
| 73 }); |
| 74 }; |
| 75 |
| 76 /** |
| 77 * @param {number} expected |
| 78 */ |
| 79 TestMediaScanner.prototype.assertScanCount = function(expected) { |
| 80 assertEquals(expected, this.scans_.length); |
| 81 }; |
| 82 |
| 83 /** |
38 * importer.MediaScanner and importer.ScanResult test double. | 84 * importer.MediaScanner and importer.ScanResult test double. |
39 * | 85 * |
40 * @constructor | 86 * @constructor |
41 * @struct | 87 * @struct |
42 * @implements {importer.MediaScanner} | |
43 * @implements {importer.ScanResult} | 88 * @implements {importer.ScanResult} |
44 * | 89 * |
45 * @param {!TestMediaScanner} scanner | 90 * @param {!Array.<!FileEntry>} fileEntries |
46 */ | 91 */ |
47 function TestScanResult(scanner) { | 92 function TestScanResult(fileEntries) { |
48 /** @private {!TestMediaScanner} */ | 93 /** |
49 this.scanner_ = scanner; | 94 * List of file entries found while scanning. |
| 95 * @type {!Array.<!FileEntry>} |
| 96 */ |
| 97 this.fileEntries = fileEntries.slice(); |
| 98 |
| 99 /** @type {number} */ |
| 100 this.totalBytes = 100; |
| 101 |
| 102 /** @type {number} */ |
| 103 this.scanDuration = 100; |
| 104 |
| 105 /** @type {function} */ |
| 106 this.resolveResult_; |
| 107 |
| 108 /** @type {function} */ |
| 109 this.rejectResult_; |
| 110 |
| 111 /** @type {boolean} */ |
| 112 this.settled_ = false; |
| 113 |
| 114 /** @type {!Promise.<!importer.ScanResult>} */ |
| 115 this.whenFinal_ = new Promise( |
| 116 function(resolve, reject) { |
| 117 this.resolveResult_ = function(result) { |
| 118 this.settled_ = true; |
| 119 resolve(result); |
| 120 }.bind(this); |
| 121 this.rejectResult_ = function() { |
| 122 this.settled_ = true; |
| 123 reject(); |
| 124 }.bind(this); |
| 125 }.bind(this)); |
50 } | 126 } |
51 | 127 |
52 /** @override */ | 128 /** @override */ |
53 TestScanResult.prototype.getFileEntries = function() { | 129 TestScanResult.prototype.getFileEntries = function() { |
54 return this.scanner_.fileEntries; | 130 return this.fileEntries; |
55 }; | 131 }; |
56 | 132 |
57 /** @override */ | 133 /** @override */ |
58 TestScanResult.prototype.getTotalBytes = function() { | 134 TestScanResult.prototype.getTotalBytes = function() { |
59 return this.scanner_.totalBytes; | 135 return this.totalBytes; |
60 }; | 136 }; |
61 | 137 |
62 /** @override */ | 138 /** @override */ |
63 TestScanResult.prototype.getScanDurationMs = function() { | 139 TestScanResult.prototype.getScanDurationMs = function() { |
64 return this.scanner_.scanDuration; | 140 return this.scanDuration; |
65 }; | 141 }; |
66 | 142 |
67 /** @override */ | 143 /** @override */ |
68 TestScanResult.prototype.whenFinished = function() { | 144 TestScanResult.prototype.finalize = function() { |
69 return this.scanner_.whenFinished; | 145 return this.resolveResult_(this); |
70 }; | 146 }; |
| 147 |
| 148 /** @override */ |
| 149 TestScanResult.prototype.whenFinal = function() { |
| 150 return this.whenFinal_; |
| 151 }; |
| 152 |
| 153 /** @override */ |
| 154 TestScanResult.prototype.isFinal = function() { |
| 155 return this.settled_; |
| 156 }; |
OLD | NEW |