/*
Copyright 2012 Selenium committers
Copyright 2012 Software Freedom Conservancy

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;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.ProxyServer;
import org.openqa.selenium.testing.TestUtilities;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import static org.openqa.selenium.testing.Ignore.Driver.ANDROID;
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Ignore.Driver.IE;
import static org.openqa.selenium.testing.Ignore.Driver.IPHONE;
import static org.openqa.selenium.testing.Ignore.Driver.OPERA;
import static org.openqa.selenium.testing.Ignore.Driver.OPERA_MOBILE;
import static org.openqa.selenium.testing.Ignore.Driver.PHANTOMJS;
import static org.openqa.selenium.testing.Ignore.Driver.SAFARI;

public class ProxySettingTest extends JUnit4TestBase {

  private ProxyServer proxyServer;

  @Before
  public void newProxyInstance() {
    proxyServer = new ProxyServer();
  }

  @Before
  public void avoidRemote() {
    // TODO: Resolve why these tests don't work on the remote server
    assumeTrue(TestUtilities.isLocal());
  }

  @Ignore({ANDROID, CHROME, HTMLUNIT, IE, IPHONE, OPERA, OPERA_MOBILE, PHANTOMJS, SAFARI})
  @Test
  public void canConfigureProxyWithRequiredCapability() {
    Proxy proxyToUse = proxyServer.asProxy();
    DesiredCapabilities requiredCaps = new DesiredCapabilities();
    requiredCaps.setCapability(PROXY, proxyToUse);

    WebDriver driver = new WebDriverBuilder().setRequiredCapabilities(requiredCaps).get();
    driver.get(pages.simpleTestPage);
    driver.quit();

    assertTrue("Proxy should have been called", proxyServer.hasBeenCalled("simpleTest.html"));
  }

  @Ignore({ANDROID, CHROME, HTMLUNIT, IE, IPHONE, OPERA, OPERA_MOBILE, PHANTOMJS, SAFARI})
  @Test
  public void requiredProxyCapabilityShouldHavePriority() {
    ProxyServer desiredProxyServer = new ProxyServer();
    Proxy desiredProxy = desiredProxyServer.asProxy();
    Proxy requiredProxy = proxyServer.asProxy();

    DesiredCapabilities desiredCaps = new DesiredCapabilities();
    desiredCaps.setCapability(PROXY, desiredProxy);
    DesiredCapabilities requiredCaps = new DesiredCapabilities();
    requiredCaps.setCapability(PROXY, requiredProxy);

    WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(desiredCaps).
        setRequiredCapabilities(requiredCaps).get();
    driver.get(pages.simpleTestPage);
    driver.quit();

    assertFalse("Desired proxy should not have been called.",
                desiredProxyServer.hasBeenCalled("simpleTest.html"));
    assertTrue("Required proxy should have been called.",
               proxyServer.hasBeenCalled("simpleTest.html"));

    desiredProxyServer.destroy();
  }

  @After
  public void deleteProxyInstance() {
    if (proxyServer != null) {
      proxyServer.destroy();
    }
  }

}
@@ -852,24 +852,82 @@
 t4TestBase;%0A
+import org.openqa.selenium.testing.NeedsLocalEnvironment;%0A
 import org.o
@@ -958,24 +958,24 @@
 roxyServer;%0A
-
 import org.o
@@ -2037,121 +2037,582 @@
 %0A  @
-Before%0A  public void avoidRemote() %7B%0A    // TODO: Resolve why these tests don't work on the remote server
+Ignore(value = %7BANDROID, IPHONE, OPERA_MOBILE, PHANTOMJS, SAFARI%7D,%0A          reason = %22Android/Iphone/PhantomJS - not tested,%22%0A                   + %22Opera mobile/Safari - not implemented%22)%0A  @NeedsLocalEnvironment%0A  @Test%0A  public void canConfigureProxyWithDesiredCapability() %7B%0A    Proxy proxyToUse = proxyServer.asProxy();%0A    DesiredCapabilities caps = new DesiredCapabilities();%0A    caps.setCapability(PROXY, proxyToUse);%0A%0A    WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();%0A    driver.get(pages.simpleTestPage);%0A    driver.quit();%0A
 %0A    ass
 umeT
@@ -2611,38 +2611,92 @@
  ass
-ume
+ert
 True(
-TestUtilities.isLocal(
+%22Proxy should have been called%22, proxyServer.hasBeenCalled(%22simpleTest.html%22
 ));%0A
@@ -2783,32 +2783,57 @@
 TOMJS, SAFARI%7D)%0A
+  @NeedsLocalEnvironment%0A
   @Test%0A  public
@@ -3287,32 +3287,32 @@
 t.html%22));%0A  %7D%0A%0A
-
   @Ignore(%7BANDRO
@@ -3386,16 +3386,41 @@
 AFARI%7D)%0A
+  @NeedsLocalEnvironment%0A
   @Test%0A

add local test for checking HTTP manual proxy