diff --git a/java/client/src/org/openqa/selenium/firefox/internal/FileExtension.java b/java/client/src/org/openqa/selenium/firefox/internal/FileExtension.java index 5adfaa138864a..228ceede27114 100644 --- a/java/client/src/org/openqa/selenium/firefox/internal/FileExtension.java +++ b/java/client/src/org/openqa/selenium/firefox/internal/FileExtension.java @@ -17,6 +17,8 @@ package org.openqa.selenium.firefox.internal; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.io.FileHandler; import org.openqa.selenium.io.TemporaryFilesystem; @@ -30,6 +32,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; +import java.io.*; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; @@ -58,7 +61,7 @@ public void writeTo(File extensionsDir) throws IOException { File root = obtainRootDirectory(toInstall); - String id = readIdFromInstallRdf(root); + String id = getExtensionId(root); File extensionDirectory = new File(extensionsDir, id); @@ -87,6 +90,46 @@ private File obtainRootDirectory(File extensionToInstall) throws IOException { return root; } + private String getExtensionId(File root) { + File manifestJson = new File(root, "manifest.json"); + File installRdf = new File(root, "install.rdf"); + + if (installRdf.exists()) + return readIdFromInstallRdf(root); + else if (manifestJson.exists()) + return readIdFromManifestJson(root); + else + throw new WebDriverException( + "Extension should contain either install.rdf or manifest.json metadata file"); + + } + + private String readIdFromManifestJson(File root) { + final String MANIFEST_JSON_FILE = "manifest.json"; + File manifestJsonFile = new File(root, MANIFEST_JSON_FILE); + try { + String addOnId = null; + JsonObject manifestObject = new JsonParser().parse(new FileReader(manifestJsonFile)).getAsJsonObject(); + if (manifestObject.has("applications")) { + JsonObject applicationObj = manifestObject.getAsJsonObject("applications"); + if (applicationObj.has("gecko")) { + JsonObject geckoObj = applicationObj.getAsJsonObject("gecko"); + if (geckoObj.has("id")) { + addOnId = geckoObj.get("id").getAsString().trim(); + } + } + } + + if (addOnId == null || addOnId.isEmpty()) { + addOnId = manifestObject.get("name").getAsString().replaceAll(" ", "") + + "@" + manifestObject.get("version").getAsString(); + } + + return addOnId; + } catch (FileNotFoundException e1) { + throw new WebDriverException("Unable to file manifest.json in xpi file"); + } + } private String readIdFromInstallRdf(File root) { try { diff --git a/java/client/test/org/openqa/selenium/firefox/FirefoxProfileTest.java b/java/client/test/org/openqa/selenium/firefox/FirefoxProfileTest.java index 3411e8f1c670d..48d4e40cd2d3b 100644 --- a/java/client/test/org/openqa/selenium/firefox/FirefoxProfileTest.java +++ b/java/client/test/org/openqa/selenium/firefox/FirefoxProfileTest.java @@ -50,6 +50,7 @@ public class FirefoxProfileTest { private static final String FIREBUG_PATH = "third_party/firebug/firebug-1.5.0-fx.xpi"; private static final String FIREBUG_RESOURCE_PATH = "/org/openqa/selenium/testing/drivers/firebug-1.5.0-fx.xpi"; + private static final String MOOLTIPASS_PATH = "third_party/firebug/mooltipass-1.1.87.xpi"; private FirefoxProfile profile; @@ -159,6 +160,14 @@ public void shouldInstallExtensionFromZip() throws IOException { assertTrue(extensionDir.exists()); } + @Test + public void shouldInstallWebExtensionFromZip() throws IOException { + profile.addExtension(InProject.locate(MOOLTIPASS_PATH).toFile()); + File profileDir = profile.layoutOnDisk(); + File extensionDir = new File(profileDir, "extensions/MooltipassExtension@1.1.87"); + assertTrue(extensionDir.exists()); + } + @Test public void shouldInstallExtensionFromDirectory() throws IOException { File extension = InProject.locate(FIREBUG_PATH).toFile(); @@ -169,6 +178,16 @@ public void shouldInstallExtensionFromDirectory() throws IOException { assertTrue(extensionDir.exists()); } + @Test + public void shouldInstallWebExtensionFromDirectory() throws IOException { + File extension = InProject.locate(MOOLTIPASS_PATH).toFile(); + File unzippedExtension = Zip.unzipToTempDir(new FileInputStream(extension), "unzip", "stream"); + profile.addExtension(unzippedExtension); + File profileDir = profile.layoutOnDisk(); + File extensionDir = new File(profileDir, "extensions/MooltipassExtension@1.1.87"); + assertTrue(extensionDir.exists()); + } + @Test public void shouldInstallExtensionUsingClasspath() throws IOException { profile.addExtension(Firebug.class, FIREBUG_RESOURCE_PATH); diff --git a/third_party/firebug/mooltipass-1.1.87.xpi b/third_party/firebug/mooltipass-1.1.87.xpi new file mode 100644 index 0000000000000..a2853a2245a46 Binary files /dev/null and b/third_party/firebug/mooltipass-1.1.87.xpi differ