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