package org.uiautomation.ios.e2e.crash;

import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.uiautomation.ios.IOSCapabilities;
import org.uiautomation.ios.SampleApps;
import org.uiautomation.ios.server.IOSServer;
import org.uiautomation.ios.server.IOSServerConfiguration;

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

public class CrashHandling {

  private IOSServer server;
  private IOSServerConfiguration config;
  private RemoteWebDriver driver;

  @BeforeClass
  public void startServer() throws Exception {
    String[] args = {"-port", "4444", "-host", "localhost",
        "-app", SampleApps.getPPNQASampleApp(), "-simulators" };
    config = IOSServerConfiguration.create(args);

    server = new IOSServer(config);
    server.start();
  }

  @AfterClass
  public void stopServer() throws Exception {
    if (server != null) {
      server.stop();
      server = null;
    }
  }

  @AfterMethod
  public void closeDriver() {
    if (driver != null) {
      driver.quit();
      driver = null;
    }
  }

  private URL getRemoteURL() {
    try {
      URL remote = new URL("http://" + config.getHost() + ":" + config.getPort() + "/wd/hub");
      return remote;
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);
    }
  }

  @Test
  public void isAppCrashDetected() throws InterruptedException {
    IOSCapabilities cap = SampleApps.ppNQASampleAppCap();
    cap.setCapability(IOSCapabilities.SIMULATOR, true);

    RemoteWebDriver driver = new RemoteWebDriver(getRemoteURL(), cap);
    WebElement crashButton = driver.findElement(By.name("Crash me!"));
    crashButton.click();
    Thread.sleep(2000);
    Assert.assertTrue("App crash detected.", false);

  }

  @Test
  public void isSimulatorCrashDetected() throws InterruptedException {
    IOSCapabilities cap = SampleApps.ppNQASampleAppCap();
    cap.setCapability(IOSCapabilities.SIMULATOR, true);

    RemoteWebDriver driver = new RemoteWebDriver(getRemoteURL(), cap);

    // kill simulator

    Assert.assertTrue("Sim crash detected.", false);

  }

  @Test
  public void isInstrumentsCrashDetected() throws InterruptedException {
    IOSCapabilities cap = SampleApps.ppNQASampleAppCap();
    cap.setCapability(IOSCapabilities.SIMULATOR, true);

    RemoteWebDriver driver = new RemoteWebDriver(getRemoteURL(), cap);

    // kill instruments

    Assert.assertTrue("Instruments crash detected.", false);

  }

}

@@ -190,60 +190,8 @@
 er;%0A
-import org.openqa.selenium.remote.RemoteWebElement;%0A
 impo
@@ -539,16 +539,67 @@
 uration;
+%0Aimport org.uiautomation.ios.utils.ClassicCommands;
 %0A%0Aimport
@@ -2319,16 +2319,89 @@
 imulator
+%0A    ClassicCommands.killall(%22iPhone Simulator%22);%0A    Thread.sleep(3000);
 %0A%0A    As
@@ -2744,16 +2744,84 @@
 truments
+%0A    ClassicCommands.killall(%22instruments%22);%0A    Thread.sleep(3000);
 %0A%0A    As

update to crash tests