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