/*
 * Copyright 2009 Google Inc.
 * 
 * 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.gwtproject.dom.client;

import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;

/**
 * Tests StyleInjector by looking for effects of injected CSS on DOM elements.
 */
public class StyleInjectorTest extends GWTTestCase {

  private static final int TEST_DELAY = 1000;

  @Override
  public String getModuleName() {
    return "org.gwtproject.dom.DOMTest";
  }

  @SuppressWarnings("deprecation")
  public void testOldMethods() {
    final DivElement elt = Document.get().createDivElement();
    elt.setId("styleInjectorTest");
    elt.setInnerHTML("Hello StyleInjector!");
    Document.get().getBody().appendChild(elt);

    StyleInjector.injectStylesheet("#styleInjectorTest {position: absolute; left: 100px; width: 50px; height 50px;}");
    StyleInjector.injectStylesheetAtStart("#styleInjectorTest {left: 25px; width: 100px !important;}");
    StyleInjector.injectStylesheetAtEnd("#styleInjectorTest {height: 100px;}");

    // We need to allow the document to be redrawn
    delayTestFinish(TEST_DELAY);

    DeferredCommand.addCommand(new Command() {
      @Override
      public void execute() {
        assertEquals(100, elt.getOffsetLeft());
        assertEquals(100, elt.getClientHeight());
        assertEquals(100, elt.getClientWidth());

        finishTest();
      }
    });
  }

  /**
   * Ensure that the IE createStyleSheet compatibility code is exercised.
   */
  @SuppressWarnings("deprecation")
  public void testOldMethodsWithLotsOfStyles() {
    StyleElement[] elements = new StyleElement[100];
    for (int i = 0, j = elements.length; i < j; i++) {
      elements[i] = StyleInjector.injectStylesheet("#styleInjectorTest" + i
          + " {position: absolute; left: 100px; width: 50px; height 50px;}");
    }

    String id = "styleInjectorTest" + (elements.length - 1);
    StyleInjector.injectStylesheetAtStart("#" + id
        + " {left: 25px; width: 100px !important;}");
    StyleInjector.injectStylesheetAtEnd("#" + id + " {height: 100px;}");

    final DivElement elt = Document.get().createDivElement();
    elt.setId(id);
    elt.setInnerHTML("Hello StyleInjector!");
    Document.get().getBody().appendChild(elt);

    // We need to allow the document to be redrawn
    delayTestFinish(TEST_DELAY);

    DeferredCommand.addCommand(new Command() {
      @Override
      public void execute() {
        assertEquals(100, elt.getOffsetLeft());
        assertEquals(100, elt.getClientHeight());
        assertEquals(100, elt.getClientWidth());
        finishTest();
      }
    });
  }

  /*
   * Tests against issue #879: Ensure that empty history tokens do not add
   * additional characters after the '#' symbol in the URL.
   */
  public void testStyleInjectorBatched() {
    testStyleInjector("testStyleInjectorBatched", false);
  }

  public void testStyleInjectorImmediate() {
    testStyleInjector("testStyleInjectorImmediate", true);
  }

  private void testStyleInjector(String testName, final boolean immediate) {

    final DivElement elt = Document.get().createDivElement();
    elt.setId(testName);
    elt.setInnerHTML("Hello");
    Document.get().getBody().appendChild(elt);

    StyleInjector.inject("#" + testName
        + " {position: absolute; left: 100px; width: 50px; height 50px;}",
        immediate);
    StyleInjector.injectAtStart("#" + testName
        + " {left: 25px; width: 100px !important;}", immediate);
    StyleInjector.injectAtEnd("#" + testName + " {height: 100px;}", immediate);

    Command command = new Command() {
      @Override
      public void execute() {
        assertEquals(100, elt.getOffsetLeft());
        assertEquals(100, elt.getClientHeight());
        assertEquals(100, elt.getClientWidth());

        if (!immediate) {
          finishTest();
        }
      }
    };

    if (immediate) {
      command.execute();
    } else {
      DeferredCommand.addCommand(command);
      // We need to allow the BatchedCommands to execute
      delayTestFinish(TEST_DELAY);
    }
  }
}

@@ -682,93 +682,44 @@
 ort 
-com.google.gwt.user.client.Command;%0Aimport com.google.gwt.user.client.DeferredCommand
+org.gwtproject.core.client.Scheduler
 ;%0A%0A/
@@ -1654,94 +1654,46 @@
     
-DeferredCommand.addCommand(new Command() %7B%0A      @Override%0A      public void execute()
+Scheduler.get().scheduleDeferred(() -%3E
  %7B%0A 
@@ -1857,32 +1857,25 @@
 nishTest();%0A
-      %7D
 %0A
+
     %7D);%0A  %7D%0A
@@ -2822,99 +2822,49 @@
     
-DeferredCommand.addCommand(new Command() %7B%0A      @Override%0A      public void execute() %7B%0A  
+Scheduler.get().scheduleDeferred(() -%3E %7B%0A
     
@@ -2903,34 +2903,32 @@
 tLeft());%0A      
- 
- 
 assertEquals(100
@@ -2951,34 +2951,32 @@
 eight());%0A      
-  
 assertEquals(100
@@ -3002,26 +3002,24 @@
 h());%0A      
- 
- 
 finishTest()
@@ -3020,24 +3020,16 @@
 Test();%0A
-      %7D%0A
     %7D);%0A
@@ -3970,16 +3970,35 @@
 );%0A%0A    
+Scheduler.Scheduled
 Command 
@@ -4011,16 +4011,35 @@
 d = new 
+Scheduler.Scheduled
 Command(
@@ -4381,33 +4381,39 @@
     
-DeferredCommand.addComman
+Scheduler.get().scheduleDeferre
 d(co

Move tests to use scheduler instead of Command