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