aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/telemetry/android/shared_android_state.py
blob: d5a4e6903723856e8aefb134450a79ddbb6e50fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from telemetry.core import android_platform
from telemetry.core import platform
from telemetry.internal.platform import android_device
from telemetry import story as story_module
from telemetry.web_perf import timeline_based_measurement


class SharedAndroidState(story_module.SharedState):
  """Manage test state/transitions across multiple android.AndroidStory's.

  WARNING: the class is not ready for public consumption.
  Email telemetry@chromium.org if you feel like you must use it.
  """

  def __init__(self, test, finder_options, story_set):
    """This method is styled on unittest.TestCase.setUpClass.

    Args:
      test: a web_perf.TimelineBasedMeasurement instance.
      options: a BrowserFinderOptions instance with command line options.
      story_set: a story.StorySet instance.
    """
    super(SharedAndroidState, self).__init__(test, finder_options, story_set)
    if not isinstance(
        test, timeline_based_measurement.TimelineBasedMeasurement):
      raise ValueError(
          'SharedAndroidState only accepts TimelineBasedMeasurement tests'
          ' (not %s).' % test.__class__)
    self._test = test
    self._finder_options = finder_options
    self._android_app = None
    self._current_story = None
    device = android_device.GetDevice(finder_options)
    assert device, 'Android device required.'
    self._android_platform = platform.GetPlatformForDevice(
        device, finder_options)
    assert self._android_platform, 'Unable to create android platform.'
    assert isinstance(
        self._android_platform, android_platform.AndroidPlatform)

  @property
  def app(self):
    return self._android_app

  @property
  def platform(self):
    return self._android_platform

  def WillRunStory(self, story):
    assert not self._android_app
    self._current_story = story
    self._android_app = self._android_platform.LaunchAndroidApplication(
        story.start_intent, story.is_app_ready_predicate)
    self._test.WillRunStory(self._android_platform.tracing_controller)

  def CanRunStory(self, story):
    """This does not apply to android app stories."""
    return True

  def RunStory(self, results):
    self._current_story.Run(self)
    self._test.Measure(self._android_platform.tracing_controller, results)

  def DidRunStory(self, results):
    self._test.DidRunStory(self._android_platform.tracing_controller)
    if self._android_app:
      self._android_app.Close()
      self._android_app = None

  def TearDownState(self):
    """Tear down anything created in the __init__ method that is not needed.

    Currently, there is no clean-up needed from SharedAndroidState.__init__.
    """
    pass

  def DumpStateUponFailure(self, story, results):
    # TODO: Dump the state of the Android app.
    pass