1# Copyright 2014 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.
4from telemetry.core import android_platform
5from telemetry.core import platform
6from telemetry.internal.platform import android_device
7from telemetry import story as story_module
8from telemetry.web_perf import timeline_based_measurement
9
10
11class SharedAndroidState(story_module.SharedState):
12  """Manage test state/transitions across multiple android.AndroidStory's.
13
14  WARNING: the class is not ready for public consumption.
15  Email telemetry@chromium.org if you feel like you must use it.
16  """
17
18  def __init__(self, test, finder_options, story_set):
19    """This method is styled on unittest.TestCase.setUpClass.
20
21    Args:
22      test: a web_perf.TimelineBasedMeasurement instance.
23      options: a BrowserFinderOptions instance with command line options.
24      story_set: a story.StorySet instance.
25    """
26    super(SharedAndroidState, self).__init__(test, finder_options, story_set)
27    if not isinstance(
28        test, timeline_based_measurement.TimelineBasedMeasurement):
29      raise ValueError(
30          'SharedAndroidState only accepts TimelineBasedMeasurement tests'
31          ' (not %s).' % test.__class__)
32    self._test = test
33    self._finder_options = finder_options
34    self._android_app = None
35    self._current_story = None
36    device = android_device.GetDevice(finder_options)
37    assert device, 'Android device required.'
38    self._android_platform = platform.GetPlatformForDevice(
39        device, finder_options)
40    assert self._android_platform, 'Unable to create android platform.'
41    assert isinstance(
42        self._android_platform, android_platform.AndroidPlatform)
43
44  @property
45  def app(self):
46    return self._android_app
47
48  @property
49  def platform(self):
50    return self._android_platform
51
52  def WillRunStory(self, story):
53    assert not self._android_app
54    self._current_story = story
55    self._android_app = self._android_platform.LaunchAndroidApplication(
56        story.start_intent, story.is_app_ready_predicate)
57    self._test.WillRunStory(self._android_platform.tracing_controller)
58
59  def CanRunStory(self, story):
60    """This does not apply to android app stories."""
61    return True
62
63  def RunStory(self, results):
64    self._current_story.Run(self)
65    self._test.Measure(self._android_platform.tracing_controller, results)
66
67  def DidRunStory(self, results):
68    self._test.DidRunStory(self._android_platform.tracing_controller)
69    if self._android_app:
70      self._android_app.Close()
71      self._android_app = None
72
73  def TearDownState(self):
74    """Tear down anything created in the __init__ method that is not needed.
75
76    Currently, there is no clean-up needed from SharedAndroidState.__init__.
77    """
78    pass
79