1#!/usr/bin/env python3 2# Copyright 2020 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import collections 7import os 8import sys 9import unittest 10 11sys.path.insert(1, os.path.join(os.path.dirname(__file__), '..')) 12from util import manifest_utils 13 14_TEST_MANIFEST = """\ 15<?xml version="1.0" ?> 16<manifest package="test.pkg" 17 android:versionCode="1234" 18 android:versionName="1.2.33.4" 19 tools:ignore="MissingVersion" 20 xmlns:android="http://schemas.android.com/apk/res/android" 21 xmlns:tools="http://schemas.android.com/tools"> 22 <!-- Should be one line. --> 23 <uses-sdk android:minSdkVersion="24" 24 android:targetSdkVersion="30"/> 25 <!-- Should have attrs sorted--> 26 <uses-feature android:required="false" android:version="1" 27 android:name="android.hardware.vr.headtracking" /> 28 <!-- Should not be wrapped since < 100 chars. --> 29 <application 30 android:name="testname"> 31 <activity 32 {extra_activity_attr} 33 android:icon="@drawable/ic_devices_48dp" 34 android:label="label with spaces" 35 android:name="to be hashed" 36 android:theme="@style/Theme.Chromium.Activity.TranslucentNoAnimations"> 37 <intent-filter> 38 {extra_intent_filter_elem} 39 <action android:name="android.intent.action.SEND"/> 40 <category android:name="android.intent.category.DEFAULT"/> 41 <data android:mimeType="text/plain"/> 42 </intent-filter> 43 </activity> 44 <!-- Should be made non-self-closing. --> 45 <receiver android:exported="false" android:name="\ 46org.chromium.chrome.browser.announcement.AnnouncementNotificationManager$Rcvr"/> 47 </application> 48</manifest> 49""" 50 51_TEST_MANIFEST_NORMALIZED = """\ 52<?xml version="1.0" ?> 53<manifest 54 xmlns:android="http://schemas.android.com/apk/res/android" 55 xmlns:tools="http://schemas.android.com/tools" 56 package="test.pkg" 57 android:versionCode="OFFSET=4" 58 android:versionName="#.#.#.#" 59 tools:ignore="MissingVersion"> 60 <uses-feature android:name="android.hardware.vr.headtracking" \ 61android:required="false" android:version="1"/> 62 <uses-sdk android:minSdkVersion="24" android:targetSdkVersion="30"/> 63 <application android:name="testname"> 64 <activity # DIFF-ANCHOR: {activity_diff_anchor} 65 android:name="to be hashed" 66 {extra_activity_attr}android:icon="@drawable/ic_devices_48dp" 67 android:label="label with spaces" 68 android:theme="@style/Theme.Chromium.Activity.TranslucentNoAnimations"> 69 <intent-filter> # DIFF-ANCHOR: {intent_filter_diff_anchor} 70 {extra_intent_filter_elem}\ 71<action android:name="android.intent.action.SEND"/> 72 <category android:name="android.intent.category.DEFAULT"/> 73 <data android:mimeType="text/plain"/> 74 </intent-filter> # DIFF-ANCHOR: {intent_filter_diff_anchor} 75 </activity> # DIFF-ANCHOR: {activity_diff_anchor} 76 <receiver # DIFF-ANCHOR: ddab3320 77 android:name=\ 78"org.chromium.chrome.browser.announcement.AnnouncementNotificationManager$Rcvr" 79 android:exported="false"> 80 </receiver> # DIFF-ANCHOR: ddab3320 81 </application> 82</manifest> 83""" 84 85_ACTIVITY_DIFF_ANCHOR = '32b3a641' 86_INTENT_FILTER_DIFF_ANCHOR = '4ee601b7' 87 88 89def _CreateTestData(intent_filter_diff_anchor=_INTENT_FILTER_DIFF_ANCHOR, 90 extra_activity_attr='', 91 extra_intent_filter_elem=''): 92 if extra_activity_attr: 93 extra_activity_attr += '\n ' 94 if extra_intent_filter_elem: 95 extra_intent_filter_elem += '\n ' 96 test_manifest = _TEST_MANIFEST.format( 97 extra_activity_attr=extra_activity_attr, 98 extra_intent_filter_elem=extra_intent_filter_elem) 99 expected = _TEST_MANIFEST_NORMALIZED.format( 100 activity_diff_anchor=_ACTIVITY_DIFF_ANCHOR, 101 intent_filter_diff_anchor=intent_filter_diff_anchor, 102 extra_activity_attr=extra_activity_attr, 103 extra_intent_filter_elem=extra_intent_filter_elem) 104 return test_manifest, expected 105 106 107class ManifestUtilsTest(unittest.TestCase): 108 # Enable diff output. 109 maxDiff = None 110 111 def testNormalizeManifest_golden(self): 112 test_manifest, expected = _CreateTestData() 113 actual = manifest_utils.NormalizeManifest(test_manifest, 1230, None) 114 self.assertMultiLineEqual(expected, actual) 115 116 def testNormalizeManifest_nameUsedForActivity(self): 117 test_manifest, expected = _CreateTestData(extra_activity_attr='a="b"') 118 actual = manifest_utils.NormalizeManifest(test_manifest, 1230, None) 119 # Checks that the DIFF-ANCHOR does not change with the added attribute. 120 self.assertMultiLineEqual(expected, actual) 121 122 def testNormalizeManifest_nameNotUsedForIntentFilter(self): 123 test_manifest, expected = _CreateTestData( 124 extra_intent_filter_elem='<a/>', intent_filter_diff_anchor='5f5c8a70') 125 actual = manifest_utils.NormalizeManifest(test_manifest, 1230, None) 126 # Checks that the DIFF-ANCHOR does change with the added element despite 127 # having a nested element with an android:name set. 128 self.assertMultiLineEqual(expected, actual) 129 130 131if __name__ == '__main__': 132 unittest.main() 133