package net.jsourcerer.webdriver.jserrorcollector;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


/**
 * Tests with Firebug ensuring that we get JS errors & console content.
 * @author Marc Guillemot
 * @version $Revision:  $
 */
public class FirebugConsoleTest {
	private static WebDriver webDriver;


	@BeforeClass
	public static void setup() throws Exception {
		FirefoxProfile ffProfile = new FirefoxProfile();
		ffProfile.addExtension(new File("firefox")); // assuming that the test is started in project's root
		ffProfile.addExtension(FirebugConsoleTest.class, "/firebug-1.11.4-fx.xpi");

		ffProfile.setPreference("extensions.firebug.showStackTrace", "true");
		ffProfile.setPreference("extensions.firebug.delayLoad", "false");
		ffProfile.setPreference("extensions.firebug.showFirstRunPage", "false");
		ffProfile.setPreference("extensions.firebug.allPagesActivation", "on");
		ffProfile.setPreference("extensions.firebug.console.enableSites", "true");
		ffProfile.setPreference("extensions.firebug.defaultPanelName", "console");

		webDriver = new FirefoxDriver(ffProfile);
	}

	@AfterClass
	public static void tearDown() {
		webDriver.quit();
	}

	/**
	 *
	 */
	@Test
	public void simple() throws Exception {
		final String url = getResource("withConsoleOutput.html");
		webDriver.get(url);

		final JavaScriptError errorSimpleHtml = new JavaScriptError("TypeError: null has no properties", url, 8, "before JS error");
		
		final List<JavaScriptError> expectedErrors = Arrays.asList(errorSimpleHtml);
		List<JavaScriptError> jsErrors = JavaScriptError.readErrors(webDriver);
		assertEquals(expectedErrors, jsErrors);
	}

	private String getResource(final String string) {
		String resource = getClass().getClassLoader().getResource(string).toExternalForm();
		if (resource.startsWith("file:/") && !resource.startsWith("file:///")) {
			resource = "file://" + resource.substring(5);
		}
		return resource;
	}
}

@@ -882,16 +882,90 @@
 xpi%22);%0A%0A
+%09%09ffProfile.setPreference(%22extensions.firebug.currentVersion%22, %221.11.4%22);%0A
 %09%09ffProf
@@ -1019,22 +1019,20 @@
 Trace%22, 
-%22
 true
-%22
 );%0A%09%09ffP
@@ -1084,23 +1084,21 @@
 yLoad%22, 
-%22
 false
-%22
 );%0A%09%09ffP
@@ -1161,15 +1161,13 @@
 e%22, 
-%22
 false
-%22
 );%0A%09
@@ -1235,24 +1235,97 @@
 on%22, %22on%22);%0A
+%09%09ffProfile.setPreference(%22extensions.firebug.throttleMessages%22, false);%0A
 %09%09ffProfile.
@@ -1384,14 +1384,12 @@
 s%22, 
-%22
 true
-%22
 );%0A%09

Update Firebug extension settings.