]>
Commit | Line | Data |
---|---|---|
2dad08ec SL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/controls/webtest.cpp | |
3 | // Purpose: wxWebView unit test | |
4 | // Author: Steven Lamerton | |
5 | // Created: 2011-07-08 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2011 Steven Lamerton | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #include "testprec.h" | |
11 | ||
30e2c046 | 12 | #if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE) |
2dad08ec SL |
13 | |
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/app.h" | |
20 | #endif // WX_PRECOMP | |
21 | ||
22 | #include "testableframe.h" | |
23 | #include "wx/uiaction.h" | |
24 | #include "wx/webview.h" | |
25 | #include "asserthelper.h" | |
26 | ||
27 | class WebTestCase : public CppUnit::TestCase | |
28 | { | |
29 | public: | |
30 | WebTestCase() { } | |
31 | ||
32 | void setUp(); | |
33 | void tearDown(); | |
34 | ||
35 | private: | |
36 | CPPUNIT_TEST_SUITE( WebTestCase ); | |
6a2ef29f | 37 | CPPUNIT_TEST( Title ); |
d07fd8b0 SL |
38 | CPPUNIT_TEST( Url ); |
39 | CPPUNIT_TEST( History ); | |
f152b4b9 SL |
40 | CPPUNIT_TEST( HistoryEnable ); |
41 | CPPUNIT_TEST( HistoryClear ); | |
1a693ec8 | 42 | CPPUNIT_TEST( HistoryList ); |
c7cbe308 | 43 | CPPUNIT_TEST( Editable ); |
c9355a3d | 44 | CPPUNIT_TEST( Selection ); |
18cf6bb5 | 45 | CPPUNIT_TEST( Zoom ); |
c9ccc09c | 46 | CPPUNIT_TEST( RunScript ); |
7f98bdd6 | 47 | CPPUNIT_TEST( SetPage ); |
2dad08ec SL |
48 | CPPUNIT_TEST_SUITE_END(); |
49 | ||
6a2ef29f | 50 | void Title(); |
d07fd8b0 SL |
51 | void Url(); |
52 | void History(); | |
f152b4b9 SL |
53 | void HistoryEnable(); |
54 | void HistoryClear(); | |
1a693ec8 | 55 | void HistoryList(); |
c7cbe308 | 56 | void Editable(); |
c9355a3d | 57 | void Selection(); |
18cf6bb5 | 58 | void Zoom(); |
c9ccc09c | 59 | void RunScript(); |
7f98bdd6 | 60 | void SetPage(); |
0609769d | 61 | void LoadUrl(int times = 1); |
6a2ef29f | 62 | |
2dad08ec | 63 | wxWebView* m_browser; |
1e0b0d7c | 64 | EventCounter* m_loaded; |
2dad08ec SL |
65 | |
66 | DECLARE_NO_COPY_CLASS(WebTestCase) | |
67 | }; | |
68 | ||
1e0b0d7c SL |
69 | //Convenience macro |
70 | #define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS((*m_loaded), 1) | |
71 | ||
2dad08ec SL |
72 | // register in the unnamed registry so that these tests are run by default |
73 | CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase ); | |
74 | ||
75 | // also include in its own registry so that these tests can be run alone | |
76 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" ); | |
77 | ||
78 | void WebTestCase::setUp() | |
79 | { | |
6a2ef29f | 80 | m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY); |
1e0b0d7c SL |
81 | |
82 | m_loaded = new EventCounter(m_browser, wxEVT_COMMAND_WEB_VIEW_LOADED); | |
83 | ENSURE_LOADED; | |
2dad08ec SL |
84 | } |
85 | ||
86 | void WebTestCase::tearDown() | |
87 | { | |
1e0b0d7c | 88 | wxDELETE(m_loaded); |
2dad08ec SL |
89 | wxDELETE(m_browser); |
90 | } | |
91 | ||
0609769d | 92 | void WebTestCase::LoadUrl(int times) |
3ce14be7 | 93 | { |
0609769d SL |
94 | //We alternate between urls as otherwise webkit merges them in the history |
95 | //we use about and about blank to avoid the need for a network connection | |
3ce14be7 SL |
96 | for(int i = 0; i < times; i++) |
97 | { | |
0609769d | 98 | if(i % 2 == 1) |
6edb3912 | 99 | m_browser->LoadURL("about:blank"); |
0609769d | 100 | else |
6edb3912 | 101 | m_browser->LoadURL("about:"); |
1e0b0d7c | 102 | ENSURE_LOADED; |
3ce14be7 SL |
103 | } |
104 | } | |
105 | ||
6a2ef29f SL |
106 | void WebTestCase::Title() |
107 | { | |
108 | CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle()); | |
109 | ||
110 | //Test title after loading raw html | |
0609769d | 111 | m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", ""); |
1e0b0d7c | 112 | ENSURE_LOADED; |
6a2ef29f SL |
113 | CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle()); |
114 | ||
115 | //Test title after loading a url, we yield to let events process | |
0609769d | 116 | LoadUrl(); |
6a2ef29f SL |
117 | CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle()); |
118 | } | |
119 | ||
d07fd8b0 SL |
120 | void WebTestCase::Url() |
121 | { | |
d563e28f | 122 | CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL()); |
d07fd8b0 | 123 | |
0609769d SL |
124 | //After first loading about:blank the next in the sequence is about: |
125 | LoadUrl(); | |
126 | CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL()); | |
d07fd8b0 SL |
127 | } |
128 | ||
129 | void WebTestCase::History() | |
130 | { | |
0609769d | 131 | LoadUrl(3); |
d07fd8b0 SL |
132 | |
133 | CPPUNIT_ASSERT(m_browser->CanGoBack()); | |
134 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
135 | ||
136 | m_browser->GoBack(); | |
1e0b0d7c | 137 | ENSURE_LOADED; |
d07fd8b0 SL |
138 | |
139 | CPPUNIT_ASSERT(m_browser->CanGoBack()); | |
140 | CPPUNIT_ASSERT(m_browser->CanGoForward()); | |
141 | ||
142 | m_browser->GoBack(); | |
1e0b0d7c | 143 | ENSURE_LOADED; |
d07fd8b0 | 144 | m_browser->GoBack(); |
1e0b0d7c | 145 | ENSURE_LOADED; |
d07fd8b0 SL |
146 | |
147 | //We should now be at the start of the history | |
148 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
149 | CPPUNIT_ASSERT(m_browser->CanGoForward()); | |
150 | } | |
151 | ||
f152b4b9 SL |
152 | void WebTestCase::HistoryEnable() |
153 | { | |
0609769d | 154 | LoadUrl(); |
f152b4b9 SL |
155 | m_browser->EnableHistory(false); |
156 | ||
157 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
158 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
159 | ||
0609769d | 160 | LoadUrl(); |
f152b4b9 SL |
161 | |
162 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
163 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
164 | } | |
165 | ||
166 | void WebTestCase::HistoryClear() | |
167 | { | |
0609769d | 168 | LoadUrl(2); |
f152b4b9 SL |
169 | |
170 | //Now we are in the 'middle' of the history | |
171 | m_browser->GoBack(); | |
1e0b0d7c | 172 | ENSURE_LOADED; |
f152b4b9 SL |
173 | |
174 | CPPUNIT_ASSERT(m_browser->CanGoForward()); | |
175 | CPPUNIT_ASSERT(m_browser->CanGoBack()); | |
176 | ||
177 | m_browser->ClearHistory(); | |
178 | ||
179 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
180 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
181 | } | |
182 | ||
1a693ec8 SL |
183 | void WebTestCase::HistoryList() |
184 | { | |
0609769d | 185 | LoadUrl(2); |
1a693ec8 | 186 | m_browser->GoBack(); |
1e0b0d7c | 187 | ENSURE_LOADED; |
1a693ec8 SL |
188 | |
189 | CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size()); | |
190 | CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size()); | |
191 | ||
192 | m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]); | |
1e0b0d7c | 193 | ENSURE_LOADED; |
1a693ec8 SL |
194 | |
195 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
196 | CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size()); | |
197 | } | |
198 | ||
c7cbe308 SL |
199 | void WebTestCase::Editable() |
200 | { | |
201 | CPPUNIT_ASSERT(!m_browser->IsEditable()); | |
202 | ||
203 | m_browser->SetEditable(true); | |
204 | ||
205 | CPPUNIT_ASSERT(m_browser->IsEditable()); | |
206 | ||
207 | m_browser->SetEditable(false); | |
208 | ||
209 | CPPUNIT_ASSERT(!m_browser->IsEditable()); | |
210 | } | |
211 | ||
c9355a3d SL |
212 | void WebTestCase::Selection() |
213 | { | |
a9c15339 | 214 | m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", ""); |
1e0b0d7c | 215 | ENSURE_LOADED; |
c9355a3d SL |
216 | CPPUNIT_ASSERT(!m_browser->HasSelection()); |
217 | ||
218 | m_browser->SelectAll(); | |
219 | ||
220 | CPPUNIT_ASSERT(m_browser->HasSelection()); | |
a9c15339 SL |
221 | CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText()); |
222 | //We lower case the result as ie returns tags in uppercase | |
1e0b0d7c | 223 | CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text", |
526292e7 | 224 | m_browser->GetSelectedSource().Lower()); |
41933aa5 SL |
225 | |
226 | m_browser->ClearSelection(); | |
1e0b0d7c | 227 | CPPUNIT_ASSERT(!m_browser->HasSelection()); |
c9355a3d SL |
228 | } |
229 | ||
18cf6bb5 SL |
230 | void WebTestCase::Zoom() |
231 | { | |
18cf6bb5 SL |
232 | if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT)) |
233 | { | |
234 | m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT); | |
235 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType()); | |
236 | ||
237 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY); | |
238 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom()); | |
239 | } | |
240 | ||
241 | //Reset the zoom level | |
242 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM); | |
243 | ||
244 | if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT)) | |
245 | { | |
246 | m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT); | |
247 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType()); | |
248 | ||
249 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY); | |
250 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom()); | |
251 | } | |
252 | } | |
253 | ||
c9ccc09c SL |
254 | void WebTestCase::RunScript() |
255 | { | |
256 | m_browser->RunScript("document.write(\"Hello World!\");"); | |
257 | CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText()); | |
258 | } | |
259 | ||
7f98bdd6 SL |
260 | void WebTestCase::SetPage() |
261 | { | |
262 | m_browser->SetPage("<html><body>text</body></html>", ""); | |
1e0b0d7c | 263 | ENSURE_LOADED; |
7f98bdd6 SL |
264 | CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText()); |
265 | ||
266 | m_browser->SetPage("<html><body>other text</body></html>", ""); | |
1e0b0d7c | 267 | ENSURE_LOADED; |
7f98bdd6 SL |
268 | CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText()); |
269 | } | |
270 | ||
30e2c046 | 271 | #endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE) |