< prev index next >

modules/javafx.web/src/test/java/test/javafx/scene/web/HistoryTest.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.javafx.scene.web;
  27 


  28 import java.util.concurrent.atomic.AtomicBoolean;

  29 import java.util.List;
  30 import java.io.File;
  31 import java.net.MalformedURLException;
  32 import javafx.beans.value.ObservableValue;

  33 import javafx.scene.web.WebHistory;
  34 import javafx.beans.value.ChangeListener;
  35 import javafx.beans.value.ObservableValue;
  36 import javafx.collections.ListChangeListener;
  37 import java.util.Date;
  38 
  39 import java.util.concurrent.Callable;
  40 import javafx.scene.web.WebHistory;
  41 import org.junit.Test;
  42 import static org.junit.Assert.assertEquals;
  43 import static org.junit.Assert.assertFalse;

  44 import static org.junit.Assert.assertTrue;
  45 import static org.junit.Assert.fail;
  46 
  47 public class HistoryTest extends TestBase {
  48     WebHistory history = getEngine().getHistory();
  49 
  50     AtomicBoolean entriesChanged = new AtomicBoolean(false);
  51     AtomicBoolean titleChanged = new AtomicBoolean(false);
  52     AtomicBoolean dateChanged = new AtomicBoolean(false);
  53     AtomicBoolean indexChanged = new AtomicBoolean(false);
  54 
  55     @Test public void test() {
  56 
  57         //
  58         // before history is populated
  59         //
  60         submit(() -> {
  61             try {
  62                 history.go(-1);
  63                 fail("go: IndexOutOfBoundsException is not thrown");


 241 
 242         ensureValueChanged(dateChanged, "date not changed after reload");
 243 
 244         //
 245         // finally, check zero and invalid maxSize
 246         //
 247 
 248         submit(() -> {
 249             // []
 250             history.setMaxSize(0);
 251             assertEquals("maxSizeProperty: wrong value", 0, history.getEntries().size());
 252 
 253             // []
 254             try {
 255                 history.maxSizeProperty().set(-1);
 256                 fail("maxSizeProperty: IllegalArgumentException is not thrown");
 257             } catch (IllegalArgumentException ex) {}
 258         });
 259     }
 260 




































































 261     void checkLoad(File file, int size, int index, String title) {
 262         load(file);
 263         check(file, size, index, title);
 264     }
 265 
 266     void check(File file, int size, int index, String title) {
 267         assertEquals("entries: size is wrong", size, history.getEntries().size());
 268         assertEquals("currentIndex: index is wrong", index, history.getCurrentIndex());
 269         assertEquals("entries: url is wrong", file.toURI().toString(), history.getEntries().get(index).getUrl());
 270         assertEquals("entries: title is wrong", title, history.getEntries().get(index).getTitle());
 271     }
 272 
 273     void ensureValueChanged(AtomicBoolean value, String errMsg) {
 274         if (!value.compareAndSet(true, false)) {
 275             fail(errMsg);
 276         }
 277     }
 278 
 279     ChangeListener newDateListener() {
 280         return new ChangeListener<Date>() {




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.javafx.scene.web;
  27 
  28 import static javafx.concurrent.Worker.State.SUCCEEDED;
  29 
  30 import java.util.concurrent.atomic.AtomicBoolean;
  31 import java.util.concurrent.CountDownLatch;
  32 import java.util.List;
  33 import java.io.File;
  34 import java.net.MalformedURLException;
  35 import javafx.beans.value.ObservableValue;
  36 import javafx.scene.web.WebEngine;
  37 import javafx.scene.web.WebHistory;
  38 import javafx.beans.value.ChangeListener;
  39 import javafx.beans.value.ObservableValue;
  40 import javafx.collections.ListChangeListener;
  41 import java.util.Date;
  42 
  43 import java.util.concurrent.Callable;
  44 import javafx.scene.web.WebHistory;
  45 import org.junit.Test;
  46 import static org.junit.Assert.assertEquals;
  47 import static org.junit.Assert.assertFalse;
  48 import static org.junit.Assert.assertNull;
  49 import static org.junit.Assert.assertTrue;
  50 import static org.junit.Assert.fail;
  51 
  52 public class HistoryTest extends TestBase {
  53     WebHistory history = getEngine().getHistory();
  54 
  55     AtomicBoolean entriesChanged = new AtomicBoolean(false);
  56     AtomicBoolean titleChanged = new AtomicBoolean(false);
  57     AtomicBoolean dateChanged = new AtomicBoolean(false);
  58     AtomicBoolean indexChanged = new AtomicBoolean(false);
  59 
  60     @Test public void test() {
  61 
  62         //
  63         // before history is populated
  64         //
  65         submit(() -> {
  66             try {
  67                 history.go(-1);
  68                 fail("go: IndexOutOfBoundsException is not thrown");


 246 
 247         ensureValueChanged(dateChanged, "date not changed after reload");
 248 
 249         //
 250         // finally, check zero and invalid maxSize
 251         //
 252 
 253         submit(() -> {
 254             // []
 255             history.setMaxSize(0);
 256             assertEquals("maxSizeProperty: wrong value", 0, history.getEntries().size());
 257 
 258             // []
 259             try {
 260                 history.maxSizeProperty().set(-1);
 261                 fail("maxSizeProperty: IllegalArgumentException is not thrown");
 262             } catch (IllegalArgumentException ex) {}
 263         });
 264     }
 265 
 266     /**
 267      * @test
 268      * @bug 8157686
 269      * Summary : Html history.pushState, history.back and history.replace
 270      * Ref : https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
 271      */
 272     @Test(timeout = 30000) public void historyOnPopStateTest() throws Exception {
 273         final CountDownLatch latch = new CountDownLatch(1);
 274         final String page0 = "archive-root0.html";
 275         final String page1 = "archive-root1.html";
 276         final String page2 = "archive-root2.html";
 277         final String page3 = "longselectorlist.html";
 278 
 279         submit(() -> {
 280             WebEngine webEngine = new WebEngine();
 281             ClassLoader loader = LoadTest.class.getClassLoader();
 282 
 283             webEngine.locationProperty().addListener((observable, oldValue, newValue) -> {
 284                 if (oldValue == null) {
 285                     return;
 286                 }
 287 
 288                 if (oldValue.substring(oldValue.lastIndexOf('?') + 1).equalsIgnoreCase(page3) &&
 289                         newValue.substring(newValue.lastIndexOf('?') + 1).equalsIgnoreCase(page1)) {
 290                     webEngine.executeScript("history.back();");
 291                 } else if (oldValue.substring(oldValue.lastIndexOf('?') + 1).equalsIgnoreCase(page1) &&
 292                         newValue.substring(newValue.lastIndexOf('/') + 1).equalsIgnoreCase(page0)) {
 293                     assertNull("history.state should be null ", webEngine.executeScript("history.state"));
 294                     webEngine.executeScript("history.go(2);");
 295                 } else if(oldValue.substring(oldValue.lastIndexOf('/') + 1).equalsIgnoreCase(page0) &&
 296                         newValue.substring(newValue.lastIndexOf('?') + 1).equalsIgnoreCase(page3)) {
 297                     latch.countDown();
 298                 }
 299             });
 300 
 301             webEngine.getLoadWorker().stateProperty().addListener(((observable, oldValue, newValue) -> {
 302                 if (newValue == SUCCEEDED) {
 303                     webEngine.executeScript("history.pushState({page: 1}, 'title 1', '?" + page1 + "');");
 304                     assertEquals("history.pushState Failed",
 305                             page1,
 306                             webEngine.getLocation().substring(webEngine.getLocation().lastIndexOf('?') + 1)
 307                     );
 308 
 309                     webEngine.executeScript("history.pushState({page: 2}, 'title 2', '?" + page2 + "');");
 310                     assertEquals("history.pushState Failed",
 311                             page2,
 312                             webEngine.getLocation().substring(webEngine.getLocation().lastIndexOf('?') + 1)
 313                     );
 314 
 315                     webEngine.executeScript("history.replaceState({page: 3}, 'title 3', '?" + page3 + "');");
 316                     assertEquals("history.pushState Failed",
 317                             page3,
 318                             webEngine.getLocation().substring(webEngine.getLocation().lastIndexOf('?') + 1)
 319                     );
 320 
 321                     webEngine.executeScript("history.back();");
 322                 }
 323             }));
 324 
 325             webEngine.load(loader.getResource("test/html/archive-root0.html").toExternalForm());
 326         });
 327         try {
 328             latch.await();
 329         } catch (InterruptedException ex) {
 330             throw new AssertionError(ex);
 331         }
 332     }
 333 
 334     void checkLoad(File file, int size, int index, String title) {
 335         load(file);
 336         check(file, size, index, title);
 337     }
 338 
 339     void check(File file, int size, int index, String title) {
 340         assertEquals("entries: size is wrong", size, history.getEntries().size());
 341         assertEquals("currentIndex: index is wrong", index, history.getCurrentIndex());
 342         assertEquals("entries: url is wrong", file.toURI().toString(), history.getEntries().get(index).getUrl());
 343         assertEquals("entries: title is wrong", title, history.getEntries().get(index).getTitle());
 344     }
 345 
 346     void ensureValueChanged(AtomicBoolean value, String errMsg) {
 347         if (!value.compareAndSet(true, false)) {
 348             fail(errMsg);
 349         }
 350     }
 351 
 352     ChangeListener newDateListener() {
 353         return new ChangeListener<Date>() {


< prev index next >