2008-09-19 04:55:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/controls/textentrytest.cpp
|
|
|
|
// Purpose: TestEntryTestCase implementation
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2008-09-19 (extracted from textctrltest.cpp)
|
|
|
|
// Copyright: (c) 2007, 2008 Vadim Zeitlin <vadim@wxwidgets.org>
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2010-08-22 18:16:05 -04:00
|
|
|
#include "wx/app.h"
|
2008-09-19 04:55:25 -04:00
|
|
|
#include "wx/event.h"
|
|
|
|
#include "wx/textentry.h"
|
|
|
|
#include "wx/window.h"
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "textentrytest.h"
|
2010-08-22 18:16:05 -04:00
|
|
|
#include "testableframe.h"
|
|
|
|
#include "wx/uiaction.h"
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
void TextEntryTestCase::SetValue()
|
|
|
|
{
|
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( entry->IsEmpty() );
|
|
|
|
|
|
|
|
entry->SetValue("foo");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "foo", entry->GetValue() );
|
|
|
|
|
|
|
|
entry->SetValue("");
|
|
|
|
CPPUNIT_ASSERT( entry->IsEmpty() );
|
|
|
|
|
|
|
|
entry->SetValue("hi");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "hi", entry->GetValue() );
|
|
|
|
|
|
|
|
entry->SetValue("bye");
|
|
|
|
CPPUNIT_ASSERT_EQUAL( "bye", entry->GetValue() );
|
|
|
|
}
|
|
|
|
|
2009-01-12 09:26:13 -05:00
|
|
|
void TextEntryTestCase::TextChangeEvents()
|
|
|
|
{
|
2013-04-25 06:11:03 -04:00
|
|
|
EventCounter updated(GetTestWindow(), wxEVT_TEXT);
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
|
|
|
|
// notice that SetValue() generates an event even if the text didn't change
|
|
|
|
entry->SetValue("");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->SetValue("foo");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->SetValue("foo");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->ChangeValue("bar");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, updated.GetCount() );
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->AppendText("bar");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->Replace(3, 6, "baz");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->Remove(0, 3);
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->WriteText("foo");
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
|
|
|
|
entry->Clear();
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, updated.GetCount() );
|
|
|
|
updated.Clear();
|
2008-09-19 04:55:25 -04:00
|
|
|
}
|
|
|
|
|
2008-09-19 06:18:30 -04:00
|
|
|
void TextEntryTestCase::CheckStringSelection(const char *sel)
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL( sel, GetTestEntry()->GetStringSelection() );
|
|
|
|
}
|
|
|
|
|
2008-09-19 04:55:25 -04:00
|
|
|
void TextEntryTestCase::AssertSelection(int from, int to, const char *sel)
|
|
|
|
{
|
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( entry->HasSelection() );
|
|
|
|
|
|
|
|
long fromReal,
|
|
|
|
toReal;
|
|
|
|
entry->GetSelection(&fromReal, &toReal);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( from, fromReal );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( to, toReal );
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( from, entry->GetInsertionPoint() );
|
2008-09-19 06:18:30 -04:00
|
|
|
|
|
|
|
CheckStringSelection(sel);
|
2008-09-19 04:55:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEntryTestCase::Selection()
|
|
|
|
{
|
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
|
|
|
|
entry->SetValue("0123456789");
|
|
|
|
|
|
|
|
entry->SetSelection(2, 4);
|
|
|
|
AssertSelection(2, 4, "23"); // not "234"!
|
|
|
|
|
|
|
|
entry->SetSelection(3, -1);
|
|
|
|
AssertSelection(3, 10, "3456789");
|
|
|
|
|
|
|
|
entry->SelectAll();
|
|
|
|
AssertSelection(0, 10, "0123456789");
|
|
|
|
|
|
|
|
entry->SetSelection(0, 0);
|
|
|
|
CPPUNIT_ASSERT( !entry->HasSelection() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEntryTestCase::InsertionPoint()
|
|
|
|
{
|
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, entry->GetLastPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
|
|
|
|
|
|
|
|
entry->SetValue("0"); // should put the insertion point in front
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, entry->GetLastPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
|
|
|
|
|
|
|
|
entry->AppendText("12"); // should update the insertion point position
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
|
|
|
|
|
|
|
|
entry->SetInsertionPoint(1);
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 3, entry->GetLastPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
|
|
|
|
|
|
|
|
entry->SetInsertionPointEnd();
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 3, entry->GetInsertionPoint() );
|
|
|
|
|
|
|
|
entry->SetInsertionPoint(0);
|
|
|
|
entry->WriteText("-"); // should move it after the written text
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 4, entry->GetLastPosition() );
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, entry->GetInsertionPoint() );
|
2010-11-05 17:37:54 -04:00
|
|
|
|
|
|
|
entry->SetValue("something different"); // should still reset the caret
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, entry->GetInsertionPoint() );
|
2008-09-19 04:55:25 -04:00
|
|
|
}
|
|
|
|
|
2008-12-11 11:46:25 -05:00
|
|
|
void TextEntryTestCase::Replace()
|
|
|
|
{
|
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
|
2009-04-14 09:22:09 -04:00
|
|
|
entry->SetValue("Hello replace!"
|
2008-12-11 11:46:25 -05:00
|
|
|
"0123456789012");
|
|
|
|
entry->SetInsertionPoint(0);
|
|
|
|
|
|
|
|
entry->Replace(6, 13, "changed");
|
|
|
|
|
2009-04-14 09:22:09 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL("Hello changed!"
|
2008-12-11 11:46:25 -05:00
|
|
|
"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());
|
|
|
|
}
|
|
|
|
|
2014-08-11 07:53:01 -04:00
|
|
|
#if wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
|
|
|
class TextEventHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TextEventHandler(wxWindow* win)
|
2014-09-10 12:51:56 -04:00
|
|
|
: m_win(win)
|
2014-08-11 07:53:01 -04:00
|
|
|
{
|
2014-09-10 12:51:56 -04:00
|
|
|
m_win->Bind(wxEVT_TEXT, &TextEventHandler::OnText, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TextEventHandler()
|
|
|
|
{
|
|
|
|
m_win->Unbind(wxEVT_TEXT, &TextEventHandler::OnText, this);
|
2014-08-11 07:53:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const wxString& GetLastString() const
|
|
|
|
{
|
|
|
|
return m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnText(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
m_string = event.GetString();
|
|
|
|
}
|
|
|
|
|
2014-09-10 12:51:56 -04:00
|
|
|
wxWindow* const m_win;
|
|
|
|
|
2014-08-11 07:53:01 -04:00
|
|
|
wxString m_string;
|
|
|
|
};
|
|
|
|
|
2010-08-22 18:16:05 -04:00
|
|
|
void TextEntryTestCase::Editable()
|
|
|
|
{
|
2013-05-12 16:34:43 -04:00
|
|
|
|
|
|
|
#ifdef __WXGTK__
|
|
|
|
// FIXME: For some reason this test regularly (although not always) fails
|
|
|
|
// in wxGTK build bot builds when testing wxBitmapComboBox, but I
|
|
|
|
// can't reproduce the failure locally. For now, disable this check
|
|
|
|
// to let the entire test suite pass in automatic tests instead of
|
|
|
|
// failing sporadically.
|
|
|
|
if ( wxStrcmp(GetTestWindow()->GetClassInfo()->GetClassName(),
|
|
|
|
"wxBitmapComboBox") == 0 &&
|
|
|
|
IsAutomaticTest() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-05-15 09:05:36 -04:00
|
|
|
#endif // __WGTK__
|
2013-05-12 16:34:43 -04:00
|
|
|
|
2010-08-22 18:16:05 -04:00
|
|
|
wxTextEntry * const entry = GetTestEntry();
|
|
|
|
wxWindow * const window = GetTestWindow();
|
|
|
|
|
2013-04-25 06:11:03 -04:00
|
|
|
EventCounter updated(window, wxEVT_TEXT);
|
2010-08-22 18:16:05 -04:00
|
|
|
|
|
|
|
window->SetFocus();
|
|
|
|
wxYield();
|
|
|
|
|
2014-08-11 07:53:01 -04:00
|
|
|
// Check that we get the expected number of events.
|
2010-08-22 18:16:05 -04:00
|
|
|
wxUIActionSimulator sim;
|
|
|
|
sim.Text("abcdef");
|
|
|
|
wxYield();
|
|
|
|
|
2013-05-10 15:21:38 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL(6, updated.GetCount());
|
|
|
|
|
2014-08-11 07:53:01 -04:00
|
|
|
|
|
|
|
// And that the event carries the right value.
|
|
|
|
TextEventHandler handler(window);
|
|
|
|
|
|
|
|
sim.Text("g");
|
|
|
|
wxYield();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL("abcdefg", handler.GetLastString());
|
|
|
|
|
|
|
|
// ... even if we generate the event programmatically and whether it uses
|
|
|
|
// the same value as the control has right now
|
|
|
|
entry->SetValue("abcdefg");
|
|
|
|
CPPUNIT_ASSERT_EQUAL("abcdefg", handler.GetLastString());
|
|
|
|
|
|
|
|
// ... or not
|
|
|
|
entry->SetValue("abcdef");
|
|
|
|
CPPUNIT_ASSERT_EQUAL("abcdef", handler.GetLastString());
|
|
|
|
|
|
|
|
// Check that making the control not editable does indeed prevent it from
|
|
|
|
// being edited.
|
2012-03-11 10:32:24 -04:00
|
|
|
updated.Clear();
|
2010-08-22 18:16:05 -04:00
|
|
|
|
|
|
|
entry->SetEditable(false);
|
|
|
|
sim.Text("gh");
|
|
|
|
wxYield();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL("abcdef", entry->GetValue());
|
2012-03-11 10:32:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0, updated.GetCount());
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
|
|
|
|
2014-08-11 07:53:01 -04:00
|
|
|
#endif // wxUSE_UIACTIONSIMULATOR
|
|
|
|
|
2010-08-22 18:16:05 -04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|