OLD | NEW |
(Empty) | |
| 1 # This Source Code Form is subject to the terms of the Mozilla Public |
| 2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
| 5 import ConfigParser |
| 6 import json |
| 7 import os |
| 8 import shutil |
| 9 import xml.etree.ElementTree as ET |
| 10 import zipfile |
| 11 |
| 12 import pytest |
| 13 |
| 14 from buildtools import packager, packagerEdge |
| 15 |
| 16 TEST_DIR = os.path.dirname(__file__) |
| 17 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') |
| 18 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) |
| 19 MESSAGES_EN_US = json.dumps({ |
| 20 'name': 'Adblock Plus', |
| 21 'description': 'Adblock Plus is the most popular ad blocker ever, ' |
| 22 'and also supports websites by not blocking ' |
| 23 'unobstrusive ads by default (configurable).' |
| 24 }) |
| 25 |
| 26 |
| 27 @pytest.fixture |
| 28 def metadata(): |
| 29 """Loaded metadata config.""" |
| 30 conf_parser = ConfigParser.ConfigParser() |
| 31 conf_parser.read(TEST_METADATA) |
| 32 return conf_parser |
| 33 |
| 34 |
| 35 @pytest.fixture |
| 36 def files(): |
| 37 """Minimal Files() for testing manifest and blockmap.""" |
| 38 files = packager.Files(set(), set()) |
| 39 for size in ['44', '50', '150']: |
| 40 files['Assets/logo_{}.png'.format(size)] = CHARS |
| 41 files['Extension/_locales/en_US/messages.json'] = MESSAGES_EN_US |
| 42 files['Extension/foo.xml'] = CHARS |
| 43 files['Extension/bar.png'] = CHARS * 200 |
| 44 return files |
| 45 |
| 46 |
| 47 @pytest.fixture |
| 48 def srcdir(tmpdir): |
| 49 """Source directory for building the package.""" |
| 50 srcdir = tmpdir.mkdir('src') |
| 51 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) |
| 52 for size in ['44', '50', '150']: |
| 53 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) |
| 54 path.write(size, ensure=True) |
| 55 localedir = srcdir.mkdir('_locales') |
| 56 en_us_dir = localedir.mkdir('en_US') |
| 57 en_us_dir.join('messages.json').write(MESSAGES_EN_US) |
| 58 return srcdir |
| 59 |
| 60 |
| 61 def blockmap2dict(xml_data): |
| 62 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
| 63 return { |
| 64 file.get('Name'): { |
| 65 'size': file.get('Size'), |
| 66 'lfhsize': file.get('LfhSize'), |
| 67 'blocks': [b.get('Hash') for b in file] |
| 68 } |
| 69 for file in ET.fromstring(xml_data) |
| 70 } |
| 71 |
| 72 |
| 73 def test_create_appx_blockmap(files): |
| 74 blockmap = blockmap2dict(packagerEdge.create_appx_blockmap(files)) |
| 75 assert blockmap['Extension\\foo.xml'] == { |
| 76 'size': '500', |
| 77 'lfhsize': '47', |
| 78 'blocks': ['Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0='] |
| 79 } |
| 80 assert blockmap['Extension\\bar.png'] == { |
| 81 'size': '100000', |
| 82 'lfhsize': '47', |
| 83 'blocks': [ |
| 84 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', |
| 85 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', |
| 86 ] |
| 87 } |
| 88 |
| 89 |
| 90 def ctm2dict(content_types_map): |
| 91 """Convert content type map to a dict.""" |
| 92 ret = {'defaults': {}, 'overrides': {}} |
| 93 for node in ET.fromstring(content_types_map): |
| 94 ct = node.get('ContentType') |
| 95 if node.tag.endswith('Default'): |
| 96 ret['defaults'][node.get('Extension')] = ct |
| 97 elif node.tag.endswith('Override'): |
| 98 ret['overrides'][node.get('PartName')] = ct |
| 99 else: |
| 100 raise ValueError('Unrecognised tag in content map: ' + node.tag) |
| 101 return ret |
| 102 |
| 103 |
| 104 def test_empty_content_types_map(): |
| 105 ctm_dict = ctm2dict(packagerEdge.create_content_types_map([])) |
| 106 assert ctm_dict['defaults'] == {} |
| 107 assert ctm_dict['overrides'] == {} |
| 108 |
| 109 |
| 110 def test_full_content_types_map(): |
| 111 filenames = ['no-extension', packagerEdge.MANIFEST, packagerEdge.BLOCKMAP] |
| 112 filenames += ['file.' + x for x in 'json html js png css git otf'.split()] |
| 113 ctm_dict = ctm2dict(packagerEdge.create_content_types_map(filenames)) |
| 114 assert ctm_dict['defaults'] == { |
| 115 'css': 'text/css', |
| 116 'html': 'text/html', |
| 117 'js': 'application/javascript', |
| 118 'json': 'application/json', |
| 119 'otf': 'application/x-font-otf', |
| 120 'png': 'image/png', |
| 121 'xml': 'application/xml' |
| 122 } |
| 123 assert ctm_dict['overrides'] == { |
| 124 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', |
| 125 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' |
| 126 } |
| 127 |
| 128 |
| 129 def test_create_appx_manifest(metadata, files): |
| 130 manifest = packagerEdge.create_appx_manifest({'metadata': metadata}, files) |
| 131 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: |
| 132 manifest_expect = fp.read() |
| 133 assert manifest.strip() == manifest_expect.strip() |
| 134 |
| 135 |
| 136 def test_move_files_to_extension(): |
| 137 files = packager.Files(set(), set()) |
| 138 files['foo.xml'] = CHARS |
| 139 files['foo/bar.xml'] = CHARS |
| 140 files['Extension/foo.xml'] = CHARS |
| 141 packagerEdge.move_files_to_extension(files) |
| 142 assert set(files.keys()) == { |
| 143 'Extension/foo.xml', |
| 144 'Extension/foo/bar.xml', |
| 145 'Extension/Extension/foo.xml' |
| 146 } |
| 147 |
| 148 |
| 149 def test_create_build(tmpdir, srcdir): |
| 150 out_file = str(tmpdir.join('abp.appx')) |
| 151 packagerEdge.createBuild(str(srcdir), outFile=out_file) |
| 152 appx = zipfile.ZipFile(out_file) |
| 153 |
| 154 names = set(appx.namelist()) |
| 155 assert 'AppxManifest.xml' in names |
| 156 assert 'AppxBlockMap.xml' in names |
| 157 assert '[Content_Types].xml' in names |
| 158 |
| 159 assert appx.read('Assets/logo_44.png') == '44' |
| 160 assert appx.read('Extension/icons/abp-44.png') == '44' |
OLD | NEW |