Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Unified Diff: build/android/pylib/utils/apk_helper.py

Issue 1126543009: [Android] Refactor the native test wrappers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: build/android/pylib/utils/apk_helper.py
diff --git a/build/android/pylib/utils/apk_helper.py b/build/android/pylib/utils/apk_helper.py
index f5e9cd3869ffddb42b7873a6937bc254abc8c89a..5354cc9f0174f84586fedeaa7ab2cb43d9fee826 100644
--- a/build/android/pylib/utils/apk_helper.py
+++ b/build/android/pylib/utils/apk_helper.py
@@ -19,14 +19,12 @@ _MANIFEST_ELEMENT_RE = re.compile(r'\s*(?:E|N): (\S*) .*$')
def GetPackageName(apk_path):
perezju 2015/05/05 16:15:01 maybe add TODO notes to deprecate these? Or we exp
jbudorick 2015/05/06 18:00:00 I definitely expect to keep using this one. Not su
"""Returns the package name of the apk."""
- aapt_cmd = [_AAPT_PATH, 'dump', 'badging', apk_path]
- aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n')
- package_name_re = re.compile(r'package: .*name=\'(\S*)\'')
- for line in aapt_output:
- m = package_name_re.match(line)
- if m:
- return m.group(1)
- raise Exception('Failed to determine package name of %s' % apk_path)
+ return ApkHelper(apk_path).GetPackageName()
+
+
+def GetInstrumentationName(apk_path):
+ """Returns the name of the Instrumentation in the apk."""
+ return ApkHelper(apk_path).GetInstrumentationName()
def _ParseManifestFromApk(apk_path):
@@ -65,12 +63,53 @@ def _ParseManifestFromApk(apk_path):
return parsed_manifest
-def GetInstrumentationName(
- apk_path, default='android.test.InstrumentationTestRunner'):
- """Returns the name of the Instrumentation in the apk."""
+class ApkHelper(object):
+ def __init__(self, apk_path):
+ self._apk_path = apk_path
+ self._manifest = None
+ self._package_name = None
+
+ def GetActivityName(self):
+ """Returns the name of the Activity in the apk."""
+ try:
+ manifest_info = self._GetManifest()
+ activity = (
+ manifest_info['manifest']['application']['activity']
+ ['android:name'][0])
perezju 2015/05/05 16:15:01 I guess only this line should be in the try-except
jbudorick 2015/05/06 18:00:00 Done.
+ if '.' not in activity:
+ activity = '%s.%s' % (self.GetPackageName(), activity)
+ elif activity.startswith('.'):
+ activity = '%s%s' % (self.GetPackageName(), activity)
+ return activity
+ except KeyError:
+ return None
+
+ def GetInstrumentationName(
+ self, default='android.test.InstrumentationTestRunner'):
+ """Returns the name of the Instrumentation in the apk."""
+ try:
+ manifest_info = self._GetManifest()
+ return manifest_info['manifest']['instrumentation']['android:name'][0]
perezju 2015/05/05 16:15:01 same here, only this line in the try-except block
jbudorick 2015/05/06 18:00:00 Done.
+ except KeyError:
+ return default
+
+ def GetPackageName(self):
+ """Returns the package name of the apk."""
+ if self._package_name:
+ return self._package_name
+
+ aapt_cmd = [_AAPT_PATH, 'dump', 'badging', self._apk_path]
+ aapt_output = cmd_helper.GetCmdOutput(aapt_cmd).split('\n')
+ package_name_re = re.compile(r'package: .*name=\'(\S*)\'')
+ for line in aapt_output:
+ m = package_name_re.match(line)
+ if m:
+ self._package_name = m.group(1)
+ return self._package_name
+ raise Exception('Failed to determine package name of %s' % self._apk_path)
+
+ def _GetManifest(self):
+ if not self._manifest:
+ self._manifest = _ParseManifestFromApk(self._apk_path)
+ return self._manifest
- try:
- manifest_info = _ParseManifestFromApk(apk_path)
- return manifest_info['manifest']['instrumentation']['android:name'][0]
- except KeyError:
- return default

Powered by Google App Engine
This is Rietveld 408576698