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.
8 from pyfakefs
import fake_filesystem_unittest
10 from telemetry
.core
import platform
11 from telemetry
.core
import util
12 from telemetry
.internal
.backends
.chrome
import desktop_browser_finder
13 from telemetry
.internal
.browser
import browser_options
14 from telemetry
.internal
.platform
import desktop_device
15 from telemetry
.testing
import system_stub
18 # This file verifies the logic for finding a browser instance on all platforms
19 # at once. It does so by providing stubs for the OS/sys/subprocess primitives
20 # that the underlying finding logic usually uses to locate a suitable browser.
21 # We prefer this approach to having to run the same test on every platform on
22 # which we want this code to work.
24 class FindTestBase(unittest
.TestCase
):
26 self
._finder
_options
= browser_options
.BrowserFinderOptions()
27 self
._finder
_options
.chrome_root
= '../../../'
28 self
._finder
_stubs
= system_stub
.Override(desktop_browser_finder
,
29 ['os', 'subprocess', 'sys'])
30 self
._path
_stubs
= system_stub
.Override(
31 desktop_browser_finder
.path_module
, ['os', 'sys'])
32 self
._catapult
_path
_stubs
= system_stub
.Override(
33 desktop_browser_finder
.path_module
.catapult_util
, ['os', 'sys'])
34 self
._util
_stubs
= system_stub
.Override(util
, ['os', 'sys'])
37 self
._finder
_stubs
.Restore()
38 self
._path
_stubs
.Restore()
39 self
._catapult
_path
_stubs
.Restore()
40 self
._util
_stubs
.Restore()
44 return self
._catapult
_path
_stubs
.os
.path
.files
47 return desktop_browser_finder
.FindAllAvailableBrowsers(
48 self
._finder
_options
, desktop_device
.DesktopDevice())
50 def DoFindAllTypes(self
):
51 browsers
= self
.DoFindAll()
52 return [b
.browser_type
for b
in browsers
]
54 def CanFindAvailableBrowsers(self
):
55 return desktop_browser_finder
.CanFindAvailableBrowsers()
58 def has_type(array
, browser_type
):
59 return len([x
for x
in array
if x
.browser_type
== browser_type
]) != 0
62 class FindSystemTest(FindTestBase
):
64 super(FindSystemTest
, self
).setUp()
65 self
._finder
_stubs
.sys
.platform
= 'win32'
66 self
._path
_stubs
.sys
.platform
= 'win32'
67 self
._util
_stubs
.sys
.platform
= 'win32'
69 def testFindProgramFiles(self
):
70 if not self
.CanFindAvailableBrowsers():
74 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe')
75 self
._path
_stubs
.os
.program_files
= 'C:\\Program Files'
76 self
.assertIn('system', self
.DoFindAllTypes())
78 def testFindProgramFilesX86(self
):
79 if not self
.CanFindAvailableBrowsers():
83 'C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe')
84 self
._path
_stubs
.os
.program_files_x86
= 'C:\\Program Files(x86)'
85 self
.assertIn('system', self
.DoFindAllTypes())
87 def testFindLocalAppData(self
):
88 if not self
.CanFindAvailableBrowsers():
92 'C:\\Local App Data\\Google\\Chrome\\Application\\chrome.exe')
93 self
._path
_stubs
.os
.local_app_data
= 'C:\\Local App Data'
94 self
.assertIn('system', self
.DoFindAllTypes())
97 class FindLocalBuildsTest(FindTestBase
):
99 super(FindLocalBuildsTest
, self
).setUp()
100 self
._finder
_stubs
.sys
.platform
= 'win32'
101 self
._path
_stubs
.sys
.platform
= 'win32'
102 self
._util
_stubs
.sys
.platform
= 'win32'
104 def testFindBuild(self
):
105 if not self
.CanFindAvailableBrowsers():
108 self
._files
.append('..\\..\\..\\build\\Release\\chrome.exe')
109 self
.assertIn('release', self
.DoFindAllTypes())
111 def testFindOut(self
):
112 if not self
.CanFindAvailableBrowsers():
115 self
._files
.append('..\\..\\..\\out\\Release\\chrome.exe')
116 self
.assertIn('release', self
.DoFindAllTypes())
118 def testFindXcodebuild(self
):
119 if not self
.CanFindAvailableBrowsers():
122 self
._files
.append('..\\..\\..\\xcodebuild\\Release\\chrome.exe')
123 self
.assertIn('release', self
.DoFindAllTypes())
126 class OSXFindTest(FindTestBase
):
128 super(OSXFindTest
, self
).setUp()
129 self
._finder
_stubs
.sys
.platform
= 'darwin'
130 self
._path
_stubs
.sys
.platform
= 'darwin'
131 self
._util
_stubs
.sys
.platform
= 'darwin'
132 self
._files
.append('/Applications/Google Chrome Canary.app/'
133 'Contents/MacOS/Google Chrome Canary')
134 self
._files
.append('/Applications/Google Chrome.app/' +
135 'Contents/MacOS/Google Chrome')
137 '../../../out/Release/Chromium.app/Contents/MacOS/Chromium')
139 '../../../out/Debug/Chromium.app/Contents/MacOS/Chromium')
141 '../../../out/Release/Content Shell.app/Contents/MacOS/Content Shell')
143 '../../../out/Debug/Content Shell.app/Contents/MacOS/Content Shell')
145 def testFindAll(self
):
146 if not self
.CanFindAvailableBrowsers():
149 types
= self
.DoFindAllTypes()
152 set(['debug', 'release',
153 'content-shell-debug', 'content-shell-release',
154 'canary', 'system']))
156 def testFindExact(self
):
157 if not self
.CanFindAvailableBrowsers():
161 '../../../foo1/Chromium.app/Contents/MacOS/Chromium')
162 self
._finder
_options
.browser_executable
= (
163 '../../../foo1/Chromium.app/Contents/MacOS/Chromium')
164 types
= self
.DoFindAllTypes()
165 self
.assertTrue('exact' in types
)
167 def testCannotFindExact(self
):
168 if not self
.CanFindAvailableBrowsers():
172 '../../../foo1/Chromium.app/Contents/MacOS/Chromium')
173 self
._finder
_options
.browser_executable
= (
174 '../../../foo2/Chromium.app/Contents/MacOS/Chromium')
175 self
.assertRaises(Exception, self
.DoFindAllTypes
)
177 class LinuxFindTest(fake_filesystem_unittest
.TestCase
):
180 if not platform
.GetHostPlatform().GetOSName() == 'linux':
181 self
.skipTest('Not running on Linux')
184 self
._finder
_options
= browser_options
.BrowserFinderOptions()
185 self
._finder
_options
.chrome_root
= '/src/'
187 def CreateBrowser(self
, path
):
188 self
.fs
.CreateFile(path
)
189 os
.chmod(path
, stat
.S_IXUSR
)
192 return desktop_browser_finder
.FindAllAvailableBrowsers(
193 self
._finder
_options
, desktop_device
.DesktopDevice())
195 def DoFindAllTypes(self
):
196 return [b
.browser_type
for b
in self
.DoFindAll()]
198 def testFindAllWithCheckout(self
):
199 for target
in ['Release', 'Debug']:
200 for browser
in ['chrome', 'content_shell']:
201 self
.CreateBrowser('/src/out/%s/%s' % (target
, browser
))
204 set(self
.DoFindAllTypes()),
205 {'debug', 'release', 'content-shell-debug', 'content-shell-release'})
207 def testFindAllFailsIfNotExecutable(self
):
208 self
.fs
.CreateFile('/src/out/Release/chrome')
210 self
.assertFalse(self
.DoFindAllTypes())
212 def testFindWithProvidedExecutable(self
):
213 self
.CreateBrowser('/foo/chrome')
214 self
._finder
_options
.browser_executable
= '/foo/chrome'
215 self
.assertIn('exact', self
.DoFindAllTypes())
217 def testFindWithProvidedApk(self
):
218 self
._finder
_options
.browser_executable
= '/foo/chrome.apk'
219 self
.assertNotIn('exact', self
.DoFindAllTypes())
221 def testNoErrorWithNonChromeExecutableName(self
):
222 self
.fs
.CreateFile('/foo/another_browser')
223 self
._finder
_options
.browser_executable
= '/foo/another_browser'
224 self
.assertNotIn('exact', self
.DoFindAllTypes())
226 def testFindAllWithInstalled(self
):
227 official_names
= ['chrome', 'chrome-beta', 'chrome-unstable']
229 for name
in official_names
:
230 self
.CreateBrowser('/opt/google/%s/chrome' % name
)
232 self
.assertEquals(set(self
.DoFindAllTypes()), {'stable', 'beta', 'dev'})
234 def testFindAllSystem(self
):
235 self
.CreateBrowser('/opt/google/chrome/chrome')
236 os
.symlink('/opt/google/chrome/chrome', '/usr/bin/google-chrome')
238 self
.assertEquals(set(self
.DoFindAllTypes()), {'system', 'stable'})
240 def testFindAllSystemIsBeta(self
):
241 self
.CreateBrowser('/opt/google/chrome/chrome')
242 self
.CreateBrowser('/opt/google/chrome-beta/chrome')
243 os
.symlink('/opt/google/chrome-beta/chrome', '/usr/bin/google-chrome')
245 google_chrome
= [browser
for browser
in self
.DoFindAll()
246 if browser
.browser_type
== 'system'][0]
247 self
.assertEquals('/opt/google/chrome-beta',
248 google_chrome
._browser
_directory
)
251 class WinFindTest(FindTestBase
):
253 super(WinFindTest
, self
).setUp()
255 self
._finder
_stubs
.sys
.platform
= 'win32'
256 self
._path
_stubs
.sys
.platform
= 'win32'
257 self
._util
_stubs
.sys
.platform
= 'win32'
258 self
._path
_stubs
.os
.local_app_data
= 'c:\\Users\\Someone\\AppData\\Local'
259 self
._files
.append('c:\\tmp\\chrome.exe')
260 self
._files
.append('..\\..\\..\\build\\Release\\chrome.exe')
261 self
._files
.append('..\\..\\..\\build\\Debug\\chrome.exe')
262 self
._files
.append('..\\..\\..\\build\\Release\\content_shell.exe')
263 self
._files
.append('..\\..\\..\\build\\Debug\\content_shell.exe')
264 self
._files
.append(self
._path
_stubs
.os
.local_app_data
+ '\\' +
265 'Google\\Chrome\\Application\\chrome.exe')
266 self
._files
.append(self
._path
_stubs
.os
.local_app_data
+ '\\' +
267 'Google\\Chrome SxS\\Application\\chrome.exe')
269 def testFindAllGivenDefaults(self
):
270 if not self
.CanFindAvailableBrowsers():
273 types
= self
.DoFindAllTypes()
274 self
.assertEquals(set(types
),
275 set(['debug', 'release',
276 'content-shell-debug', 'content-shell-release',
277 'system', 'canary']))
279 def testFindAllWithExact(self
):
280 if not self
.CanFindAvailableBrowsers():
283 self
._finder
_options
.browser_executable
= 'c:\\tmp\\chrome.exe'
284 types
= self
.DoFindAllTypes()
289 'content-shell-debug', 'content-shell-release',
290 'system', 'canary']))
292 def testNoErrorWithUnrecognizedExecutableName(self
):
293 if not self
.CanFindAvailableBrowsers():
296 self
._files
.append('c:\\foo\\another_browser.exe')
297 self
._finder
_options
.browser_dir
= 'c:\\foo\\another_browser.exe'
298 self
.assertNotIn('exact', self
.DoFindAllTypes())