]> git.saurik.com Git - wxWidgets.git/blame - tests/controls/webtest.cpp
Removed redundant DoEnable
[wxWidgets.git] / tests / controls / webtest.cpp
CommitLineData
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
27class WebTestCase : public CppUnit::TestCase
28{
29public:
30 WebTestCase() { }
31
32 void setUp();
33 void tearDown();
34
35private:
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
SL
63 wxWebView* m_browser;
64
65 DECLARE_NO_COPY_CLASS(WebTestCase)
66};
67
68// register in the unnamed registry so that these tests are run by default
69CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
70
71// also include in its own registry so that these tests can be run alone
72CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
73
74void WebTestCase::setUp()
75{
6a2ef29f 76 m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
d563e28f
SL
77 //We yield to let the initial page load
78 wxYield();
2dad08ec
SL
79}
80
81void WebTestCase::tearDown()
82{
83 wxDELETE(m_browser);
84}
85
0609769d 86void WebTestCase::LoadUrl(int times)
3ce14be7 87{
0609769d
SL
88 //We alternate between urls as otherwise webkit merges them in the history
89 //we use about and about blank to avoid the need for a network connection
3ce14be7
SL
90 for(int i = 0; i < times; i++)
91 {
0609769d 92 if(i % 2 == 1)
6edb3912 93 m_browser->LoadURL("about:blank");
0609769d 94 else
6edb3912 95 m_browser->LoadURL("about:");
3ce14be7
SL
96 wxYield();
97 }
98}
99
6a2ef29f
SL
100void WebTestCase::Title()
101{
102 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
103
104 //Test title after loading raw html
0609769d
SL
105 m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", "");
106 wxYield();
6a2ef29f
SL
107 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
108
109 //Test title after loading a url, we yield to let events process
0609769d 110 LoadUrl();
6a2ef29f
SL
111 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
112}
113
d07fd8b0
SL
114void WebTestCase::Url()
115{
8a316482
SL
116 // FIXME: This test fails on MSW buildbot slaves although works fine on
117 // development machine.
118 if ( wxGetUserId().Lower().Matches("buildslave*") )
119 return;
120
d563e28f 121 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
d07fd8b0 122
0609769d
SL
123 //After first loading about:blank the next in the sequence is about:
124 LoadUrl();
125 CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
d07fd8b0
SL
126}
127
128void WebTestCase::History()
129{
8a316482
SL
130 // FIXME: This test fails on MSW buildbot slaves although works fine on
131 // development machine.
132 if ( wxGetUserId().Lower().Matches("buildslave*") )
133 return;
134
0609769d 135 LoadUrl(3);
d07fd8b0
SL
136
137 CPPUNIT_ASSERT(m_browser->CanGoBack());
138 CPPUNIT_ASSERT(!m_browser->CanGoForward());
139
140 m_browser->GoBack();
ed1c9160 141 wxYield();
d07fd8b0
SL
142
143 CPPUNIT_ASSERT(m_browser->CanGoBack());
144 CPPUNIT_ASSERT(m_browser->CanGoForward());
145
146 m_browser->GoBack();
ed1c9160 147 wxYield();
d07fd8b0 148 m_browser->GoBack();
ed1c9160 149 wxYield();
d07fd8b0
SL
150
151 //We should now be at the start of the history
152 CPPUNIT_ASSERT(!m_browser->CanGoBack());
153 CPPUNIT_ASSERT(m_browser->CanGoForward());
154}
155
f152b4b9
SL
156void WebTestCase::HistoryEnable()
157{
0609769d 158 LoadUrl();
f152b4b9
SL
159 m_browser->EnableHistory(false);
160
161 CPPUNIT_ASSERT(!m_browser->CanGoForward());
162 CPPUNIT_ASSERT(!m_browser->CanGoBack());
163
0609769d 164 LoadUrl();
f152b4b9
SL
165
166 CPPUNIT_ASSERT(!m_browser->CanGoForward());
167 CPPUNIT_ASSERT(!m_browser->CanGoBack());
168}
169
170void WebTestCase::HistoryClear()
171{
d296f9a1
SL
172 // FIXME: This test fails on MSW buildbot slaves although works fine on
173 // development machine.
174 if ( wxGetUserId().Lower().Matches("buildslave*") )
175 return;
176
0609769d 177 LoadUrl(2);
f152b4b9
SL
178
179 //Now we are in the 'middle' of the history
180 m_browser->GoBack();
0609769d 181 wxYield();
f152b4b9
SL
182
183 CPPUNIT_ASSERT(m_browser->CanGoForward());
184 CPPUNIT_ASSERT(m_browser->CanGoBack());
185
186 m_browser->ClearHistory();
187
188 CPPUNIT_ASSERT(!m_browser->CanGoForward());
189 CPPUNIT_ASSERT(!m_browser->CanGoBack());
190}
191
1a693ec8
SL
192void WebTestCase::HistoryList()
193{
8a316482
SL
194 // FIXME: This test fails on MSW buildbot slaves although works fine on
195 // development machine.
196 if ( wxGetUserId().Lower().Matches("buildslave*") )
197 return;
198
0609769d 199 LoadUrl(2);
1a693ec8 200 m_browser->GoBack();
ed1c9160 201 wxYield();
1a693ec8
SL
202
203 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
204 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
205
206 m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
ed1c9160 207 wxYield();
1a693ec8
SL
208
209 CPPUNIT_ASSERT(!m_browser->CanGoForward());
210 CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
211}
212
c7cbe308
SL
213void WebTestCase::Editable()
214{
215 CPPUNIT_ASSERT(!m_browser->IsEditable());
216
217 m_browser->SetEditable(true);
218
219 CPPUNIT_ASSERT(m_browser->IsEditable());
220
221 m_browser->SetEditable(false);
222
223 CPPUNIT_ASSERT(!m_browser->IsEditable());
224}
225
c9355a3d
SL
226void WebTestCase::Selection()
227{
a9c15339 228 m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
0609769d 229 wxYield();
c9355a3d
SL
230 CPPUNIT_ASSERT(!m_browser->HasSelection());
231
232 m_browser->SelectAll();
233
234 CPPUNIT_ASSERT(m_browser->HasSelection());
a9c15339
SL
235 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
236 //We lower case the result as ie returns tags in uppercase
237 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
526292e7 238 m_browser->GetSelectedSource().Lower());
41933aa5
SL
239
240 m_browser->ClearSelection();
241 CPPUNIT_ASSERT(!m_browser->HasSelection());
c9355a3d
SL
242}
243
18cf6bb5
SL
244void WebTestCase::Zoom()
245{
8a316482
SL
246 // FIXME: This test fails on MSW buildbot slaves although works fine on
247 // development machine.
248 if ( wxGetUserId().Lower().Matches("buildslave*") )
249 return;
250
18cf6bb5
SL
251 if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT))
252 {
253 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT);
254 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType());
255
256 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
257 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom());
258 }
259
260 //Reset the zoom level
261 m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM);
262
263 if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT))
264 {
265 m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT);
266 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType());
267
268 m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY);
269 CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom());
270 }
271}
272
c9ccc09c
SL
273void WebTestCase::RunScript()
274{
275 m_browser->RunScript("document.write(\"Hello World!\");");
276 CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
277}
278
7f98bdd6
SL
279void WebTestCase::SetPage()
280{
281 m_browser->SetPage("<html><body>text</body></html>", "");
282 CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText());
283
284 m_browser->SetPage("<html><body>other text</body></html>", "");
285 CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText());
286}
287
30e2c046 288#endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)