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.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link StorageHolder}.
24   *
25   * @author Ronald Brill
26   */
27  public class StorageHolderTest extends SimpleWebTestCase {
28  
29      /**
30       * @throws Exception in case of error
31       */
32      @Test
33      public void localStorage() throws Exception {
34          final WebClient webClient = getWebClient();
35  
36          final Map<String, String> localStorage = webClient.getStorageHolder().getLocalStorage(URL_FIRST);
37          assertEquals(0, localStorage.size());
38  
39          String html = DOCTYPE_HTML
40              + "<html><body>\n"
41              + "<script>\n"
42              + "  localStorage.setItem('myCat', 'Tom');"
43              + "</script>\n"
44              + "</body></html>";
45  
46          loadPage(html);
47          assertEquals(1, localStorage.size());
48          assertEquals("Tom", localStorage.get("myCat"));
49  
50          html = DOCTYPE_HTML
51              + "<html><body>\n"
52              + "<script>\n"
53              + "  localStorage.clear();"
54              + "</script>\n"
55              + "</body></html>";
56  
57          loadPage(html);
58          assertEquals(0, localStorage.size());
59      }
60  
61      /**
62       * @throws Exception in case of error
63       */
64      @Test
65      public void populateLocalStorage() throws Exception {
66          final WebClient webClient = getWebClient();
67  
68          final Map<String, String> localStorage = webClient.getStorageHolder().getLocalStorage(URL_FIRST);
69          assertEquals(0, localStorage.size());
70  
71          localStorage.put("myCat", "Tom");
72  
73          final String html = DOCTYPE_HTML
74              + "<html><body>\n"
75              + "<script>\n"
76              + "  document.title = localStorage.getItem('myCat', 'Tom');"
77              + "</script>\n"
78              + "</body></html>";
79  
80          final HtmlPage page = loadPage(html);
81          assertEquals(1, localStorage.size());
82          assertEquals("Tom", page.getTitleText());
83      }
84  
85      /**
86       * @throws Exception in case of error
87       */
88      @Test
89      public void sessionStorage() throws Exception {
90          final WebClient webClient = getWebClient();
91  
92          final Map<String, String> sessionStorage =
93                  webClient.getStorageHolder().getSessionStorage(webClient.getCurrentWindow());
94          assertEquals(0, sessionStorage.size());
95  
96          String html = DOCTYPE_HTML
97              + "<html><body>\n"
98              + "<script>\n"
99              + "  sessionStorage.setItem('myCat', 'Tom');"
100             + "</script>\n"
101             + "</body></html>";
102 
103         loadPage(html);
104         assertEquals(1, sessionStorage.size());
105         assertEquals("Tom", sessionStorage.get("myCat"));
106 
107         html = DOCTYPE_HTML
108             + "<html><body>\n"
109             + "<script>\n"
110             + "  sessionStorage.clear();"
111             + "</script>\n"
112             + "</body></html>";
113 
114         loadPage(html);
115         assertEquals(0, sessionStorage.size());
116     }
117 
118     /**
119      * @throws Exception in case of error
120      */
121     @Test
122     public void populateSessionStorage() throws Exception {
123         final WebClient webClient = getWebClient();
124 
125         final Map<String, String> sessionStorage =
126                     webClient.getStorageHolder().getSessionStorage(webClient.getCurrentWindow());
127         assertEquals(0, sessionStorage.size());
128 
129         sessionStorage.put("myCat", "Tom");
130 
131         final String html = DOCTYPE_HTML
132             + "<html><body>\n"
133             + "<script>\n"
134             + "  document.title = sessionStorage.getItem('myCat', 'Tom');"
135             + "</script>\n"
136             + "</body></html>";
137 
138         final HtmlPage page = loadPage(html);
139         assertEquals(1, sessionStorage.size());
140         assertEquals("Tom", page.getTitleText());
141     }
142 }