]> git.saurik.com Git - wxWidgets.git/commitdiff
add wxURL::GetInputStream test unit; add a global IsNetworkAvailable() utility to...
authorFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Mon, 1 Jun 2009 11:30:50 +0000 (11:30 +0000)
committerFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Mon, 1 Jun 2009 11:30:50 +0000 (11:30 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60851 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

tests/test.cpp
tests/testprec.h
tests/uris/uris.cpp
tests/uris/url.cpp [new file with mode: 0644]

index 2f7f71903cd0fa024d8d14824ce7d9c35e853932..758c5fcb1a4e404bc2a4699a3ad0fc96c798719f 100644 (file)
@@ -7,6 +7,10 @@
 // Licence:     wxWidgets licence
 ///////////////////////////////////////////////////////////////////////////////
 
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
 // For compilers that support precompilation, includes "wx/wx.h"
 // and "wx/cppunit.h"
 #include "testprec.h"
     #include "wx/osx/private.h"
 #endif
 
+#include "wx/socket.h"
+
 using namespace std;
 
 using CppUnit::Test;
 using CppUnit::TestSuite;
 using CppUnit::TestFactoryRegistry;
 
+
+// ----------------------------------------------------------------------------
+// helper classes
+// ----------------------------------------------------------------------------
+
 // exception class for MSVC debug CRT assertion failures
 #ifdef wxUSE_VC_CRTDBG
 
@@ -79,7 +90,7 @@ static string GetExceptionMessage()
     {
         msg << "wxWidgets assert: " << e.m_cond << " failed "
                "at " << e.m_file << ":" << e.m_line << " in " << e.m_func
-            << " with message " << e.m_msg;
+            << " with message '" << e.m_msg << "'";
     }
 #endif // wxDEBUG_LEVEL
 #ifdef wxUSE_VC_CRTDBG
@@ -244,6 +255,11 @@ private:
 
 IMPLEMENT_APP_NO_MAIN(TestApp)
 
+
+// ----------------------------------------------------------------------------
+// global functions
+// ----------------------------------------------------------------------------
+
 #ifdef wxUSE_VC_CRTDBG
 
 static int TestCrtReportHook(int reportType, char *message, int *)
@@ -295,6 +311,63 @@ int main(int argc, char **argv)
     return -1;
 }
 
+extern void SetFilterEventFunc(FilterEventFunc func)
+{
+    wxGetApp().SetFilterEventFunc(func);
+}
+
+extern void SetProcessEventFunc(ProcessEventFunc func)
+{
+    wxGetApp().SetProcessEventFunc(func);
+}
+
+extern bool IsNetworkAvailable()
+{
+    // NOTE: we could use wxDialUpManager here if it was in wxNet; since it's in
+    //       wxCore we use a simple rough test:
+    
+    wxSocketBase::Initialize();
+    
+    wxIPV4address addr;
+    if (!addr.Hostname("www.google.com") || !addr.Service("www"))
+    {
+        wxSocketBase::Shutdown();
+        return false;
+    }
+
+    wxSocketClient sock;
+    bool online = sock.Connect(addr);
+    
+    wxSocketBase::Shutdown();
+    
+    return online;
+}
+
+// helper of OnRun(): gets the test with the given name, returning NULL (and
+// not an empty test suite) if there is no such test
+static Test *GetTestByName(const wxString& name)
+{
+    Test *
+      test = TestFactoryRegistry::getRegistry(string(name.mb_str())).makeTest();
+    if ( test )
+    {
+        TestSuite * const suite = dynamic_cast<TestSuite *>(test);
+        if ( !suite || !suite->countTestCases() )
+        {
+            // it's a bogus test, don't use it
+            delete test;
+            test = NULL;
+        }
+    }
+
+    return test;
+}
+
+
+// ----------------------------------------------------------------------------
+// TestApp
+// ----------------------------------------------------------------------------
+
 TestApp::TestApp()
   : m_list(false),
     m_longlist(false)
@@ -406,36 +479,6 @@ bool TestApp::ProcessEvent(wxEvent& event)
     return TestAppBase::ProcessEvent(event);
 }
 
-extern void SetFilterEventFunc(FilterEventFunc func)
-{
-    wxGetApp().SetFilterEventFunc(func);
-}
-
-extern void SetProcessEventFunc(ProcessEventFunc func)
-{
-    wxGetApp().SetProcessEventFunc(func);
-}
-
-// helper of OnRun(): gets the test with the given name, returning NULL (and
-// not an empty test suite) if there is no such test
-static Test *GetTestByName(const wxString& name)
-{
-    Test *
-      test = TestFactoryRegistry::getRegistry(string(name.mb_str())).makeTest();
-    if ( test )
-    {
-        TestSuite * const suite = dynamic_cast<TestSuite *>(test);
-        if ( !suite || !suite->countTestCases() )
-        {
-            // it's a bogus test, don't use it
-            delete test;
-            test = NULL;
-        }
-    }
-
-    return test;
-}
-
 // Run
 //
 int TestApp::OnRun()
