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 org.htmlunit.html.HtmlPage;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link History} with {@link WebClient}.
23   *
24   * @author Madis Pärn
25   * @author Ronald Brill
26   */
27  public class History2Test extends SimpleWebTestCase {
28  
29      /**
30       * Tests history {@link WebClientOptions#setHistorySizeLimit(int) sizeLimit}.
31       *
32       * @throws Exception if an error occurs
33       */
34      @Test
35      @Alerts("5")
36      public void historyCacheLimit() throws Exception {
37          final String content = DOCTYPE_HTML
38                  + "<html><head>\n"
39                  + "<script>\n"
40                  + "function log(msg) { window.document.title = msg; }\n"
41                  + "function test() {\n"
42                  + "  for(var idx = 0; idx < 100; idx++) {\n"
43                  + "    history.pushState({}, 'Page '+idx, 'page_'+idx+'.html');\n"
44                  + "  }\n"
45                  + "  log(history.length);\n"
46                  + "}\n"
47                  + "</script>\n"
48                  + "</head><body onload='test()'>\n"
49                  + "</body></html>";
50  
51          final WebClient webClient = getWebClientWithMockWebConnection();
52          webClient.getOptions().setHistorySizeLimit(5);
53          final HtmlPage page = loadPage(content);
54          assertEquals(getExpectedAlerts()[0], page.getTitleText());
55      }
56  
57      /**
58       * Tests going back in history should use the page cache.
59       *
60       * @throws Exception if an error occurs
61       */
62      @Test
63      public void historyCacheSize() throws Exception {
64          final String content = DOCTYPE_HTML
65                  + "<html><head><title></title>\n"
66                  + "</head>\n"
67                  + "<body>\n"
68                  + "</body></html>";
69  
70          final int startCount = getMockWebConnection().getRequestCount();
71          getMockWebConnection().setDefaultResponse(content);
72          final WebClient webClient = getWebClientWithMockWebConnection();
73          webClient.getOptions().setHistorySizeLimit(5);
74  
75          final TopLevelWindow window = (TopLevelWindow) webClient.getCurrentWindow();
76          final History history = window.getHistory();
77  
78          loadPage(content);
79          loadPage(content);
80          loadPage(content);
81          loadPage(content);
82          history.back();
83          history.back();
84          history.back();
85          history.back();
86  
87          assertEquals(4, getMockWebConnection().getRequestCount() - startCount);
88      }
89  
90      /**
91       * Tests going back in history should use the page cache, but we have
92       * to respect the HistoryPageCacheLimit.
93       *
94       * @throws Exception if an error occurs
95       */
96      @Test
97      public void historyPageCacheLimit() throws Exception {
98          final String content = DOCTYPE_HTML
99                  + "<html><head>\n"
100                 + "</head>\n"
101                 + "<body>\n"
102                 + "</body></html>";
103 
104         final int startCount = getMockWebConnection().getRequestCount();
105         getMockWebConnection().setDefaultResponse(content);
106         final WebClient webClient = getWebClientWithMockWebConnection();
107         webClient.getOptions().setHistorySizeLimit(5);
108         webClient.getOptions().setHistoryPageCacheLimit(2);
109 
110         final TopLevelWindow window = (TopLevelWindow) webClient.getCurrentWindow();
111         final History history = window.getHistory();
112 
113         loadPage(content);
114         loadPage(content);
115         loadPage(content);
116         loadPage(content);
117         history.back();
118         history.back();
119         history.back();
120 
121         assertEquals(6, getMockWebConnection().getRequestCount() - startCount);
122     }
123 
124     /**
125      * Tests going back in history should use the page cache, but we have
126      * to respect the HistoryPageCacheLimit.
127      *
128      * @throws Exception if an error occurs
129      */
130     @Test
131     public void historyPageCacheLimitZero() throws Exception {
132         final String content = DOCTYPE_HTML
133                 + "<html><head>\n"
134                 + "</head>\n"
135                 + "<body>\n"
136                 + "</body></html>";
137 
138         final int startCount = getMockWebConnection().getRequestCount();
139         getMockWebConnection().setDefaultResponse(content);
140         final WebClient webClient = getWebClientWithMockWebConnection();
141         webClient.getOptions().setHistorySizeLimit(5);
142         webClient.getOptions().setHistoryPageCacheLimit(0);
143 
144         final TopLevelWindow window = (TopLevelWindow) webClient.getCurrentWindow();
145         final History history = window.getHistory();
146 
147         loadPage(content);
148         loadPage(content);
149         loadPage(content);
150         loadPage(content);
151         history.back();
152         history.back();
153         history.back();
154 
155         assertEquals(7, getMockWebConnection().getRequestCount() - startCount);
156     }
157 
158     /**
159      * Tests going back in history should use the page cache, but we have
160      * to respect the HistoryPageCacheLimit.
161      *
162      * @throws Exception if an error occurs
163      */
164     @Test
165     public void historyPageCacheLimitMinusOne() throws Exception {
166         final String content = DOCTYPE_HTML
167                 + "<html><head>\n"
168                 + "</head>\n"
169                 + "<body>\n"
170                 + "</body></html>";
171 
172         final int startCount = getMockWebConnection().getRequestCount();
173         getMockWebConnection().setDefaultResponse(content);
174         final WebClient webClient = getWebClientWithMockWebConnection();
175         webClient.getOptions().setHistorySizeLimit(5);
176         webClient.getOptions().setHistoryPageCacheLimit(-1);
177 
178         final TopLevelWindow window = (TopLevelWindow) webClient.getCurrentWindow();
179         final History history = window.getHistory();
180 
181         loadPage(content);
182         loadPage(content);
183         loadPage(content);
184         loadPage(content);
185         history.back();
186         history.back();
187         history.back();
188 
189         assertEquals(7, getMockWebConnection().getRequestCount() - startCount);
190     }
191 }