Yield after control creation to allow it to load the initial page.
[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 //We yield to let the initial page load
72 wxYield();
73 }
74
75 void WebTestCase::tearDown()
76 {
77 wxDELETE(m_browser);
78 }
79
80 void WebTestCase::LoadUrl(const wxString& url, int times)
81 {
82 for(int i = 0; i < times; i++)
83 {
84 m_browser->LoadUrl(url);
85 wxYield();
86 }
87 }
88
89 void WebTestCase::Title()
90 {
91 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
92
93 //Test title after loading raw html
94 m_browser->SetPage("<html><title>Title</title></html>", "");
95 CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
96
97 //Test title after loading a url, we yield to let events process
98 LoadUrl("about:blank");
99 CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
100 }
101
102 void WebTestCase::Url()
103 {
104 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
105
106 LoadUrl("about:blank");
107 CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
108 }
109
110 void WebTestCase::History()
111 {
112 //We use about:blank to remove the need for a network connection
113 LoadUrl("about:blank", 3);
114
115 CPPUNIT_ASSERT(m_browser->CanGoBack());
116 CPPUNIT_ASSERT(!m_browser->CanGoForward());
117
118 m_browser->GoBack();
119
120 CPPUNIT_ASSERT(m_browser->CanGoBack());
121 CPPUNIT_ASSERT(m_browser->CanGoForward());
122
123 m_browser->GoBack();
124 m_browser->GoBack();
125
126 //We should now be at the start of the history
127 CPPUNIT_ASSERT(!m_browser->CanGoBack());
128 CPPUNIT_ASSERT(m_browser->CanGoForward());
129 }
130
131 void WebTestCase::HistoryEnable()
132 {
133 LoadUrl("about:blank");
134 m_browser->EnableHistory(false);
135
136 CPPUNIT_ASSERT(!m_browser->CanGoForward());
137 CPPUNIT_ASSERT(!m_browser->CanGoBack());
138
139 LoadUrl("about:blank");
140
141 CPPUNIT_ASSERT(!m_browser->CanGoForward());
142 CPPUNIT_ASSERT(!m_browser->CanGoBack());
143 }
144
145 void WebTestCase::HistoryClear()
146 {
147 LoadUrl("about:blank", 2);
148
149 //Now we are in the 'middle' of the history
150 m_browser->GoBack();
151
152 CPPUNIT_ASSERT(m_browser->CanGoForward());
153 CPPUNIT_ASSERT(m_browser->CanGoBack());
154
155 m_browser->ClearHistory();
156
157 CPPUNIT_ASSERT(!m_browser->CanGoForward());
158 CPPUNIT_ASSERT(!m_browser->CanGoBack());
159 }
160
161 void WebTestCase::HistoryList()
162 {
163 LoadUrl("about:blank", 2);
164 m_browser->GoBack();
165
166 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
167 CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
168
169 m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
170
171 CPPUNIT_ASSERT(!m_browser->CanGoForward());
172 CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
173 }
174
175 void WebTestCase::Editable()
176 {
177 CPPUNIT_ASSERT(!m_browser->IsEditable());
178
179 m_browser->SetEditable(true);
180
181 CPPUNIT_ASSERT(m_browser->IsEditable());
182
183 m_browser->SetEditable(false);
184
185 CPPUNIT_ASSERT(!m_browser->IsEditable());
186 }
187
188 void WebTestCase::Selection()
189 {
190 m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
191 CPPUNIT_ASSERT(!m_browser->HasSelection());
192
193 m_browser->SelectAll();
194
195 CPPUNIT_ASSERT(m_browser->HasSelection());
196 CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
197 //We lower case the result as ie returns tags in uppercase
198 CPPUNIT_ASSERT_EQUAL("some <strong>strong</strong> text",
199 m_browser->GetSelectedSource().Lower());
200
201 m_browser->DeleteSelection();
202
203 CPPUNIT_ASSERT(!m_browser->HasSelection());
204 }
205
206 #endif //wxUSE_WEB