blob: b255bee1684ee6a071ed5b6c33fda09e8c13856f [file] [log] [blame]
jingwen741dbc02017-12-19 09:30:45 -08001# Copyright 2017 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Unit tests for instrumentation_test_check."""
15
16import unittest
17
18from tools.android.instrumentation_test_check import _ExtractTargetPackageName
19from tools.android.instrumentation_test_check import _ExtractTargetPackageToInstrument
20from tools.android.instrumentation_test_check import _ValidateManifestPackageNames
21from tools.android.instrumentation_test_check import ManifestError
22
23INSTRUMENTATION_MANIFEST = """<?xml version="1.0" encoding="utf-8"?>
24<manifest xmlns:android="http://schemas.android.com/apk/res/android"
25 package="com.example.test" >
26 <instrumentation android:targetPackage="com.example"
27 android:name="android.support.test.runner.AndroidJUnitRunner"/>
28 <application android:label="Test"/>
29</manifest>
30"""
31
32INCORRECT_INSTRUMENTATION_MANIFEST = """<?xml version="1.0" encoding="utf-8"?>
33<manifest xmlns:android="http://schemas.android.com/apk/res/android"
34 package="com.example.test" >
35 <instrumentation android:targetPackage="not.com.example"
36 android:name="android.support.test.runner.AndroidJUnitRunner"/>
37 <application android:label="Test"/>
38</manifest>
39"""
40
41TARGET_MANIFEST = """<?xml version="2.0" encoding="utf-8"?>
42<manifest xmlns:android="http://schemas.android.com/apk/res/android"
43 package="com.example" >
44 <application android:label="App" />
45</manifest>
46"""
47
48
49class InstrumentationTestCheckTest(unittest.TestCase):
50
51 def test_extract_instrumentation_target_package(self):
52 self.assertEqual(
53 _ExtractTargetPackageToInstrument(INSTRUMENTATION_MANIFEST, ""),
54 "com.example")
55
56 def test_extract_target_package(self):
57 self.assertEqual(
58 _ExtractTargetPackageName(TARGET_MANIFEST, "unused"), "com.example")
59
60 def test_target_package_check(self):
61 self.assertEqual(
62 _ValidateManifestPackageNames(INSTRUMENTATION_MANIFEST, "unused",
63 TARGET_MANIFEST, "unused"),
64 ("com.example", "com.example"))
65
66 def test_target_package_check_failure(self):
67 with self.assertRaises(ManifestError):
68 _ValidateManifestPackageNames(INCORRECT_INSTRUMENTATION_MANIFEST,
69 "unused", TARGET_MANIFEST, "unused")
70
71
72if __name__ == "__main__":
73 unittest.main()