blob: 05f0b5f686cdb237ae4ef508a569d6d72c317833 [file] [log] [blame]
Chris Craikb122baf2015-03-05 13:58:42 -08001<!DOCTYPE html>
2<!--
3Copyright 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
Chris Craik44c28202015-05-12 17:25:16 -07008<link rel="import" href="/base/event_target.html">
Chris Craikb122baf2015-03-05 13:58:42 -08009<link rel="import" href="/base/task.html">
Chris Craik44c28202015-05-12 17:25:16 -070010<link rel="import" href="/core/scripting_controller.html">
Chris Craikb122baf2015-03-05 13:58:42 -080011<link rel="import" href="/extras/tquery/tquery.html">
12
Chris Craik44c28202015-05-12 17:25:16 -070013<script>
14'use strict';
Chris Craikb122baf2015-03-05 13:58:42 -080015
Chris Craik44c28202015-05-12 17:25:16 -070016tv.exportTo('tv.e.tquery', function() {
17 function FakeSelectionController() {
18 tv.b.EventTarget.call(this);
19 this.allEvents_ = [];
20 }
21
22 FakeSelectionController.prototype = {
23 __proto__: tv.b.EventTarget.prototype,
24
25 get allEvents() {
26 return this.allEvents_;
Chris Craikb122baf2015-03-05 13:58:42 -080027 },
28
Chris Craik44c28202015-05-12 17:25:16 -070029 set allEvents(allEvents) {
30 this.allEvents_ = allEvents;
31 var e = new tv.b.Event('model-changed');
32 this.dispatchEvent(e);
33 },
34
35 addAllEventsMatchingFilterToSelectionAsTask: function(filter, selection) {
Chris Craikb122baf2015-03-05 13:58:42 -080036 return new tv.b.Task(function() {
Chris Craik44c28202015-05-12 17:25:16 -070037 var n = this.allEvents.length;
Chris Craikb122baf2015-03-05 13:58:42 -080038 for (var i = 0; i < n; i++) {
Chris Craik44c28202015-05-12 17:25:16 -070039 this.addSubtreeToSelection_(selection, this.allEvents[i]);
Chris Craikb122baf2015-03-05 13:58:42 -080040 }
41 }, this);
42 },
43
44 addSubtreeToSelection_: function(selection, event) {
45 selection.push(event);
46 if (event.subSlices) {
47 for (var i = 0; i < event.subSlices.length; i++) {
48 this.addSubtreeToSelection_(selection, event.subSlices[i]);
49 }
50 }
51 },
Chris Craik44c28202015-05-12 17:25:16 -070052 showScriptControlSelection: function(selection) {
53 this.selection = new tv.c.Selection();
54 this.highlight = selection;
Chris Craikb122baf2015-03-05 13:58:42 -080055 }
Chris Craik44c28202015-05-12 17:25:16 -070056 };
57 return {
58 FakeSelectionController: FakeSelectionController
59 };
60});
Chris Craikb122baf2015-03-05 13:58:42 -080061
62tv.b.unittest.testSuite(function() {
Chris Craik44c28202015-05-12 17:25:16 -070063 var FakeSelectionController = tv.e.tquery.FakeSelectionController;
64
65 function createSliceArray(sliceCount) {
66 var allEvents = [];
Chris Craikb122baf2015-03-05 13:58:42 -080067 for (var i = 0; i < sliceCount; i++) {
Chris Craik44c28202015-05-12 17:25:16 -070068 allEvents.push({guid: i});
Chris Craikb122baf2015-03-05 13:58:42 -080069 }
Chris Craik44c28202015-05-12 17:25:16 -070070 return allEvents;
Chris Craikb122baf2015-03-05 13:58:42 -080071 }
72
Chris Craik44c28202015-05-12 17:25:16 -070073 function createFakeSelectionController_(sliceCount) {
74 var selectionController = new FakeSelectionController();
75 selectionController.allEvents = createSliceArray(sliceCount);
76 return selectionController;
77 }
78
79 function getScriptObject(name) {
80 var typeInfos = tv.c.ScriptingObjectRegistry.getAllRegisteredTypeInfos();
81 for (var i = 0; i < typeInfos.length; i++) {
82 if (typeInfos[i].metadata.name === name) {
83 return typeInfos[i].constructor;
84 }
85 }
Chris Craikb122baf2015-03-05 13:58:42 -080086 }
87
88 test('tqueryAsyncSelection', function() {
Chris Craik44c28202015-05-12 17:25:16 -070089 var selectionController = createFakeSelectionController_(3);
90 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikb122baf2015-03-05 13:58:42 -080091
92 var result = tquery.show();
93 tv.b.Task.RunSynchronously(result);
Chris Craik44c28202015-05-12 17:25:16 -070094 assert.equal(selectionController.highlight.length, 3);
Chris Craikb122baf2015-03-05 13:58:42 -080095 });
96
97 test('tquerySyncSelection', function() {
Chris Craik44c28202015-05-12 17:25:16 -070098 var selectionController = createFakeSelectionController_(3);
99 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikb122baf2015-03-05 13:58:42 -0800100
101 assert.equal(tquery.selection.length, 3);
102
Chris Craik44c28202015-05-12 17:25:16 -0700103 // Selection should get reset when the model changes.
104 selectionController.allEvents = createSliceArray(5);
105 tquery.onModelChanged();
Chris Craikb122baf2015-03-05 13:58:42 -0800106 assert.equal(tquery.selection.length, 5);
107 });
108
109 test('tqueryPassThroughFiltering', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700110 var selectionController = new createFakeSelectionController_(3);
111 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikb122baf2015-03-05 13:58:42 -0800112
113 var result = tquery.filter().filter().show();
114 tv.b.Task.RunSynchronously(result);
Chris Craik44c28202015-05-12 17:25:16 -0700115 assert.equal(selectionController.highlight.length, 3);
Chris Craikb122baf2015-03-05 13:58:42 -0800116 });
117
118 test('tqueryFilterHasTitle', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700119 var hasTitle = getScriptObject('hasTitle');
120 var selectionController = new FakeSelectionController();
121 selectionController.allEvents = [
Chris Craikb122baf2015-03-05 13:58:42 -0800122 {guid: 1, title: 'a'},
123 {guid: 2, title: 'b'},
124 {guid: 3, title: 'c'}
Chris Craik44c28202015-05-12 17:25:16 -0700125 ];
126 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikb122baf2015-03-05 13:58:42 -0800127
128 var result = tquery.filter(hasTitle('a')).selection;
129 assert.equal(result.length, 1);
130 assert.equal(result[0].guid, 1);
131
132 var result = tquery.filter('b').selection;
133 assert.equal(result.length, 1);
134 assert.equal(result[0].guid, 2);
135
136 var result = tquery.filter(/^c$/).selection;
137 assert.equal(result.length, 1);
138 assert.equal(result[0].guid, 3);
139 });
140
141 test('tqueryFilterHasAncestor', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700142 var hasAncestor = getScriptObject('hasAncestor');
143 var selectionController = new FakeSelectionController();
144 selectionController.allEvents = [
Chris Craikb122baf2015-03-05 13:58:42 -0800145 {guid: 1, title: 'a'},
146 {guid: 2, title: 'b', subSlices: [{guid: 4}]},
147 {guid: 3, title: 'c'}
Chris Craik44c28202015-05-12 17:25:16 -0700148 ];
149 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikb122baf2015-03-05 13:58:42 -0800150
151 var result = tquery.filter(hasAncestor('b')).selection;
152 assert.equal(result.length, 1);
153 assert.equal(result[0].guid, 4);
154
155 var result = tquery.filter(hasAncestor()).selection;
156 assert.equal(result.length, 1);
157 assert.equal(result[0].guid, 4);
158
159 var result = tquery.filter(hasAncestor('a')).selection;
160 assert.equal(result.length, 0);
161 });
Chris Craikbeca7ae2015-04-07 13:29:55 -0700162
163 test('tqueryFilterAllOf', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700164 var allOf = getScriptObject('allOf');
165 var selectionController = new FakeSelectionController();
166 selectionController.allEvents = [
Chris Craikbeca7ae2015-04-07 13:29:55 -0700167 {guid: 1, title: 'a1'},
168 {guid: 2, title: 'b1'},
169 {guid: 3, title: 'c1'}
Chris Craik44c28202015-05-12 17:25:16 -0700170 ];
171 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikbeca7ae2015-04-07 13:29:55 -0700172
173 var result = tquery.filter(allOf('a1')).selection;
174 assert.equal(result.length, 1);
175 assert.equal(result[0].guid, 1);
176
177 var result = tquery.filter(allOf('a1', /1/)).selection;
178 assert.equal(result.length, 1);
179 assert.equal(result[0].guid, 1);
180
181 var result = tquery.filter(allOf()).selection;
182 assert.equal(result.length, 3);
183 });
184
185 test('tqueryFilterAnyOf', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700186 var anyOf = getScriptObject('anyOf');
187 var selectionController = new FakeSelectionController();
188 selectionController.allEvents = [
Chris Craikbeca7ae2015-04-07 13:29:55 -0700189 {guid: 1, title: 'a'},
190 {guid: 2, title: 'b'},
191 {guid: 3, title: 'c'}
Chris Craik44c28202015-05-12 17:25:16 -0700192 ];
193 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikbeca7ae2015-04-07 13:29:55 -0700194
195 var result = tquery.filter(anyOf('a', 'b')).selection;
196 assert.equal(result.length, 2);
197 assert.equal(result[0].guid, 1);
198 assert.equal(result[1].guid, 2);
199
200 var result = tquery.filter(anyOf('not there', 'a')).selection;
201 assert.equal(result.length, 1);
202 assert.equal(result[0].guid, 1);
203
204 var result = tquery.filter(anyOf()).selection;
205 assert.equal(result.length, 3);
206 });
207
208 test('tqueryFilterIsTopLevel', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700209 var isTopLevel = getScriptObject('isTopLevel');
210 var selectionController = new FakeSelectionController();
211 selectionController.allEvents = [
Chris Craikbeca7ae2015-04-07 13:29:55 -0700212 {guid: 1, title: 'a'},
213 {guid: 2, title: 'b', subSlices: [{guid: 4}]},
214 {guid: 3, title: 'c'}
Chris Craik44c28202015-05-12 17:25:16 -0700215 ];
216 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craikbeca7ae2015-04-07 13:29:55 -0700217
218 var result = tquery.filter(isTopLevel()).selection;
219 assert.equal(result.length, 3);
220 assert.equal(result[0].guid, 1);
221 assert.equal(result[1].guid, 2);
222 assert.equal(result[2].guid, 3);
223
224 var result = tquery.filter(isTopLevel('a')).selection;
225 assert.equal(result.length, 1);
226 assert.equal(result[0].guid, 1);
227 });
Chris Craik19832152015-04-16 15:43:38 -0700228
229 test('tqueryFilterHasDuration', function() {
Chris Craik44c28202015-05-12 17:25:16 -0700230 var hasDuration = getScriptObject('hasDuration');
231 var selectionController = new FakeSelectionController();
232 selectionController.allEvents = [
Chris Craik19832152015-04-16 15:43:38 -0700233 {guid: 1, title: 'a', duration: 1},
234 {guid: 2, title: 'b', duration: 2},
235 {guid: 3, title: 'c', duration: 3},
236 {guid: 4, title: 'no duration'}
Chris Craik44c28202015-05-12 17:25:16 -0700237 ];
238 var tquery = new tv.e.tquery.TQuery(selectionController);
Chris Craik19832152015-04-16 15:43:38 -0700239
240 var result = tquery.filter(hasDuration(1.5, 2.5)).selection;
241 assert.equal(result.length, 1);
242 assert.equal(result[0].guid, 2);
243
244 var result = tquery.filter(hasDuration(3, Infinity)).selection;
245 assert.equal(result.length, 1);
246 assert.equal(result[0].guid, 3);
247
248 var result = tquery.filter(hasDuration(-1, 0)).selection;
249 assert.equal(result.length, 0);
250
251 var result = tquery.filter(hasDuration(function(d) {
252 return d > 2;
253 })).selection;
254 assert.equal(result.length, 1);
255 assert.equal(result[0].guid, 3);
256 });
Chris Craikb122baf2015-03-05 13:58:42 -0800257});
258</script>