summaryrefslogtreecommitdiffstats
path: root/chrome/test/chromedriver/test_environment.py
blob: 4cbc1ab9369650c7f8e761d7cb98bd0b5c681efc (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright (c) 2012 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.

"""TestEnvironment classes.

These classes abstract away the various setups needed to run the WebDriver java
tests in various environments.
"""

import os
import sys

_THIS_DIR = os.path.abspath(os.path.dirname(__file__))

sys.path.insert(0, os.path.join(_THIS_DIR, os.pardir, 'pylib'))

from common import util

if util.IsLinux():
  sys.path.insert(0, os.path.join(_THIS_DIR, os.pardir, os.pardir, os.pardir,
                                  'build', 'android'))
  from pylib import android_commands
  from pylib import forwarder
  from pylib import valgrind_tools

ANDROID_TEST_HTTP_PORT = 2311
ANDROID_TEST_HTTPS_PORT = 2411

_EXPECTATIONS = {}
execfile(os.path.join(_THIS_DIR, 'test_expectations'), _EXPECTATIONS)


class BaseTestEnvironment(object):
  """Manages the environment java tests require to run."""

  def __init__(self, chrome_version='HEAD'):
    """Initializes a desktop test environment.

    Args:
      chrome_version: Optionally a chrome version to run the tests against.
    """
    self._chrome_version = chrome_version

  def GetOS(self):
    """Name of the OS."""
    raise NotImplementedError

  def GlobalSetUp(self):
    """Sets up the global test environment state."""
    pass

  def GlobalTearDown(self):
    """Tears down the global test environment state."""
    pass

  def GetPassedJavaTestFilter(self):
    """Get the test filter for running all passing tests.

    Returns:
      Filter string, in Google Test (C++) format.
    """
    return _EXPECTATIONS['GetPassedJavaTestFilter'](
        'android', self._chrome_version)

  def GetPassedJavaTests(self):
    """Get the list of passed java tests.

    Returns:
      List of passed test names.
    """
    with open(os.path.join(_THIS_DIR, 'java_tests.txt'), 'r') as f:
      return _EXPECTATIONS['ApplyJavaTestFilter'](
          'android', self._chrome_version,
          [t.strip('\n') for t in f.readlines()])


class DesktopTestEnvironment(BaseTestEnvironment):
  """Manages the environment java tests require to run on Desktop."""

  #override
  def GetOS(self):
    return util.GetPlatformName()


class AndroidTestEnvironment(DesktopTestEnvironment):
  """Manages the environment java tests require to run on Android."""

  def __init__(self, chrome_version='HEAD'):
    super(AndroidTestEnvironment, self).__init__(chrome_version)
    self._adb = None
    self._forwarder = None

  #override
  def GlobalSetUp(self):
    os.putenv('TEST_HTTP_PORT', str(ANDROID_TEST_HTTP_PORT))
    os.putenv('TEST_HTTPS_PORT', str(ANDROID_TEST_HTTPS_PORT))
    self._adb = android_commands.AndroidCommands()
    self._forwarder = forwarder.Forwarder(self._adb, 'Debug')
    self._forwarder.Run(
        [(ANDROID_TEST_HTTP_PORT, ANDROID_TEST_HTTP_PORT),
         (ANDROID_TEST_HTTPS_PORT, ANDROID_TEST_HTTPS_PORT)],
        valgrind_tools.BaseTool(), '127.0.0.1')

  #override
  def GlobalTearDown(self):
    if self._adb is not None:
      forwarder.Forwarder.KillDevice(self._adb, valgrind_tools.BaseTool())
    if self._forwarder is not None:
      self._forwarder.Close()

  #override
  def GetOS(self):
    return 'android'