package com.opera.core.systems;

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

public class JavascriptTest extends TestBase
{

  @Before
  public void setUp()
  {
    driver.get(fixture("javascript.html"));
    // Click to focus the document
    driver.mouseEvent(1, 1, 1);
  }

  @Test
  public void testTyping()
  {
	  String text = "Hello, world!";

	  driver.executeScript("document.getElementById('one').focus()");
	  driver.type(text);

	  Assert.assertEquals(text, driver.findElementById("one").getValue());
  }

  // Make sure that typing actually happens. When the focus switches half way
  // through typing we should continue typing on the other textbox
  @Test
  public void testTypingKeyEvents()
  {
	  driver.get(fixture("keys.html"));

    driver.type("hi");

    String log = driver.findElementById("log").getValue();
    Assert.assertTrue(log.contains("press, 104, h,"));
    Assert.assertTrue(log.contains("up, 73, I,"));
  }

  // TODO finish this test
  /*
oper  public void testConsoleListener()
  {
    driver.addConsoleListener(new IConsoleListener() {
      public List<FilterRule> getFilters() {
        List<FilterRule> list = new ArrayList<FilterRule>();
        list.add(new FilterRule("*", FilterRule.FilterType.STARTS_WITH, ""));
        return list;
      }

      public void onMessage(String message) {
        Assert.assertEquals("one", message);
      }
    });
  }
  */

  @Test
  public void testDoubleClick()
  {
    WebElement one = driver.findElementById("one");
    one.click();
    one.click();
    Assert.assertEquals(driver.findElementById("two").getValue(), "double");
  }
  
  @Test
  public void testWindowCount() throws Exception {
    int numWindows = driver.getWindowCount();
    
    driver.findElementById("open_window").click();
    
    Assert.assertEquals(numWindows + 1, driver.getWindowCount());
  }
  
  @Test
  public void testWindowCount2() throws Exception {
    int numWindows = driver.getWindowCount();
    
    driver.close();
    
    Assert.assertEquals(numWindows - 1, driver.getWindowCount()); 
  }
}

@@ -1009,478 +1009,16 @@
  %7D%0A%0A
-  // TODO finish this test%0A  /*%0Aoper  public void testConsoleListener()%0A  %7B%0A    driver.addConsoleListener(new IConsoleListener() %7B%0A      public List%3CFilterRule%3E getFilters() %7B%0A        List%3CFilterRule%3E list = new ArrayList%3CFilterRule%3E();%0A        list.add(new FilterRule(%22*%22, FilterRule.FilterType.STARTS_WITH, %22%22));%0A        return list;%0A      %7D%0A%0A      public void onMessage(String message) %7B%0A        Assert.assertEquals(%22one%22, message);%0A      %7D%0A    %7D);%0A  %7D%0A  */%0A%0A
   @Test%0A
   pu
@@ -1005,32 +1005,32 @@
 );%0A  %7D%0A%0A  @Test%0A
+
   public void te
@@ -1212,27 +1212,25 @@
 uble%22);%0A  %7D%0A
-  
 %0A
+
   @Test%0A  pu
@@ -1314,37 +1314,33 @@
 tWindowCount();%0A
-    
 %0A
+
     driver.findE
@@ -1370,29 +1370,25 @@
 %22).click();%0A
-    
 %0A
+
     Assert.a
@@ -1445,19 +1445,17 @@
 ));%0A  %7D%0A
-  
 %0A
+
   @Test%0A
@@ -1548,29 +1548,25 @@
 dowCount();%0A
-    
 %0A
+
     driver.c
@@ -1573,20 +1573,16 @@
 lose();%0A
-    
 %0A    Ass
@@ -1639,16 +1639,15 @@
 ount());
- 
 %0A  %7D%0A%7D%0A

Remove commented code