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