| 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 os |
| 6 |
import unittest |
| 7 |
|
| 8 |
import mozunit |
| 9 |
|
| 10 |
from mozpack.chrome.manifest import ( |
| 11 |
MANIFESTS_TYPES, |
| 12 |
Manifest, |
| 13 |
ManifestBinaryComponent, |
| 14 |
ManifestCategory, |
| 15 |
ManifestComponent, |
| 16 |
ManifestContent, |
| 17 |
ManifestContract, |
| 18 |
ManifestInterfaces, |
| 19 |
ManifestLocale, |
| 20 |
ManifestOverlay, |
| 21 |
ManifestOverride, |
| 22 |
ManifestResource, |
| 23 |
ManifestSkin, |
| 24 |
ManifestStyle, |
| 25 |
parse_manifest, |
| 26 |
parse_manifest_line, |
| 27 |
) |
| 28 |
from mozpack.errors import AccumulatedErrors, errors |
| 29 |
from test_errors import TestErrors |
| 30 |
|
| 31 |
|
| 32 |
class TestManifest(unittest.TestCase): |
| 33 |
def test_parse_manifest(self): |
| 34 |
manifest = [ |
| 35 |
"content global content/global/", |
| 36 |
"content global content/global/ application=foo application=bar" |
| 37 |
+ " platform", |
| 38 |
"locale global en-US content/en-US/", |
| 39 |
"locale global en-US content/en-US/ application=foo", |
| 40 |
"skin global classic/1.0 content/skin/classic/", |
| 41 |
"skin global classic/1.0 content/skin/classic/ application=foo" |
| 42 |
+ " os=WINNT", |
| 43 |
"", |
| 44 |
"manifest pdfjs/chrome.manifest", |
| 45 |
"resource gre-resources toolkit/res/", |
| 46 |
"override chrome://global/locale/netError.dtd" |
| 47 |
+ " chrome://browser/locale/netError.dtd", |
| 48 |
"# Comment", |
| 49 |
"component {b2bba4df-057d-41ea-b6b1-94a10a8ede68} foo.js", |
| 50 |
"contract @mozilla.org/foo;1" + " {b2bba4df-057d-41ea-b6b1-94a10a8ede68}", |
| 51 |
"interfaces foo.xpt", |
| 52 |
"binary-component bar.so", |
| 53 |
"category command-line-handler m-browser" |
| 54 |
+ " @mozilla.org/browser/clh;1" |
| 55 |
+ " application={ec8030f7-c20a-464f-9b0e-13a3a9e97384}", |
| 56 |
"style chrome://global/content/viewSource.xul" + " chrome://browser/skin/", |
| 57 |
"overlay chrome://global/content/viewSource.xul" |
| 58 |
+ " chrome://browser/content/viewSourceOverlay.xul", |
| 59 |
] |
| 60 |
other_manifest = ["content global content/global/"] |
| 61 |
expected_result = [ |
| 62 |
ManifestContent("", "global", "content/global/"), |
| 63 |
ManifestContent( |
| 64 |
"", |
| 65 |
"global", |
| 66 |
"content/global/", |
| 67 |
"application=foo", |
| 68 |
"application=bar", |
| 69 |
"platform", |
| 70 |
), |
| 71 |
ManifestLocale("", "global", "en-US", "content/en-US/"), |
| 72 |
ManifestLocale("", "global", "en-US", "content/en-US/", "application=foo"), |
| 73 |
ManifestSkin("", "global", "classic/1.0", "content/skin/classic/"), |
| 74 |
ManifestSkin( |
| 75 |
"", |
| 76 |
"global", |
| 77 |
"classic/1.0", |
| 78 |
"content/skin/classic/", |
| 79 |
"application=foo", |
| 80 |
"os=WINNT", |
| 81 |
), |
| 82 |
Manifest("", "pdfjs/chrome.manifest"), |
| 83 |
ManifestResource("", "gre-resources", "toolkit/res/"), |
| 84 |
ManifestOverride( |
| 85 |
"", |
| 86 |
"chrome://global/locale/netError.dtd", |
| 87 |
"chrome://browser/locale/netError.dtd", |
| 88 |
), |
| 89 |
ManifestComponent("", "{b2bba4df-057d-41ea-b6b1-94a10a8ede68}", "foo.js"), |
| 90 |
ManifestContract( |
| 91 |
"", "@mozilla.org/foo;1", "{b2bba4df-057d-41ea-b6b1-94a10a8ede68}" |
| 92 |
), |
| 93 |
ManifestInterfaces("", "foo.xpt"), |
| 94 |
ManifestBinaryComponent("", "bar.so"), |
| 95 |
ManifestCategory( |
| 96 |
"", |
| 97 |
"command-line-handler", |
| 98 |
"m-browser", |
| 99 |
"@mozilla.org/browser/clh;1", |
| 100 |
"application=" + "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}", |
| 101 |
), |
| 102 |
ManifestStyle( |
| 103 |
"", "chrome://global/content/viewSource.xul", "chrome://browser/skin/" |
| 104 |
), |
| 105 |
ManifestOverlay( |
| 106 |
"", |
| 107 |
"chrome://global/content/viewSource.xul", |
| 108 |
"chrome://browser/content/viewSourceOverlay.xul", |
| 109 |
), |
| 110 |
] |
| 111 |
with mozunit.MockedOpen( |
| 112 |
{ |
| 113 |
"manifest": "\n".join(manifest), |
| 114 |
"other/manifest": "\n".join(other_manifest), |
| 115 |
} |
| 116 |
): |
| 117 |
# Ensure we have tests for all types of manifests. |
| 118 |
self.assertEqual( |
| 119 |
set(type(e) for e in expected_result), set(MANIFESTS_TYPES.values()) |
| 120 |
) |
| 121 |
self.assertEqual( |
| 122 |
list(parse_manifest(os.curdir, "manifest")), expected_result |
| 123 |
) |
| 124 |
self.assertEqual( |
| 125 |
list(parse_manifest(os.curdir, "other/manifest")), |
| 126 |
[ManifestContent("other", "global", "content/global/")], |
| 127 |
) |
| 128 |
|
| 129 |
def test_manifest_rebase(self): |
| 130 |
m = parse_manifest_line("chrome", "content global content/global/") |
| 131 |
m = m.rebase("") |
| 132 |
self.assertEqual(str(m), "content global chrome/content/global/") |
| 133 |
m = m.rebase("chrome") |
| 134 |
self.assertEqual(str(m), "content global content/global/") |
| 135 |
|
| 136 |
m = parse_manifest_line("chrome/foo", "content global content/global/") |
| 137 |
m = m.rebase("chrome") |
| 138 |
self.assertEqual(str(m), "content global foo/content/global/") |
| 139 |
m = m.rebase("chrome/foo") |
| 140 |
self.assertEqual(str(m), "content global content/global/") |
| 141 |
|
| 142 |
m = parse_manifest_line("modules/foo", "resource foo ./") |
| 143 |
m = m.rebase("modules") |
| 144 |
self.assertEqual(str(m), "resource foo foo/") |
| 145 |
m = m.rebase("modules/foo") |
| 146 |
self.assertEqual(str(m), "resource foo ./") |
| 147 |
|
| 148 |
m = parse_manifest_line("chrome", "content browser browser/content/") |
| 149 |
m = m.rebase("chrome/browser").move("jar:browser.jar!").rebase("") |
| 150 |
self.assertEqual(str(m), "content browser jar:browser.jar!/content/") |
| 151 |
|
| 152 |
|
| 153 |
class TestManifestErrors(TestErrors, unittest.TestCase): |
| 154 |
def test_parse_manifest_errors(self): |
| 155 |
manifest = [ |
| 156 |
"skin global classic/1.0 content/skin/classic/ platform", |
| 157 |
"", |
| 158 |
"binary-component bar.so", |
| 159 |
"unsupported foo", |
| 160 |
] |
| 161 |
with mozunit.MockedOpen({"manifest": "\n".join(manifest)}): |
| 162 |
with self.assertRaises(AccumulatedErrors): |
| 163 |
with errors.accumulate(): |
| 164 |
list(parse_manifest(os.curdir, "manifest")) |
| 165 |
out = self.get_output() |
| 166 |
# Expecting 2 errors |
| 167 |
self.assertEqual(len(out), 2) |
| 168 |
path = os.path.abspath("manifest") |
| 169 |
# First on line 1 |
| 170 |
self.assertTrue(out[0].startswith("error: %s:1: " % path)) |
| 171 |
# Second on line 4 |
| 172 |
self.assertTrue(out[1].startswith("error: %s:4: " % path)) |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
mozunit.main() |