webtest.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/webtest.cpp
  3. // Purpose: wxWebView unit test
  4. // Author: Steven Lamerton
  5. // Created: 2011-07-08
  6. // Copyright: (c) 2011 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #if wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #ifndef WX_PRECOMP
  14. #include "wx/app.h"
  15. #endif // WX_PRECOMP
  16. #include "testableframe.h"
  17. #include "wx/uiaction.h"
  18. #include "wx/webview.h"
  19. #include "asserthelper.h"
  20. class WebTestCase : public CppUnit::TestCase
  21. {
  22. public:
  23. WebTestCase() { }
  24. void setUp();
  25. void tearDown();
  26. private:
  27. CPPUNIT_TEST_SUITE( WebTestCase );
  28. CPPUNIT_TEST( Title );
  29. CPPUNIT_TEST( Url );
  30. CPPUNIT_TEST( History );
  31. CPPUNIT_TEST( HistoryEnable );
  32. CPPUNIT_TEST( HistoryClear );
  33. CPPUNIT_TEST( HistoryList );
  34. CPPUNIT_TEST( Editable );
  35. CPPUNIT_TEST( Selection );
  36. CPPUNIT_TEST( Zoom );
  37. CPPUNIT_TEST( RunScript );
  38. CPPUNIT_TEST( SetPage );
  39. CPPUNIT_TEST_SUITE_END();
  40. void Title();
  41. void Url();
  42. void History();
  43. void HistoryEnable();
  44. void HistoryClear();
  45. void HistoryList();
  46. void Editable();
  47. void Selection();
  48. void Zoom();
  49. void RunScript();
  50. void SetPage();
  51. void LoadUrl(int times = 1);
  52. wxWebView* m_browser;
  53. EventCounter* m_loaded;
  54. DECLARE_NO_COPY_CLASS(WebTestCase)
  55. };
  56. //Convenience macro
  57. #define ENSURE_LOADED WX_ASSERT_EVENT_OCCURS((*m_loaded), 1)
  58. // register in the unnamed registry so that these tests are run by default
  59. CPPUNIT_TEST_SUITE_REGISTRATION( WebTestCase );
  60. // also include in its own registry so that these tests can be run alone
  61. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WebTestCase, "WebTestCase" );
  62. void WebTestCase::setUp()
  63. {
  64. m_browser = wxWebView::New(wxTheApp->GetTopWindow(), wxID_ANY);
  65. m_loaded = new EventCounter(m_browser, wxEVT_WEBVIEW_LOADED);
  66. m_browser->LoadURL("about:blank");
  67. ENSURE_LOADED;
  68. }
  69. void WebTestCase::tearDown()
  70. {
  71. wxDELETE(m_loaded);
  72. wxDELETE(m_browser);
  73. }
  74. void WebTestCase::LoadUrl(int times)
  75. {
  76. //We alternate between urls as otherwise webkit merges them in the history
  77. //we use about and about blank to avoid the need for a network connection
  78. for(int i = 0; i < times; i++)
  79. {
  80. if(i % 2 == 1)
  81. m_browser->LoadURL("about:blank");
  82. else
  83. m_browser->LoadURL("about:");
  84. ENSURE_LOADED;
  85. }
  86. }
  87. void WebTestCase::Title()
  88. {
  89. CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
  90. //Test title after loading raw html
  91. m_browser->SetPage("<html><title>Title</title><body>Text</body></html>", "");
  92. ENSURE_LOADED;
  93. CPPUNIT_ASSERT_EQUAL("Title", m_browser->GetCurrentTitle());
  94. //Test title after loading a url, we yield to let events process
  95. LoadUrl();
  96. CPPUNIT_ASSERT_EQUAL("", m_browser->GetCurrentTitle());
  97. }
  98. void WebTestCase::Url()
  99. {
  100. CPPUNIT_ASSERT_EQUAL("about:blank", m_browser->GetCurrentURL());
  101. //After first loading about:blank the next in the sequence is about:
  102. LoadUrl();
  103. CPPUNIT_ASSERT_EQUAL("about:", m_browser->GetCurrentURL());
  104. }
  105. void WebTestCase::History()
  106. {
  107. LoadUrl(3);
  108. CPPUNIT_ASSERT(m_browser->CanGoBack());
  109. CPPUNIT_ASSERT(!m_browser->CanGoForward());
  110. m_browser->GoBack();
  111. ENSURE_LOADED;
  112. CPPUNIT_ASSERT(m_browser->CanGoBack());
  113. CPPUNIT_ASSERT(m_browser->CanGoForward());
  114. m_browser->GoBack();
  115. ENSURE_LOADED;
  116. m_browser->GoBack();
  117. ENSURE_LOADED;
  118. //We should now be at the start of the history
  119. CPPUNIT_ASSERT(!m_browser->CanGoBack());
  120. CPPUNIT_ASSERT(m_browser->CanGoForward());
  121. }
  122. void WebTestCase::HistoryEnable()
  123. {
  124. LoadUrl();
  125. m_browser->EnableHistory(false);
  126. CPPUNIT_ASSERT(!m_browser->CanGoForward());
  127. CPPUNIT_ASSERT(!m_browser->CanGoBack());
  128. LoadUrl();
  129. CPPUNIT_ASSERT(!m_browser->CanGoForward());
  130. CPPUNIT_ASSERT(!m_browser->CanGoBack());
  131. }
  132. void WebTestCase::HistoryClear()
  133. {
  134. LoadUrl(2);
  135. //Now we are in the 'middle' of the history
  136. m_browser->GoBack();
  137. ENSURE_LOADED;
  138. CPPUNIT_ASSERT(m_browser->CanGoForward());
  139. CPPUNIT_ASSERT(m_browser->CanGoBack());
  140. m_browser->ClearHistory();
  141. CPPUNIT_ASSERT(!m_browser->CanGoForward());
  142. CPPUNIT_ASSERT(!m_browser->CanGoBack());
  143. }
  144. void WebTestCase::HistoryList()
  145. {
  146. LoadUrl(2);
  147. m_browser->GoBack();
  148. ENSURE_LOADED;
  149. CPPUNIT_ASSERT_EQUAL(1, m_browser->GetBackwardHistory().size());
  150. CPPUNIT_ASSERT_EQUAL(1, m_browser->GetForwardHistory().size());
  151. m_browser->LoadHistoryItem(m_browser->GetForwardHistory()[0]);
  152. ENSURE_LOADED;
  153. CPPUNIT_ASSERT(!m_browser->CanGoForward());
  154. CPPUNIT_ASSERT_EQUAL(2, m_browser->GetBackwardHistory().size());
  155. }
  156. void WebTestCase::Editable()
  157. {
  158. CPPUNIT_ASSERT(!m_browser->IsEditable());
  159. m_browser->SetEditable(true);
  160. CPPUNIT_ASSERT(m_browser->IsEditable());
  161. m_browser->SetEditable(false);
  162. CPPUNIT_ASSERT(!m_browser->IsEditable());
  163. }
  164. void WebTestCase::Selection()
  165. {
  166. m_browser->SetPage("<html><body>Some <strong>strong</strong> text</body></html>", "");
  167. ENSURE_LOADED;
  168. CPPUNIT_ASSERT(!m_browser->HasSelection());
  169. m_browser->SelectAll();
  170. CPPUNIT_ASSERT(m_browser->HasSelection());
  171. CPPUNIT_ASSERT_EQUAL("Some strong text", m_browser->GetSelectedText());
  172. // The web engine doesn't necessarily represent the HTML in the same way as
  173. // we used above, e.g. IE uses upper case for all the tags while WebKit
  174. // under OS X inserts plenty of its own <span> tags, so don't test for
  175. // equality and just check that the source contains things we'd expect it
  176. // to.
  177. const wxString selSource = m_browser->GetSelectedSource();
  178. WX_ASSERT_MESSAGE
  179. (
  180. ("Unexpected selection source: \"%s\"", selSource),
  181. selSource.Lower().Matches("*some*<strong*strong</strong>*text*")
  182. );
  183. m_browser->ClearSelection();
  184. CPPUNIT_ASSERT(!m_browser->HasSelection());
  185. }
  186. void WebTestCase::Zoom()
  187. {
  188. if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
  189. {
  190. m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT);
  191. CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_LAYOUT, m_browser->GetZoomType());
  192. m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
  193. CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
  194. }
  195. //Reset the zoom level
  196. m_browser->SetZoom(wxWEBVIEW_ZOOM_MEDIUM);
  197. if(m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT))
  198. {
  199. m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
  200. CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TYPE_TEXT, m_browser->GetZoomType());
  201. m_browser->SetZoom(wxWEBVIEW_ZOOM_TINY);
  202. CPPUNIT_ASSERT_EQUAL(wxWEBVIEW_ZOOM_TINY, m_browser->GetZoom());
  203. }
  204. }
  205. void WebTestCase::RunScript()
  206. {
  207. m_browser->RunScript("document.write(\"Hello World!\");");
  208. CPPUNIT_ASSERT_EQUAL("Hello World!", m_browser->GetPageText());
  209. }
  210. void WebTestCase::SetPage()
  211. {
  212. m_browser->SetPage("<html><body>text</body></html>", "");
  213. ENSURE_LOADED;
  214. CPPUNIT_ASSERT_EQUAL("text", m_browser->GetPageText());
  215. m_browser->SetPage("<html><body>other text</body></html>", "");
  216. ENSURE_LOADED;
  217. CPPUNIT_ASSERT_EQUAL("other text", m_browser->GetPageText());
  218. }
  219. #endif //wxUSE_WEBVIEW && (wxUSE_WEBVIEW_WEBKIT || wxUSE_WEBVIEW_IE)