#!/usr/bin/env python3
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import collections
import os
import sys
import unittest
sys.path.insert(1, os.path.join(os.path.dirname(__file__), '..'))
from util import manifest_utils
_TEST_MANIFEST = """\
{extra_intent_filter_elem}
"""
_TEST_MANIFEST_NORMALIZED = """\
# DIFF-ANCHOR: {intent_filter_diff_anchor}
{extra_intent_filter_elem}\
# DIFF-ANCHOR: {intent_filter_diff_anchor}
# DIFF-ANCHOR: {activity_diff_anchor}
# DIFF-ANCHOR: ddab3320
"""
_ACTIVITY_DIFF_ANCHOR = '32b3a641'
_INTENT_FILTER_DIFF_ANCHOR = '4ee601b7'
def _CreateTestData(intent_filter_diff_anchor=_INTENT_FILTER_DIFF_ANCHOR,
extra_activity_attr='',
extra_intent_filter_elem=''):
if extra_activity_attr:
extra_activity_attr += '\n '
if extra_intent_filter_elem:
extra_intent_filter_elem += '\n '
test_manifest = _TEST_MANIFEST.format(
extra_activity_attr=extra_activity_attr,
extra_intent_filter_elem=extra_intent_filter_elem)
expected = _TEST_MANIFEST_NORMALIZED.format(
activity_diff_anchor=_ACTIVITY_DIFF_ANCHOR,
intent_filter_diff_anchor=intent_filter_diff_anchor,
extra_activity_attr=extra_activity_attr,
extra_intent_filter_elem=extra_intent_filter_elem)
return test_manifest, expected
class ManifestUtilsTest(unittest.TestCase):
# Enable diff output.
maxDiff = None
def testNormalizeManifest_golden(self):
test_manifest, expected = _CreateTestData()
actual = manifest_utils.NormalizeManifest(test_manifest)
self.assertMultiLineEqual(expected, actual)
def testNormalizeManifest_nameUsedForActivity(self):
test_manifest, expected = _CreateTestData(extra_activity_attr='a="b"')
actual = manifest_utils.NormalizeManifest(test_manifest)
# Checks that the DIFF-ANCHOR does not change with the added attribute.
self.assertMultiLineEqual(expected, actual)
def testNormalizeManifest_nameNotUsedForIntentFilter(self):
test_manifest, expected = _CreateTestData(
extra_intent_filter_elem='', intent_filter_diff_anchor='5f5c8a70')
actual = manifest_utils.NormalizeManifest(test_manifest)
# Checks that the DIFF-ANCHOR does change with the added element despite
# having a nested element with an android:name set.
self.assertMultiLineEqual(expected, actual)
if __name__ == '__main__':
unittest.main()