Update web units tests so that they compile after api change.
[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_SUITE_END();
46
47 void Title();
48 void Url();
49 void History();
50 void HistoryEnable();
51 void HistoryClear();
52 void HistoryList();
53 void Editable();
54 void Selection();
55 void LoadUrl(const wxString& url, int times = 1);
56
57 wxWebView* m_browser;
58
59 DECLARE_NO_COPY_CLASS(WebTestCase)
60 };
61
62 // register in the unnamed registry so that these tests are run by default
63 CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
64
65 // also include in its own registry so that these tests can be run alone
66 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
67
68 void WebTestCase::setUp()
69 {
70 m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
71 }
72
73 void WebTestCase::tearDown()
74 {
75 wxDELETE(m_browser);
76 }
77
78 void WebTestCase::LoadUrl(const wxString& url, int times)
79 {
80 for(int i = 0; i < times; i++)
81 {
82 m_browser->LoadUrl(url);
83 wxYield();
84 }
85 }
86
87 void WebTestCase::Title()
88 {
89 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
90
91 //Test title after loading raw html
92 m_browser->SetPage("<html><title>Title</title></html>", "");
93 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
94
95 //Test title after loading a url, we yield to let events process
96 LoadUrl("about:blank");
97 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
98 }
99
100 void WebTestCase::Url()
101 {
102 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentURL());
103
104 LoadUrl("about:blank");
105 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
106 }
107
108 void WebTestCase::History()
109 {
110 //We use about:blank to remove the need for a network connection
111 LoadUrl("about:blank", 3);
112
113 CPPUNIT_ASSERT(m_browser->CanGoBack());
114 CPPUNIT_ASSERT(!m_browser->CanGoForward());
115
116 m_browser->GoBack();
117
118 CPPUNIT_ASSERT(m_browser->CanGoBack());
119 CPPUNIT_ASSERT(m_browser->CanGoForward());
120
121 m_browser->GoBack();
122 m_browser->GoBack();
123
124 //We should now be at the start of the history
125 CPPUNIT_ASSERT(!m_browser->CanGoBack());
126 CPPUNIT_ASSERT(m_browser->CanGoForward());
127 }
128
129 void WebTestCase::HistoryEnable()
130 {
131 LoadUrl("about:blank");
132 m_browser->EnableHistory(false);
133
134 CPPUNIT_ASSERT(!m_browser->CanGoForward());
135 CPPUNIT_ASSERT(!m_browser->CanGoBack());
136
137 LoadUrl("about:blank");
138
139 CPPUNIT_ASSERT(!m_browser->CanGoForward());
140 CPPUNIT_ASSERT(!m_browser->CanGoBack());
141 }
142
143 void WebTestCase::HistoryClear()
144 {
145 LoadUrl("about:blank", 2);
146
147 //Now we are in the 'middle' of the history
148 m_browser->GoBack();
149
150 CPPUNIT_ASSERT(m_browser->CanGoForward());
151 CPPUNIT_ASSERT(m_browser->CanGoBack());
152
153 m_browser->ClearHistory();
154
155 CPPUNIT_ASSERT(!m_browser->CanGoForward());
156 CPPUNIT_ASSERT(!m_browser->CanGoBack());
157 }
158
159 void WebTestCase::HistoryList()
160 {
161 LoadUrl("about:blank", 2);
162 m_browser->GoBack();
163
164 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
165 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
166
167 m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
168
169 CPPUNIT_ASSERT(!m_browser->CanGoForward());
170 CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
171 }
172
173 void WebTestCase::Editable()
174 {
175 CPPUNIT_ASSERT(!m_browser->IsEditable());
176
177 m_browser->SetEditable(true);
178
179 CPPUNIT_ASSERT(m_browser->IsEditable());
180
181 m_browser->SetEditable(false);
182
183 CPPUNIT_ASSERT(!m_browser->IsEditable());
184 }
185
186 void WebTestCase::Selection()
187 {
188 m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
189 CPPUNIT_ASSERT(!m_browser->HasSelection());
190
191 m_browser->SelectAll();
192
193 CPPUNIT_ASSERT(m_browser->HasSelection());
194 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
195 //We lower case the result as ie returns tags in uppercase
196 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
197 m_browser->GetSelectedSource().Lower());
198
199 m_browser->DeleteSelection();
200
201 CPPUNIT_ASSERT(!m_browser->HasSelection());
202 }
203
204 #endif //wxUSE_WEB