package org.gwtproject.core.client;

import com.google.gwt.junit.client.GWTTestCase;
import org.gwtproject.core.client.Scheduler.ScheduledCommand;

public class SchedulerTest extends GWTTestCase {

    private static final int TEST_DELAY = 500000;

    @Override
    public String getModuleName() {
        return "org.gwtproject.core.Core";
    }

    /**
     * Tests that a deferred command can schedule a finally command, which can schedule another
     * finally command
     */
    public void testEndToEnd() {
        final boolean[] ranEntry = {false};

        final ScheduledCommand finallyCommand = new ScheduledCommand() {
            @Override
            public void execute() {
                assertTrue(ranEntry[0]);
                Scheduler.get().scheduleFinally(new ScheduledCommand() {
                    @Override
                    public void execute() {
                        finishTest();

                    }
                });
            }
        };

        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            @Override
            public void execute() {
                ranEntry[0] = true;
                Scheduler.get().scheduleFinally(finallyCommand);
            }
        });

        delayTestFinish(TEST_DELAY);
    }

    /**
     * Tests that an entry command can schedule a finally command where the whole
     * thing is kicked off by a deferred command.
     */
    public void testEndToEndLegacy() {
        final boolean[] ranEntry = {false};

        final ScheduledCommand finallyCommand = new ScheduledCommand() {
            @Override
            public void execute() {
                assertTrue(ranEntry[0]);
                finishTest();
            }
        };

        Scheduler.get().scheduleEntry(new ScheduledCommand() {
            @Override
            public void execute() {
                ranEntry[0] = true;
                Scheduler.get().scheduleFinally(finallyCommand);
            }
        });

        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            @Override
            public void execute() {
                assertTrue(ranEntry[0]);
            }
        });

        delayTestFinish(TEST_DELAY);
    }
}
@@ -1273,32 +1273,34 @@
 AY);%0A    %7D%0A%0A
+//
     /**%0A
      * Tests
@@ -1279,32 +1279,34 @@
    %7D%0A%0A//    /**%0A
+//
      * Tests tha
@@ -1371,16 +1371,18 @@
 e whole%0A
+//
      * t
@@ -1423,24 +1423,26 @@
 ommand.%0A
+//
      */%0A
     publ
@@ -1425,32 +1425,34 @@
 mand.%0A//     */%0A
+//
     public void 
@@ -1470,24 +1470,26 @@
 dLegacy() %7B%0A
+//
         fina
@@ -1512,33 +1512,37 @@
 ntry = %7Bfalse%7D;%0A
-%0A
+//%0A//
         final Sc
@@ -1590,32 +1590,34 @@
 uledCommand() %7B%0A
+//
             @Ove
@@ -1614,32 +1614,34 @@
       @Override%0A
+//
             publ
@@ -1652,32 +1652,34 @@
 oid execute() %7B%0A
+//
                 
@@ -1695,32 +1695,34 @@
 e(ranEntry%5B0%5D);%0A
+//
                 
@@ -1735,16 +1735,18 @@
 Test();%0A
+//
         
@@ -1743,32 +1743,34 @@
 //            %7D%0A
+//
         %7D;%0A%0A    
@@ -1756,33 +1756,37 @@
  %7D%0A//        %7D;%0A
-%0A
+//%0A//
         Schedule
@@ -1824,32 +1824,34 @@
 uledCommand() %7B%0A
+//
             @Ove
@@ -1848,32 +1848,34 @@
       @Override%0A
+//
             publ
@@ -1886,32 +1886,34 @@
 oid execute() %7B%0A
+//
                 
@@ -1924,32 +1924,34 @@
 ntry%5B0%5D = true;%0A
+//
                 
@@ -1991,32 +1991,34 @@
 inallyCommand);%0A
+//
             %7D%0A  
@@ -2007,32 +2007,34 @@
 //            %7D%0A
+//
         %7D);%0A%0A   
@@ -2021,33 +2021,37 @@
 %7D%0A//        %7D);%0A
-%0A
+//%0A//
         Schedule
@@ -2092,32 +2092,34 @@
 uledCommand() %7B%0A
+//
             @Ove
@@ -2116,32 +2116,34 @@
       @Override%0A
+//
             publ
@@ -2154,32 +2154,34 @@
 oid execute() %7B%0A
+//
                 
@@ -2197,32 +2197,34 @@
 e(ranEntry%5B0%5D);%0A
+//
             %7D%0A  
@@ -2213,32 +2213,34 @@
 //            %7D%0A
+//
         %7D);%0A%0A   
@@ -2227,33 +2227,37 @@
 %7D%0A//        %7D);%0A
-%0A
+//%0A//
         delayTes
@@ -2273,19 +2273,21 @@
 EST_DELAY);%0A
+//
     %7D%0A%7D

Comment out deliberately broken test