Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [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 | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 9 | <link rel="import" href="/core/scripting_controller.html"> |
| 10 | <link rel="import" href="/core/test_utils.html"> |
| 11 | <link rel="import" href="/core/timeline_track_view.html"> |
| 12 | |
| 13 | <script> |
| 14 | 'use strict'; |
| 15 | |
| 16 | tv.b.unittest.testSuite(function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 17 | function FakeSelectionController() { |
| 18 | tv.b.EventTarget.call(this); |
| 19 | |
| 20 | this.addAllEventsMatchingFilterToSelectionReturnValue = []; |
| 21 | |
| 22 | this.viewport = undefined; |
| 23 | this.model = undefined; |
| 24 | this.selection = new tv.c.Selection(); |
| 25 | this.highlight = new tv.c.Selection(); |
| 26 | } |
| 27 | FakeSelectionController.prototype = { |
| 28 | __proto__: tv.b.EventTarget.prototype |
| 29 | }; |
| 30 | |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 31 | test('scriptingControllerBasicArithmetic', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 32 | var selectionController = new FakeSelectionController(); |
| 33 | var controller = new tv.c.ScriptingController(selectionController); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 34 | var result = controller.executeCommand('1 + 1'); |
| 35 | assert.equal(result, 2); |
| 36 | }); |
| 37 | |
| 38 | test('scriptingControllerNonLocalContext', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 39 | var selectionController = new FakeSelectionController(); |
| 40 | var controller = new tv.c.ScriptingController(selectionController); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 41 | var x = 1; |
| 42 | controller.executeCommand('x = 2'); |
| 43 | assert.equal(x, 1); |
| 44 | }); |
| 45 | |
| 46 | test('scriptingControllerModifyGlobalContext', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 47 | var selectionController = new FakeSelectionController(); |
| 48 | var controller = new tv.c.ScriptingController(selectionController); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 49 | window._x = 1; |
| 50 | controller.executeCommand('_x = 2'); |
| 51 | assert.equal(window._x, 2); |
| 52 | delete window._x; |
| 53 | }); |
| 54 | |
| 55 | test('scriptingControllerPersistentContext', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 56 | var selectionController = new FakeSelectionController(); |
| 57 | var controller = new tv.c.ScriptingController(selectionController); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 58 | controller.executeCommand('a = 42'); |
| 59 | var result = controller.executeCommand('a'); |
| 60 | assert.equal(result, 42); |
| 61 | }); |
| 62 | |
| 63 | test('scriptingControllerAddScriptObject', function() { |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 64 | var selectionController = new FakeSelectionController(); |
| 65 | var controller = new tv.c.ScriptingController(selectionController); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 66 | controller.addScriptObject('z', 123); |
| 67 | var result = controller.executeCommand('z'); |
| 68 | assert.equal(result, 123); |
| 69 | }); |
Chris Craik | 44c2820 | 2015-05-12 17:25:16 -0700 | [diff] [blame^] | 70 | |
| 71 | test('scriptingControllerObjectRegistry', function() { |
| 72 | var selectionController = new FakeSelectionController(); |
| 73 | |
| 74 | tv.c.ScriptingObjectRegistry.register( |
| 75 | function() { return 123; }, |
| 76 | { |
| 77 | name: 'testFunctionName' |
| 78 | } |
| 79 | ); |
| 80 | var controller = new tv.c.ScriptingController(selectionController); |
| 81 | var result = controller.executeCommand('testFunctionName()'); |
| 82 | assert.equal(result, 123); |
| 83 | }); |
Chris Craik | f516a62 | 2015-04-01 17:52:39 -0700 | [diff] [blame] | 84 | }); |
| 85 | </script> |
| 86 | |