def testFindTimelineInteractionRecords(self):
    metric = tbm_module._TimelineBasedMetrics( # pylint: disable=W0212
      self.model, self.renderer_thread, lambda _: [] )
    interactions = metric.FindTimelineInteractionRecords()
    self.assertEquals(2, len(interactions))
    self.assertTrue(interactions[0].is_smooth)
    self.assertEquals(0, interactions[0].start)
    self.assertEquals(20, interactions[0].end)

    self.assertTrue(interactions[1].is_loading_resources)
    self.assertEquals(25, interactions[1].start)
    self.assertEquals(30, interactions[1].end)
    def testAddResults(self):
        results = page_measurement_results.PageMeasurementResults()

        class FakeSmoothMetric(timeline_based_metric.TimelineBasedMetric):
            def AddResults(self, model, renderer_thread, interaction_record,
                           results):
                results.Add('FakeSmoothMetric', 'ms', 1)

        class FakeLoadingMetric(timeline_based_metric.TimelineBasedMetric):
            def AddResults(self, model, renderer_thread, interaction_record,
                           results):
                assert interaction_record.logical_name == 'LogicalName2'
                results.Add('FakeLoadingMetric', 'ms', 2)

        def CreateMetricsForTimelineInteractionRecord(interaction):
            res = []
            if interaction.is_smooth:
                res.append(FakeSmoothMetric())
            if interaction.is_loading_resources:
                res.append(FakeLoadingMetric())
            return res

        metric = tbm_module._TimelineBasedMetrics(  # pylint: disable=W0212
            self.model, self.renderer_thread,
            CreateMetricsForTimelineInteractionRecord)
        ps = page_set.PageSet.FromDict(
            {
                "description": "hello",
                "archive_path": "foo.wpr",
                "pages": [{
                    "url": "http://www.bar.com/"
                }]
            }, os.path.dirname(__file__))
        results.WillMeasurePage(ps.pages[0])
        metric.AddResults(results)
        results.DidMeasurePage()

        v = results.FindAllPageSpecificValuesNamed(
            'LogicalName1-FakeSmoothMetric')
        self.assertEquals(len(v), 1)
        v = results.FindAllPageSpecificValuesNamed(
            'LogicalName2-FakeLoadingMetric')
        self.assertEquals(len(v), 1)
  def testAddResults(self):
    results = page_measurement_results.PageMeasurementResults()
    class FakeSmoothMetric(timeline_based_metric.TimelineBasedMetric):
      def AddResults(self, model, renderer_thread,
                     interaction_record, results):
        results.Add('FakeSmoothMetric', 'ms', 1)

    class FakeLoadingMetric(timeline_based_metric.TimelineBasedMetric):
      def AddResults(self, model, renderer_thread,
                     interaction_record, results):
        assert interaction_record.logical_name == 'LogicalName2'
        results.Add('FakeLoadingMetric', 'ms', 2)

    def CreateMetricsForTimelineInteractionRecord(interaction):
      res = []
      if interaction.is_smooth:
        res.append(FakeSmoothMetric())
      if interaction.is_loading_resources:
        res.append(FakeLoadingMetric())
      return res

    metric = tbm_module._TimelineBasedMetrics( # pylint: disable=W0212
        self.model, self.renderer_thread,
        CreateMetricsForTimelineInteractionRecord)
    ps = page_set.PageSet.FromDict({
      "description": "hello",
      "archive_path": "foo.wpr",
      "pages": [
        {"url": "http://www.bar.com/"}
      ]
    }, os.path.dirname(__file__))
    results.WillMeasurePage(ps.pages[0])
    metric.AddResults(results)
    results.DidMeasurePage()

    v = results.FindAllPageSpecificValuesNamed('LogicalName1-FakeSmoothMetric')
    self.assertEquals(len(v), 1)
    v = results.FindAllPageSpecificValuesNamed('LogicalName2-FakeLoadingMetric')
    self.assertEquals(len(v), 1)