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.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28
29 @RunWith(BrowserRunner.class)
30 public class History2Test extends SimpleWebTestCase {
31
32
33
34
35
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
62
63
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
95
96
97
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
129
130
131
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
163
164
165
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 }