1# Copyright (c) 2012 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.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._options = browser_options.BrowserOptions()
19    self._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._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 testFindSconsbuild(self):
77    self._files.append('..\\..\\..\\sconsbuild\\Release\\chrome.exe')
78    self.assertTrue('release' in self.DoFindAllTypes())
79
80  def testFindXcodebuild(self):
81    self._files.append('..\\..\\..\\xcodebuild\\Release\\chrome.exe')
82    self.assertTrue('release' in self.DoFindAllTypes())
83
84class OSXFindTest(FindTestBase):
85  def setUp(self):
86    super(OSXFindTest, self).setUp()
87    self._stubs.sys.platform = 'darwin'
88    self._files.append('/Applications/Google Chrome Canary.app/'
89                       'Contents/MacOS/Google Chrome Canary')
90    self._files.append('/Applications/Google Chrome.app/' +
91                       'Contents/MacOS/Google Chrome')
92    self._files.append(
93      '../../../out/Release/Chromium.app/Contents/MacOS/Chromium')
94    self._files.append(
95      '../../../out/Debug/Chromium.app/Contents/MacOS/Chromium')
96    self._files.append(
97      '../../../out/Release/Content Shell.app/Contents/MacOS/Content Shell')
98    self._files.append(
99      '../../../out/Debug/Content Shell.app/Contents/MacOS/Content Shell')
100
101  def testFindAll(self):
102    types = self.DoFindAllTypes()
103    self.assertEquals(
104      set(types),
105      set(['debug', 'release',
106           'content-shell-debug', 'content-shell-release',
107           'canary', 'system']))
108
109
110class LinuxFindTest(FindTestBase):
111  def setUp(self):
112    super(LinuxFindTest, self).setUp()
113
114    self._stubs.sys.platform = 'linux2'
115    self._files.append('/foo/chrome')
116    self._files.append('../../../out/Release/chrome')
117    self._files.append('../../../out/Debug/chrome')
118    self._files.append('../../../out/Release/content_shell')
119    self._files.append('../../../out/Debug/content_shell')
120
121    self.has_google_chrome_on_path = False
122    this = self
123    def call_hook(*args, **kwargs): # pylint: disable=W0613
124      if this.has_google_chrome_on_path:
125        return 0
126      raise OSError('Not found')
127    self._stubs.subprocess.call = call_hook
128
129  def testFindAllWithExact(self):
130    types = self.DoFindAllTypes()
131    self.assertEquals(
132        set(types),
133        set(['debug', 'release',
134             'content-shell-debug', 'content-shell-release']))
135
136  def testFindWithProvidedExecutable(self):
137    self._options.browser_executable = '/foo/chrome'
138    self.assertTrue('exact' in self.DoFindAllTypes())
139
140  def testFindUsingDefaults(self):
141    self.has_google_chrome_on_path = True
142    self.assertTrue('release' in self.DoFindAllTypes())
143
144    del self._files[1]
145    self.has_google_chrome_on_path = True
146    self.assertTrue('system' in self.DoFindAllTypes())
147
148    self.has_google_chrome_on_path = False
149    del self._files[1]
150    self.assertEquals(['content-shell-debug', 'content-shell-release'],
151                      self.DoFindAllTypes())
152
153  def testFindUsingRelease(self):
154    self.assertTrue('release' in self.DoFindAllTypes())
155
156
157class WinFindTest(FindTestBase):
158  def setUp(self):
159    super(WinFindTest, self).setUp()
160
161    self._stubs.sys.platform = 'win32'
162    self._stubs.os.local_app_data = 'c:\\Users\\Someone\\AppData\\Local'
163    self._files.append('c:\\tmp\\chrome.exe')
164    self._files.append('..\\..\\..\\build\\Release\\chrome.exe')
165    self._files.append('..\\..\\..\\build\\Debug\\chrome.exe')
166    self._files.append('..\\..\\..\\build\\Release\\content_shell.exe')
167    self._files.append('..\\..\\..\\build\\Debug\\content_shell.exe')
168    self._files.append(self._stubs.os.local_app_data + '\\' +
169                       'Google\\Chrome\\Application\\chrome.exe')
170    self._files.append(self._stubs.os.local_app_data + '\\' +
171                       'Google\\Chrome SxS\\Application\\chrome.exe')
172
173  def testFindAllGivenDefaults(self):
174    types = self.DoFindAllTypes()
175    self.assertEquals(set(types),
176                      set(['debug', 'release',
177                           'content-shell-debug', 'content-shell-release',
178                           'system', 'canary']))
179
180  def testFindAllWithExact(self):
181    self._options.browser_executable = 'c:\\tmp\\chrome.exe'
182    types = self.DoFindAllTypes()
183    self.assertEquals(
184        set(types),
185        set(['exact',
186             'debug', 'release',
187             'content-shell-debug', 'content-shell-release',
188             'system', 'canary']))
189