OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 import unittest | 4 import unittest |
5 | 5 |
6 from chrome_remote_control import browser_options | 6 from chrome_remote_control import browser_options |
7 from chrome_remote_control import desktop_browser_finder | 7 from chrome_remote_control import desktop_browser_finder |
8 from chrome_remote_control.system_stub import ( | 8 from chrome_remote_control import system_stub |
9 OSModuleStub, | |
10 SysModuleStub | |
11 ) | |
12 | 9 |
13 # This file verifies the logic for finding a browser instance on all platforms | 10 # This file verifies the logic for finding a browser instance on all platforms |
14 # at once. It does so by providing stubs for the OS/sys/subprocess primitives | 11 # at once. It does so by providing stubs for the OS/sys/subprocess primitives |
15 # that the underlying finding logic usually uses to locate a suitable browser. | 12 # that the underlying finding logic usually uses to locate a suitable browser. |
16 # We prefer this approach to having to run the same test on every platform on | 13 # We prefer this approach to having to run the same test on every platform on |
17 # which we want this code to work. | 14 # which we want this code to work. |
18 | 15 |
19 class StubSubprocess(object): | |
20 def __init__(self): | |
21 self.call_hook = None | |
22 | |
23 def call(self, *args, **kwargs): | |
24 assert self.call_hook | |
25 return self.call_hook(*args, **kwargs) | |
26 | |
27 class FindTestBase(unittest.TestCase): | 16 class FindTestBase(unittest.TestCase): |
28 def setUp(self): | 17 def setUp(self): |
29 self._options = browser_options.BrowserOptions() | 18 self._options = browser_options.BrowserOptions() |
30 self._options.chrome_root = '../../../' | 19 self._options.chrome_root = '../../../' |
31 self._sys_stub = SysModuleStub() | 20 self._stubs = system_stub.Override(desktop_browser_finder, |
32 self._os_stub = OSModuleStub(self._sys_stub) | 21 ['os', 'subprocess', 'sys']) |
33 self._subprocess_stub = StubSubprocess() | 22 |
| 23 def tearDown(self): |
| 24 self._stubs.Restore() |
34 | 25 |
35 @property | 26 @property |
36 def _files(self): | 27 def _files(self): |
37 return self._os_stub.files | 28 return self._stubs.os.path.files |
38 | 29 |
39 def DoFindAll(self): | 30 def DoFindAll(self): |
40 return desktop_browser_finder.FindAllAvailableBrowsers(self._options, | 31 return desktop_browser_finder.FindAllAvailableBrowsers(self._options) |
41 self._os_stub, self._sys_stub, self._subprocess_stub) | |
42 | 32 |
43 def DoFindAllTypes(self): | 33 def DoFindAllTypes(self): |
44 browsers = self.DoFindAll() | 34 browsers = self.DoFindAll() |
45 return [b.browser_type for b in browsers] | 35 return [b.browser_type for b in browsers] |
46 | 36 |
47 def has_type(array, browser_type): | 37 def has_type(array, browser_type): |
48 return len([x for x in array if x.browser_type == browser_type]) != 0 | 38 return len([x for x in array if x.browser_type == browser_type]) != 0 |
49 | 39 |
50 class OSXFindTest(FindTestBase): | 40 class OSXFindTest(FindTestBase): |
51 def setUp(self): | 41 def setUp(self): |
52 super(OSXFindTest, self).setUp() | 42 super(OSXFindTest, self).setUp() |
53 self._sys_stub.platform = 'darwin' | 43 self._stubs.sys.platform = 'darwin' |
54 self._files.append('/Applications/Google Chrome Canary.app/' | 44 self._files.append('/Applications/Google Chrome Canary.app/' |
55 'Contents/MacOS/Google Chrome Canary') | 45 'Contents/MacOS/Google Chrome Canary') |
56 self._files.append('/Applications/Google Chrome.app/' + | 46 self._files.append('/Applications/Google Chrome.app/' + |
57 'Contents/MacOS/Google Chrome') | 47 'Contents/MacOS/Google Chrome') |
58 self._files.append( | 48 self._files.append( |
59 '../../../out/Release/Chromium.app/Contents/MacOS/Chromium') | 49 '../../../out/Release/Chromium.app/Contents/MacOS/Chromium') |
60 self._files.append( | 50 self._files.append( |
61 '../../../out/Debug/Chromium.app/Contents/MacOS/Chromium') | 51 '../../../out/Debug/Chromium.app/Contents/MacOS/Chromium') |
62 self._files.append( | 52 self._files.append( |
63 '../../../out/Release/Content Shell.app/Contents/MacOS/Content Shell') | 53 '../../../out/Release/Content Shell.app/Contents/MacOS/Content Shell') |
64 self._files.append( | 54 self._files.append( |
65 '../../../out/Debug/Content Shell.app/Contents/MacOS/Content Shell') | 55 '../../../out/Debug/Content Shell.app/Contents/MacOS/Content Shell') |
66 | 56 |
67 def testFindAll(self): | 57 def testFindAll(self): |
68 types = self.DoFindAllTypes() | 58 types = self.DoFindAllTypes() |
69 self.assertEquals( | 59 self.assertEquals( |
70 set(types), | 60 set(types), |
71 set(['debug', 'release', | 61 set(['debug', 'release', |
72 'content-shell-debug', 'content-shell-release', | 62 'content-shell-debug', 'content-shell-release', |
73 'canary', 'system'])) | 63 'canary', 'system'])) |
74 | 64 |
75 | 65 |
76 class LinuxFindTest(FindTestBase): | 66 class LinuxFindTest(FindTestBase): |
77 def setUp(self): | 67 def setUp(self): |
78 super(LinuxFindTest, self).setUp() | 68 super(LinuxFindTest, self).setUp() |
79 | 69 |
80 self._sys_stub.platform = 'linux2' | 70 self._stubs.sys.platform = 'linux2' |
81 self._files.append('/foo/chrome') | 71 self._files.append('/foo/chrome') |
82 self._files.append('../../../out/Release/chrome') | 72 self._files.append('../../../out/Release/chrome') |
83 self._files.append('../../../out/Debug/chrome') | 73 self._files.append('../../../out/Debug/chrome') |
84 self._files.append('../../../out/Release/content_shell') | 74 self._files.append('../../../out/Release/content_shell') |
85 self._files.append('../../../out/Debug/content_shell') | 75 self._files.append('../../../out/Debug/content_shell') |
86 | 76 |
87 self.has_google_chrome_on_path = False | 77 self.has_google_chrome_on_path = False |
88 this = self | 78 this = self |
89 def call_hook(*args, **kwargs): # pylint: disable=W0613 | 79 def call_hook(*args, **kwargs): # pylint: disable=W0613 |
90 if this.has_google_chrome_on_path: | 80 if this.has_google_chrome_on_path: |
91 return 0 | 81 return 0 |
92 raise OSError('Not found') | 82 raise OSError('Not found') |
93 self._subprocess_stub.call_hook = call_hook | 83 self._stubs.subprocess.call = call_hook |
94 | 84 |
95 def testFindAllWithExact(self): | 85 def testFindAllWithExact(self): |
96 types = self.DoFindAllTypes() | 86 types = self.DoFindAllTypes() |
97 self.assertEquals( | 87 self.assertEquals( |
98 set(types), | 88 set(types), |
99 set(['debug', 'release', | 89 set(['debug', 'release', |
100 'content-shell-debug', 'content-shell-release'])) | 90 'content-shell-debug', 'content-shell-release'])) |
101 | 91 |
102 def testFindWithProvidedExecutable(self): | 92 def testFindWithProvidedExecutable(self): |
103 self._options.browser_executable = '/foo/chrome' | 93 self._options.browser_executable = '/foo/chrome' |
(...skipping 13 matching lines...) Expand all Loading... |
117 self.DoFindAllTypes()) | 107 self.DoFindAllTypes()) |
118 | 108 |
119 def testFindUsingRelease(self): | 109 def testFindUsingRelease(self): |
120 self.assertTrue('release' in self.DoFindAllTypes()) | 110 self.assertTrue('release' in self.DoFindAllTypes()) |
121 | 111 |
122 | 112 |
123 class WinFindTest(FindTestBase): | 113 class WinFindTest(FindTestBase): |
124 def setUp(self): | 114 def setUp(self): |
125 super(WinFindTest, self).setUp() | 115 super(WinFindTest, self).setUp() |
126 | 116 |
127 self._sys_stub.platform = 'win32' | 117 self._stubs.sys.platform = 'win32' |
128 self._os_stub.local_app_data = 'c:\\Users\\Someone\\AppData\\Local' | 118 self._stubs.os.local_app_data = 'c:\\Users\\Someone\\AppData\\Local' |
129 self._files.append('c:\\tmp\\chrome.exe') | 119 self._files.append('c:\\tmp\\chrome.exe') |
130 self._files.append('..\\..\\..\\build\\Release\\chrome.exe') | 120 self._files.append('..\\..\\..\\build\\Release\\chrome.exe') |
131 self._files.append('..\\..\\..\\build\\Debug\\chrome.exe') | 121 self._files.append('..\\..\\..\\build\\Debug\\chrome.exe') |
132 self._files.append('..\\..\\..\\build\\Release\\content_shell.exe') | 122 self._files.append('..\\..\\..\\build\\Release\\content_shell.exe') |
133 self._files.append('..\\..\\..\\build\\Debug\\content_shell.exe') | 123 self._files.append('..\\..\\..\\build\\Debug\\content_shell.exe') |
134 self._files.append(self._os_stub.local_app_data + '\\' + | 124 self._files.append(self._stubs.os.local_app_data + '\\' + |
135 'Google\\Chrome\\Application\\chrome.exe') | 125 'Google\\Chrome\\Application\\chrome.exe') |
136 self._files.append(self._os_stub.local_app_data + '\\' + | 126 self._files.append(self._stubs.os.local_app_data + '\\' + |
137 'Google\\Chrome SxS\\Application\\chrome.exe') | 127 'Google\\Chrome SxS\\Application\\chrome.exe') |
138 | 128 |
139 def testFindAllGivenDefaults(self): | 129 def testFindAllGivenDefaults(self): |
140 types = self.DoFindAllTypes() | 130 types = self.DoFindAllTypes() |
141 self.assertEquals(set(types), | 131 self.assertEquals(set(types), |
142 set(['debug', 'release', | 132 set(['debug', 'release', |
143 'content-shell-debug', 'content-shell-release', | 133 'content-shell-debug', 'content-shell-release', |
144 'system', 'canary'])) | 134 'system', 'canary'])) |
145 | 135 |
146 def testFindAllWithExact(self): | 136 def testFindAllWithExact(self): |
147 self._options.browser_executable = 'c:\\tmp\\chrome.exe' | 137 self._options.browser_executable = 'c:\\tmp\\chrome.exe' |
148 types = self.DoFindAllTypes() | 138 types = self.DoFindAllTypes() |
149 self.assertEquals( | 139 self.assertEquals( |
150 set(types), | 140 set(types), |
151 set(['exact', | 141 set(['exact', |
152 'debug', 'release', | 142 'debug', 'release', |
153 'content-shell-debug', 'content-shell-release', | 143 'content-shell-debug', 'content-shell-release', |
154 'system', 'canary'])) | 144 'system', 'canary'])) |
OLD | NEW |