1# Copyright 2013 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
5"""TestEnvironment classes.
6
7These classes abstract away the various setups needed to run the WebDriver java
8tests in various environments.
9"""
10
11import os
12import sys
13
14import chrome_paths
15import util
16
17_THIS_DIR = os.path.abspath(os.path.dirname(__file__))
18
19if util.IsLinux():
20  sys.path.insert(0, os.path.join(chrome_paths.GetSrc(), 'build', 'android'))
21  from pylib import android_commands
22  from pylib import forwarder
23  from pylib import valgrind_tools
24
25ANDROID_TEST_HTTP_PORT = 2311
26ANDROID_TEST_HTTPS_PORT = 2411
27
28_EXPECTATIONS = {}
29execfile(os.path.join(_THIS_DIR, 'test_expectations'), _EXPECTATIONS)
30
31
32class BaseTestEnvironment(object):
33  """Manages the environment java tests require to run."""
34
35  def __init__(self, chrome_version='HEAD'):
36    """Initializes a desktop test environment.
37
38    Args:
39      chrome_version: Optionally a chrome version to run the tests against.
40    """
41    self._chrome_version = chrome_version
42
43  def GetOS(self):
44    """Name of the OS."""
45    raise NotImplementedError
46
47  def GlobalSetUp(self):
48    """Sets up the global test environment state."""
49    pass
50
51  def GlobalTearDown(self):
52    """Tears down the global test environment state."""
53    pass
54
55  def GetDisabledJavaTestMatchers(self):
56    """Get the list of disabled java test matchers.
57
58    Returns:
59      List of disabled test matchers, which may contain '*' wildcards.
60    """
61    return _EXPECTATIONS['GetDisabledTestMatchers'](
62        self.GetOS(), self._chrome_version)
63
64  def GetPassedJavaTests(self):
65    """Get the list of passed java tests.
66
67    Returns:
68      List of passed test names.
69    """
70    with open(os.path.join(_THIS_DIR, 'java_tests.txt'), 'r') as f:
71      return _EXPECTATIONS['ApplyJavaTestFilter'](
72          self.GetOS(), self._chrome_version,
73          [t.strip('\n') for t in f.readlines()])
74
75
76class DesktopTestEnvironment(BaseTestEnvironment):
77  """Manages the environment java tests require to run on Desktop."""
78
79  # override
80  def GetOS(self):
81    return util.GetPlatformName()
82
83
84class AndroidTestEnvironment(DesktopTestEnvironment):
85  """Manages the environment java tests require to run on Android."""
86
87  def __init__(self, package, chrome_version='HEAD'):
88    super(AndroidTestEnvironment, self).__init__(chrome_version)
89    self._package = package
90    self._adb = None
91    self._forwarder = None
92
93  # override
94  def GlobalSetUp(self):
95    os.putenv('TEST_HTTP_PORT', str(ANDROID_TEST_HTTP_PORT))
96    os.putenv('TEST_HTTPS_PORT', str(ANDROID_TEST_HTTPS_PORT))
97    self._adb = android_commands.AndroidCommands()
98    forwarder.Forwarder.Map(
99        [(ANDROID_TEST_HTTP_PORT, ANDROID_TEST_HTTP_PORT),
100         (ANDROID_TEST_HTTPS_PORT, ANDROID_TEST_HTTPS_PORT)],
101        self._adb)
102
103  # override
104  def GlobalTearDown(self):
105    forwarder.Forwarder.UnmapAllDevicePorts(self._adb)
106
107  # override
108  def GetOS(self):
109    return 'android:%s' % self._package
110