]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/net/socket.cpp
Don't use GetThreadId() in wxMSW code.
[wxWidgets.git] / tests / net / socket.cpp
index 80318efb0b146d2a70afb9224f8891d1e32a987d..3532618f8cfbc03ec3602b4b06f0756907874e2a 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Vadim Zeitlin
 // RCS-ID:      $Id$
 // Copyright:   (c) 2008 Vadim Zeitlin
-// Licence:     wxWidgets licence
+// Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
 /*
@@ -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>
 
@@ -37,7 +39,7 @@ static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
 class SocketTestCase : public CppUnit::TestCase
 {
 public:
-    SocketTestCase() { m_useLoop = false; }
+    SocketTestCase() { }
 
 private:
     // we need to repeat the tests twice as the sockets behave differently when
@@ -48,7 +50,8 @@ private:
         CPPUNIT_TEST( ReadNormal ); \
         CPPUNIT_TEST( ReadBlock ); \
         CPPUNIT_TEST( ReadNowait ); \
-        CPPUNIT_TEST( ReadWaitall )
+        CPPUNIT_TEST( ReadWaitall ); \
+        CPPUNIT_TEST( UrlTest )
 
     CPPUNIT_TEST_SUITE( SocketTestCase );
         ALL_SOCKET_TESTS();
@@ -65,17 +68,23 @@ private:
         {
             m_useLoop = useLoop;
             if ( useLoop )
+            {
+                m_evtLoopOld = wxEventLoopBase::GetActive();
                 SetActive(this);
+            }
         }
 
         virtual ~SocketTestEventLoop()
         {
             if ( m_useLoop )
-                SetActive(NULL);
+            {
+                wxEventLoopBase::SetActive(m_evtLoopOld);
+            }
         }
 
     private:
         bool m_useLoop;
+        wxEventLoopBase *m_evtLoopOld;
     };
 
     // get the address to connect to, if NULL is returned it means that the
@@ -86,7 +95,7 @@ private:
     // disabled
     wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
 
-    void PseudoTest_SetUseEventLoop() { m_useLoop = true; }
+    void PseudoTest_SetUseEventLoop() { ms_useLoop = true; }
 
     void BlockingConnect();
     void NonblockingConnect();
@@ -95,11 +104,15 @@ private:
     void ReadNowait();
     void ReadWaitall();
 
-    bool m_useLoop;
+    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" );
 
@@ -151,7 +164,7 @@ void SocketTestCase::NonblockingConnect()
     if ( !addr.get() )
         return;
 
-    SocketTestEventLoop loop(m_useLoop);
+    SocketTestEventLoop loop(ms_useLoop);
 
     wxSocketClient sock;
     sock.Connect(*addr, false);
@@ -162,7 +175,7 @@ void SocketTestCase::NonblockingConnect()
 
 void SocketTestCase::ReadNormal()
 {
-    SocketTestEventLoop loop(m_useLoop);
+    SocketTestEventLoop loop(ms_useLoop);
 
     wxSocketClientPtr sock(GetHTTPSocket());
     if ( !sock.get() )
@@ -172,7 +185,8 @@ 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() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() );
 
 
     char bufBig[102400];
@@ -180,6 +194,7 @@ void SocketTestCase::ReadNormal()
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
     CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
+    CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() );
 }
 
 void SocketTestCase::ReadBlock()
@@ -192,7 +207,8 @@ 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() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() );
 
 
     char bufBig[102400];
@@ -200,6 +216,7 @@ void SocketTestCase::ReadBlock()
 
     CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
     CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
+    CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() );
 }
 
 void SocketTestCase::ReadNowait()
@@ -218,7 +235,7 @@ void SocketTestCase::ReadNowait()
 
 void SocketTestCase::ReadWaitall()
 {
-    SocketTestEventLoop loop(m_useLoop);
+    SocketTestEventLoop loop(ms_useLoop);
 
     wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
     if ( !sock.get() )
@@ -228,7 +245,24 @@ 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() );
+    CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastReadCount() );
+}
+
+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