]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/net/socket.cpp
Document ctors creating a wxString from repeated characters.
[wxWidgets.git] / tests / net / socket.cpp
index 0a8200f649f2f2f7db6521cd243e8b95a8073b28..5928915e410a556341833d6936a22e8e3b09f03c 100644 (file)
@@ -26,6 +26,8 @@
 #if wxUSE_SOCKETS
 
 #include "wx/socket.h"
+#include "wx/url.h"
+#include "wx/sstream.h"
 #include "wx/evtloop.h"
 #include <memory>
 
@@ -40,15 +42,45 @@ public:
     SocketTestCase() { }
 
 private:
+    // we need to repeat the tests twice as the sockets behave differently when
+    // there is an active event loop and without it
+    #define ALL_SOCKET_TESTS() \
+        CPPUNIT_TEST( BlockingConnect ); \
+        CPPUNIT_TEST( NonblockingConnect ); \
+        CPPUNIT_TEST( ReadNormal ); \
+        CPPUNIT_TEST( ReadBlock ); \
+        CPPUNIT_TEST( ReadNowait ); \
+        CPPUNIT_TEST( ReadWaitall ); \
+        CPPUNIT_TEST( UrlTest )
+
     CPPUNIT_TEST_SUITE( SocketTestCase );
-        CPPUNIT_TEST( BlockingConnect );
-        CPPUNIT_TEST( NonblockingConnect );
-        CPPUNIT_TEST( ReadNormal );
-        CPPUNIT_TEST( ReadBlock );
-        CPPUNIT_TEST( ReadNowait );
-        CPPUNIT_TEST( ReadWaitall );
+        ALL_SOCKET_TESTS();
+        CPPUNIT_TEST( PseudoTest_SetUseEventLoop );
+        ALL_SOCKET_TESTS();
     CPPUNIT_TEST_SUITE_END();
 
+    // helper event loop class which sets itself as active only if we pass it
+    // true in ctor
+    class SocketTestEventLoop : public wxEventLoop
+    {
+    public:
+        SocketTestEventLoop(bool useLoop)
+        {
+            m_useLoop = useLoop;
+            if ( useLoop )
+                SetActive(this);
+        }
+
+        virtual ~SocketTestEventLoop()
+        {
+            if ( m_useLoop )
+                SetActive(NULL);
+        }
+
+    private:
+        bool m_useLoop;
+    };
+
     // get the address to connect to, if NULL is returned it means that the
     // test is disabled and shouldn't run at all
     wxSockAddressPtr GetServer() const;
@@ -57,6 +89,8 @@ private:
     // disabled
     wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
 
+    void PseudoTest_SetUseEventLoop() { ms_useLoop = true; }
+
     void BlockingConnect();
     void NonblockingConnect();
     void ReadNormal();
@@ -64,9 +98,15 @@ private:
     void ReadNowait();
     void ReadWaitall();
 
+    void UrlTest();
+
+    static bool ms_useLoop;
+
     DECLARE_NO_COPY_CLASS(SocketTestCase)
 };
 
+bool SocketTestCase::ms_useLoop = false;
+
 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
 
@@ -118,7 +158,7 @@ void SocketTestCase::NonblockingConnect()
     if ( !addr.get() )
         return;
 
-    wxEventLoopGuarantor loop;
+    SocketTestEventLoop loop(ms_useLoop);
 
     wxSocketClient sock;
     sock.Connect(*addr, false);
@@ -129,7 +169,7 @@ void SocketTestCase::NonblockingConnect()
 
 void SocketTestCase::ReadNormal()
 {
-    wxEventLoopGuarantor loop;
+    SocketTestEventLoop loop(ms_useLoop);
 
     wxSocketClientPtr sock(GetHTTPSocket());
     if ( !sock.get() )
@@ -139,7 +179,7 @@ void SocketTestCase::ReadNormal()
     sock->Read(bufSmall, WXSIZEOF(bufSmall));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
-    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
 
 
     char bufBig[102400];
@@ -159,7 +199,7 @@ void SocketTestCase::ReadBlock()
     sock->Read(bufSmall, WXSIZEOF(bufSmall));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
-    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
 
 
     char bufBig[102400];
@@ -185,7 +225,7 @@ void SocketTestCase::ReadNowait()
 
 void SocketTestCase::ReadWaitall()
 {
-    wxEventLoopGuarantor loop;
+    SocketTestEventLoop loop(ms_useLoop);
 
     wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
     if ( !sock.get() )
@@ -195,7 +235,23 @@ void SocketTestCase::ReadWaitall()
     sock->Read(buf, WXSIZEOF(buf));
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
-    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), sock->LastCount() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() );
+}
+
+void SocketTestCase::UrlTest()
+{
+    if ( gs_serverHost.empty() )
+        return;
+
+    SocketTestEventLoop loop(ms_useLoop);
+
+    wxURL url("http://" + gs_serverHost);
+
+    const std::auto_ptr<wxInputStream> in(url.GetInputStream());
+    CPPUNIT_ASSERT( in.get() );
+
+    wxStringOutputStream out;
+    CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() );
 }
 
 #endif // wxUSE_SOCKETS