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.
5 """Defines TestPackageApk to help run APK-based native tests."""
6 # pylint: disable=W0212
15 from pylib
import android_commands
16 from pylib
import constants
17 from pylib
import pexpect
18 from pylib
.device
import device_errors
19 from pylib
.device
import intent
20 from pylib
.gtest
import gtest_test_instance
21 from pylib
.gtest
.test_package
import TestPackage
24 class TestPackageApk(TestPackage
):
25 """A helper class for running APK-based native tests."""
27 def __init__(self
, suite_name
):
30 suite_name: Name of the test suite (e.g. base_unittests).
32 TestPackage
.__init
__(self
, suite_name
)
33 if suite_name
== 'content_browsertests':
34 self
.suite_path
= os
.path
.join(
35 constants
.GetOutDirectory(), 'apks', '%s.apk' % suite_name
)
36 self
._package
_info
= constants
.PACKAGE_INFO
['content_browsertests']
38 self
.suite_path
= os
.path
.join(
39 constants
.GetOutDirectory(), '%s_apk' % suite_name
,
40 '%s-debug.apk' % suite_name
)
41 self
._package
_info
= constants
.PACKAGE_INFO
['gtest']
43 def _CreateCommandLineFileOnDevice(self
, device
, options
):
44 device
.WriteFile(self
._package
_info
.cmdline_file
,
45 self
.suite_name
+ ' ' + options
)
48 # The test.fifo path is determined by:
49 # testing/android/java/src/org/chromium/native_test/
50 # ChromeNativeTestActivity.java and
51 # testing/android/native_test_launcher.cc
52 return '/data/data/' + self
._package
_info
.package
+ '/files/test.fifo'
54 def _ClearFifo(self
, device
):
55 device
.RunShellCommand('rm -f ' + self
._GetFifo
())
57 def _WatchFifo(self
, device
, timeout
, logfile
=None):
59 if device
.FileExists(self
._GetFifo
()):
60 logging
.info('Fifo created. Slept for %f secs' % (i
* 0.5))
64 raise device_errors
.DeviceUnreachableError(
65 'Unable to find fifo on device %s ' % self
._GetFifo
())
66 args
= shlex
.split(device
.old_interface
.Adb()._target
_arg
)
67 args
+= ['shell', 'cat', self
._GetFifo
()]
68 return pexpect
.spawn('adb', args
, timeout
=timeout
, logfile
=logfile
)
70 def _StartActivity(self
, device
, force_stop
=True):
72 intent
.Intent(package
=self
._package
_info
.package
,
73 activity
=self
._package
_info
.activity
,
74 action
='android.intent.action.MAIN'),
75 # No wait since the runner waits for FIFO creation anyway.
77 force_stop
=force_stop
)
80 def ClearApplicationState(self
, device
):
81 device
.ClearApplicationState(self
._package
_info
.package
)
82 # Content shell creates a profile on the sdscard which accumulates cache
84 if self
.suite_name
== 'content_browsertests':
86 device
.RunShellCommand(
87 'rm -r %s/content_shell' % device
.GetExternalStoragePath(),
89 except device_errors
.CommandFailedError
:
90 # TODO(jbudorick) Handle this exception appropriately once the
91 # conversions are done.
95 def CreateCommandLineFileOnDevice(self
, device
, test_filter
, test_arguments
):
96 self
._CreateCommandLineFileOnDevice
(
97 device
, '--gtest_filter=%s %s' % (test_filter
, test_arguments
))
100 def GetAllTests(self
, device
):
101 self
._CreateCommandLineFileOnDevice
(device
, '--gtest_list_tests')
103 self
.tool
.SetupEnvironment()
104 # Clear and start monitoring logcat.
105 self
._ClearFifo
(device
)
106 self
._StartActivity
(device
)
107 # Wait for native test to complete.
108 p
= self
._WatchFifo
(device
, timeout
=30 * self
.tool
.GetTimeoutScale())
109 p
.expect('<<ScopedMainEntryLogger')
112 self
.tool
.CleanUpEnvironment()
113 # We need to strip the trailing newline.
114 content
= [line
.rstrip() for line
in p
.before
.splitlines()]
115 return gtest_test_instance
.ParseGTestListTests(content
)
118 def SpawnTestProcess(self
, device
):
120 self
.tool
.SetupEnvironment()
121 self
._ClearFifo
(device
)
122 # Doesn't need to stop an Activity because ClearApplicationState() is
123 # always called before this call and so it is already stopped at this
125 self
._StartActivity
(device
, force_stop
=False)
127 self
.tool
.CleanUpEnvironment()
128 logfile
= android_commands
.NewLineNormalizer(sys
.stdout
)
129 return self
._WatchFifo
(device
, timeout
=10, logfile
=logfile
)
132 def Install(self
, device
):
133 self
.tool
.CopyFiles(device
)
134 device
.Install(self
.suite_path
)