/*
Copyright 2011 WebDriver committers
Copyright 2011 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.grid.e2e.node;

import java.net.MalformedURLException;
import java.net.URL;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

import org.openqa.grid.common.GridRole;
import org.openqa.grid.e2e.utils.GridTestHelper;
import org.openqa.grid.e2e.utils.RegistryTestHelper;
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * checks that the browser is properly stopped when a selenium1 session times out.
 */
public class NodeTimeOutTest {

  private Hub hub;
  private SelfRegisteringRemote node;

  @BeforeClass(alwaysRun = true)
  public void setup() throws Exception {
    hub = GridTestHelper.getHub();

    // register a selenium 1

    node = GridTestHelper.getRemoteWithoutCapabilities(hub.getUrl(), GridRole.NODE);
    node.addBrowser(GridTestHelper.getSelenium1FirefoxCapability(), 1);
    node.addBrowser(DesiredCapabilities.firefox(), 1);
    node.setTimeout(5000, 2000);
    node.startRemoteServer();
    node.sendRegistrationRequest();

    RegistryTestHelper.waitForNode(hub.getRegistry(), 1);
  }

  @Test
  public void selenium1TimesOut() throws InterruptedException {
    String url = "http://" + hub.getHost() + ":" + hub.getPort() + "/grid/console";
    System.out.println("A");
    Selenium selenium = new DefaultSelenium(hub.getHost(), hub.getPort(), "*firefox", url);
    selenium.start();
    System.out.println("A2");
    selenium.open(url);
    Thread.sleep(8000);
    Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0);

  }

  @Test
  public void webDriverTimesOut() throws InterruptedException, MalformedURLException {
    String url = "http://" + hub.getHost() + ":" + hub.getPort() + "/grid/console";
    System.out.println("B");
    DesiredCapabilities ff = DesiredCapabilities.firefox();
    WebDriver driver = new RemoteWebDriver(new URL(hub.getUrl() + "/wd/hub"), ff);
    driver.get(url);
    System.out.println("B2");
    Assert.assertEquals(driver.getTitle(), "Grid overview");
    Thread.sleep(8000);
    Assert.assertEquals(hub.getRegistry().getActiveSessions().size(), 0);

  }

  @AfterClass
  public void teardown() throws Exception {
    //node.stopRemoteServer();
    //hub.stop();

  }
}

@@ -721,16 +721,117 @@
 et.URL;%0D
+%0Aimport java.sql.Time;%0D%0Aimport java.util.concurrent.Callable;%0D%0Aimport java.util.concurrent.TimeUnit;%0D
 %0A%0D%0Aimpor
@@ -1162,16 +1162,56 @@
 b.Hub;%0D%0A
+import org.openqa.selenium.TestWaiter;%0D%0A
 import o
@@ -2385,38 +2385,8 @@
 %22;%0D%0A
-    System.out.println(%22A%22);%0D%0A
     
@@ -2505,81 +2505,330 @@
     
-System.out.println(%22A2%22);
+selenium.open(url);%0D%0A    %0D%0A    TestWaiter.waitFor(new Callable%3CInteger%3E() %7B
 %0D%0A    
+ 
-selenium.open(url)
+ public Integer call() throws Exception %7B%0D%0A        Integer i = hub.getRegistry().getActiveSessions().size();%0D%0A        if (i != 0) %7B%0D%0A          return null
 ;%0D%0A    
+ 
-Thread.sleep(8000
+   %7D else %7B%0D%0A          return i;%0D%0A        %7D%0D%0A      %7D%0D%0A    %7D,8,TimeUnit.SECONDS
 );%0D%0A
@@ -3097,38 +3097,8 @@
 %22;%0D%0A
-    System.out.println(%22B%22);%0D%0A
     
@@ -3264,39 +3264,8 @@
 );%0D%0A
-    System.out.println(%22B2%22);%0D%0A
     
@@ -3331,24 +3331,298 @@
    T
-hread.sleep(8000
+estWaiter.waitFor(new Callable%3CInteger%3E() %7B%0D%0A      public Integer call() throws Exception %7B%0D%0A        Integer i = hub.getRegistry().getActiveSessions().size();%0D%0A        if (i != 0) %7B%0D%0A          return null;%0D%0A        %7D else %7B%0D%0A          return i;%0D%0A        %7D%0D%0A      %7D%0D%0A    %7D,8,TimeUnit.SECONDS
 );%0D%0A
@@ -3769,18 +3769,16 @@
  %7B%0D%0A    
-//
 node.sto
@@ -3799,18 +3799,16 @@
 );%0D%0A    
-//
 hub.stop

clean up,remove pauses