index 923d45fc06ce02eb4221bbf0fab04ea8e9682da9..432d0405a223f107964ac761d4d363b49dd9b4c8 100644 (file)
@@ -59,3 +59,5 @@ typedef bool (*ProcessEventFunc)(wxEvent&);
 
 extern void SetFilterEventFunc(FilterEventFunc func);
 extern void SetProcessEventFunc(ProcessEventFunc func);
+
+extern bool IsNetworkAvailable();
index f2cc591394b5fff3f31e92aab102661bf72d27ee..66dd225ecb4f09c0aeb034631fcdadb97089a8df 100644 (file)
 // Test wxURL & wxURI compat?
 #define TEST_URL wxUSE_URL
 
-// Define this as 1 to test network connections, this is disabled by default as
-// some machines running automatic builds don't allow outgoing connections and
-// so the tests fail
-#define TEST_NETWORK 0
-
 // ----------------------------------------------------------------------------
 // test class
 // ----------------------------------------------------------------------------
@@ -373,14 +368,7 @@ void URITestCase::URLCompat()
 {
     wxURL url("http://user:password@wxwidgets.org");
 
-    CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
-
-#if TEST_NETWORK
-    wxInputStream* pInput = url.GetInputStream();
-
-    CPPUNIT_ASSERT( pInput != NULL );
-#endif
-
+    CPPUNIT_ASSERT( url.GetError() == wxURL_NOERR );
     CPPUNIT_ASSERT( url == wxURL("http://user:password@wxwidgets.org") );
 
     wxURI uri("http://user:password@wxwidgets.org");
diff --git a/tests/uris/url.cpp b/tests/uris/url.cpp
new file mode 100644 (file)
index 0000000..8cf9d09
--- /dev/null
@@ -0,0 +1,84 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        tests/uris/url.cpp
+// Purpose:     wxURL unit test
+// Author:      Francesco Montorsi
+// Created:     2009-5-31
+// RCS-ID:      $Id: uris.cpp 58272 2009-01-21 17:02:11Z VZ $
+// Copyright:   (c) 2009 Francesco Montorsi
+///////////////////////////////////////////////////////////////////////////////
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
+#include "testprec.h"
+
+#ifdef __BORLANDC__
+    #pragma hdrstop
+#endif
+
+#ifndef WX_PRECOMP
+    #include "wx/wx.h"
+#endif // WX_PRECOMP
+
+#include "wx/url.h"
+#include "wx/mstream.h"
+
+// ----------------------------------------------------------------------------
+// test class
+// ----------------------------------------------------------------------------
+
+class URLTestCase : public CppUnit::TestCase
+{
+public:
+    URLTestCase(); 
+    ~URLTestCase();
+
+private:
+    CPPUNIT_TEST_SUITE( URLTestCase );
+        CPPUNIT_TEST( GetInputStream );
+    CPPUNIT_TEST_SUITE_END();
+
+    void GetInputStream();
+
+    DECLARE_NO_COPY_CLASS(URLTestCase)
+};
+
+// register in the unnamed registry so that these tests are run by default
+CPPUNIT_TEST_SUITE_REGISTRATION( URLTestCase );
+
+// also include in it's own registry so that these tests can be run alone
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URLTestCase, "URLTestCase" );
+
+
+URLTestCase::URLTestCase()
+{
+    wxSocketBase::Initialize();
+}
+
+URLTestCase::~URLTestCase()
+{
+    wxSocketBase::Shutdown();
+}
+
+void URLTestCase::GetInputStream()
+{
+    if (!IsNetworkAvailable())      // implemented in test.cpp
+    {
+        wxLogWarning("No network connectivity; skipping the URLTestCase::GetInputStream test unit.");
+        return;
+    }
+    
+    wxURL url("http://wxwidgets.org/logo9.jpg");
+    CPPUNIT_ASSERT_EQUAL(wxURL_NOERR, url.GetError());
+
+    wxInputStream *in_stream = url.GetInputStream();
+    CPPUNIT_ASSERT(in_stream && in_stream->IsOk());
+
+    wxMemoryOutputStream ostream;
+    CPPUNIT_ASSERT(in_stream->Read(ostream).GetLastError() == wxSTREAM_EOF);
+    
+    // wx logo image currently is 13219 bytes
+    CPPUNIT_ASSERT(ostream.GetSize() == 13219);
+}
+