Home | History | Annotate | Download | only in new
      1 <!--
      2 Copyright 2013 The Chromium Authors. All rights reserved.
      3 Use of this source code is governed by a BSD-style license that can be
      4 found in the LICENSE file.
      5 -->
      6 <!DOCTYPE html>
      7 <html>
      8   <head>
      9     <script src="webui_resource_test.js"></script>
     10     <script src="player_manager.js"></script>
     11     <script src="player_info.js"></script>
     12   </head>
     13   <body>
     14     <script>
     15       window.setUp = function() {
     16         window.pi = new PlayerInfo('example_id');
     17       };
     18 
     19       window.tearDown = function() {
     20         window.pi = null;
     21       };
     22 
     23       // Test that an ID is set correctly.
     24       window.testConstructorStringID = function() {
     25         assertEquals('example_id', window.pi.id);
     26       };
     27 
     28       // Test that numerical IDs are valid.
     29       window.testConstructorNumberId = function() {
     30         var pi = new PlayerInfo(5);
     31         assertEquals(5, pi.id);
     32       };
     33 
     34       // Make sure that a new PlayerInfo has no events.
     35       window.testEmptyEvents = function() {
     36         assertEquals(0, window.pi.allEvents.length);
     37       };
     38 
     39       // Check that the most recent property gets updated.
     40       window.testAddProperty = function() {
     41         var key = 'key',
     42           value = 'value',
     43           value2 = 'value2';
     44 
     45         window.pi.addProperty(0, key, value);
     46         assertEquals(value, window.pi.properties[key]);
     47 
     48         window.pi.addProperty(0, key, value2);
     49         assertEquals(value2, window.pi.properties[key]);
     50 
     51       };
     52 
     53       // Make sure that the first timestamp that gets sent
     54       // is recorded as the base timestamp.
     55       window.testFirstTimestamp = function() {
     56         var pi = new PlayerInfo('example_ID');
     57         var timestamp = 5000;
     58         pi.addProperty(timestamp, 'key', 'value');
     59 
     60         assertEquals(timestamp, pi.firstTimestamp_);
     61       };
     62 
     63       // Adding a property with a non-string key should
     64       // throw an exception.
     65       window.testWrongKeyType = function() {
     66         var pi = new PlayerInfo('example_ID');
     67         assertThrows(function() {
     68           pi.addProperty(0, 5, 'some value');
     69         });
     70       };
     71 
     72       // Subsequent events should have their log offset based
     73       // on the first timestamp added.
     74       window.testAddPropertyTimestampOffset = function() {
     75         var firstTimestamp = 500,
     76           secondTimestamp = 550,
     77           deltaT = secondTimestamp - firstTimestamp,
     78           key = 'key',
     79           value = 'value';
     80 
     81         var pi = new PlayerInfo('example_ID');
     82         pi.addProperty(firstTimestamp, key, value);
     83         pi.addProperty(secondTimestamp, key, value);
     84 
     85         assertEquals(firstTimestamp, pi.firstTimestamp_);
     86         assertEquals(0, pi.allEvents[0].time);
     87         assertEquals(deltaT, pi.allEvents[1].time);
     88 
     89         assertTrue(undefined !== pi.pastValues[key]);
     90 
     91         console.log(pi.pastValues);
     92 
     93         assertEquals(0, pi.pastValues[key][0].time);
     94         assertEquals(deltaT, pi.pastValues[key][1].time);
     95       };
     96 
     97       // Check to make sure that properties are correctly
     98       // added to the relevant pastValues array.
     99       window.testAddPropertyPastValues = function() {
    100         var pi = new PlayerInfo('example_ID'),
    101           timestamp = 50,
    102           key = 'key',
    103           value = 'value';
    104 
    105         pi.addProperty(timestamp, key, value);
    106 
    107         assertEquals(value, pi.pastValues[key][0].value);
    108         assertEquals(key, pi.pastValues[key][0].key);
    109         assertEquals(0, pi.pastValues[key][0].time);
    110       };
    111 
    112       // The list of all events should be recorded in correctly.
    113       window.testAllEvents = function() {
    114         var pi = new PlayerInfo('example_ID'),
    115           timestamp = 50,
    116           key = 'key',
    117           value = 'value',
    118           key2 = 'key2',
    119           value2 = 'value2';
    120 
    121         pi.addProperty(timestamp, key, value);
    122         assertEquals(value, pi.allEvents[0].value);
    123         assertEquals(key, pi.allEvents[0].key);
    124 
    125         pi.addProperty(timestamp, key2, value2);
    126         assertEquals(value2, pi.allEvents[1].value);
    127         assertEquals(key2, pi.allEvents[1].key);
    128       };
    129 
    130       // Using noRecord should make it not show up in allEvents,
    131       // but it should still show up in pastValues[key].
    132       window.testNoRecord = function() {
    133         var pi = new PlayerInfo('example_ID'),
    134           timestamp = 50,
    135           key = 'key',
    136           value = 'value';
    137         pi.addPropertyNoRecord(timestamp, key, value);
    138 
    139         assertEquals(value, pi.properties[key]);
    140         assertEquals(0, pi.allEvents.length);
    141         assertEquals(1, pi.pastValues[key].length);
    142       };
    143       runTests();
    144     </script>
    145   </body>
    146 </html>
    147