Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
| 3 | Copyright 2015 The Chromium Authors. All rights reserved. |
| 4 | Use of this source code is governed by a BSD-style license that can be |
| 5 | found in the LICENSE file. |
| 6 | --> |
| 7 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 8 | <link rel="import" href="/base/event_target.html"> |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 9 | <link rel="import" href="/base/task.html"> |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 10 | <link rel="import" href="/core/scripting_controller.html"> |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 11 | <link rel="import" href="/extras/tquery/tquery.html"> |
| 12 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 13 | <script> |
| 14 | 'use strict'; |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 15 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 16 | tv.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 Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 27 | }, |
| 28 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 29 | 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 Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 36 | return new tv.b.Task(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 37 | var n = this.allEvents.length; |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 38 | for (var i = 0; i < n; i++) { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 39 | this.addSubtreeToSelection_(selection, this.allEvents[i]); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 40 | } |
| 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 52 | showScriptControlSelection: function(selection) { |
| 53 | this.selection = new tv.c.Selection(); |
| 54 | this.highlight = selection; |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 55 | } |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 56 | }; |
| 57 | return { |
| 58 | FakeSelectionController: FakeSelectionController |
| 59 | }; |
| 60 | }); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 61 | |
| 62 | tv.b.unittest.testSuite(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 63 | var FakeSelectionController = tv.e.tquery.FakeSelectionController; |
| 64 | |
| 65 | function createSliceArray(sliceCount) { |
| 66 | var allEvents = []; |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 67 | for (var i = 0; i < sliceCount; i++) { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 68 | allEvents.push({guid: i}); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 69 | } |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 70 | return allEvents; |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 73 | 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 Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | test('tqueryAsyncSelection', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 89 | var selectionController = createFakeSelectionController_(3); |
| 90 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 91 | |
| 92 | var result = tquery.show(); |
| 93 | tv.b.Task.RunSynchronously(result); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 94 | assert.equal(selectionController.highlight.length, 3); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | test('tquerySyncSelection', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 98 | var selectionController = createFakeSelectionController_(3); |
| 99 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 100 | |
| 101 | assert.equal(tquery.selection.length, 3); |
| 102 | |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 103 | // Selection should get reset when the model changes. |
| 104 | selectionController.allEvents = createSliceArray(5); |
| 105 | tquery.onModelChanged(); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 106 | assert.equal(tquery.selection.length, 5); |
| 107 | }); |
| 108 | |
| 109 | test('tqueryPassThroughFiltering', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 110 | var selectionController = new createFakeSelectionController_(3); |
| 111 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 112 | |
| 113 | var result = tquery.filter().filter().show(); |
| 114 | tv.b.Task.RunSynchronously(result); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 115 | assert.equal(selectionController.highlight.length, 3); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 116 | }); |
| 117 | |
| 118 | test('tqueryFilterHasTitle', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 119 | var hasTitle = getScriptObject('hasTitle'); |
| 120 | var selectionController = new FakeSelectionController(); |
| 121 | selectionController.allEvents = [ |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 122 | {guid: 1, title: 'a'}, |
| 123 | {guid: 2, title: 'b'}, |
| 124 | {guid: 3, title: 'c'} |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 125 | ]; |
| 126 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 127 | |
| 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 142 | var hasAncestor = getScriptObject('hasAncestor'); |
| 143 | var selectionController = new FakeSelectionController(); |
| 144 | selectionController.allEvents = [ |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 145 | {guid: 1, title: 'a'}, |
| 146 | {guid: 2, title: 'b', subSlices: [{guid: 4}]}, |
| 147 | {guid: 3, title: 'c'} |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 148 | ]; |
| 149 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 150 | |
| 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 Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 162 | |
| 163 | test('tqueryFilterAllOf', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 164 | var allOf = getScriptObject('allOf'); |
| 165 | var selectionController = new FakeSelectionController(); |
| 166 | selectionController.allEvents = [ |
Chris Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 167 | {guid: 1, title: 'a1'}, |
| 168 | {guid: 2, title: 'b1'}, |
| 169 | {guid: 3, title: 'c1'} |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 170 | ]; |
| 171 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 172 | |
| 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 186 | var anyOf = getScriptObject('anyOf'); |
| 187 | var selectionController = new FakeSelectionController(); |
| 188 | selectionController.allEvents = [ |
Chris Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 189 | {guid: 1, title: 'a'}, |
| 190 | {guid: 2, title: 'b'}, |
| 191 | {guid: 3, title: 'c'} |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 192 | ]; |
| 193 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 194 | |
| 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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 209 | var isTopLevel = getScriptObject('isTopLevel'); |
| 210 | var selectionController = new FakeSelectionController(); |
| 211 | selectionController.allEvents = [ |
Chris Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 212 | {guid: 1, title: 'a'}, |
| 213 | {guid: 2, title: 'b', subSlices: [{guid: 4}]}, |
| 214 | {guid: 3, title: 'c'} |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 215 | ]; |
| 216 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | beca7ae | 2015-04-07 13:29:55 -0700 | [diff] [blame] | 217 | |
| 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 Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 228 | |
| 229 | test('tqueryFilterHasDuration', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 230 | var hasDuration = getScriptObject('hasDuration'); |
| 231 | var selectionController = new FakeSelectionController(); |
| 232 | selectionController.allEvents = [ |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 233 | {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 Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 237 | ]; |
| 238 | var tquery = new tv.e.tquery.TQuery(selectionController); |
Chris Craik | 1983215 | 2015-04-16 15:43:38 -0700 | [diff] [blame] | 239 | |
| 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 Craik | b122baf | 2015-03-05 13:58:42 -0800 | [diff] [blame] | 257 | }); |
| 258 | </script> |