1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   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 package com.sun.fx.webnode.tests.html5.webstorage;
  26 
  27 import com.sun.fx.webnode.tests.commonUtils.GenericTestClass;
  28 import static com.sun.fx.webnode.tests.commonUtils.GenericTestClass.getPath;
  29 import com.sun.fx.webnode.tests.commonUtils.ToolkitInitializer;
  30 import static com.sun.fx.webnode.tests.html5.webstorage.LocalStorageTest.key;
  31 import static com.sun.fx.webnode.tests.html5.webstorage.LocalStorageTest.secondValue;
  32 import static com.sun.fx.webnode.tests.html5.webstorage.LocalStorageTest.uriDomain1;
  33 import static com.sun.fx.webnode.tests.html5.webstorage.LocalStorageTest.uriDomain2;
  34 import static com.sun.fx.webnode.tests.html5.webstorage.LocalStorageTest.value;
  35 import static com.sun.fx.webnode.tests.html5.webstorage.LocalStorageTest.veryLongString;
  36 import javafx.application.Platform;
  37 import javafx.beans.value.ChangeListener;
  38 import javafx.beans.value.ObservableValue;
  39 import javafx.concurrent.Worker;
  40 import static org.junit.Assert.assertTrue;
  41 import org.junit.BeforeClass;
  42 import org.junit.Test;
  43 
  44 /**
  45  *
  46  * @author Dmitry Ginzburg <dmitry.ginzburg@oracle.com>
  47  */
  48 public class SessionStorageTest extends GenericTestClass {
  49 
  50     protected Object result;
  51     protected Tester resultReady = new Tester() {
  52         public boolean isPassed() {
  53             return result != null;
  54         }
  55     };
  56 
  57     @BeforeClass
  58     public static void init() {
  59         test.javaclient.shared.Utils.launch(ToolkitInitializer.class, new String[0]);
  60     }
  61 
  62     public void initPage() {
  63         result = null;
  64         Platform.runLater(new Runnable() {
  65             @Override
  66             public void run() {
  67                 initWebEngine();
  68                 engine.load(getPath(this.getClass(), "resources/empty.html"));
  69 
  70             }
  71         });
  72         doWaitPageLoading();
  73     }
  74 
  75     @Test(timeout = 10000)
  76     public void testCheckSessionStorageVariableExists() {
  77         initPage();
  78         Platform.runLater(new Runnable() {
  79             @Override
  80             public void run() {
  81                 result = engine.executeScript("window.sessionStorage;");
  82             }
  83         });
  84         doWait(resultReady);
  85         assertTrue("Javascript variable \"sessionStorage\" does not exist", result != null);
  86     }
  87 
  88     @Test(timeout = 10000)
  89     public void testCheckSessionStorageBasicReadWrite() {
  90         initPage();
  91         Platform.runLater(new Runnable() {
  92             @Override
  93             public void run() {
  94                 engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + value + "\");");
  95                 result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
  96             }
  97         });
  98         doWait(resultReady);
  99         assertTrue("SessionStorage doesn't support basic read/write operations", result.equals(value));
 100     }
 101 
 102     @Test(timeout = 10000)
 103     public void testCheckSessionStorageBasicReadWriteAccessThroughInnerVariable() {
 104         initPage();
 105         Platform.runLater(new Runnable() {
 106             @Override
 107             public void run() {
 108                 engine.executeScript("window.sessionStorage." + key + " = \"" + value + "\";");
 109                 result = engine.executeScript("window.sessionStorage." + key + ";");
 110             }
 111         });
 112         doWait(resultReady);
 113         assertTrue("SessionStorage doesn't support basic read/write operations through dot", result.equals(value));
 114     }
 115 
 116     @Test(timeout = 10000)
 117     public void testCheckSessionStorageBasicReadWriteMixedAccessSetThroughInnerVariable() {
 118         initPage();
 119         Platform.runLater(new Runnable() {
 120             @Override
 121             public void run() {
 122                 engine.executeScript("window.sessionStorage." + key + " = \"" + value + "\";");
 123                 result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
 124             }
 125         });
 126         doWait(resultReady);
 127         assertTrue("SessionStorage doesn't support basic mixed read/write operations through dot", result.equals(value));
 128     }
 129 
 130     @Test(timeout = 10000)
 131     public void testCheckSessionStorageBasicReadWriteMixedAccessSetThroughMethod() {
 132         initPage();
 133         Platform.runLater(new Runnable() {
 134             @Override
 135             public void run() {
 136                 engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + value + "\");");
 137                 result = engine.executeScript("window.sessionStorage." + key + ";");
 138             }
 139         });
 140         doWait(resultReady);
 141         assertTrue("SessionStorage doesn't support basic mixed read/write operations through dot", result.equals(value));
 142     }
 143 
 144     @Test(timeout = 10000)
 145     public void testCheckSessionStorageWithLongString() {
 146         initPage();
 147         Platform.runLater(new Runnable() {
 148             @Override
 149             public void run() {
 150                 engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + veryLongString + "\");");
 151                 result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
 152             }
 153         });
 154         doWait(resultReady);
 155         assertTrue("SessionStorage doesn't support basic read/write operations with long strings", result.equals(veryLongString));
 156     }
 157 
 158     @Test(timeout = 10000)
 159     public void testCheckSessionStorageWithEmptyString() {
 160         initPage();
 161         Platform.runLater(new Runnable() {
 162             @Override
 163             public void run() {
 164                 engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"\");");
 165                 result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
 166             }
 167         });
 168         doWait(resultReady);
 169         assertTrue("SessionStorage doesn't support basic read/write operations with empty strings", result.toString().isEmpty());
 170     }
 171 
 172     @Test(timeout = 10000)
 173     public void testCheckSessionStorageDoubleWrite() {
 174         initPage();
 175         Platform.runLater(new Runnable() {
 176             @Override
 177             public void run() {
 178                 result = engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + value + "\");");
 179             }
 180         });
 181         doWait(resultReady);
 182         result = null;
 183         Platform.runLater(new Runnable() {
 184             @Override
 185             public void run() {
 186                 engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + secondValue + "\");");
 187                 result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
 188             }
 189         });
 190         doWait(resultReady);
 191         assertTrue("SessionStorage doesn't support basic read/write operations double writing", result.equals (secondValue));
 192     }
 193 
 194     @Test(timeout = 10000)
 195     public void testCheckSessionStorageNotSaveBetweenRuns() {
 196         initPage();
 197         Platform.runLater(new Runnable() {
 198             @Override
 199             public void run() {
 200                 engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + value + "\");");
 201                 engine = null;
 202                 initWebEngine();
 203                 engine.load(getPath(this.getClass(), "resources/empty.html"));
 204             }
 205         });
 206         doWaitPageLoading();
 207         Platform.runLater(new Runnable() {
 208             @Override
 209             public void run() {
 210                 result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
 211             }
 212         });
 213         try {
 214             doWait(resultReady);
 215             throw new RuntimeException("sessionStorage content is saved between runs: bad behavior");
 216         } catch (org.jemmy.TimeoutExpiredException ex) {
 217 
 218         }
 219     }
 220 
 221     @Test(timeout = 10000)
 222     public void testCheckSessionStorageCrossDomainVariables() {
 223         result = null;
 224         Platform.runLater(new Runnable() {
 225             @Override
 226             public void run() {
 227                 engine = null;
 228                 initWebEngine();
 229                 engine.load(uriDomain1);
 230                 engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
 231                     @Override
 232                     public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
 233                         if (t1.equals(Worker.State.SUCCEEDED)) {
 234                             result = engine.executeScript("window.sessionStorage.setItem(\"" + key + "\", \"" + value + "\");");
 235                         }
 236                     }
 237                 });
 238             }
 239         });
 240         doWait(resultReady);
 241         result = new Object();
 242         Platform.runLater(new Runnable() {
 243             @Override
 244             public void run() {
 245                 engine = null;
 246                 initWebEngine();
 247                 engine.load(uriDomain2);
 248                 engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
 249                     @Override
 250                     public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
 251                         if (t1.equals(Worker.State.SUCCEEDED)) {
 252                             result = engine.executeScript("window.sessionStorage.getItem(\"" + key + "\");");
 253                         }
 254                     }
 255                 });
 256             }
 257         });
 258         doWait(new Tester() {
 259             @Override
 260             public boolean isPassed() {
 261                 return result == null;
 262             }
 263         });
 264     }
 265 }