• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Unit tests for test_config_fixer.py."""
18
19import io
20import sys
21import unittest
22from xml.dom import minidom
23
24import test_config_fixer
25
26sys.dont_write_bytecode = True
27
28
29class OverwritePackageNameTest(unittest.TestCase):
30  """ Unit tests for overwrite_package_name function """
31
32  manifest = (
33      '<?xml version="1.0" encoding="utf-8"?>\n'
34      '<manifest xmlns:android="http://schemas.android.com/apk/res/android"\n'
35      '    package="com.android.foo">\n'
36      '    <application>\n'
37      '    </application>\n'
38      '</manifest>\n')
39
40  test_config = (
41      '<?xml version="1.0" encoding="utf-8"?>\n'
42      '<configuration description="Runs some tests.">\n'
43      '    <option name="test-suite-tag" value="apct"/>\n'
44      '    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">\n'
45      '        <option name="package" value="%s"/>\n'
46      '    </target_preparer>\n'
47      '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n'
48      '        <option name="package" value="%s"/>\n'
49      '        <option name="runtime-hint" value="20s"/>\n'
50      '    </test>\n'
51      '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n'
52      '        <option name="package" value="%s"/>\n'
53      '        <option name="runtime-hint" value="15s"/>\n'
54      '    </test>\n'
55      '</configuration>\n')
56
57  def test_all(self):
58    doc = minidom.parseString(self.test_config % ("com.android.foo", "com.android.foo", "com.android.bar"))
59    manifest = minidom.parseString(self.manifest)
60
61    test_config_fixer.overwrite_package_name(doc, manifest, "com.soong.foo")
62    output = io.StringIO()
63    test_config_fixer.write_xml(output, doc)
64
65    # Only the matching package name in a test node should be updated.
66    expected = self.test_config % ("com.android.foo", "com.soong.foo", "com.android.bar")
67    self.assertEqual(expected, output.getvalue())
68
69
70class OverwriteTestFileNameTest(unittest.TestCase):
71  """ Unit tests for overwrite_test_file_name function """
72
73  test_config_test_app_install_setup = (
74      '<?xml version="1.0" encoding="utf-8"?>\n'
75      '<configuration description="Runs some tests.">\n'
76      '    <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">\n'
77      '        <option name="test-file-name" value="%s"/>\n'
78      '    </target_preparer>\n'
79      '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n'
80      '        <option name="package" value="com.android.foo"/>\n'
81      '        <option name="runtime-hint" value="20s"/>\n'
82      '    </test>\n'
83      '</configuration>\n')
84
85  test_config_suite_apk_installer = (
86      '<?xml version="1.0" encoding="utf-8"?>\n'
87      '<configuration description="Runs some tests.">\n'
88      '    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">\n'
89      '        <option name="test-file-name" value="%s"/>\n'
90      '    </target_preparer>\n'
91      '    <test class="com.android.tradefed.testtype.AndroidJUnitTest">\n'
92      '        <option name="package" value="com.android.foo"/>\n'
93      '        <option name="runtime-hint" value="20s"/>\n'
94      '    </test>\n'
95      '</configuration>\n')
96
97  def test_testappinstallsetup(self):
98    doc = minidom.parseString(self.test_config_test_app_install_setup % ("foo.apk"))
99
100    test_config_fixer.overwrite_test_file_name(doc, "bar.apk")
101    output = io.StringIO()
102    test_config_fixer.write_xml(output, doc)
103
104    # Only the matching package name in a test node should be updated.
105    expected = self.test_config_test_app_install_setup % ("bar.apk")
106    self.assertEqual(expected, output.getvalue())
107
108  def test_suiteapkinstaller(self):
109    doc = minidom.parseString(self.test_config_suite_apk_installer % ("foo.apk"))
110
111    test_config_fixer.overwrite_test_file_name(doc, "bar.apk")
112    output = io.StringIO()
113    test_config_fixer.write_xml(output, doc)
114
115    # Only the matching package name in a test node should be updated.
116    expected = self.test_config_suite_apk_installer % ("bar.apk")
117    self.assertEqual(expected, output.getvalue())
118
119
120if __name__ == '__main__':
121  unittest.main(verbosity=2)
122