michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/NetUtil.jsm"); michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/FileUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/Promise.jsm"); michael@0: Components.utils.import("resource://testing-common/AppInfo.jsm"); michael@0: michael@0: const BROWSER_SEARCH_PREF = "browser.search."; michael@0: const NS_APP_SEARCH_DIR = "SrchPlugns"; michael@0: michael@0: const MODE_RDONLY = FileUtils.MODE_RDONLY; michael@0: const MODE_WRONLY = FileUtils.MODE_WRONLY; michael@0: const MODE_CREATE = FileUtils.MODE_CREATE; michael@0: const MODE_TRUNCATE = FileUtils.MODE_TRUNCATE; michael@0: michael@0: // Need to create and register a profile folder. michael@0: var gProfD = do_get_profile(); michael@0: michael@0: function dumpn(text) michael@0: { michael@0: dump("search test: " + text + "\n"); michael@0: } michael@0: michael@0: /** michael@0: * Clean the profile of any metadata files left from a previous run. michael@0: */ michael@0: function removeMetadata() michael@0: { michael@0: let file = gProfD.clone(); michael@0: file.append("search-metadata.json"); michael@0: if (file.exists()) { michael@0: file.remove(false); michael@0: } michael@0: michael@0: file = gProfD.clone(); michael@0: file.append("search.sqlite"); michael@0: if (file.exists()) { michael@0: file.remove(false); michael@0: } michael@0: } michael@0: michael@0: function removeCacheFile() michael@0: { michael@0: let file = gProfD.clone(); michael@0: file.append("search.json"); michael@0: if (file.exists()) { michael@0: file.remove(false); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Clean the profile of any cache file left from a previous run. michael@0: */ michael@0: function removeCache() michael@0: { michael@0: let file = gProfD.clone(); michael@0: file.append("search.json"); michael@0: if (file.exists()) { michael@0: file.remove(false); michael@0: } michael@0: michael@0: } michael@0: michael@0: /** michael@0: * Run some callback once metadata has been committed to disk. michael@0: */ michael@0: function afterCommit(callback) michael@0: { michael@0: let obs = function(result, topic, verb) { michael@0: if (verb == "write-metadata-to-disk-complete") { michael@0: Services.obs.removeObserver(obs, topic); michael@0: callback(result); michael@0: } else { michael@0: dump("TOPIC: " + topic+ "\n"); michael@0: } michael@0: } michael@0: Services.obs.addObserver(obs, "browser-search-service", false); michael@0: } michael@0: michael@0: /** michael@0: * Run some callback once cache has been built. michael@0: */ michael@0: function afterCache(callback) michael@0: { michael@0: let obs = function(result, topic, verb) { michael@0: do_print("afterCache: " + verb); michael@0: if (verb == "write-cache-to-disk-complete") { michael@0: Services.obs.removeObserver(obs, topic); michael@0: callback(result); michael@0: } else { michael@0: dump("TOPIC: " + topic+ "\n"); michael@0: } michael@0: } michael@0: Services.obs.addObserver(obs, "browser-search-service", false); michael@0: } michael@0: michael@0: function parseJsonFromStream(aInputStream) { michael@0: const json = Cc["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON); michael@0: const data = json.decodeFromStream(aInputStream, aInputStream.available()); michael@0: return data; michael@0: } michael@0: michael@0: /** michael@0: * Read a JSON file and return the JS object michael@0: */ michael@0: function readJSONFile(aFile) { michael@0: let stream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: try { michael@0: stream.init(aFile, MODE_RDONLY, FileUtils.PERMS_FILE, 0); michael@0: return parseJsonFromStream(stream, stream.available()); michael@0: } catch(ex) { michael@0: dumpn("readJSONFile: Error reading JSON file: " + ex); michael@0: } finally { michael@0: stream.close(); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * Recursively compare two objects and check that every property of expectedObj has the same value michael@0: * on actualObj. michael@0: */ michael@0: function isSubObjectOf(expectedObj, actualObj) { michael@0: for (let prop in expectedObj) { michael@0: if (expectedObj[prop] instanceof Object) { michael@0: do_check_eq(expectedObj[prop].length, actualObj[prop].length); michael@0: isSubObjectOf(expectedObj[prop], actualObj[prop]); michael@0: } else { michael@0: do_check_eq(expectedObj[prop], actualObj[prop]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Expand the amount of information available in error logs michael@0: Services.prefs.setBoolPref("browser.search.log", true); michael@0: