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    events = self.counter.IterEventsInThisContainer(
45        event_type_predicate=lambda x: True,
46        event_predicate=lambda x: True)
47    self.assertIsInstance(events, types.GeneratorType)
48    self.assertEqual([111, 222], [s.start for s in events])
49
50  def testPartialMatch(self):
51    self.counter.timestamps = [111, 222]
52    events = self.counter.IterEventsInThisContainer(
53        event_type_predicate=lambda x: True,
54        event_predicate=lambda x: x.start > 200)
55    self.assertIsInstance(events, types.GeneratorType)
56    self.assertEqual([222], [s.start for s in events])
57