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.
6 from telemetry.core import browser_options
7 from telemetry.core.backends.chrome import desktop_browser_finder
8 from telemetry.unittest import system_stub
11 # This file verifies the logic for finding a browser instance on all platforms
12 # at once. It does so by providing stubs for the OS/sys/subprocess primitives
13 # that the underlying finding logic usually uses to locate a suitable browser.
14 # We prefer this approach to having to run the same test on every platform on
15 # which we want this code to work.
17 class FindTestBase(unittest.TestCase):
19 self._finder_options = browser_options.BrowserFinderOptions()
20 self._finder_options.chrome_root = '../../../'
21 self._finder_stubs = system_stub.Override(desktop_browser_finder,
22 ['os', 'subprocess', 'sys'])
23 self._path_stubs = system_stub.Override(desktop_browser_finder.path,
27 self._finder_stubs.Restore()
28 self._path_stubs.Restore()
32 return self._path_stubs.os.path.files
35 return desktop_browser_finder.FindAllAvailableBrowsers(self._finder_options)
37 def DoFindAllTypes(self):
38 browsers = self.DoFindAll()
39 return [b.browser_type for b in browsers]
41 def CanFindAvailableBrowsers(self):
42 return desktop_browser_finder.CanFindAvailableBrowsers()
45 def has_type(array, browser_type):
46 return len([x for x in array if x.browser_type == browser_type]) != 0
49 class FindSystemTest(FindTestBase):
51 super(FindSystemTest, self).setUp()
52 self._finder_stubs.sys.platform = 'win32'
53 self._path_stubs.sys.platform = 'win32'
55 def testFindProgramFiles(self):
56 if not self.CanFindAvailableBrowsers():
60 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe')
61 self._path_stubs.os.program_files = 'C:\\Program Files'
62 self.assertIn('system', self.DoFindAllTypes())
64 def testFindProgramFilesX86(self):
65 if not self.CanFindAvailableBrowsers():
69 'C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe')
70 self._path_stubs.os.program_files_x86 = 'C:\\Program Files(x86)'
71 self.assertIn('system', self.DoFindAllTypes())
73 def testFindLocalAppData(self):
74 if not self.CanFindAvailableBrowsers():
78 'C:\\Local App Data\\Google\\Chrome\\Application\\chrome.exe')
79 self._path_stubs.os.local_app_data = 'C:\\Local App Data'
80 self.assertIn('system', self.DoFindAllTypes())
83 class FindLocalBuildsTest(FindTestBase):
85 super(FindLocalBuildsTest, self).setUp()
86 self._finder_stubs.sys.platform = 'win32'
87 self._path_stubs.sys.platform = 'win32'
89 def testFindBuild(self):
90 if not self.CanFindAvailableBrowsers():
93 self._files.append('..\\..\\..\\build\\Release\\chrome.exe')
94 self.assertIn('release', self.DoFindAllTypes())
96 def testFindOut(self):
97 if not self.CanFindAvailableBrowsers():
100 self._files.append('..\\..\\..\\out\\Release\\chrome.exe')
101 self.assertIn('release', self.DoFindAllTypes())
103 def testFindXcodebuild(self):
104 if not self.CanFindAvailableBrowsers():
107 self._files.append('..\\..\\..\\xcodebuild\\Release\\chrome.exe')
108 self.assertIn('release', self.DoFindAllTypes())
111 class OSXFindTest(FindTestBase):
113 super(OSXFindTest, self).setUp()
114 self._finder_stubs.sys.platform = 'darwin'
115 self._path_stubs.sys.platform = 'darwin'
116 self._files.append('/Applications/Google Chrome Canary.app/'
117 'Contents/MacOS/Google Chrome Canary')
118 self._files.append('/Applications/Google Chrome.app/' +
119 'Contents/MacOS/Google Chrome')
121 '../../../out/Release/Chromium.app/Contents/MacOS/Chromium')
123 '../../../out/Debug/Chromium.app/Contents/MacOS/Chromium')
125 '../../../out/Release/Content Shell.app/Contents/MacOS/Content Shell')
127 '../../../out/Debug/Content Shell.app/Contents/MacOS/Content Shell')
129 def testFindAll(self):
130 if not self.CanFindAvailableBrowsers():
133 types = self.DoFindAllTypes()
136 set(['debug', 'release',
137 'content-shell-debug', 'content-shell-release',
138 'canary', 'system']))
141 class LinuxFindTest(FindTestBase):
143 super(LinuxFindTest, self).setUp()
145 self._finder_stubs.sys.platform = 'linux2'
146 self._path_stubs.sys.platform = 'linux2'
147 self._files.append('/foo/chrome')
148 self._files.append('../../../out/Release/chrome')
149 self._files.append('../../../out/Debug/chrome')
150 self._files.append('../../../out/Release/content_shell')
151 self._files.append('../../../out/Debug/content_shell')
153 self.has_google_chrome_on_path = False
155 def call_hook(*args, **kwargs): # pylint: disable=W0613
156 if this.has_google_chrome_on_path:
158 raise OSError('Not found')
159 self._finder_stubs.subprocess.call = call_hook
161 def testFindAllWithExact(self):
162 if not self.CanFindAvailableBrowsers():
165 types = self.DoFindAllTypes()
168 set(['debug', 'release',
169 'content-shell-debug', 'content-shell-release']))
171 def testFindWithProvidedExecutable(self):
172 if not self.CanFindAvailableBrowsers():
175 self._finder_options.browser_executable = '/foo/chrome'
176 self.assertIn('exact', self.DoFindAllTypes())
178 def testFindUsingDefaults(self):
179 if not self.CanFindAvailableBrowsers():
182 self.has_google_chrome_on_path = True
183 self.assertIn('release', self.DoFindAllTypes())
186 self.has_google_chrome_on_path = True
187 self.assertIn('system', self.DoFindAllTypes())
189 self.has_google_chrome_on_path = False
191 self.assertEquals(['content-shell-debug', 'content-shell-release'],
192 self.DoFindAllTypes())
194 def testFindUsingRelease(self):
195 if not self.CanFindAvailableBrowsers():
198 self.assertIn('release', self.DoFindAllTypes())
201 class WinFindTest(FindTestBase):
203 super(WinFindTest, self).setUp()
205 self._finder_stubs.sys.platform = 'win32'
206 self._path_stubs.sys.platform = 'win32'
207 self._path_stubs.os.local_app_data = 'c:\\Users\\Someone\\AppData\\Local'
208 self._files.append('c:\\tmp\\chrome.exe')
209 self._files.append('..\\..\\..\\build\\Release\\chrome.exe')
210 self._files.append('..\\..\\..\\build\\Debug\\chrome.exe')
211 self._files.append('..\\..\\..\\build\\Release\\content_shell.exe')
212 self._files.append('..\\..\\..\\build\\Debug\\content_shell.exe')
213 self._files.append(self._path_stubs.os.local_app_data + '\\' +
214 'Google\\Chrome\\Application\\chrome.exe')
215 self._files.append(self._path_stubs.os.local_app_data + '\\' +
216 'Google\\Chrome SxS\\Application\\chrome.exe')
218 def testFindAllGivenDefaults(self):
219 if not self.CanFindAvailableBrowsers():
222 types = self.DoFindAllTypes()
223 self.assertEquals(set(types),
224 set(['debug', 'release',
225 'content-shell-debug', 'content-shell-release',
226 'system', 'canary']))
228 def testFindAllWithExact(self):
229 if not self.CanFindAvailableBrowsers():
232 self._finder_options.browser_executable = 'c:\\tmp\\chrome.exe'
233 types = self.DoFindAllTypes()
238 'content-shell-debug', 'content-shell-release',
239 'system', 'canary']))