michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: const CC = Components.Constructor; michael@0: michael@0: Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); michael@0: Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm"); michael@0: michael@0: let gClient, gActor; michael@0: michael@0: function connect(onDone) { michael@0: // Initialize a loopback remote protocol connection michael@0: DebuggerServer.init(function () { return true; }); michael@0: // We need to register browser actors to have `listTabs` working michael@0: // and also have a root actor michael@0: DebuggerServer.addBrowserActors(); michael@0: michael@0: // Setup client and actor used in all tests michael@0: gClient = new DebuggerClient(DebuggerServer.connectPipe()); michael@0: gClient.connect(function onConnect() { michael@0: gClient.listTabs(function onListTabs(aResponse) { michael@0: gActor = aResponse.webappsActor; michael@0: onDone(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function webappActorRequest(request, onResponse) { michael@0: if (!gActor) { michael@0: connect(webappActorRequest.bind(null, request, onResponse)); michael@0: return; michael@0: } michael@0: michael@0: request.to = gActor; michael@0: gClient.request(request, onResponse); michael@0: } michael@0: michael@0: // Install a test packaged webapp from data folder michael@0: function installTestApp(zipName, appId, onDone) { michael@0: // Copy our package to tmp folder, where the actor retrieves it michael@0: let zip = do_get_file("data/" + zipName); michael@0: let appDir = FileUtils.getDir("TmpD", ["b2g", appId], true, true); michael@0: zip.copyTo(appDir, "application.zip"); michael@0: michael@0: let request = {type: "install", appId: appId}; michael@0: webappActorRequest(request, function (aResponse) { michael@0: do_check_eq(aResponse.appId, appId); michael@0: if ("error" in aResponse) { michael@0: do_throw("Error: " + aResponse.error); michael@0: } michael@0: if ("message" in aResponse) { michael@0: do_throw("Error message: " + aResponse.message); michael@0: } michael@0: do_check_false("error" in aResponse); michael@0: michael@0: onDone(); michael@0: }); michael@0: }; michael@0: michael@0: function setup() { michael@0: // We have to setup a profile, otherwise indexed db used by webapps michael@0: // will throw random exception when trying to get profile folder michael@0: do_get_profile(); michael@0: michael@0: // The webapps dir isn't registered on b2g xpcshell tests, michael@0: // we have to manually set it to the directory service. michael@0: do_get_webappsdir(); michael@0: michael@0: // We also need a valid nsIXulAppInfo service as Webapps.jsm is querying it michael@0: Components.utils.import("resource://testing-common/AppInfo.jsm"); michael@0: updateAppInfo(); michael@0: michael@0: // We have to toggle this flag in order to have apps being listed in getAll michael@0: // as only launchable apps are returned michael@0: Components.utils.import('resource://gre/modules/Webapps.jsm'); michael@0: DOMApplicationRegistry.allAppsLaunchable = true; michael@0: } michael@0: michael@0: function do_get_webappsdir() { michael@0: var webappsDir = Services.dirsvc.get("ProfD", Ci.nsILocalFile); michael@0: webappsDir.append("test_webapps"); michael@0: if (!webappsDir.exists()) michael@0: webappsDir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("755", 8)); michael@0: michael@0: var coreAppsDir = Services.dirsvc.get("ProfD", Ci.nsILocalFile); michael@0: coreAppsDir.append("test_coreapps"); michael@0: if (!coreAppsDir.exists()) michael@0: coreAppsDir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("755", 8)); michael@0: michael@0: // Register our own provider for the profile directory. michael@0: // It will return our special docshell profile directory. michael@0: var provider = { michael@0: getFile: function(prop, persistent) { michael@0: persistent.value = true; michael@0: if (prop == "webappsDir") { michael@0: return webappsDir.clone(); michael@0: } michael@0: else if (prop == "coreAppsDir") { michael@0: return coreAppsDir.clone(); michael@0: } michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: }, michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsIDirectoryServiceProvider) || michael@0: iid.equals(Ci.nsISupports)) { michael@0: return this; michael@0: } michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }; michael@0: Services.dirsvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider); michael@0: } michael@0: michael@0: