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 | 4 |
5 """Class for running instrumentation tests on a single device.""" | 5 """Class for running instrumentation tests on a single device.""" |
6 | 6 |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import re | 9 import re |
10 import sys | 10 import sys |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 return 5 * 60 | 332 return 5 * 60 |
333 if 'MediumTest' in annotations: | 333 if 'MediumTest' in annotations: |
334 return 3 * 60 | 334 return 3 * 60 |
335 if 'SmallTest' in annotations: | 335 if 'SmallTest' in annotations: |
336 return 1 * 60 | 336 return 1 * 60 |
337 | 337 |
338 logging.warn(("Test size not found in annotations for test '{0}', using " + | 338 logging.warn(("Test size not found in annotations for test '{0}', using " + |
339 "1 minute for timeout.").format(test)) | 339 "1 minute for timeout.").format(test)) |
340 return 1 * 60 | 340 return 1 * 60 |
341 | 341 |
342 def RunInstrumentationTest(self, test, test_package, instr_args, timeout): | 342 def _RunTest(self, test, timeout): |
343 """Runs a single instrumentation test. | 343 """Runs a single instrumentation test. |
344 | 344 |
345 Args: | 345 Args: |
346 test: Test class/method. | 346 test: Test class/method. |
347 test_package: Package name of test apk. | |
348 instr_args: Extra key/value to pass to am instrument. | |
349 timeout: Timeout time in seconds. | 347 timeout: Timeout time in seconds. |
350 | 348 |
351 Returns: | 349 Returns: |
352 An instance of InstrumentationTestResult | 350 The raw output of am instrument as a list of lines. |
353 """ | 351 """ |
354 # Build the 'am instrument' command | 352 # Build the 'am instrument' command |
355 instrumentation_path = ( | 353 instrumentation_path = ( |
356 '%s/%s' % (test_package, self.options.test_runner)) | 354 '%s/%s' % (self.test_pkg.GetPackageName(), self.options.test_runner)) |
357 | 355 |
358 cmd = ['am', 'instrument', '-r'] | 356 cmd = ['am', 'instrument', '-r'] |
359 for k, v in instr_args.iteritems(): | 357 for k, v in self._GetInstrumentationArgs().iteritems(): |
360 cmd.extend(['-e', k, "'%s'" % v]) | 358 cmd.extend(['-e', k, "'%s'" % v]) |
361 cmd.extend(['-e', 'class', "'%s'" % test]) | 359 cmd.extend(['-e', 'class', "'%s'" % test]) |
362 cmd.extend(['-w', instrumentation_path]) | 360 cmd.extend(['-w', instrumentation_path]) |
363 | 361 return self.device.RunShellCommand(cmd, timeout=timeout, retries=0) |
364 time_ms = lambda: int(time.time() * 1000) | |
365 | |
366 # Run the test. | |
367 start_ms = time_ms() | |
368 try: | |
369 instr_output = self.device.RunShellCommand( | |
370 cmd, timeout=timeout, retries=0) | |
371 except device_errors.CommandTimeoutError: | |
372 return test_result.InstrumentationTestResult( | |
373 test, base_test_result.ResultType.TIMEOUT, start_ms, | |
374 time_ms() - start_ms) | |
375 duration_ms = time_ms() - start_ms | |
376 | |
377 # Parse the test output | |
378 _, _, statuses = self._ParseAmInstrumentRawOutput(instr_output) | |
379 return self._GenerateTestResult(test, statuses, start_ms, duration_ms) | |
380 | 362 |
381 @staticmethod | 363 @staticmethod |
382 def _ParseAmInstrumentRawOutput(raw_output): | 364 def _ParseAmInstrumentRawOutput(raw_output): |
383 """Parses the output of an |am instrument -r| call. | 365 """Parses the output of an |am instrument -r| call. |
384 | 366 |
385 Args: | 367 Args: |
386 raw_output: the output of an |am instrument -r| call as a list of lines | 368 raw_output: the output of an |am instrument -r| call as a list of lines |
387 Returns: | 369 Returns: |
388 A 3-tuple containing: | 370 A 3-tuple containing: |
389 - the instrumentation code as an integer | 371 - the instrumentation code as an integer |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 | 477 |
496 return test_result.InstrumentationTestResult( | 478 return test_result.InstrumentationTestResult( |
497 test, result_type, start_ms, duration_ms, log=log) | 479 test, result_type, start_ms, duration_ms, log=log) |
498 | 480 |
499 #override | 481 #override |
500 def RunTest(self, test): | 482 def RunTest(self, test): |
501 results = base_test_result.TestRunResults() | 483 results = base_test_result.TestRunResults() |
502 timeout = (self._GetIndividualTestTimeoutSecs(test) * | 484 timeout = (self._GetIndividualTestTimeoutSecs(test) * |
503 self._GetIndividualTestTimeoutScale(test) * | 485 self._GetIndividualTestTimeoutScale(test) * |
504 self.tool.GetTimeoutScale()) | 486 self.tool.GetTimeoutScale()) |
| 487 |
| 488 start_ms = 0 |
| 489 duration_ms = 0 |
505 try: | 490 try: |
506 self.TestSetup(test) | 491 self.TestSetup(test) |
507 result = self.RunInstrumentationTest( | 492 |
508 test, self.test_pkg.GetPackageName(), self._GetInstrumentationArgs(), | 493 time_ms = lambda: int(time.time() * 1000) |
509 timeout) | 494 start_ms = time_ms() |
| 495 raw_output = self._RunTest(test, timeout) |
| 496 duration_ms = time_ms() - start_ms |
| 497 |
| 498 # Parse the test output |
| 499 _, _, statuses = self._ParseAmInstrumentRawOutput(raw_output) |
| 500 result = self._GenerateTestResult(test, statuses, start_ms, duration_ms) |
510 results.AddResult(result) | 501 results.AddResult(result) |
511 except (device_errors.CommandTimeoutError, | 502 except device_errors.CommandTimeoutError as e: |
512 device_errors.DeviceUnreachableError) as e: | |
513 message = str(e) | |
514 if not message: | |
515 message = 'No information.' | |
516 results.AddResult(test_result.InstrumentationTestResult( | 503 results.AddResult(test_result.InstrumentationTestResult( |
517 test, base_test_result.ResultType.CRASH, int(time.time() * 1000), 0, | 504 test, base_test_result.ResultType.TIMEOUT, start_ms, duration_ms, |
518 log=message)) | 505 log=str(e) or 'No information')) |
| 506 except device_errors.DeviceUnreachableError as e: |
| 507 results.AddResult(test_result.InstrumentationTestResult( |
| 508 test, base_test_result.ResultType.CRASH, start_ms, duration_ms, |
| 509 log=str(e) or 'No information')) |
519 self.TestTeardown(test, results) | 510 self.TestTeardown(test, results) |
520 return (results, None if results.DidRunPass() else test) | 511 return (results, None if results.DidRunPass() else test) |
OLD | NEW |