Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: ui/file_manager/file_manager/background/js/task_queue_unittest.js

Issue 789603004: Add a TaskQueue. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix missing assert reference in the unit test. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 /** @type {!TaskQueue} */
6 var queue;
7
8 /** @type {!Object<importer.TaskQueue.UpdateType, number>} */
9 var updates = {};
10
11 function setUp() {
12 queue = new importer.TaskQueue();
13
14 // Set up a callback to log updates from running tasks.
15 for (var updateType in importer.TaskQueue.UpdateType) {
16 // Reset counts for all update types.
17 updates[importer.TaskQueue.UpdateType[updateType]] = 0;
18 }
19 // Counts the number of updates of each type that have been received.
20 var updateCallback = function(type, updatedTask) {
21 updates[type]++;
22 };
23 queue.addUpdateCallback(updateCallback);
24 }
25
26 /**
27 * A Task subclass for testing.
28 * @constructor
29 * @extends {importer.TaskQueue.BaseTask}
30 */
31 var TestTask = function() {
32 importer.TaskQueue.Task.call(this);
33
34 /** @type {boolean} */
35 this.wasRun = false;
36
37 /**
38 * @private {Function}
39 */
40 this.runResolver_ = null;
41
42 this.runPromise_ = new Promise(function(resolve) {
43 this.runResolver_ = resolve;
44 }.bind(this));
45 };
46 TestTask.prototype.__proto__ = importer.TaskQueue.BaseTask.prototype;
47
48 /** @override */
49 TestTask.prototype.run = function() {
50 this.wasRun = true;
51 this.runResolver_(this);
52 };
53
54 /** @return {!Promise} A promise that settles once #run is called. */
55 TestTask.prototype.whenRun = function() {
56 return this.runPromise_;
57 };
58
59 // Verifies that a queued task gets run.
60 function testRunsTask(callback) {
61 var task = new TestTask();
62 queue.queueTask(task);
63 reportPromise(task.whenRun(), callback);
64 }
65
66 // Verifies that multiple queued tasks get run.
67 function testRunsTasks(callback) {
68 var task0 = new TestTask();
69 var task1 = new TestTask();
70
71 // Make the tasks call Task#notifySuccess when they are run.
72 task0.whenRun().then(function(task) { task.notifySuccess(); });
73 task1.whenRun().then(function(task) { task.notifySuccess(); });
74
75 // Enqueue both tasks, and then verify that they were run.
76 queue.queueTask(task0);
77 queue.queueTask(task1);
78 reportPromise(
79 Promise.all([task0.whenRun(), task1.whenRun()]),
80 callback);
81 }
82
83 // Verifies that the active callback triggers when the queue starts doing work
84 function testOnActiveCalled(callback) {
85 var task = new TestTask();
86
87 // Make a promise that resolves when the active callback is triggered.
88 var whenActive = new Promise(function(resolve) {
89 queue.setActiveCallback(
90 function() {
91 // Verify that the active callback is called before the task runs.
92 assertFalse(task.wasRun);
93 resolve();
94 });
95 });
96
97 // Queue a task, and then check that the active callback was triggered.
98 queue.queueTask(task);
99 reportPromise(whenActive, callback);
100 }
101
102 // Verifies that the idle callback triggers when the queue is empty.
103 function testOnIdleCalled(callback) {
104 var task = new TestTask();
105
106 task.whenRun().then(function(task) { task.notifySuccess(); });
107
108 // Make a promise that resolves when the idle callback is triggered
109 // (i.e. after all queued tasks have finished running).
110 var whenDone = new Promise(function(resolve) {
111 queue.setIdleCallback(
112 function() {
113 // Verify that the idle callback is called after the task runs.
114 assertTrue(task.wasRun);
115 resolve();
116 });
117 });
118
119 // Queue a task, then check that the idle callback was triggered.
120 queue.queueTask(task);
121 reportPromise(whenDone, callback);
122 }
123
124 // Verifies that the update callback is called when a task reports progress.
125 function testProgressUpdate(callback) {
126 var task = new TestTask();
127
128 // Get the task to report some progress, then success, when it's run.
129 task.whenRun()
130 .then(
131 function(task) {
132 task.notifyProgress();
133 return task;
134 })
135 .then(
136 function(task) {
137 task.notifySuccess();
138 return task;
139 });
140
141 // Make a promise that resolves after the task runs.
142 var whenDone = new Promise(function(resolve) {
143 queue.setIdleCallback(
144 function() {
145 // Verify that progress was recorded.
146 assertEquals(1, updates[importer.TaskQueue.UpdateType.PROGRESS]);
147 resolve();
148 });
149 });
150
151 queue.queueTask(task);
152 reportPromise(whenDone, callback);
153 }
154
155 // Verifies that the update callback is called to report successful task
156 // completion.
157 function testSuccessUpdate(callback) {
158 var task = new TestTask();
159
160 // Get the task to report success when it's run.
161 task.whenRun().then(function(task) { task.notifySuccess(); });
162
163 queue.queueTask(task);
164
165 var whenDone = new Promise(function(resolve) {
166 queue.setIdleCallback(
167 function() {
168 // Verify that the done callback was called.
169 assertEquals(1, updates[importer.TaskQueue.UpdateType.SUCCESS]);
170 resolve();
171 });
172 });
173
174 reportPromise(whenDone, callback);
175 }
176
177 // Verifies that the update callback is called to report task errors.
178 function testErrorUpdate(callback) {
179 var task = new TestTask();
180
181 // Get the task to report an error when it's run.
182 task.whenRun().then(function(task) { task.notifyError(); });
183
184 queue.queueTask(task);
185
186 var whenDone = new Promise(function(resolve) {
187 queue.setIdleCallback(
188 function() {
189 // Verify that the done callback was called.
190 assertEquals(1, updates[importer.TaskQueue.UpdateType.ERROR]);
191 resolve();
192 });
193 });
194
195 reportPromise(whenDone, callback);
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698