• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2012 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 telemetry.core import exceptions
8from telemetry.core import util
9from telemetry.page import page_runner
10from telemetry.page import page as page_module
11from telemetry.page import page_set as page_set_module
12from telemetry.page import page_test
13from telemetry.page import test_expectations
14# pylint: disable=W0401,W0614
15from telemetry.page.actions.all_page_actions import *
16from telemetry.unittest import options_for_unittests
17
18
19class BasicTestPage(page_module.Page):
20  def __init__(self, url, page_set, base_dir):
21    super(BasicTestPage, self).__init__(url, page_set, base_dir)
22
23  def RunSmoothness(self, action_runner):
24    action_runner.RunAction(ScrollAction())
25
26
27class PageMeasurementUnitTestBase(unittest.TestCase):
28  """unittest.TestCase-derived class to help in the construction of unit tests
29  for a measurement."""
30
31  def CreatePageSetFromFileInUnittestDataDir(self, test_filename):
32    ps = self.CreateEmptyPageSet()
33    page = BasicTestPage('file://' + test_filename, ps, base_dir=ps.base_dir)
34    ps.AddPage(page)
35    return ps
36
37  def CreateEmptyPageSet(self):
38    base_dir = util.GetUnittestDataDir()
39    ps = page_set_module.PageSet(file_path=base_dir)
40    return ps
41
42  def RunMeasurement(self, measurement, ps,
43      expectations=test_expectations.TestExpectations(),
44      options=None):
45    """Runs a measurement against a pageset, returning the rows its outputs."""
46    if options is None:
47      options = options_for_unittests.GetCopy()
48    assert options
49    temp_parser = options.CreateParser()
50    page_runner.AddCommandLineArgs(temp_parser)
51    measurement.AddCommandLineArgs(temp_parser)
52    measurement.SetArgumentDefaults(temp_parser)
53    defaults = temp_parser.get_default_values()
54    for k, v in defaults.__dict__.items():
55      if hasattr(options, k):
56        continue
57      setattr(options, k, v)
58
59    measurement.CustomizeBrowserOptions(options.browser_options)
60    options.output_file = None
61    options.output_format = 'none'
62    options.output_trace_tag = None
63    page_runner.ProcessCommandLineArgs(temp_parser, options)
64    measurement.ProcessCommandLineArgs(temp_parser, options)
65    return page_runner.Run(measurement, ps, expectations, options)
66
67  def TestTracingCleanedUp(self, measurement_class, options=None):
68    ps = self.CreatePageSetFromFileInUnittestDataDir('blank.html')
69    start_tracing_called = [False]
70    stop_tracing_called = [False]
71
72    class BuggyMeasurement(measurement_class):
73      def __init__(self, *args, **kwargs):
74        measurement_class.__init__(self, *args, **kwargs)
75
76      # Inject fake tracing methods to browser
77      def TabForPage(self, page, browser):
78        ActualStartTracing = browser.StartTracing
79        def FakeStartTracing(*args, **kwargs):
80          ActualStartTracing(*args, **kwargs)
81          start_tracing_called[0] = True
82          raise exceptions.IntentionalException
83        browser.StartTracing = FakeStartTracing
84
85        ActualStopTracing = browser.StopTracing
86        def FakeStopTracing(*args, **kwargs):
87          ActualStopTracing(*args, **kwargs)
88          stop_tracing_called[0] = True
89        browser.StopTracing = FakeStopTracing
90
91        return measurement_class.TabForPage(self, page, browser)
92
93    measurement = BuggyMeasurement()
94    try:
95      self.RunMeasurement(measurement, ps, options=options)
96    except page_test.TestNotSupportedOnPlatformFailure:
97      pass
98    if start_tracing_called[0]:
99      self.assertTrue(stop_tracing_called[0])
100