/*
Copyright 2011 WebDriver committers
Copyright 2011 Software Freedom Conservancy.

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 org.openqa.selenium;

import org.junit.Test;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;

import java.util.concurrent.Callable;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.TestWaiter.waitFor;
import static org.openqa.selenium.testing.Ignore.Driver.ANDROID;
import static org.openqa.selenium.testing.Ignore.Driver.CHROME;
import static org.openqa.selenium.testing.Ignore.Driver.HTMLUNIT;
import static org.openqa.selenium.testing.Ignore.Driver.IE;
import static org.openqa.selenium.testing.Ignore.Driver.IPHONE;
import static org.openqa.selenium.testing.Ignore.Driver.OPERA;
import static org.openqa.selenium.testing.Ignore.Driver.SELENESE;

@Ignore(value = {ANDROID, CHROME, HTMLUNIT, IPHONE, OPERA, SELENESE},
        reason = "Not yet implemented.")
public class WindowTest extends JUnit4TestBase {

  @Test
  public void testGetsTheSizeOfTheCurrentWindow() {
    Dimension size = driver.manage().window().getSize();

    assertThat(size.width, is(greaterThan(0)));
    assertThat(size.height, is(greaterThan(0)));
  }

  @Test
  public void testSetsTheSizeOfTheCurrentWindow() {
    WebDriver.Window window = driver.manage().window();
    Dimension size = window.getSize();

    // resize relative to the initial size, since we don't know what it is
    Dimension targetSize = new Dimension(size.width - 20, size.height - 20);
    window.setSize(targetSize);

    waitFor(windowSizeToEqual(driver, targetSize));
  }

  @Ignore(IE)
  @Test
  public void testGetsThePositionOfTheCurrentWindow() {
    Point position = driver.manage().window().getPosition();

    assertThat(position.x, is(greaterThanOrEqualTo(0)));
    assertThat(position.y, is(greaterThanOrEqualTo(0)));
  }

  @Test
  public void testSetsThePositionOfTheCurrentWindow() {
    WebDriver.Window window = driver.manage().window();
    Point position = window.getPosition();

    Point targetPosition = new Point(position.x + 10, position.y + 10);
    window.setPosition(targetPosition);

    Point newLocation = window.getPosition();

    assertEquals(targetPosition.x, newLocation.x);
    assertEquals(targetPosition.y, newLocation.y);
  }

  @Ignore({ANDROID, CHROME, HTMLUNIT, IE, IPHONE, OPERA, SELENESE})
  @Test
  public void testCanMaximizeTheWindow() throws InterruptedException {
    WebDriver.Window window = driver.manage().window();

    Dimension targetSize = new Dimension(200, 200);
    window.setSize(targetSize);
    waitFor(windowSizeToEqual(driver, targetSize));

    Dimension size = window.getSize();
    window.maximize();
    waitFor(windowSizeToBeGreaterThan(driver, size));
  }

  private Callable<Boolean> windowSizeToBeGreaterThan(final WebDriver driver, final Dimension size) {
    return new Callable<Boolean>() {
      public Boolean call() throws Exception {
        Dimension newSize = driver.manage().window().getSize();
        if(newSize.width > size.width && newSize.height > size.height) {
          return true;
        }

        return null;
      }
    };
  }

  private Callable<Boolean> windowSizeToEqual(final WebDriver driver, final Dimension size) {
    return new Callable<Boolean>() {
      public Boolean call() throws Exception {
        Dimension newSize = driver.manage().window().getSize();
        if(newSize.width == size.width && newSize.height == size.height) {
          return true;
        }

        return null;
      }
    };
  }


}

@@ -638,16 +638,84 @@
 enium;%0A%0A
+import java.util.concurrent.Callable;%0A%0Aimport org.hamcrest.Matcher;%0A
 import o
@@ -828,47 +828,8 @@
 e;%0A%0A
-import java.util.concurrent.Callable;%0A%0A
 impo
@@ -931,16 +931,62 @@
 qualTo;%0A
+import static org.hamcrest.Matchers.lessThan;%0A
 import s
@@ -1109,62 +1109,8 @@
 at;%0A
-import static org.openqa.selenium.TestWaiter.waitFor;%0A
 impo
@@ -2284,53 +2284,147 @@
     
-waitFor(windowSizeToEqual(driver, targetSize)
+Dimension newSize = window.getSize();%0A    assertEquals(targetSize.width, newSize.width);%0A    assertEquals(targetSize.height, newSize.height
 );%0A 
@@ -3325,39 +3325,31 @@
 );%0A%0A    
-Dimension targ
+window.s
 etSize
- = 
+(
 new Dime
@@ -3367,91 +3367,68 @@
 200)
+)
 ;%0A    
-window.setSize(targetSize);%0A    waitFor(windowSizeToEqual(driver, targetSize)
+// TODO convert to WebDriverWait%0A    Thread.sleep(500
 );%0A%0A
@@ -3462,24 +3462,25 @@
 .getSize();%0A
+%0A
     window.m
@@ -3498,253 +3498,69 @@
     
-waitFor(windowSizeToBeGreaterThan(driver, size));%0A  %7D%0A%0A  private Callable%3CBoolean%3E windowSizeToBeGreaterThan(final WebDriver driver, final Dimension size) %7B%0A    return new Callable%3CBoolean%3E() %7B%0A      public Boolean call() throws Exception %7B%0A    
+// TODO convert to WebDriverWait%0A    Thread.sleep(500);%0A    %0A
     
@@ -3583,32 +3583,14 @@
 e = 
-driver.manage().
 window
-()
 .get
@@ -3605,543 +3605,122 @@
     
- 
-   if(newSize.width %3E size.width && newSize.height %3E size.height) %7B%0A          return true;%0A        %7D%0A%0A        return null;%0A      %7D%0A    %7D;%0A  %7D%0A%0A  private Callable%3CBoolean%3E windowSizeToEqual(final WebDriver driver, final Dimension size) %7B%0A    return new Callable%3CBoolean%3E() %7B%0A      public Boolean call() throws Exception %7B%0A        Dimension newSize = driver.manage().window().getSize();%0A        if(newSize.width == size.width && newSize.height == size.height) %7B%0A          return true;%0A        %7D%0A%0A        return null;%0A      %7D%0A    %7D;%0A  %7D%0A%0A
+assertThat(newSize.width, greaterThan(size.width));%0A    assertThat(newSize.height, greaterThan(size.height));  %0A  %7D
 %0A%7D%0A

Revert r16325, apparently it makes things worse than before.