1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2019 The Chromium OS Authors. All rights reserved. 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 9from __future__ import print_function 10 11__author__ = 'yunlian@google.com (Yunlian Jiang)' 12 13import time 14import unittest 15 16from cros_utils import timeline 17 18 19class TimeLineTest(unittest.TestCase): 20 """Tests for the Timeline class.""" 21 22 def testRecord(self): 23 tl = timeline.Timeline() 24 tl.Record('A') 25 t = time.time() 26 t1 = tl.events[0].timestamp 27 self.assertEqual(int(t1 - t), 0) 28 self.assertRaises(AssertionError, tl.Record, 'A') 29 30 def testGetEvents(self): 31 tl = timeline.Timeline() 32 tl.Record('A') 33 e = tl.GetEvents() 34 self.assertEqual(e, ['A']) 35 tl.Record('B') 36 e = tl.GetEvents() 37 self.assertEqual(e, ['A', 'B']) 38 39 def testGetEventTime(self): 40 tl = timeline.Timeline() 41 tl.Record('A') 42 t = time.time() 43 t1 = tl.GetEventTime('A') 44 self.assertEqual(int(t1 - t), 0) 45 self.assertRaises(IndexError, tl.GetEventTime, 'B') 46 47 def testGetLastEventTime(self): 48 tl = timeline.Timeline() 49 self.assertRaises(IndexError, tl.GetLastEventTime) 50 tl.Record('A') 51 t = time.time() 52 t1 = tl.GetLastEventTime() 53 self.assertEqual(int(t1 - t), 0) 54 time.sleep(2) 55 tl.Record('B') 56 t = time.time() 57 t1 = tl.GetLastEventTime() 58 self.assertEqual(int(t1 - t), 0) 59 60 61if __name__ == '__main__': 62 unittest.main() 63