Home | History | Annotate | Download | only in page
      1 # Copyright (c) 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 import os
      5 import unittest
      6 
      7 from telemetry.page import page_measurement_results
      8 from telemetry.page import page_set
      9 from telemetry.page import perf_tests_helper
     10 
     11 def _MakePageSet():
     12   return page_set.PageSet.FromDict({
     13       "description": "hello",
     14       "archive_path": "foo.wpr",
     15       "pages": [
     16         {"url": "http://www.foo.com/"},
     17         {"url": "http://www.bar.com/"}
     18         ]
     19       }, os.path.dirname(__file__))
     20 
     21 class NonPrintingPageMeasurementResults(
     22     page_measurement_results.PageMeasurementResults):
     23   def __init__(self):
     24     super(NonPrintingPageMeasurementResults, self).__init__()
     25 
     26   def _PrintPerfResult(self, *args):
     27     pass
     28 
     29 class SummarySavingPageMeasurementResults(
     30     page_measurement_results.PageMeasurementResults):
     31   def __init__(self):
     32     super(SummarySavingPageMeasurementResults, self).__init__()
     33     self.results = []
     34 
     35   def _PrintPerfResult(self, *args):
     36     res = perf_tests_helper.PrintPerfResult(*args, print_to_stdout=False)
     37     self.results.append(res)
     38 
     39 class PageMeasurementResultsTest(unittest.TestCase):
     40   def test_basic(self):
     41     test_page_set = _MakePageSet()
     42 
     43     measurement_results = NonPrintingPageMeasurementResults()
     44     measurement_results.WillMeasurePage(test_page_set.pages[0])
     45     measurement_results.Add('a', 'seconds', 3)
     46     measurement_results.DidMeasurePage()
     47 
     48     measurement_results.WillMeasurePage(test_page_set.pages[1])
     49     measurement_results.Add('a', 'seconds', 3)
     50     measurement_results.DidMeasurePage()
     51 
     52     measurement_results.PrintSummary()
     53 
     54   def test_url_is_invalid_value(self):
     55     test_page_set = _MakePageSet()
     56 
     57     measurement_results = NonPrintingPageMeasurementResults()
     58     measurement_results.WillMeasurePage(test_page_set.pages[0])
     59     self.assertRaises(
     60       AssertionError,
     61       lambda: measurement_results.Add('url', 'string', 'foo'))
     62 
     63   def test_unit_change(self):
     64     test_page_set = _MakePageSet()
     65 
     66     measurement_results = NonPrintingPageMeasurementResults()
     67     measurement_results.WillMeasurePage(test_page_set.pages[0])
     68     measurement_results.Add('a', 'seconds', 3)
     69     measurement_results.DidMeasurePage()
     70 
     71     measurement_results.WillMeasurePage(test_page_set.pages[1])
     72     self.assertRaises(
     73       AssertionError,
     74       lambda: measurement_results.Add('a', 'foobgrobbers', 3))
     75 
     76   def test_type_change(self):
     77     test_page_set = _MakePageSet()
     78 
     79     measurement_results = NonPrintingPageMeasurementResults()
     80     measurement_results.WillMeasurePage(test_page_set.pages[0])
     81     measurement_results.Add('a', 'seconds', 3)
     82     measurement_results.DidMeasurePage()
     83 
     84     measurement_results.WillMeasurePage(test_page_set.pages[1])
     85     self.assertRaises(
     86       AssertionError,
     87       lambda: measurement_results.Add('a', 'seconds', 3, data_type='histogram'))
     88 
     89   def test_basic_summary_all_pages_fail(self):
     90     """If all pages fail, no summary is printed."""
     91     test_page_set = _MakePageSet()
     92 
     93     measurement_results = SummarySavingPageMeasurementResults()
     94     measurement_results.WillMeasurePage(test_page_set.pages[0])
     95     measurement_results.Add('a', 'seconds', 3)
     96     measurement_results.DidMeasurePage()
     97     measurement_results.AddFailureMessage(test_page_set.pages[0], 'message')
     98 
     99     measurement_results.WillMeasurePage(test_page_set.pages[1])
    100     measurement_results.Add('a', 'seconds', 7)
    101     measurement_results.DidMeasurePage()
    102     measurement_results.AddFailureMessage(test_page_set.pages[1], 'message')
    103 
    104     measurement_results.PrintSummary()
    105     self.assertEquals(measurement_results.results, [])
    106