]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/controls/itemcontainertest.cpp
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / tests / controls / itemcontainertest.cpp
index b405e64a69ebde6ac6dcbc5fac893c99fa47420d..48944ad0a87c55ce1c9d50e9a874ff0e5f85c29b 100644 (file)
@@ -14,6 +14,8 @@
     #include "wx/ctrlsub.h"
 #endif // WX_PRECOMP
 
+#include "wx/scopeguard.h"
+
 #include "itemcontainertest.h"
 
 void ItemContainerTestCase::Append()
@@ -45,23 +47,21 @@ void ItemContainerTestCase::Insert()
 {
     wxItemContainer * const container = GetContainer();
 
-    container->Insert("item 0", 0);
-
+    CPPUNIT_ASSERT_EQUAL( 0, container->Insert("item 0", 0) );
     CPPUNIT_ASSERT_EQUAL("item 0", container->GetString(0));
 
     wxArrayString testitems;
     testitems.Add("item 1");
     testitems.Add("item 2");
 
-    container->Insert(testitems, 0);
+    CPPUNIT_ASSERT_EQUAL( 1, container->Insert(testitems, 0) );
 
     CPPUNIT_ASSERT_EQUAL("item 1", container->GetString(0));
     CPPUNIT_ASSERT_EQUAL("item 2", container->GetString(1));
 
     wxString arritems[] = { "item 3", "item 4" };
 
-    container->Insert(2, arritems, 1);
-
+    CPPUNIT_ASSERT_EQUAL( 2, container->Insert(2, arritems, 1) );
     CPPUNIT_ASSERT_EQUAL("item 3", container->GetString(1));
     CPPUNIT_ASSERT_EQUAL("item 4", container->GetString(2));
 }
@@ -105,22 +105,27 @@ void ItemContainerTestCase::ItemSelection()
     testitems.Add("item 0");
     testitems.Add("item 1");
     testitems.Add("item 2");
-    testitems.Add("item 3");
+    testitems.Add("ITEM 2"); // The same as the last one except for case.
 
     container->Append(testitems);
 
     container->SetSelection(wxNOT_FOUND);
-
     CPPUNIT_ASSERT_EQUAL(wxNOT_FOUND, container->GetSelection());
     CPPUNIT_ASSERT_EQUAL("", container->GetStringSelection());
 
     container->SetSelection(1);
-
     CPPUNIT_ASSERT_EQUAL(1, container->GetSelection());
     CPPUNIT_ASSERT_EQUAL("item 1", container->GetStringSelection());
 
-    container->SetStringSelection("item 2");
+    CPPUNIT_ASSERT( container->SetStringSelection("item 2") );
+    CPPUNIT_ASSERT_EQUAL(2, container->GetSelection());
+    CPPUNIT_ASSERT_EQUAL("item 2", container->GetStringSelection());
+
+    // Check that selecting a non-existent item fails.
+    CPPUNIT_ASSERT( !container->SetStringSelection("bloordyblop") );
 
+    // Check that SetStringSelection() is case-insensitive.
+    CPPUNIT_ASSERT( container->SetStringSelection("ITEM 2") );
     CPPUNIT_ASSERT_EQUAL(2, container->GetSelection());
     CPPUNIT_ASSERT_EQUAL("item 2", container->GetStringSelection());
 }
@@ -165,6 +170,9 @@ void ItemContainerTestCase::ClientData()
 
     CPPUNIT_ASSERT_EQUAL(static_cast<wxClientData*>(item2data),
                          container->GetClientObject(2));
+
+    WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientObject((unsigned)-1, item0data) );
+    WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientObject(12345, item0data) );
 }
 
 void ItemContainerTestCase::VoidData()
@@ -190,6 +198,9 @@ void ItemContainerTestCase::VoidData()
     container->Insert("item 2", 2, item2);
 
     CPPUNIT_ASSERT_EQUAL(item2, container->GetClientData(2));
+
+    WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientData((unsigned)-1, NULL) );
+    WX_ASSERT_FAILS_WITH_ASSERT( container->SetClientData(12345, NULL) );
 }
 
 void ItemContainerTestCase::Set()
@@ -233,13 +244,51 @@ void ItemContainerTestCase::SetString()
 
     container->Append(testitems);
 
+    container->SetSelection(0);
     container->SetString(0, "new item 0");
-#ifndef __WXOSX__
-    container->SetString(2, "");
-#endif
-
     CPPUNIT_ASSERT_EQUAL("new item 0", container->GetString(0));
+
+    // Modifying the item shouldn't deselect it.
+    CPPUNIT_ASSERT_EQUAL(0, container->GetSelection());
+
+    // wxOSX doesn't support having empty items in some containers.
 #ifndef __WXOSX__
+    container->SetString(2, "");
     CPPUNIT_ASSERT_EQUAL("", container->GetString(2));
 #endif
 }
+
+void ItemContainerTestCase::SetSelection()
+{
+    wxItemContainer * const container = GetContainer();
+
+    container->Append("first");
+    container->Append("second");
+
+    // This class is used to check that SetSelection() doesn't generate any
+    // events, as documented.
+    class CommandEventHandler : public wxEvtHandler
+    {
+    public:
+        virtual bool ProcessEvent(wxEvent& event)
+        {
+            CPPUNIT_ASSERT_MESSAGE
+            (
+                "unexpected command event from SetSelection",
+                !event.IsCommandEvent()
+            );
+
+            return wxEvtHandler::ProcessEvent(event);
+        }
+    } h;
+
+    wxWindow * const win = GetContainerWindow();
+    win->PushEventHandler(&h);
+    wxON_BLOCK_EXIT_OBJ1( *win, wxWindow::PopEventHandler, false );
+
+    container->SetSelection(0);
+    CPPUNIT_ASSERT_EQUAL( 0, container->GetSelection() );
+
+    container->SetSelection(1);
+    CPPUNIT_ASSERT_EQUAL( 1, container->GetSelection() );
+}