| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import os | 6 import os |
| 7 import Queue | 7 import Queue |
| 8 import sys | 8 import sys |
| 9 import subprocess | 9 import subprocess |
| 10 import threading | 10 import threading |
| 11 import unittest | 11 import unittest |
| 12 import urllib2 | 12 import urllib2 |
| 13 | 13 |
| 14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 15 TOOLS_DIR = os.path.dirname(SCRIPT_DIR) | 15 TOOLS_DIR = os.path.dirname(SCRIPT_DIR) |
| 16 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(TOOLS_DIR))) | 16 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(TOOLS_DIR))) |
| 17 MOCK_DIR = os.path.join(CHROME_SRC, "third_party", "pymock") | 17 MOCK_DIR = os.path.join(CHROME_SRC, 'third_party', 'pymock') |
| 18 | 18 |
| 19 sys.path.append(TOOLS_DIR) | 19 sys.path.append(TOOLS_DIR) |
| 20 sys.path.append(MOCK_DIR) | 20 sys.path.append(MOCK_DIR) |
| 21 | 21 |
| 22 import httpd | 22 import httpd |
| 23 from mock import patch | 23 from mock import patch, Mock |
| 24 | 24 |
| 25 | 25 |
| 26 class HTTPDTest(unittest.TestCase): | 26 class HTTPDTest(unittest.TestCase): |
| 27 def setUp(self): | 27 def setUp(self): |
| 28 patcher = patch('BaseHTTPServer.BaseHTTPRequestHandler.log_message') | 28 patcher = patch('BaseHTTPServer.BaseHTTPRequestHandler.log_message') |
| 29 patcher.start() | 29 patcher.start() |
| 30 self.addCleanup(patcher.stop) | 30 self.addCleanup(patcher.stop) |
| 31 | 31 |
| 32 self.server = httpd.LocalHTTPServer('.', 0) | 32 self.server = httpd.LocalHTTPServer('.', 0) |
| 33 self.addCleanup(self.server.Shutdown) | 33 self.addCleanup(self.server.Shutdown) |
| 34 | 34 |
| 35 def testQuit(self): | 35 def testQuit(self): |
| 36 urllib2.urlopen(self.server.GetURL('?quit=1')) | 36 urllib2.urlopen(self.server.GetURL('?quit=1')) |
| 37 self.server.process.join(10) # Wait 10 seconds for the process to finish. | 37 self.server.process.join(10) # Wait 10 seconds for the process to finish. |
| 38 self.assertFalse(self.server.process.is_alive()) | 38 self.assertFalse(self.server.process.is_alive()) |
| 39 | 39 |
| 40 | 40 |
| 41 class MainTest(unittest.TestCase): |
| 42 @patch('httpd.LocalHTTPServer') |
| 43 @patch('sys.stdout', Mock()) |
| 44 def testArgs(self, mock_server_ctor): |
| 45 mock_server = Mock() |
| 46 mock_server_ctor.return_value = mock_server |
| 47 httpd.main(['-p', '123', '-C', 'dummy']) |
| 48 mock_server_ctor.assert_called_once_with('dummy', 123) |
| 49 |
| 50 |
| 41 class RunTest(unittest.TestCase): | 51 class RunTest(unittest.TestCase): |
| 42 def setUp(self): | 52 def setUp(self): |
| 43 self.process = None | 53 self.process = None |
| 44 | 54 |
| 45 def tearDown(self): | 55 def tearDown(self): |
| 46 if self.process and self.process.returncode is None: | 56 if self.process and self.process.returncode is None: |
| 47 self.process.kill() | 57 self.process.kill() |
| 48 | 58 |
| 49 @staticmethod | 59 @staticmethod |
| 50 def _SubprocessThread(process, queue): | 60 def _SubprocessThread(process, queue): |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 def testSubprocessDies(self): | 105 def testSubprocessDies(self): |
| 96 args = self._GetChromeMockArgs(page=None, http_request_type=None, sleep=0, | 106 args = self._GetChromeMockArgs(page=None, http_request_type=None, sleep=0, |
| 97 expect_to_be_killed=False) | 107 expect_to_be_killed=False) |
| 98 returncode, stdout, _ = self._Run(args, timeout=10) | 108 returncode, stdout, _ = self._Run(args, timeout=10) |
| 99 self.assertNotEqual(-1, returncode) | 109 self.assertNotEqual(-1, returncode) |
| 100 self.assertIn('Starting', stdout) | 110 self.assertIn('Starting', stdout) |
| 101 | 111 |
| 102 | 112 |
| 103 if __name__ == '__main__': | 113 if __name__ == '__main__': |
| 104 unittest.main() | 114 unittest.main() |
| OLD | NEW |