]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/controls/textentrytest.cpp
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / tests / controls / textentrytest.cpp
index 80e6482fa90d9efabc9ee5a4c5a6edfd127d7658..2bf31ed555aead06bc5990a4966fb5311d17bedb 100644 (file)
 #include "testprec.h"
 
 #ifndef WX_PRECOMP
+    #include "wx/app.h"
     #include "wx/event.h"
     #include "wx/textentry.h"
     #include "wx/window.h"
 #endif // WX_PRECOMP
 
 #include "textentrytest.h"
+#include "testableframe.h"
+#include "wx/uiaction.h"
 
 void TextEntryTestCase::SetValue()
 {
@@ -38,62 +41,50 @@ void TextEntryTestCase::SetValue()
 
 void TextEntryTestCase::TextChangeEvents()
 {
-    class TextTestEventHandler : public wxEvtHandler
-    {
-    public:
-        TextTestEventHandler() { m_events = 0; }
-
-        // calling this automatically resets the events counter
-        int GetEvents()
-        {
-            const int events = m_events;
-            m_events = 0;
-            return events;
-        }
-
-        void OnText(wxCommandEvent& WXUNUSED(event)) { m_events++; }
-
-    private:
-        int m_events;
-    } handler;
-
-    GetTestWindow()->Connect
-                     (
-                        wxEVT_COMMAND_TEXT_UPDATED,
-                        wxCommandEventHandler(TextTestEventHandler::OnText),
-                        NULL,
-                        &handler
-                     );
+    EventCounter updated(GetTestWindow(), wxEVT_COMMAND_TEXT_UPDATED);
 
     wxTextEntry * const entry = GetTestEntry();
 
     // notice that SetValue() generates an event even if the text didn't change
     entry->SetValue("");
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->SetValue("foo");
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->SetValue("foo");
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->ChangeValue("bar");
-    CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 0, updated.GetCount() );
 
     entry->AppendText("bar");
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->Replace(3, 6, "baz");
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->Remove(0, 3);
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->WriteText("foo");
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
 
     entry->Clear();
-    CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
+    CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
+    updated.Clear();
+}
+
+void TextEntryTestCase::CheckStringSelection(const char *sel)
+{
+    CPPUNIT_ASSERT_EQUAL( sel, GetTestEntry()->GetStringSelection() );
 }
 
 void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
@@ -107,9 +98,10 @@ void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
     entry->GetSelection(&fromReal, &toReal);
     CPPUNIT_ASSERT_EQUAL( from, fromReal );
     CPPUNIT_ASSERT_EQUAL( to, toReal );
-    CPPUNIT_ASSERT_EQUAL( sel, entry->GetStringSelection() );
 
     CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
+
+    CheckStringSelection(sel);
 }
 
 void TextEntryTestCase::Selection()
@@ -157,5 +149,107 @@ void TextEntryTestCase::InsertionPoint()
     entry->WriteText("-"); // should move it after the written text
     CPPUNIT_ASSERT_EQUAL( 4, entry->GetLastPosition() );
     CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
+
+    entry->SetValue("something different"); // should still reset the caret
+    CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
+}
+
+void TextEntryTestCase::Replace()
+{
+    wxTextEntry * const entry = GetTestEntry();
+
+    entry->SetValue("Hello replace!"
+                    "0123456789012");
+    entry->SetInsertionPoint(0);
+
+    entry->Replace(6, 13, "changed");
+
+    CPPUNIT_ASSERT_EQUAL("Hello changed!"
+                         "0123456789012",
+                         entry->GetValue());
+    CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
+
+    entry->Replace(13, -1, "");
+    CPPUNIT_ASSERT_EQUAL("Hello changed", entry->GetValue());
+    CPPUNIT_ASSERT_EQUAL(13, entry->GetInsertionPoint());
+
+    entry->Replace(0, 6, "Un");
+    CPPUNIT_ASSERT_EQUAL("Unchanged", entry->GetValue());
+    CPPUNIT_ASSERT_EQUAL(2, entry->GetInsertionPoint());
+}
+
+void TextEntryTestCase::Editable()
+{
+#if wxUSE_UIACTIONSIMULATOR
+    wxTextEntry * const entry = GetTestEntry();
+    wxWindow * const window = GetTestWindow();
+
+    EventCounter updated(window, wxEVT_COMMAND_TEXT_UPDATED);
+
+    window->SetFocus();
+    wxYield();
+
+    wxUIActionSimulator sim;
+    sim.Text("abcdef");
+    wxYield();
+
+    CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
+    CPPUNIT_ASSERT_EQUAL(6, updated.GetCount());
+
+    updated.Clear();
+
+    entry->SetEditable(false);
+    sim.Text("gh");
+    wxYield();
+
+    CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
+    CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
+#endif
+}
+
+void TextEntryTestCase::Hint()
+{
+    GetTestEntry()->SetHint("This is a hint");
+    CPPUNIT_ASSERT_EQUAL("", GetTestEntry()->GetValue());
+}
+
+void TextEntryTestCase::CopyPaste()
+{
+#ifndef __WXOSX__
+    wxTextEntry * const entry = GetTestEntry();
+
+    entry->AppendText("sometext");
+    entry->SelectAll();
+
+    if(entry->CanCopy() && entry->CanPaste())
+    {
+        entry->Copy();
+        entry->Clear();
+        CPPUNIT_ASSERT(entry->IsEmpty());
+
+        wxYield();
+
+        entry->Paste();
+        CPPUNIT_ASSERT_EQUAL("sometext", entry->GetValue());
+    }
+#endif
 }
 
+void TextEntryTestCase::UndoRedo()
+{
+    wxTextEntry * const entry = GetTestEntry();
+
+    entry->AppendText("sometext");
+
+    if(entry->CanUndo())
+    {
+        entry->Undo();
+        CPPUNIT_ASSERT(entry->IsEmpty());
+
+        if(entry->CanRedo())
+        {
+            entry->Redo();
+            CPPUNIT_ASSERT_EQUAL("sometext", entry->GetValue());
+        }
+    }
+}