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.
4import unittest
5
6from telemetry.core import browser_options
7from telemetry.core.backends.chrome import desktop_browser_finder
8from telemetry.unittest import system_stub
9
10# This file verifies the logic for finding a browser instance on all platforms
11# at once. It does so by providing stubs for the OS/sys/subprocess primitives
12# that the underlying finding logic usually uses to locate a suitable browser.
13# We prefer this approach to having to run the same test on every platform on
14# which we want this code to work.
15
16class FindTestBase(unittest.TestCase):
17  def setUp(self):
18    self._finder_options = browser_options.BrowserFinderOptions()
19    self._finder_options.chrome_root = '../../../'
20    self._stubs = system_stub.Override(desktop_browser_finder,
21                                       ['os', 'subprocess', 'sys'])
22
23  def tearDown(self):
24    self._stubs.Restore()
25
26  @property
27  def _files(self):
28    return self._stubs.os.path.files
29
30  def DoFindAll(self):
31    return desktop_browser_finder.FindAllAvailableBrowsers(self._finder_options)
32
33  def DoFindAllTypes(self):
34    browsers = self.DoFindAll()
35    return [b.browser_type for b in browsers]
36
37def has_type(array, browser_type):
38  return len([x for x in array if x.browser_type == browser_type]) != 0
39
40class FindSystemTest(FindTestBase):
41  def setUp(self):
42    super(FindSystemTest, self).setUp()
43    self._stubs.sys.platform = 'win32'
44
45  def testFindProgramFiles(self):
46    self._files.append(
47        'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe')
48    self._stubs.os.program_files = 'C:\\Program Files'
49    self.assertTrue('system' in self.DoFindAllTypes())
50
51  def testFindProgramFilesX86(self):
52    self._files.append(
53        'C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe')
54    self._stubs.os.program_files_x86 = 'C:\\Program Files(x86)'
55    self.assertTrue('system' in self.DoFindAllTypes())
56
57  def testFindLocalAppData(self):
58    self._files.append(
59        'C:\\Local App Data\\Google\\Chrome\\Application\\chrome.exe')
60    self._stubs.os.local_app_data = 'C:\\Local App Data'
61    self.assertTrue('system' in self.DoFindAllTypes())
62
63class FindLocalBuildsTest(FindTestBase):
64  def setUp(self):
65    super(FindLocalBuildsTest, self).setUp()
66    self._stubs.sys.platform = 'win32'
67
68  def testFindBuild(self):
69    self._files.append('..\\..\\..\\build\\Release\\chrome.exe')
70    self.assertTrue('release' in self.DoFindAllTypes())
71
72  def testFindOut(self):
73    self._files.append('..\\..\\..\\out\\Release\\chrome.exe')
74    self.assertTrue('release' in self.DoFindAllTypes())
75
76  def testFindXcodebuild(self):
77    self._files.append('..\\..\\..\\xcodebuild\\Release\\chrome.exe')
78    self.assertTrue('release' in self.DoFindAllTypes())
79
80class OSXFindTest(FindTestBase):
81  def setUp(self):
82    super(OSXFindTest, self).setUp()
83    self._stubs.sys.platform = 'darwin'
84    self._files.append('/Applications/Google Chrome Canary.app/'
85                       'Contents/MacOS/Google Chrome Canary')
86    self._files.append('/Applications/Google Chrome.app/' +
87                       'Contents/MacOS/Google Chrome')
88    self._files.append(
89      '../../../out/Release/Chromium.app/Contents/MacOS/Chromium')
90    self._files.append(
91      '../../../out/Debug/Chromium.app/Contents/MacOS/Chromium')
92    self._files.append(
93      '../../../out/Release/Content Shell.app/Contents/MacOS/Content Shell')
94    self._files.append(
95      '../../../out/Debug/Content Shell.app/Contents/MacOS/Content Shell')
96
97  def testFindAll(self):
98    types = self.DoFindAllTypes()
99    self.assertEquals(
100      set(types),
101      set(['debug', 'release',
102           'content-shell-debug', 'content-shell-release',
103           'canary', 'system']))
104
105
106class LinuxFindTest(FindTestBase):
107  def setUp(self):
108    super(LinuxFindTest, self).setUp()
109
110    self._stubs.sys.platform = 'linux2'
111    self._files.append('/foo/chrome')
112    self._files.append('../../../out/Release/chrome')
113    self._files.append('../../../out/Debug/chrome')
114    self._files.append('../../../out/Release/content_shell')
115    self._files.append('../../../out/Debug/content_shell')
116
117    self.has_google_chrome_on_path = False
118    this = self
119    def call_hook(*args, **kwargs): # pylint: disable=W0613
120      if this.has_google_chrome_on_path:
121        return 0
122      raise OSError('Not found')
123    self._stubs.subprocess.call = call_hook
124
125  def testFindAllWithExact(self):
126    types = self.DoFindAllTypes()
127    self.assertEquals(
128        set(types),
129        set(['debug', 'release',
130             'content-shell-debug', 'content-shell-release']))
131
132  def testFindWithProvidedExecutable(self):
133    self._finder_options.browser_executable = '/foo/chrome'
134    self.assertTrue('exact' in self.DoFindAllTypes())
135
136  def testFindUsingDefaults(self):
137    self.has_google_chrome_on_path = True
138    self.assertTrue('release' in self.DoFindAllTypes())
139
140    del self._files[1]
141    self.has_google_chrome_on_path = True
142    self.assertTrue('system' in self.DoFindAllTypes())
143
144    self.has_google_chrome_on_path = False
145    del self._files[1]
146    self.assertEquals(['content-shell-debug', 'content-shell-release'],
147                      self.DoFindAllTypes())
148
149  def testFindUsingRelease(self):
150    self.assertTrue('release' in self.DoFindAllTypes())
151
152
153class WinFindTest(FindTestBase):
154  def setUp(self):
155    super(WinFindTest, self).setUp()
156
157    self._stubs.sys.platform = 'win32'
158    self._stubs.os.local_app_data = 'c:\\Users\\Someone\\AppData\\Local'
159    self._files.append('c:\\tmp\\chrome.exe')
160    self._files.append('..\\..\\..\\build\\Release\\chrome.exe')
161    self._files.append('..\\..\\..\\build\\Debug\\chrome.exe')
162    self._files.append('..\\..\\..\\build\\Release\\content_shell.exe')
163    self._files.append('..\\..\\..\\build\\Debug\\content_shell.exe')
164    self._files.append(self._stubs.os.local_app_data + '\\' +
165                       'Google\\Chrome\\Application\\chrome.exe')
166    self._files.append(self._stubs.os.local_app_data + '\\' +
167                       'Google\\Chrome SxS\\Application\\chrome.exe')
168
169  def testFindAllGivenDefaults(self):
170    types = self.DoFindAllTypes()
171    self.assertEquals(set(types),
172                      set(['debug', 'release',
173                           'content-shell-debug', 'content-shell-release',
174                           'system', 'canary']))
175
176  def testFindAllWithExact(self):
177    self._finder_options.browser_executable = 'c:\\tmp\\chrome.exe'
178    types = self.DoFindAllTypes()
179    self.assertEquals(
180        set(types),
181        set(['exact',
182             'debug', 'release',
183             'content-shell-debug', 'content-shell-release',
184             'system', 'canary']))
185