url.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/uris/url.cpp
  3. // Purpose: wxURL unit test
  4. // Author: Francesco Montorsi
  5. // Created: 2009-5-31
  6. // Copyright: (c) 2009 Francesco Montorsi
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/url.h"
  19. #include "wx/mstream.h"
  20. // ----------------------------------------------------------------------------
  21. // test class
  22. // ----------------------------------------------------------------------------
  23. class URLTestCase : public CppUnit::TestCase
  24. {
  25. public:
  26. URLTestCase();
  27. ~URLTestCase();
  28. private:
  29. CPPUNIT_TEST_SUITE( URLTestCase );
  30. CPPUNIT_TEST( GetInputStream );
  31. CPPUNIT_TEST( CopyAndAssignment );
  32. CPPUNIT_TEST_SUITE_END();
  33. void GetInputStream();
  34. void CopyAndAssignment();
  35. DECLARE_NO_COPY_CLASS(URLTestCase)
  36. };
  37. // register in the unnamed registry so that these tests are run by default
  38. CPPUNIT_TEST_SUITE_REGISTRATION( URLTestCase );
  39. // also include in its own registry so that these tests can be run alone
  40. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URLTestCase, "URLTestCase" );
  41. URLTestCase::URLTestCase()
  42. {
  43. wxSocketBase::Initialize();
  44. }
  45. URLTestCase::~URLTestCase()
  46. {
  47. wxSocketBase::Shutdown();
  48. }
  49. void URLTestCase::GetInputStream()
  50. {
  51. if (!IsNetworkAvailable()) // implemented in test.cpp
  52. {
  53. wxLogWarning("No network connectivity; skipping the URLTestCase::GetInputStream test unit.");
  54. return;
  55. }
  56. wxURL url("http://www.wxwidgets.org/assets/img/header-logo.png");
  57. CPPUNIT_ASSERT_EQUAL(wxURL_NOERR, url.GetError());
  58. wxInputStream *in_stream = url.GetInputStream();
  59. CPPUNIT_ASSERT(in_stream && in_stream->IsOk());
  60. wxMemoryOutputStream ostream;
  61. CPPUNIT_ASSERT(in_stream->Read(ostream).GetLastError() == wxSTREAM_EOF);
  62. CPPUNIT_ASSERT_EQUAL(13677, ostream.GetSize());
  63. // we have to delete the object created by GetInputStream()
  64. delete in_stream;
  65. }
  66. void URLTestCase::CopyAndAssignment()
  67. {
  68. wxURL url1("http://www.example.org/");
  69. wxURL url2;
  70. wxURI *puri = &url2; // downcast
  71. { // Copy constructor
  72. wxURL url3(url1);
  73. CPPUNIT_ASSERT(url1 == url3);
  74. }
  75. { // Constructor for string
  76. wxURL url3(url1.GetURL());
  77. CPPUNIT_ASSERT(url1 == url3);
  78. }
  79. { // 'Copy' constructor for uri
  80. wxURL url3(*puri);
  81. CPPUNIT_ASSERT(url2 == url3);
  82. }
  83. // assignment for uri
  84. *puri = url1;
  85. CPPUNIT_ASSERT(url1 == url2);
  86. // assignment to self through base pointer
  87. *puri = url2;
  88. // Assignment of string
  89. url1 = wxS("http://www.example2.org/index.html");
  90. *puri = wxS("http://www.example2.org/index.html");
  91. CPPUNIT_ASSERT(url1 == url2);
  92. // Assignment
  93. url1 = wxS("");
  94. url2 = url1;
  95. CPPUNIT_ASSERT(url1 == url2);
  96. // assignment to self
  97. url2 = url2;
  98. // check for destructor (with base pointer!)
  99. puri = new wxURL();
  100. delete puri;
  101. }