package com.opera.core.systems;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;

public class KeyEventsTest extends TestBase {
  private WebElement logEl;

  private boolean logContains(CharSequence s) {
    return logEl.getValue().contains(s);
  }

  @Before
  public void setUp() {
    getFixture("keys.html");
    logEl = driver.findElementById("log");
  }

  @After
  public void tearDown() {
    driver.releaseKeys();
  }

  @Test
  public void testKey() throws Exception {
    driver.key("a");

    Assert.assertTrue("keyDown event fired", logContains("down, 65, A"));
    Assert.assertTrue("keyPress event fired", logContains("press"));
    Assert.assertTrue("keyUp event fired", logContains("up, 65, A"));
  }

  @Test
  public void testKeyDown() throws Exception {
    driver.keyDown("a");

    Assert.assertTrue("keyDown event fired", logContains("down, 65, A"));
  }

  @Test
  public void testKeyUp() throws Exception {
    driver.keyDown("a");
    driver.keyUp("a");

    Assert.assertTrue("keyUp event fired", logContains("up, 65, A"));
  }

  @Test
  public void testRepeatedKeys() throws Exception {
    driver.keyDown("a");
    Thread.sleep(1000);
    driver.keyUp("a");

    Assert.assertTrue("repeated press events",
        logContains("down, 65, A, \npress, 0, , \npress, 0, ,"));
  }

  @Test
  public void testMultipleKeys() throws Exception {
    driver.keyDown("control");
    driver.keyDown("shift");
    driver.keyUp("control");

    Assert.assertTrue("control down", logContains("down, 17"));
    Assert.assertTrue("shift down", logContains("down, 16"));
    Assert.assertTrue("control up", logContains("up, 17"));
    Assert.assertFalse("shift up", logContains("up, 16"));
  }

  @Test
  public void testSpecialKeys() throws Exception {
    driver.key("f5");
    Assert.assertTrue("F5", logContains("press, 116"));

    driver.key("home");
    Assert.assertTrue("Home", logContains("press, 36"));

    driver.key("pagedown");
    Assert.assertTrue("page down", logContains("press, 34"));

    driver.key("del");
    Assert.assertTrue("delete", logContains("press, 46"));

    driver.key("backspace");
    Assert.assertTrue("backspace", logContains("press, 8"));
  }

  @Test
  public void testReleaseKeys() throws Exception {
    driver.keyDown("control");
    driver.keyDown("shift");

    driver.releaseKeys();
    Assert.assertTrue("released", logContains("up, 16"));
    Assert.assertTrue("released", logContains("up, 17"));
  }
}

@@ -1717,32 +1717,69 @@
 ins(%22up, 17%22));%0A
+    // shift should not have come up%0A
     Assert.asser
@@ -1817,24 +1817,24 @@
 %22up, 16%22));%0A
-
   %7D%0A%0A  @Test
@@ -2307,32 +2307,399 @@
 ess, 8%22));%0A  %7D%0A%0A
+  @Test%0A  public void testAffectsOpera() throws Exception %7B%0A    // Fixture that doesn't preventDefault%0A    getFixture(%22test.html%22);%0A    ((OperaWebElement) driver.findElementById(%22input_email%22)).sendKeys(%22before refresh%22);%0A%0A    driver.key(%22f5%22);%0A    //driver.waitForLoadToComplete();%0A    Assert.assertEquals(%22%22, driver.findElementById(%22input_email%22).getValue());%0A  %7D%0A%0A
   @Test%0A  public
@@ -2824,24 +2824,25 @@
 easeKeys();%0A
+%0A
     Assert.a
@@ -2879,32 +2879,32 @@
 ins(%22up, 16%22));%0A
-
     Assert.asser
@@ -2948,11 +2948,375 @@
 %22));%0A  %7D
+%0A%0A  @Test%0A  public void testReleaseAndPressKey() throws Exception %7B%0A    driver.keyDown(%22control%22);%0A    driver.releaseKeys();%0A%0A    Assert.assertTrue(%22released%22, logContains(%22down, 17%22));%0A    Assert.assertTrue(%22released%22, logContains(%22up, 17%22));%0A%0A    driver.keyDown(%22control%22);%0A%0A    Assert.assertTrue(%22released%22, logEl.getValue().endsWith(%22down, 17, , ctrl%5Cn%22));%0A  %7D
 %0A%7D%0A

Add tests for press and then releaseKeys() and F5 refreshing the page