1# Copyright 2015 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 collections import namedtuple 8from telemetry.internal.results import page_test_results 9from telemetry.page import page 10from telemetry.web_perf.metrics import jitter_timeline 11from telemetry.web_perf import timeline_interaction_record 12 13 14FakeEvent = namedtuple('Event', 'name, start, end, thread_duration, args') 15Interaction = timeline_interaction_record.TimelineInteractionRecord 16TEST_INTERACTION_LABEL = 'Action_TestInteraction' 17JITTER_EVENT_NAME = 'jitter' 18 19def GetJitterMetrics(events, interactions): 20 results = page_test_results.PageTestResults() 21 test_page = page.Page('file://blank.html') 22 results.WillRunPage(test_page) 23 jitter_timeline.JitterTimelineMetric()._AddJitterResultsInternal( 24 events, interactions, results) 25 return_dict = dict((value.name, value.values) for value in 26 results.current_page_run.values) 27 results.DidRunPage(test_page) 28 return return_dict 29 30def FakeJitterEvent(start, end, value, thread_duration=None): 31 if not thread_duration: 32 thread_duration = end - start 33 return FakeEvent(jitter_timeline.JITTER_EVENT_NAME, 34 start, end, thread_duration, {'value':value}) 35 36def TestInteraction(start, end): 37 return Interaction(TEST_INTERACTION_LABEL, start, end) 38 39 40class JitterTimelineMetricUnitTest(unittest.TestCase): 41 def testJitterMetric(self): 42 events = [FakeJitterEvent(0, 1, 10), 43 FakeJitterEvent(5, 10, 5), 44 FakeJitterEvent(15, 34, 45)] 45 interactions = [TestInteraction(4, 14)] 46 # The first and the last event do not start during the interaction, so 47 # they are ignored. The second event starts during the interaction, and its 48 # value is 5. 49 self.assertEqual({'jitter-amount': [5]}, 50 GetJitterMetrics(events, interactions)) 51