View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.util.Map;
18  
19  import org.htmlunit.html.HtmlPage;
20  import org.htmlunit.junit.BrowserRunner;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link StorageHolder}.
26   *
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class StorageHolderTest extends SimpleWebTestCase {
31  
32      /**
33       * @throws Exception in case of error
34       */
35      @Test
36      public void localStorage() throws Exception {
37          final WebClient webClient = getWebClient();
38  
39          final Map<String, String> localStorage = webClient.getStorageHolder().getLocalStorage(URL_FIRST);
40          assertEquals(0, localStorage.size());
41  
42          String html = DOCTYPE_HTML
43              + "<html><body>\n"
44              + "<script>\n"
45              + "  localStorage.setItem('myCat', 'Tom');"
46              + "</script>\n"
47              + "</body></html>";
48  
49          loadPage(html);
50          assertEquals(1, localStorage.size());
51          assertEquals("Tom", localStorage.get("myCat"));
52  
53          html = DOCTYPE_HTML
54              + "<html><body>\n"
55              + "<script>\n"
56              + "  localStorage.clear();"
57              + "</script>\n"
58              + "</body></html>";
59  
60          loadPage(html);
61          assertEquals(0, localStorage.size());
62      }
63  
64      /**
65       * @throws Exception in case of error
66       */
67      @Test
68      public void populateLocalStorage() throws Exception {
69          final WebClient webClient = getWebClient();
70  
71          final Map<String, String> localStorage = webClient.getStorageHolder().getLocalStorage(URL_FIRST);
72          assertEquals(0, localStorage.size());
73  
74          localStorage.put("myCat", "Tom");
75  
76          final String html = DOCTYPE_HTML
77              + "<html><body>\n"
78              + "<script>\n"
79              + "  document.title = localStorage.getItem('myCat', 'Tom');"
80              + "</script>\n"
81              + "</body></html>";
82  
83          final HtmlPage page = loadPage(html);
84          assertEquals(1, localStorage.size());
85          assertEquals("Tom", page.getTitleText());
86      }
87  
88      /**
89       * @throws Exception in case of error
90       */
91      @Test
92      public void sessionStorage() throws Exception {
93          final WebClient webClient = getWebClient();
94  
95          final Map<String, String> sessionStorage =
96                  webClient.getStorageHolder().getSessionStorage(webClient.getCurrentWindow());
97          assertEquals(0, sessionStorage.size());
98  
99          String html = DOCTYPE_HTML
100             + "<html><body>\n"
101             + "<script>\n"
102             + "  sessionStorage.setItem('myCat', 'Tom');"
103             + "</script>\n"
104             + "</body></html>";
105 
106         loadPage(html);
107         assertEquals(1, sessionStorage.size());
108         assertEquals("Tom", sessionStorage.get("myCat"));
109 
110         html = DOCTYPE_HTML
111             + "<html><body>\n"
112             + "<script>\n"
113             + "  sessionStorage.clear();"
114             + "</script>\n"
115             + "</body></html>";
116 
117         loadPage(html);
118         assertEquals(0, sessionStorage.size());
119     }
120 
121     /**
122      * @throws Exception in case of error
123      */
124     @Test
125     public void populateSessionStorage() throws Exception {
126         final WebClient webClient = getWebClient();
127 
128         final Map<String, String> sessionStorage =
129                     webClient.getStorageHolder().getSessionStorage(webClient.getCurrentWindow());
130         assertEquals(0, sessionStorage.size());
131 
132         sessionStorage.put("myCat", "Tom");
133 
134         final String html = DOCTYPE_HTML
135             + "<html><body>\n"
136             + "<script>\n"
137             + "  document.title = sessionStorage.getItem('myCat', 'Tom');"
138             + "</script>\n"
139             + "</body></html>";
140 
141         final HtmlPage page = loadPage(html);
142         assertEquals(1, sessionStorage.size());
143         assertEquals("Tom", page.getTitleText());
144     }
145 }