# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This file contains unit tests for the merge_manifests script."""
import re
import unittest
import xml.dom.minidom
from tools.android import merge_manifests
FIRST_MANIFEST = """
"""
SECOND_MANIFEST = """
"""
THIRD_MANIFEST = """
"""
MANUALLY_MERGED = """
"""
ALIAS_MANIFEST = """
"""
# This case exists when a library manifest relies on
# dependent manifests to provide required elements, i.e. a
INVALID_MERGER_MANIFEST = """
"""
VALID_MANIFEST = """
"""
def Reformat(string):
"""Reformat for comparison."""
string = re.compile(r'^[ \t]*\n?', re.MULTILINE).sub('', string)
return string
class MergeManifestsTest(unittest.TestCase):
"""Unit tests for the MergeManifest class."""
def testMerge(self):
self.maxDiff = None
merger = merge_manifests.MergeManifests(
(FIRST_MANIFEST, 'FIRST_MANIFEST'),
[(SECOND_MANIFEST, 'SECOND_MANIFEST'),
(THIRD_MANIFEST, 'THIRD_MANIFEST')],
['android.permission.READ_LOGS'])
result = merger.Merge()
expected = xml.dom.minidom.parseString(MANUALLY_MERGED).toprettyxml()
self.assertEquals(Reformat(expected), Reformat(result))
def testReformat(self):
text = ' a\n b\n\n\n \t c'
expected = 'a\nb\nc'
self.assertEquals(expected, Reformat(text))
def testValidateAndWarnPermissions(self):
permissions = ['android.permission.VIBRATE', 'android.permission.LAUGH']
warnings = merge_manifests._ValidateAndWarnPermissions(permissions)
self.assertTrue('android.permission.VIBRATE' not in warnings)
self.assertTrue('android.permission.LAUGH' in warnings)
def testExcludeAllPermissions(self):
merger = merge_manifests.MergeManifests(
(FIRST_MANIFEST, 'FIRST_MANIFEST'),
[(SECOND_MANIFEST, 'SECOND_MANIFEST'),
(THIRD_MANIFEST, 'THIRD_MANIFEST')],
['all'])
result = merger.Merge()
self.assertFalse('android.permission.READ_LOGS' in result)
self.assertFalse('android.permission.INTERNET' in result)
self.assertTrue('android.permission.ACCESS_COARSE_LOCATION' in result)
def testUndefinedArgumentPlaceholder(self):
bad_manifest = SECOND_MANIFEST.replace(
'${packageName}', '${unknownPlaceHolder}')
merger = merge_manifests.MergeManifests(
(FIRST_MANIFEST, 'FIRST_MANIFEST'),
[(bad_manifest, 'invalidManifest'),
(THIRD_MANIFEST, 'THIRD_MANIFEST')])
try:
merger.Merge()
self.fail('merging manifests with unknown placeholders didn\'t fail')
except merge_manifests.UndefinedPlaceholderException:
pass
def testActivityAliasesAreAlwaysLast(self):
merger = merge_manifests.MergeManifests(
(FIRST_MANIFEST, 'FIRST_MANIFEST'),
[(SECOND_MANIFEST, 'SECOND_MANIFEST'),
(ALIAS_MANIFEST, 'THIRD_MANIFEST')],
['all'])
result = merger.Merge()
last_occurence_of_activity = result.rfind('