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