package com.opera.core.systems;

import com.opera.core.systems.testing.OperaDriverTestCase;

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.WebDriverException;

import java.io.File;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.containsString;

public class OperaBinaryTest extends OperaDriverTestCase {

  @After
  public void resetEnvironment() {
    environment.unset(OperaBinary.OPERA_PATH_ENV_VAR);
  }

  @Test
  public void constructWithProduct() {
    OperaBinary binary = new OperaBinary(OperaProduct.DESKTOP);
    assertTrue(binary.getFile().exists());
    assertTrue(binary.getFile().isFile());
    assertTrue(binary.getFile().canExecute());
  }

  // TODO(andreastt): Test when unsetting PATH here

  @Test
  public void constructWithUserSpecifiedPath() {
    OperaBinary binary = new OperaBinary(resources.executableBinary().getPath());
    assertTrue(binary.getFile().exists());
    assertTrue(binary.getFile().isFile());
    assertTrue(binary.getFile().canExecute());
  }

  @Test
  public void constructWithUserSpecifiedBinary() {
    OperaBinary binary = new OperaBinary(resources.executableBinary());
    assertTrue(binary.getFile().exists());
    assertTrue(binary.getFile().isFile());
    assertTrue(binary.getFile().canExecute());
  }

  @Test
  public void constructionWithInvalidPath() {
    Exception exception = null;

    try {
      new OperaBinary("does/not/exist");
    } catch (RuntimeException e) {
      exception = e;
    }

    assertThat(exception, is(instanceOf(WebDriverException.class)));
    assertThat(exception.getMessage(),
               containsString("Opera binary does not exist or is not a real file"));
  }

  @Test
  public void constructionWithNonExecutableBinary() {
    Exception exception = null;

    try {
      new OperaBinary(resources.textFile());
    } catch (RuntimeException e) {
      exception = e;
    }

    assertThat(exception, is(instanceOf(WebDriverException.class)));
    assertThat(exception.getMessage(), containsString("Opera binary is not an executable"));
  }

  @Test
  public void findBasedOnEnvironmentalVariable() {
    environment.set(OperaBinary.OPERA_PATH_ENV_VAR, resources.executableBinary().getPath());
    assertNotNull(OperaBinary.find(OperaProduct.DESKTOP));
  }

  // TODO(andreastt): Test for endings on Windows and Mac

  @Test
  public void findBasedOnPlatform() {
    assertNotNull(OperaBinary.find(OperaProduct.DESKTOP));
  }

  @Test
  public void findBasedOnPlatformAll() {
    assertNotNull(OperaBinary.find(OperaProduct.ALL));
    assertEquals(OperaBinary.find(OperaProduct.DESKTOP), OperaBinary.find(OperaProduct.DESKTOP));
  }

  @Test
  public void findBasedOnPlatformCore() {
    assertNull(OperaBinary.find(OperaProduct.CORE));
  }

  @Test
  public void findBasedOnPlatformSDK() {
    assertNull(OperaBinary.find(OperaProduct.SDK));
  }

  @Test
  public void findBasedOnPlatformMini() {
    assertNull(OperaBinary.find(OperaProduct.MINI));
  }

  @Test
  public void findBasedOnPlatformMobile() {
    assertNotNull(OperaBinary.find(OperaProduct.MOBILE));
  }

  // TODO(andreastt): Possibly mock build*Paths() for testing?

  // TODO(andreastt): Possibly mock build*Binaries() for testing?

}
@@ -110,16 +110,41 @@
 .After;%0A
+import org.junit.Ignore;%0A
 import o
@@ -2836,35 +2836,32 @@
 () %7B%0A    assertN
-otN
 ull(OperaBinary.
@@ -2889,106 +2889,8 @@
 ));%0A
-    assertEquals(OperaBinary.find(OperaProduct.DESKTOP), OperaBinary.find(OperaProduct.DESKTOP));%0A
   %7D%0A
@@ -3212,32 +3212,82 @@
 );%0A  %7D%0A%0A  @Test%0A
+  @Ignore(%22requires operamobile to be installed%22)%0A
   public void fi

Fix OperaBinary tests