/*
Copyright 2011-2012 Opera Software ASA

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 com.opera.core.systems;

import com.opera.core.systems.scope.internal.OperaIntervals;
import com.opera.core.systems.testing.Ignore;
import com.opera.core.systems.testing.OperaDriverTestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

import static com.opera.core.systems.OperaProduct.MOBILE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class NavigationTest extends OperaDriverTestCase {

  @Before
  public void navigateToThreePages() {
    driver.navigate().to(pages.javascript);
    driver.navigate().to(pages.test);
    driver.navigate().to(pages.keys);
  }

  @After
  public void resetPageLoadTimeout() {
    // Make sure page load timeout is reset after each test, if one happens to fail
    driver.manage().timeouts().pageLoadTimeout(OperaIntervals.PAGE_LOAD_TIMEOUT.getMs(),
                                               TimeUnit.MILLISECONDS);
  }

  @Test
  @Ignore(products = MOBILE)
  public void back() {
    driver.navigate().back();
    assertTrue(driver.getCurrentUrl().endsWith("test.html"));
  }

  @Test
  @Ignore(products = MOBILE)
  public void forward() {
    back();
    driver.navigate().forward();
    assertTrue(driver.getCurrentUrl().endsWith("keys.html"));
  }

  @Test
  @Ignore(products = MOBILE)
  public void backTwice() {
    driver.navigate().back();
    driver.navigate().back();
    assertTrue(driver.getCurrentUrl().endsWith("javascript.html"));
  }

  @Test
  @Ignore(products = MOBILE)
  public void forwardTwice() {
    backTwice();
    driver.navigate().forward();
    driver.navigate().forward();
    assertTrue(driver.getCurrentUrl().endsWith("keys.html"));
  }

  @Test
  public void httpRedirect() {
    final String fetchedUrl = server.whereIs("redirect");
    driver.navigate().to(fetchedUrl);

    // Wait for redirect
    Wait<WebDriver> wait = new WebDriverWait(driver, OperaIntervals.PAGE_LOAD_TIMEOUT.getMs());
    wait.until(new ExpectedCondition<Object>() {
      public Boolean apply(WebDriver driver) {
        return !driver.getCurrentUrl().equals(fetchedUrl);
      }
    });

    assertEquals(pages.result, driver.getCurrentUrl());
  }

  // If this fails, there's a high likelihood that idle is enabled
  @Test
  public void handBackControlAfterPageLoadTimeout() {
    driver.manage().timeouts().pageLoadTimeout(100, TimeUnit.MILLISECONDS);
    driver.navigate().to(pages.ecmascriptTimeout);
    assertEquals("Waiting", driver.findElement(By.id("out")).getText());
    sleep(1000);
    assertEquals("done", driver.findElement(By.id("out")).getText());
  }

  // If this fails, there's a high likelihood that idle is enabled
  @Test
  public void handBackControlAfterPageLoadTimeoutByOverload() {
    driver.get(pages.ecmascriptTimeout, 100);
    assertEquals("Waiting", driver.findElement(By.id("out")).getText());
    sleep(1000);
    assertEquals("done", driver.findElement(By.id("out")).getText());
  }

  private void sleep(long ms) {
    try {
      Thread.sleep(ms);
    } catch (InterruptedException e) {
      // fall through
    }
  }

}
@@ -663,54 +663,8 @@
 ls;%0A
-import com.opera.core.systems.testing.Ignore;%0A
 impo
@@ -718,16 +718,16 @@
 stCase;%0A
+
 %0Aimport 
@@ -1058,66 +1058,8 @@
 t;%0A%0A
-import static com.opera.core.systems.OperaProduct.MOBILE;%0A
 impo
@@ -1685,37 +1685,8 @@
 est%0A
-  @Ignore(products = MOBILE)%0A
   pu
@@ -1813,37 +1813,8 @@
 est%0A
-  @Ignore(products = MOBILE)%0A
   pu
@@ -1959,37 +1959,8 @@
 est%0A
-  @Ignore(products = MOBILE)%0A
   pu
@@ -2116,24 +2116,24 @@
 );%0A  %7D%0A%0A
+
   @Test%0A
   @Ignor
@@ -2128,37 +2128,8 @@
 est%0A
-  @Ignore(products = MOBILE)%0A
   pu

Remove Opera Mobile ignores for NavigationTest