/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium.remote.server;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import static org.openqa.selenium.Ignore.Driver.FIREFOX;
import static org.openqa.selenium.Ignore.Driver.REMOTE;
import org.openqa.selenium.TestSuiteBuilder;
import org.openqa.selenium.environment.webserver.AppServer;
import org.openqa.selenium.environment.webserver.Jetty6AppServer;
import org.openqa.selenium.remote.server.DriverServlet;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.net.URL;

public class RemoteWebDriverTestSuite extends TestCase {
  public static Test suite() throws Exception {
    System.setProperty("webdriver.development", "true");

    Test rawSuite =
        new TestSuiteBuilder()
            .addSourceDir("common")
            .addSourceDir("remote/client")
            .addSourceDir("remote/server")
            .keepDriverInstance()
            .includeJavascriptTests()
            .usingDriver(RemoteWebDriverForTest.class)
            .exclude(FIREFOX)
            .exclude(REMOTE)
            .excludePattern(".*IntegrationTest")
            .excludePattern(".*CaptureNetworkTrafficTest")
            .excludePattern(".*FirefoxMiniHTMLRunnerTest")
            .excludePattern(".*LinuxHTMLRunnerMultiWindowTest")
            .excludePattern(".*CaptureNetworkTrafficCommandTest")
            .excludePattern(".*HtmlIdentifierTest")
            .excludePattern(".*\\.browserlaunchers\\..*")
            .excludePattern(".*FunctionalTest")
            .excludePattern(".*UnitTest")
            .excludePattern(".*LauncherTest")
            .create();

    TestSuite toReturn = new TestSuite();
    toReturn.addTest(new RemoteDriverServerStarter(rawSuite));
    return toReturn;
  }

  public static class RemoteWebDriverForTest extends RemoteWebDriver {
    public RemoteWebDriverForTest() throws Exception {
      super(new URL("http://localhost:6000/common/hub"), DesiredCapabilities.firefox());
    }
  }

  public static class RemoteDriverServerStarter extends TestSetup {
    private AppServer appServer;

    public RemoteDriverServerStarter(Test test) {
      super(test);
    }

    @Override
    protected void setUp() throws Exception {
      appServer = new Jetty6AppServer() {
        protected File findRootOfWebApp() {
          File common = super.findRootOfWebApp();
          File file = new File(common, "../../../remote/server/src/web");
          System.out.println(
              String.format("file exists %s and is: %s", file.exists(), file.getAbsolutePath()));
          return file;
        }

        protected File getKeyStore() {
          return new File(findRootOfWebApp(), "../../../../common/test/java/keystore");
        }
      };
      appServer.listenOn(6000);
      appServer.listenSecurelyOn(7000);
      appServer.addServlet("remote webdriver", "/hub/*", DriverServlet.class);
      appServer.start();

      super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
      appServer.stop();

      super.tearDown();
    }
  }
}

@@ -786,113 +786,43 @@
 ort 
-static org.openqa.selenium.Ignore.Driver.FIREFOX;%0Aimport static org.openqa.selenium.Ignore.Driver.REMOTE;
+java.io.File;%0Aimport java.net.URL;%0A
 %0Aimp
@@ -1020,35 +1020,29 @@
 ium.
-remote.server.DriverServlet
+firefox.FirefoxDriver
 ;%0Aim
@@ -1073,31 +1073,35 @@
 .remote.
-RemoteWebDriver
+DesiredCapabilities
 ;%0Aimport
@@ -1132,70 +1132,184 @@
 ote.
-DesiredCapabilities;%0A%0Aimport java.io.File;%0Aimport java.net.URL
+HttpRequest;%0Aimport org.openqa.selenium.remote.RemoteWebDriver;%0A%0Aimport static org.openqa.selenium.Ignore.Driver.FIREFOX;%0Aimport static org.openqa.selenium.Ignore.Driver.REMOTE
 ;%0A%0Ap
@@ -3696,21 +3696,423 @@
     
-super.setUp()
+if (isInDevMode()) %7B%0A        new HttpRequest(%0A            HttpRequest.Method.POST, %22http://localhost:6000/common/hub/config/drivers%22,%0A            DesiredCapabilities.firefox(),%0A            %22org.openqa.selenium.firefox.FirefoxDriverTestSuite$TestFirefoxDriver%22);%0A      %7D%0A%0A      super.setUp();%0A    %7D%0A%0A    private boolean isInDevMode() %7B%0A      return FirefoxDriver.class.getResource(%22/webdriver-extension.zip%22) == null
 ;%0A  

Substitute the firefox driver with the test one if we find we're running in an IDE and using the remote webdriver.