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 StringIO
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 = StringIO.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 = (
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  def test_all(self):
86    doc = minidom.parseString(self.test_config % ("foo.apk"))
87
88    test_config_fixer.overwrite_test_file_name(doc, "bar.apk")
89    output = StringIO.StringIO()
90    test_config_fixer.write_xml(output, doc)
91
92    # Only the matching package name in a test node should be updated.
93    expected = self.test_config % ("bar.apk")
94    self.assertEqual(expected, output.getvalue())
95
96
97if __name__ == '__main__':
98  unittest.main(verbosity=2)
99