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']
37 elif suite_name
== 'components_browsertests':
38 self
.suite_path
= os
.path
.join(
39 constants
.GetOutDirectory(), 'apks', '%s.apk' % suite_name
)
40 self
._package
_info
= constants
.PACKAGE_INFO
['components_browsertests']
42 self
.suite_path
= os
.path
.join(
43 constants
.GetOutDirectory(), '%s_apk' % suite_name
,
44 '%s-debug.apk' % suite_name
)
45 self
._package
_info
= constants
.PACKAGE_INFO
['gtest']
47 def _CreateCommandLineFileOnDevice(self
, device
, options
):
48 device
.WriteFile(self
._package
_info
.cmdline_file
,
49 self
.suite_name
+ ' ' + options
)
52 # The test.fifo path is determined by:
53 # testing/android/native_test/java/src/org/chromium/native_test/
54 # ChromeNativeTestActivity.java and
55 # testing/android/native_test_launcher.cc
56 return '/data/data/' + self
._package
_info
.package
+ '/files/test.fifo'
58 def _ClearFifo(self
, device
):
59 device
.RunShellCommand('rm -f ' + self
._GetFifo
())
61 def _WatchFifo(self
, device
, timeout
, logfile
=None):
63 if device
.FileExists(self
._GetFifo
()):
64 logging
.info('Fifo created. Slept for %f secs' % (i
* 0.5))
68 raise device_errors
.DeviceUnreachableError(
69 'Unable to find fifo on device %s ' % self
._GetFifo
())
70 args
= shlex
.split(device
.old_interface
.Adb()._target
_arg
)
71 args
+= ['shell', 'cat', self
._GetFifo
()]
72 return pexpect
.spawn('adb', args
, timeout
=timeout
, logfile
=logfile
)
74 def _StartActivity(self
, device
, force_stop
=True):
76 intent
.Intent(package
=self
._package
_info
.package
,
77 activity
=self
._package
_info
.activity
,
78 action
='android.intent.action.MAIN'),
79 # No wait since the runner waits for FIFO creation anyway.
81 force_stop
=force_stop
)
84 def ClearApplicationState(self
, device
):
85 device
.ClearApplicationState(self
._package
_info
.package
)
86 # Content shell creates a profile on the sdscard which accumulates cache
88 if self
.suite_name
== 'content_browsertests':
90 device
.RunShellCommand(
91 'rm -r %s/content_shell' % device
.GetExternalStoragePath(),
93 except device_errors
.CommandFailedError
:
94 # TODO(jbudorick) Handle this exception appropriately once the
95 # conversions are done.
97 elif self
.suite_name
== 'components_browsertests':
99 device
.RunShellCommand(
100 'rm -r %s/components_shell' % device
.GetExternalStoragePath(),
102 except device_errors
.CommandFailedError
:
103 # TODO(jbudorick) Handle this exception appropriately once the
104 # conversions are done.
108 def CreateCommandLineFileOnDevice(self
, device
, test_filter
, test_arguments
):
109 self
._CreateCommandLineFileOnDevice
(
110 device
, '--gtest_filter=%s %s' % (test_filter
, test_arguments
))
113 def GetAllTests(self
, device
):
114 self
._CreateCommandLineFileOnDevice
(device
, '--gtest_list_tests')
116 self
.tool
.SetupEnvironment()
117 # Clear and start monitoring logcat.
118 self
._ClearFifo
(device
)
119 self
._StartActivity
(device
)
120 # Wait for native test to complete.
121 p
= self
._WatchFifo
(device
, timeout
=30 * self
.tool
.GetTimeoutScale())
122 p
.expect('<<ScopedMainEntryLogger')
125 self
.tool
.CleanUpEnvironment()
126 # We need to strip the trailing newline.
127 content
= [line
.rstrip() for line
in p
.before
.splitlines()]
128 return gtest_test_instance
.ParseGTestListTests(content
)
131 def SpawnTestProcess(self
, device
):
133 self
.tool
.SetupEnvironment()
134 self
._ClearFifo
(device
)
135 # Doesn't need to stop an Activity because ClearApplicationState() is
136 # always called before this call and so it is already stopped at this
138 self
._StartActivity
(device
, force_stop
=False)
140 self
.tool
.CleanUpEnvironment()
141 logfile
= android_commands
.NewLineNormalizer(sys
.stdout
)
142 return self
._WatchFifo
(device
, timeout
=10, logfile
=logfile
)
145 def Install(self
, device
):
146 self
.tool
.CopyFiles(device
)
147 device
.Install(self
.suite_path
)