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 srcdir(tmpdir): |
| 37 """Source directory for building the package.""" |
| 38 srcdir = tmpdir.mkdir('src') |
| 39 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) |
| 40 for size in ['44', '50', '150']: |
| 41 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) |
| 42 path.write(size, ensure=True) |
| 43 localedir = srcdir.mkdir('_locales') |
| 44 en_us_dir = localedir.mkdir('en_US') |
| 45 en_us_dir.join('messages.json').write(MESSAGES_EN_US) |
| 46 return srcdir |
| 47 |
| 48 |
| 49 def blockmap2dict(xml_data): |
| 50 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
| 51 return { |
| 52 file.get('Name'): { |
| 53 'size': file.get('Size'), |
| 54 'lfhsize': file.get('LfhSize'), |
| 55 'blocks': [ |
| 56 {'hash': b.get('Hash'), 'size': b.get('Size', None)} |
| 57 for b in file |
| 58 ] |
| 59 } |
| 60 for file in ET.fromstring(xml_data) |
| 61 } |
| 62 |
| 63 |
| 64 def test_create_appx_blockmap(): |
| 65 files = packager.Files(set(), set()) |
| 66 files['foo.xml'] = CHARS |
| 67 files['foo/bar.png'] = CHARS * 200 |
| 68 blockmap = blockmap2dict(packagerEdge.create_appx_blockmap(files)) |
| 69 assert blockmap['foo.xml'] == { |
| 70 'size': '500', |
| 71 'lfhsize': '37', |
| 72 'blocks': [ |
| 73 {'hash': 'Vhwfmzss1Ney+j/ssR2QVISvFyMNBQeS2P+UjeE/di0=', |
| 74 'size': None} |
| 75 ] |
| 76 } |
| 77 assert blockmap['foo\\bar.png'] == { |
| 78 'size': '100000', |
| 79 'lfhsize': '41', |
| 80 'blocks': [ |
| 81 {'hash': 'KPW2SxeEikUEGhoKmKxruUSexKun0bGXMppOqUFrX5E=', |
| 82 'size': None}, |
| 83 {'hash': 'KQHnov1SZ1z34ttdDUjX2leYtpIIGndUVoUteieS2cw=', |
| 84 'size': None} |
| 85 ] |
| 86 } |
| 87 |
| 88 |
| 89 def ctm2dict(content_types_map): |
| 90 """Convert content type map to a dict.""" |
| 91 ret = {'defaults': {}, 'overrides': {}} |
| 92 for node in ET.fromstring(content_types_map): |
| 93 ct = node.get('ContentType') |
| 94 if node.tag.endswith('Default'): |
| 95 ret['defaults'][node.get('Extension')] = ct |
| 96 elif node.tag.endswith('Override'): |
| 97 ret['overrides'][node.get('PartName')] = ct |
| 98 else: |
| 99 raise ValueError('Unrecognised tag in content map: ' + node.tag) |
| 100 return ret |
| 101 |
| 102 |
| 103 def test_empty_content_types_map(): |
| 104 ctm_dict = ctm2dict(packagerEdge.create_content_types_map([])) |
| 105 assert ctm_dict['defaults'] == {} |
| 106 assert ctm_dict['overrides'] == {} |
| 107 |
| 108 |
| 109 def test_full_content_types_map(): |
| 110 filenames = ['no-extension', packagerEdge.MANIFEST, packagerEdge.BLOCKMAP] |
| 111 filenames += ['file.' + x for x in 'json html js png css git otf'.split()] |
| 112 ctm_dict = ctm2dict(packagerEdge.create_content_types_map(filenames)) |
| 113 assert ctm_dict['defaults'] == { |
| 114 'css': 'text/css', |
| 115 'html': 'text/html', |
| 116 'js': 'application/javascript', |
| 117 'json': 'application/json', |
| 118 'otf': 'application/x-font-otf', |
| 119 'png': 'image/png', |
| 120 'xml': 'application/xml' |
| 121 } |
| 122 assert ctm_dict['overrides'] == { |
| 123 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', |
| 124 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' |
| 125 } |
| 126 |
| 127 |
| 128 def test_create_appx_manifest(metadata): |
| 129 files = packager.Files(set(), set()) |
| 130 for size in ['44', '50', '150']: |
| 131 files['Assets/logo_{}.png'.format(size)] = CHARS |
| 132 files['Extension/_locales/en_US/messages.json'] = MESSAGES_EN_US |
| 133 manifest = packagerEdge.create_appx_manifest({'metadata': metadata}, files) |
| 134 with open(os.path.join(TEST_DIR, 'AppManifest.xml.expect')) as fp: |
| 135 manifest_expect = fp.read() |
| 136 assert manifest.strip() == manifest_expect.strip() |
| 137 |
| 138 |
| 139 def test_move_files_to_extension(): |
| 140 files = packager.Files(set(), set()) |
| 141 files['foo.xml'] = CHARS |
| 142 files['foo/bar.xml'] = CHARS |
| 143 files['Extension/foo.xml'] = CHARS |
| 144 packagerEdge.move_files_to_extension(files) |
| 145 assert set(files.keys()) == { |
| 146 'Extension/foo.xml', |
| 147 'Extension/foo/bar.xml', |
| 148 'Extension/Extension/foo.xml' |
| 149 } |
| 150 |
| 151 |
| 152 def test_create_build(tmpdir, srcdir): |
| 153 out_file = str(tmpdir.join('abp.appx')) |
| 154 packagerEdge.createBuild(str(srcdir), outFile=out_file) |
| 155 appx = zipfile.ZipFile(out_file) |
| 156 |
| 157 names = set(appx.namelist()) |
| 158 assert 'AppxManifest.xml' in names |
| 159 assert 'AppxBlockMap.xml' in names |
| 160 assert '[Content_Types].xml' in names |
| 161 |
| 162 assert appx.read('Assets/logo_44.png') == '44' |
| 163 assert appx.read('Extension/icons/abp-44.png') == '44' |
OLD | NEW |