]>
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 | ||
e7aef23e | 12 | #if wxUSE_WEBVIEW |
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 ); |
2dad08ec SL |
47 | CPPUNIT_TEST_SUITE_END(); |
48 | ||
6a2ef29f | 49 | void Title(); |
d07fd8b0 SL |
50 | void Url(); |
51 | void History(); | |
f152b4b9 SL |
52 | void HistoryEnable(); |
53 | void HistoryClear(); | |
1a693ec8 | 54 | void HistoryList(); |
c7cbe308 | 55 | void Editable(); |
c9355a3d | 56 | void Selection(); |
18cf6bb5 | 57 | void Zoom(); |
c9ccc09c | 58 | void RunScript(); |
0609769d | 59 | void LoadUrl(int times = 1); |
6a2ef29f | 60 | |
2dad08ec SL |
61 | wxWebView* m_browser; |
62 | ||
63 | DECLARE_NO_COPY_CLASS(WebTestCase) | |
64 | }; | |
65 | ||
66 | // register in the unnamed registry so that these tests are run by default | |
67 | CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase ); | |
68 | ||
69 | // also include in its own registry so that these tests can be run alone | |
70 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" ); | |
71 | ||
72 | void WebTestCase::setUp() | |
73 | { | |
6a2ef29f | 74 | m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY); |
d563e28f SL |
75 | //We yield to let the initial page load |
76 | wxYield(); | |
2dad08ec SL |
77 | } |
78 | ||
79 | void WebTestCase::tearDown() | |
80 | { | |
81 | wxDELETE(m_browser); | |
82 | } | |
83 | ||
0609769d | 84 | void WebTestCase::LoadUrl(int times) |
3ce14be7 | 85 | { |
0609769d SL |
86 | //We alternate between urls as otherwise webkit merges them in the history |
87 | //we use about and about blank to avoid the need for a network connection | |
3ce14be7 SL |
88 | for(int i = 0; i < times; i++) |
89 | { | |
0609769d | 90 | if(i % 2 == 1) |
6edb3912 | 91 | m_browser->LoadURL("about:blank"); |
0609769d | 92 | else |
6edb3912 | 93 | m_browser->LoadURL("about:"); |
3ce14be7 SL |
94 | wxYield(); |
95 | } | |
96 | } | |
97 | ||
6a2ef29f SL |
98 | void WebTestCase::Title() |
99 | { | |
100 | CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle()); | |
101 | ||
102 | //Test title after loading raw html | |
0609769d SL |
103 | m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", ""); |
104 | wxYield(); | |
6a2ef29f SL |
105 | CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle()); |
106 | ||
107 | //Test title after loading a url, we yield to let events process | |
0609769d | 108 | LoadUrl(); |
6a2ef29f SL |
109 | CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle()); |
110 | } | |
111 | ||
d07fd8b0 SL |
112 | void WebTestCase::Url() |
113 | { | |
d563e28f | 114 | CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL()); |
d07fd8b0 | 115 | |
0609769d SL |
116 | //After first loading about:blank the next in the sequence is about: |
117 | LoadUrl(); | |
118 | CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL()); | |
d07fd8b0 SL |
119 | } |
120 | ||
121 | void WebTestCase::History() | |
122 | { | |
0609769d | 123 | LoadUrl(3); |
d07fd8b0 SL |
124 | |
125 | CPPUNIT_ASSERT(m_browser->CanGoBack()); | |
126 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
127 | ||
128 | m_browser->GoBack(); | |
129 | ||
130 | CPPUNIT_ASSERT(m_browser->CanGoBack()); | |
131 | CPPUNIT_ASSERT(m_browser->CanGoForward()); | |
132 | ||
133 | m_browser->GoBack(); | |
134 | m_browser->GoBack(); | |
135 | ||
136 | //We should now be at the start of the history | |
137 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
138 | CPPUNIT_ASSERT(m_browser->CanGoForward()); | |
139 | } | |
140 | ||
f152b4b9 SL |
141 | void WebTestCase::HistoryEnable() |
142 | { | |
0609769d | 143 | LoadUrl(); |
f152b4b9 SL |
144 | m_browser->EnableHistory(false); |
145 | ||
146 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
147 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
148 | ||
0609769d | 149 | LoadUrl(); |
f152b4b9 SL |
150 | |
151 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
152 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
153 | } | |
154 | ||
155 | void WebTestCase::HistoryClear() | |
156 | { | |
0609769d | 157 | LoadUrl(2); |
f152b4b9 SL |
158 | |
159 | //Now we are in the 'middle' of the history | |
160 | m_browser->GoBack(); | |
0609769d | 161 | wxYield(); |
f152b4b9 SL |
162 | |
163 | CPPUNIT_ASSERT(m_browser->CanGoForward()); | |
164 | CPPUNIT_ASSERT(m_browser->CanGoBack()); | |
165 | ||
166 | m_browser->ClearHistory(); | |
167 | ||
168 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
169 | CPPUNIT_ASSERT(!m_browser->CanGoBack()); | |
170 | } | |
171 | ||
1a693ec8 SL |
172 | void WebTestCase::HistoryList() |
173 | { | |
0609769d | 174 | LoadUrl(2); |
1a693ec8 SL |
175 | m_browser->GoBack(); |
176 | ||
177 | CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size()); | |
178 | CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size()); | |
179 | ||
180 | m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]); | |
181 | ||
182 | CPPUNIT_ASSERT(!m_browser->CanGoForward()); | |
183 | CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size()); | |
184 | } | |
185 | ||
c7cbe308 SL |
186 | void WebTestCase::Editable() |
187 | { | |
188 | CPPUNIT_ASSERT(!m_browser->IsEditable()); | |
189 | ||
190 | m_browser->SetEditable(true); | |
191 | ||
192 | CPPUNIT_ASSERT(m_browser->IsEditable()); | |
193 | ||
194 | m_browser->SetEditable(false); | |
195 | ||
196 | CPPUNIT_ASSERT(!m_browser->IsEditable()); | |
197 | } | |
198 | ||
c9355a3d SL |
199 | void WebTestCase::Selection() |
200 | { | |
a9c15339 | 201 | m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", ""); |
0609769d | 202 | wxYield(); |
c9355a3d SL |
203 | CPPUNIT_ASSERT(!m_browser->HasSelection()); |
204 | ||
205 | m_browser->SelectAll(); | |
206 | ||
207 | CPPUNIT_ASSERT(m_browser->HasSelection()); | |
a9c15339 SL |
208 | CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText()); |
209 | //We lower case the result as ie returns tags in uppercase | |
210 | CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text", | |
526292e7 | 211 | m_browser->GetSelectedSource().Lower()); |
41933aa5 SL |
212 | |
213 | m_browser->ClearSelection(); | |
214 | CPPUNIT_ASSERT(!m_browser->HasSelection()); | |
c9355a3d SL |
215 | } |
216 | ||
18cf6bb5 SL |
217 | void WebTestCase::Zoom() |
218 | { | |
18cf6bb5 SL |
219 | if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT)) |
220 | { | |
221 | m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_LAYOUT); | |
222 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType()); | |
223 | ||
224 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY); | |
225 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom()); | |
226 | } | |
227 | ||
228 | //Reset the zoom level | |
229 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_MEDIUM); | |
230 | ||
231 | if(m_browser->CanSetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT)) | |
232 | { | |
233 | m_browser->SetZoomType(wxWEB_VIEW_ZOOM_TYPE_TEXT); | |
234 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType()); | |
235 | ||
236 | m_browser->SetZoom(wxWEB_VIEW_ZOOM_TINY); | |
237 | CPPUNIT_ASSERT_EQUAL(wxWEB_VIEW_ZOOM_TINY, m_browser->GetZoom()); | |
238 | } | |
239 | } | |
240 | ||
c9ccc09c SL |
241 | void WebTestCase::RunScript() |
242 | { | |
243 | m_browser->RunScript("document.write(\"Hello World!\");"); | |
244 | CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText()); | |
245 | } | |
246 | ||
e7aef23e | 247 | #endif //wxUSE_WEBVIEW |