1
2
3
4
5
6
7
8
9
10
11
12
13
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
23
24
25
26
27 public class History2Test extends SimpleWebTestCase {
28
29
30
31
32
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
59
60
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
92
93
94
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
126
127
128
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
160
161
162
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 }