| <!-- |
| Copyright 2013 The Chromium Authors. All rights reserved. |
| Use of this source code is governed by a BSD-style license that can be |
| found in the LICENSE file. |
| --> |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="webui_resource_test.js"></script> |
| <script src="player_manager.js"></script> |
| <script src="player_info.js"></script> |
| </head> |
| <body> |
| <script> |
| window.setUp = function() { |
| window.pi = new PlayerInfo('example_id'); |
| }; |
| |
| window.tearDown = function() { |
| window.pi = null; |
| }; |
| |
| // Test that an ID is set correctly. |
| window.testConstructorStringID = function() { |
| assertEquals('example_id', window.pi.id); |
| }; |
| |
| // Test that numerical IDs are valid. |
| window.testConstructorNumberId = function() { |
| var pi = new PlayerInfo(5); |
| assertEquals(5, pi.id); |
| }; |
| |
| // Make sure that a new PlayerInfo has no events. |
| window.testEmptyEvents = function() { |
| assertEquals(0, window.pi.allEvents.length); |
| }; |
| |
| // Check that the most recent property gets updated. |
| window.testAddProperty = function() { |
| var key = 'key', |
| value = 'value', |
| value2 = 'value2'; |
| |
| window.pi.addProperty(0, key, value); |
| assertEquals(value, window.pi.properties[key]); |
| |
| window.pi.addProperty(0, key, value2); |
| assertEquals(value2, window.pi.properties[key]); |
| |
| }; |
| |
| // Make sure that the first timestamp that gets sent |
| // is recorded as the base timestamp. |
| window.testFirstTimestamp = function() { |
| var pi = new PlayerInfo('example_ID'); |
| var timestamp = 5000; |
| pi.addProperty(timestamp, 'key', 'value'); |
| |
| assertEquals(timestamp, pi.firstTimestamp_); |
| }; |
| |
| // Adding a property with a non-string key should |
| // throw an exception. |
| window.testWrongKeyType = function() { |
| var pi = new PlayerInfo('example_ID'); |
| assertThrows(function() { |
| pi.addProperty(0, 5, 'some value'); |
| }); |
| }; |
| |
| // Subsequent events should have their log offset based |
| // on the first timestamp added. |
| window.testAddPropertyTimestampOffset = function() { |
| var firstTimestamp = 500, |
| secondTimestamp = 550, |
| deltaT = secondTimestamp - firstTimestamp, |
| key = 'key', |
| value = 'value'; |
| |
| var pi = new PlayerInfo('example_ID'); |
| pi.addProperty(firstTimestamp, key, value); |
| pi.addProperty(secondTimestamp, key, value); |
| |
| assertEquals(firstTimestamp, pi.firstTimestamp_); |
| assertEquals(0, pi.allEvents[0].time); |
| assertEquals(deltaT, pi.allEvents[1].time); |
| |
| assertTrue(undefined !== pi.pastValues[key]); |
| |
| console.log(pi.pastValues); |
| |
| assertEquals(0, pi.pastValues[key][0].time); |
| assertEquals(deltaT, pi.pastValues[key][1].time); |
| }; |
| |
| // Check to make sure that properties are correctly |
| // added to the relevant pastValues array. |
| window.testAddPropertyPastValues = function() { |
| var pi = new PlayerInfo('example_ID'), |
| timestamp = 50, |
| key = 'key', |
| value = 'value'; |
| |
| pi.addProperty(timestamp, key, value); |
| |
| assertEquals(value, pi.pastValues[key][0].value); |
| assertEquals(key, pi.pastValues[key][0].key); |
| assertEquals(0, pi.pastValues[key][0].time); |
| }; |
| |
| // The list of all events should be recorded in correctly. |
| window.testAllEvents = function() { |
| var pi = new PlayerInfo('example_ID'), |
| timestamp = 50, |
| key = 'key', |
| value = 'value', |
| key2 = 'key2', |
| value2 = 'value2'; |
| |
| pi.addProperty(timestamp, key, value); |
| assertEquals(value, pi.allEvents[0].value); |
| assertEquals(key, pi.allEvents[0].key); |
| |
| pi.addProperty(timestamp, key2, value2); |
| assertEquals(value2, pi.allEvents[1].value); |
| assertEquals(key2, pi.allEvents[1].key); |
| }; |
| |
| // Using noRecord should make it not show up in allEvents, |
| // but it should still show up in pastValues[key]. |
| window.testNoRecord = function() { |
| var pi = new PlayerInfo('example_ID'), |
| timestamp = 50, |
| key = 'key', |
| value = 'value'; |
| pi.addPropertyNoRecord(timestamp, key, value); |
| |
| assertEquals(value, pi.properties[key]); |
| assertEquals(0, pi.allEvents.length); |
| assertEquals(1, pi.pastValues[key].length); |
| }; |
| runTests(); |
| </script> |
| </body> |
| </html> |