1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2019 The ChromiumOS Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Tests for time_line.py.""" 8 9 10__author__ = "yunlian@google.com (Yunlian Jiang)" 11 12import time 13import unittest 14 15from cros_utils import timeline 16 17 18class TimeLineTest(unittest.TestCase): 19 """Tests for the Timeline class.""" 20 21 def testRecord(self): 22 tl = timeline.Timeline() 23 tl.Record("A") 24 t = time.time() 25 t1 = tl.events[0].timestamp 26 self.assertEqual(int(t1 - t), 0) 27 self.assertRaises(AssertionError, tl.Record, "A") 28 29 def testGetEvents(self): 30 tl = timeline.Timeline() 31 tl.Record("A") 32 e = tl.GetEvents() 33 self.assertEqual(e, ["A"]) 34 tl.Record("B") 35 e = tl.GetEvents() 36 self.assertEqual(e, ["A", "B"]) 37 38 def testGetEventTime(self): 39 tl = timeline.Timeline() 40 tl.Record("A") 41 t = time.time() 42 t1 = tl.GetEventTime("A") 43 self.assertEqual(int(t1 - t), 0) 44 self.assertRaises(IndexError, tl.GetEventTime, "B") 45 46 def testGetLastEventTime(self): 47 tl = timeline.Timeline() 48 self.assertRaises(IndexError, tl.GetLastEventTime) 49 tl.Record("A") 50 t = time.time() 51 t1 = tl.GetLastEventTime() 52 self.assertEqual(int(t1 - t), 0) 53 time.sleep(2) 54 tl.Record("B") 55 t = time.time() 56 t1 = tl.GetLastEventTime() 57 self.assertEqual(int(t1 - t), 0) 58 59 60if __name__ == "__main__": 61 unittest.main() 62