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 types
6import unittest
7
8from telemetry.timeline import counter as counter_module
9
10
11class FakeProcess(object):
12  pass
13
14
15class CounterIterEventsInThisContainerTest(unittest.TestCase):
16
17  def setUp(self):
18    parent = FakeProcess()
19    self.counter = counter_module.Counter(parent, 'cat', 'name')
20
21  def assertIsEmptyIterator(self, itr):
22    self.assertIsInstance(itr, types.GeneratorType)
23    self.assertRaises(StopIteration, itr.next)
24
25  def testEmptyTimestamps(self):
26    self.assertIsEmptyIterator(self.counter.IterEventsInThisContainer(
27        event_type_predicate=lambda x: True,
28        event_predicate=lambda x: True))
29
30  def testEventTypeMismatch(self):
31    self.counter.timestamps = [111, 222]
32    self.assertIsEmptyIterator(self.counter.IterEventsInThisContainer(
33        event_type_predicate=lambda x: False,
34        event_predicate=lambda x: True))
35
36  def testNoEventMatch(self):
37    self.counter.timestamps = [111, 222]
38    self.assertIsEmptyIterator(self.counter.IterEventsInThisContainer(
39        event_type_predicate=lambda x: True,
40        event_predicate=lambda x: False))
41
42  def testAllMatch(self):
43    self.counter.timestamps = [111, 222]
44    self.counter.samples = [100, 200]
45    events = self.counter.IterEventsInThisContainer(
46        event_type_predicate=lambda x: True,
47        event_predicate=lambda x: True)
48    self.assertIsInstance(events, types.GeneratorType)
49    eventlist = list(events)
50    self.assertEqual([111, 222], [s.start for s in eventlist])
51    self.assertEqual(['cat.name', 'cat.name'], [s.name for s in eventlist])
52    self.assertEqual([100, 200], [s.value for s in eventlist])
53
54  def testPartialMatch(self):
55    self.counter.timestamps = [111, 222]
56    self.counter.samples = [100, 200]
57    events = self.counter.IterEventsInThisContainer(
58        event_type_predicate=lambda x: True,
59        event_predicate=lambda x: x.start > 200)
60    self.assertIsInstance(events, types.GeneratorType)
61    eventlist = list(events)
62    self.assertEqual([222], [s.start for s in eventlist])
63    self.assertEqual(['cat.name'], [s.name for s in eventlist])
64    self.assertEqual([200], [s.value for s in eventlist])
65