OLD | NEW |
1 # This Source Code Form is subject to the terms of the Mozilla Public | 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 | 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/. | 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
5 import ConfigParser | |
6 import json | |
7 import os | 5 import os |
8 import shutil | |
9 import xml.etree.ElementTree as ET | |
10 import zipfile | |
11 | |
12 import pytest | 6 import pytest |
13 | 7 |
14 from buildtools import packager, packagerEdge | 8 import xml.etree.ElementTree as ET |
15 | 9 |
16 TEST_DIR = os.path.dirname(__file__) | 10 from buildtools.tests.tools import run_webext_build |
17 TEST_METADATA = os.path.join(TEST_DIR, 'metadata.edge') | 11 from buildtools.tests.tools import locale_files |
18 CHARS = b''.join(chr(i % 200 + 30) for i in range(500)) | 12 from buildtools.tests.tools import copy_metadata |
19 MESSAGES_EN_US = json.dumps({ | 13 from buildtools.tests.tools import ZipContent |
20 'name': {'message': 'Adblock Plus'}, | 14 from buildtools.tests.tools import assert_manifest_content |
21 'name_devbuild': {'message': 'devbuild-marker'}, | 15 from buildtools.tests.tools import assert_all_locales_present |
22 'description': { | 16 from buildtools.tests.conftest import ALL_LANGUAGES |
23 'message': 'Adblock Plus is the most popular ad blocker ever, ' | 17 from buildtools import packager |
24 'and also supports websites by not blocking ' | 18 from buildtools import packagerEdge |
25 'unobstrusive ads by default (configurable).' | |
26 }, | |
27 }) | |
28 | 19 |
29 | 20 |
30 @pytest.fixture | 21 @pytest.fixture |
31 def metadata(): | 22 def locale_files_edge(tmpdir): |
32 """Loaded metadata config.""" | 23 return locale_files(ALL_LANGUAGES, '_locales', tmpdir) |
33 conf_parser = ConfigParser.ConfigParser() | 24 |
34 conf_parser.read(TEST_METADATA) | 25 |
35 return conf_parser | 26 @pytest.fixture |
| 27 def edge_metadata(tmpdir): |
| 28 filename = 'metadata.edge' |
| 29 copy_metadata(filename, tmpdir) |
| 30 |
| 31 return packager.readMetadata(str(tmpdir), 'edge') |
36 | 32 |
37 | 33 |
38 @pytest.fixture | 34 @pytest.fixture |
39 def files(): | 35 def files(): |
40 """Minimal Files() for testing manifest and blockmap.""" | 36 """Minimal Files() for testing blockmap.""" |
| 37 str500 = b''.join(chr(i % 200 + 30) for i in range(500)) |
41 files = packager.Files(set(), set()) | 38 files = packager.Files(set(), set()) |
42 for size in ['44', '50', '150']: | 39 files['Extension/foo.xml'] = str500 |
43 files['Assets/logo_{}.png'.format(size)] = CHARS | 40 files['Extension/bar.png'] = str500 * 200 |
44 files['Extension/_locales/en_US/messages.json'] = MESSAGES_EN_US | |
45 files['Extension/foo.xml'] = CHARS | |
46 files['Extension/bar.png'] = CHARS * 200 | |
47 return files | 41 return files |
48 | 42 |
49 | 43 |
50 @pytest.fixture | |
51 def srcdir(tmpdir): | |
52 """Source directory for building the package.""" | |
53 srcdir = tmpdir.mkdir('src') | |
54 shutil.copy(TEST_METADATA, str(srcdir.join('metadata.edge'))) | |
55 for size in ['44', '50', '150']: | |
56 path = srcdir.join('chrome', 'icons', 'abp-{}.png'.format(size)) | |
57 path.write(size, ensure=True) | |
58 localedir = srcdir.mkdir('_locales') | |
59 en_us_dir = localedir.mkdir('en_US') | |
60 en_us_dir.join('messages.json').write(MESSAGES_EN_US) | |
61 return srcdir | |
62 | |
63 | |
64 def blockmap2dict(xml_data): | 44 def blockmap2dict(xml_data): |
65 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" | 45 """Convert AppxBlockMap.xml to a dict of dicts easier to inspect.""" |
66 return { | 46 return { |
67 file.get('Name'): { | 47 file.get('Name'): { |
68 'size': file.get('Size'), | 48 'size': file.get('Size'), |
69 'lfhsize': file.get('LfhSize'), | 49 'lfhsize': file.get('LfhSize'), |
70 'blocks': [b.get('Hash') for b in file] | 50 'blocks': [b.get('Hash') for b in file] |
71 } | 51 } |
72 for file in ET.fromstring(xml_data) | 52 for file in ET.fromstring(xml_data) |
73 } | 53 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 'otf': 'application/octet-stream', | 102 'otf': 'application/octet-stream', |
123 'png': 'image/png', | 103 'png': 'image/png', |
124 'xml': 'application/xml' | 104 'xml': 'application/xml' |
125 } | 105 } |
126 assert ctm_dict['overrides'] == { | 106 assert ctm_dict['overrides'] == { |
127 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', | 107 '/AppxBlockMap.xml': 'application/vnd.ms-appx.blockmap+xml', |
128 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' | 108 '/AppxManifest.xml': 'application/vnd.ms-appx.manifest+xml' |
129 } | 109 } |
130 | 110 |
131 | 111 |
132 def test_create_appx_manifest(metadata, files): | 112 @pytest.mark.usefixtures('locale_files_edge') |
133 namespaces = { | 113 @pytest.mark.parametrize('build_release', ['release', 'build']) |
134 'ns': 'http://schemas.microsoft.com/' | 114 def test_build_edge(build_release, tmpdir, srcdir, edge_metadata): |
135 'appx/manifest/foundation/windows10', | 115 from buildtools import packagerEdge |
136 'uap': 'http://schemas.microsoft.com/appx/manifest/uap/windows10', | 116 release = build_release == 'release' |
137 'uap3': 'http://schemas.microsoft.com/appx/manifest/uap/windows10/3', | |
138 } | |
139 | 117 |
140 def first(elem): | 118 run_webext_build('edge', build_release, srcdir, packagerEdge) |
141 return elem[0] | |
142 | 119 |
143 def text(elem): | 120 if release: |
144 return elem.text | 121 out_file = 'adblockplusedge-1.2.3.appx' |
| 122 else: |
| 123 out_file = 'adblockplusedge-1.2.3.0.appx' |
145 | 124 |
146 def attr(attr): | 125 with ZipContent(out_file) as package: |
147 def wrapper(elem): | 126 filenames = set(package.namelist()) |
148 return elem.attrib[attr] | |
149 return wrapper | |
150 | 127 |
151 base = [ | 128 assert_all_locales_present(package, 'Extension/_locales') |
152 ('.//*', [len], 21.0), | |
153 ('./ns:Identity', [first, attr('Publisher')], | |
154 'CN=4F066043-8AFE-41C9-B762-6C15E77E3F88'), | |
155 ('./ns:Identity', [first, attr('Version')], '1.2.3.0'), | |
156 ('./ns:Properties/ns:PublisherDisplayName', [first, text], | |
157 'Eyeo GmbH'), | |
158 ('./ns:Properties/ns:Logo', [first, text], 'Assets\\logo_50.png'), | |
159 ('./ns:Dependencies/ns:TargetDeviceFamily', | |
160 [first, attr('MinVersion')], | |
161 '10.0.14332.0'), | |
162 ('./ns:Dependencies/ns:TargetDeviceFamily', | |
163 [first, attr('MaxVersionTested')], | |
164 '12.0.0.0'), | |
165 ('./ns:Applications/ns:Application/uap:VisualElements', | |
166 [first, attr('Square150x150Logo')], | |
167 'Assets\\logo_150.png'), | |
168 ('./ns:Applications/ns:Application/uap:VisualElements', | |
169 [first, attr('Square44x44Logo')], | |
170 'Assets\\logo_44.png'), | |
171 ('./ns:Applications/ns:Application/uap:VisualElements', | |
172 [first, attr('Description')], | |
173 'Adblock Plus is the most popular ad blocker ever, and also ' | |
174 'supports websites by not blocking unobstrusive ads by ' | |
175 'default (configurable).'), | |
176 ('./ns:Applications/ns:Application/uap:VisualElements', | |
177 [first, attr('BackgroundColor')], | |
178 'red'), | |
179 ] | |
180 | 129 |
181 devbuild = base + [ | 130 assert 'AppxManifest.xml' in filenames |
182 ('./ns:Identity', [first, attr('Name')], | 131 assert 'AppxBlockMap.xml' in filenames |
183 'EyeoGmbH.AdblockPlusdevelopmentbuild'), | 132 assert '[Content_Types].xml' in filenames |
184 ('./ns:Properties/ns:DisplayName', [first, text], 'devbuild-marker'), | |
185 ('./ns:Applications/ns:Application/uap:VisualElements', | |
186 [first, attr('DisplayName')], | |
187 'devbuild-marker'), | |
188 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
189 'uap3:AppExtension', | |
190 [first, attr('Id')], | |
191 'EdgeExtension'), | |
192 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
193 'uap3:AppExtension', | |
194 [first, attr('DisplayName')], | |
195 'devbuild-marker'), | |
196 ] | |
197 | 133 |
198 release = base + [ | 134 assert package.read('Assets/logo_44.png') == '44' |
199 ('./ns:Identity', [first, attr('Name')], 'EyeoGmbH.AdblockPlus'), | 135 assert package.read('Extension/icons/abp-44.png') == '44' |
200 ('./ns:Properties/ns:DisplayName', [first, text], 'Adblock Plus'), | |
201 ('./ns:Applications/ns:Application/uap:VisualElements', | |
202 [first, attr('DisplayName')], | |
203 'Adblock Plus'), | |
204 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
205 'uap3:AppExtension', | |
206 [first, attr('Id')], | |
207 '1.0'), | |
208 ('./ns:Applications/ns:Application/ns:Extensions/uap3:Extension/' | |
209 'uap3:AppExtension', | |
210 [first, attr('DisplayName')], | |
211 'Adblock Plus'), | |
212 ] | |
213 | 136 |
214 for release_build, pairs in [(False, devbuild), (True, release)]: | 137 if release: |
215 manifest = ET.fromstring(packagerEdge.create_appx_manifest( | 138 filename = 'manifest_edge_True.xml' |
216 {'metadata': metadata}, | 139 else: |
217 files, | 140 filename = 'manifest_edge_False.xml' |
218 release_build=release_build)) | |
219 for expression, modifiers, value in pairs: | |
220 res = reduce( | |
221 lambda val, func: func(val), | |
222 modifiers, | |
223 manifest.findall(expression, namespaces=namespaces)) | |
224 assert res == value | |
225 | 141 |
226 | 142 expected = os.path.join( |
227 def test_move_files_to_extension(): | 143 os.path.dirname(__file__), |
228 files = packager.Files(set(), set()) | 144 'expecteddata', |
229 files['foo.xml'] = CHARS | 145 filename, |
230 files['foo/bar.xml'] = CHARS | 146 ) |
231 files['Extension/foo.xml'] = CHARS | 147 assert_manifest_content(package.read('AppxManifest.xml'), expected) |
232 packagerEdge.move_files_to_extension(files) | |
233 assert set(files.keys()) == { | |
234 'Extension/foo.xml', | |
235 'Extension/foo/bar.xml', | |
236 'Extension/Extension/foo.xml' | |
237 } | |
238 | |
239 | |
240 def test_create_build(tmpdir, srcdir): | |
241 out_file = str(tmpdir.join('abp.appx')) | |
242 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=True) | |
243 appx = zipfile.ZipFile(out_file) | |
244 | |
245 names = set(appx.namelist()) | |
246 assert 'AppxManifest.xml' in names | |
247 assert 'AppxBlockMap.xml' in names | |
248 assert '[Content_Types].xml' in names | |
249 | |
250 assert 'devbuild-marker' not in appx.read('AppxManifest.xml') | |
251 assert appx.read('Assets/logo_44.png') == '44' | |
252 assert appx.read('Extension/icons/abp-44.png') == '44' | |
253 | |
254 | |
255 def test_create_devbuild(tmpdir, srcdir): | |
256 out_file = str(tmpdir.join('abp.appx')) | |
257 packagerEdge.createBuild(str(srcdir), outFile=out_file, releaseBuild=False) | |
258 appx = zipfile.ZipFile(out_file) | |
259 assert 'devbuild-marker' in appx.read('AppxManifest.xml') | |
OLD | NEW |