1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from telemetry.core.platform import process_statistic_timeline_data
8
9
10class ProcessStatisticTimelineDataTest(unittest.TestCase):
11
12  def testProcessStatisticValueMath(self):
13    pid1 = 1
14    pid2 = 2
15
16    a = process_statistic_timeline_data.ProcessStatisticTimelineData(pid1, 5)
17    b = process_statistic_timeline_data.ProcessStatisticTimelineData(pid2, 1)
18    c = process_statistic_timeline_data.ProcessStatisticTimelineData(pid1, 1)
19
20    # Test addition.
21    addition_result = (a + b).value_by_pid
22    self.assertEquals(5, addition_result[pid1])
23    self.assertEquals(1, addition_result[pid2])
24    self.assertEquals(2, len(addition_result.keys()))
25
26    # Test subtraction.
27    subtraction_result = ((a + b) - c).value_by_pid
28    self.assertEquals(4, subtraction_result[pid1])
29    self.assertEquals(1, subtraction_result[pid2])
30    self.assertEquals(2, len(subtraction_result.keys()))
31
32    # Test subtraction with a pid that exists only in rhs.
33    subtraction_results1 = (a - (b + c)).value_by_pid
34    self.assertEquals(4, subtraction_results1[pid1])
35    self.assertEquals(1, len(subtraction_results1.keys()))
36
37    # Test calculation of total sum.
38    self.assertEquals(6, (a + b).total_sum())
39
40  def testProcessStatisticValueSummary(self):
41    pid1 = 1
42    pid2 = 2
43
44    a = process_statistic_timeline_data.ProcessStatisticTimelineData(pid1, 1)
45    b = process_statistic_timeline_data.ProcessStatisticTimelineData(pid2, 99)
46    c = a + b
47    self.assertEquals(100, c.total_sum())
48