# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import unittest
import zipfile
import pprint
import shutil
import simplejson as json
from cuddlefish import xpi, packaging, manifest, buildJID
from cuddlefish.tests import test_packaging
from test_linker import up
import xml.etree.ElementTree as ElementTree
xpi_template_path = os.path.join(test_packaging.static_files_path,
'xpi-template')
fake_manifest = ''
class PrefsTests(unittest.TestCase):
def makexpi(self, pkg_name):
self.xpiname = "%s.xpi" % pkg_name
create_xpi(self.xpiname, pkg_name, 'preferences-files')
self.xpi = zipfile.ZipFile(self.xpiname, 'r')
options = self.xpi.read('harness-options.json')
self.xpi_harness_options = json.loads(options)
def setUp(self):
self.xpiname = None
self.xpi = None
def tearDown(self):
if self.xpi:
self.xpi.close()
if self.xpiname and os.path.exists(self.xpiname):
os.remove(self.xpiname)
def testPackageWithSimplePrefs(self):
self.makexpi('simple-prefs')
packageName = 'jid1-fZHqN9JfrDBa8A@jetpack'
self.failUnless('options.xul' in self.xpi.namelist())
optsxul = self.xpi.read('options.xul').decode("utf-8")
self.failUnlessEqual(self.xpi_harness_options["jetpackID"], packageName)
self.failUnlessEqual(self.xpi_harness_options["preferencesBranch"], packageName)
root = ElementTree.XML(optsxul.encode('utf-8'))
xulNamespacePrefix = \
"{http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul}"
settings = root.findall(xulNamespacePrefix + 'setting')
def assertPref(setting, name, prefType, title):
self.failUnlessEqual(setting.get('data-jetpack-id'), packageName)
self.failUnlessEqual(setting.get('pref'),
'extensions.' + packageName + '.' + name)
self.failUnlessEqual(setting.get('pref-name'), name)
self.failUnlessEqual(setting.get('type'), prefType)
self.failUnlessEqual(setting.get('title'), title)
assertPref(settings[0], 'test', 'bool', u't\u00EBst')
assertPref(settings[1], 'test2', 'string', u't\u00EBst')
assertPref(settings[2], 'test3', 'menulist', '"> 75:
# Ideally we would json-decode this, but it results
# in an annoying 'u' before every string literal,
# since json decoding makes all strings unicode.
contents = eval(contents)
contents = pprint.pformat(contents)
lines = contents.splitlines()
contents = "\n ".join(lines)
print "%s:\n %s" % (normpath(name), contents)
zip.close()
def document_dir_files(path):
filename_contents_tuples = []
for dirpath, dirnames, filenames in os.walk(path):
relpath = dirpath[len(path)+1:]
for filename in filenames:
abspath = os.path.join(dirpath, filename)
contents = open(abspath, 'r').read()
contents = "\n ".join(contents.splitlines())
relfilename = os.path.join(relpath, filename)
filename_contents_tuples.append((normpath(relfilename), contents))
filename_contents_tuples.sort()
for filename, contents in filename_contents_tuples:
print "%s:" % filename
print " %s" % contents
def create_xpi(xpiname, pkg_name='aardvark', dirname='static-files',
extra_harness_options={}):
configs = test_packaging.get_configs(pkg_name, dirname)
options = {'main': configs.target_cfg.main,
'jetpackID': buildJID(configs.target_cfg), }
options.update(configs.build)
xpi.build_xpi(template_root_dir=xpi_template_path,
manifest=fake_manifest,
xpi_path=xpiname,
harness_options=options,
extra_harness_options=extra_harness_options)
if __name__ == '__main__':
unittest.main()