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