How to edit wxTextCtrl with wxUIActionSimulator

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
matya
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Sep 13, 2022 5:18 pm

How to edit wxTextCtrl with wxUIActionSimulator

Post by matya »

Hi, I learned "how to put controls in tests" from this post.
viewtopic.php?f=1&t=49662

But I'm getting stuck in text editing with the simulator now.

Here is an example.

Code: Select all

// main.cpp
#include <gtest/gtest.h>
#include "wx/wx.h"
#include "wx/app.h"
#include "wx/uiaction.h"

class MyApp: public wxApp{
 public:
    wxFrame* m_frame;
    bool OnInit() {
        m_frame = new wxFrame(nullptr, wxID_ANY, "test");
        m_frame->Show();
        return true;
    }
};

int main(int argc, char ** argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    MyApp* app = new MyApp();
    wxEntryStart(argc, argv);
    app->OnInit();
    return RUN_ALL_TESTS();
}

TEST(SomeTests, test1) {
    wxTextCtrl* text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
    text->SetFocus();
    wxUIActionSimulator sim;
    sim.Text("test");
    wxYield();
    EXPECT_STREQ("test", text->GetValue());
}
I thought it'll work, but it says

Code: Select all

error: Expected equality of these values:
  "test"
  text->GetValue()
    Which is: ""
What am I missing?
How can I edit wxTextCtrl with the simulator?

I'm using Windows10, MSVC 19.31, and wxWidgets 3.1.5.
Thanks in advance.
User avatar
doublemax
Moderator
Moderator
Posts: 17605
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by doublemax »

I've never used wxUIActionSimulator , but maybe you need another wxYield() after SetFocus().
Use the source, Luke!
matya
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Sep 13, 2022 5:18 pm

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by matya »

doublemax wrote: Fri Sep 23, 2022 6:33 am I've never used wxUIActionSimulator , but maybe you need another wxYield() after SetFocus().
Thanks for the comment, but it can't be.

There are some tests in the source code, but they won't use wxYield() after SetFocus().
https://github.com/wxWidgets/wxWidgets/ ... rltest.cpp
https://github.com/wxWidgets/wxWidgets/ ... action.cpp
matya
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Sep 13, 2022 5:18 pm

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by matya »

I tried the same code as this post, but it didn't work.
viewtopic.php?t=44878

Here is the code.

Code: Select all

// main.cpp
#include <gtest/gtest.h>
#include "wx/wx.h"
#include "wx/app.h"
#include "wx/uiaction.h"

class TestFrame : public wxFrame 
{ 
    wxTextCtrl *textIn; 
    wxButton *button; 
    wxTextCtrl *textOut; 

    FRIEND_TEST(GuiTest, CopyTest); 

    public: 

        TestFrame() : wxFrame(NULL, wxID_ANY, "wxUnitTest", wxPoint(50, 50), wxSize(450, 340)) 
        { 
            textIn = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0); 
            button = new wxButton(this, wxID_ANY, wxT(" => "), wxDefaultPosition, wxDefaultSize, 0); 
            button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &TestFrame::OnButton, this); 
            textOut = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0); 
            wxBoxSizer *boxSizer = new wxBoxSizer(wxHORIZONTAL); boxSizer->Add(textIn, 1, wxALL, 5); 
            boxSizer->Add(button, 0, wxALL, 5); boxSizer->Add(textOut, 1, wxALL, 5); 
            this->SetSizer(boxSizer); this->Layout(); this->Centre(wxBOTH); } 

        void OnButton(wxCommandEvent &WXUNUSED(event)) { this->textOut->SetValue(this->textIn->GetValue()); } 
}; 

class TestApp : public wxApp 
{ 
    public: TestFrame *frame; virtual bool OnInit() 
    { 
        frame = new TestFrame(); 
        frame->Show(true); 
        return true; 
    } 
};

class GuiTest : public testing::Test 
{
    protected: 
        TestApp *app; 
        virtual void SetUp() 
        { 
            char appname[] = "wxUnitTest.exe"; 
            int argc = 1; 
            char *argv[1] = { appname }; 
            app = new TestApp(); 
            wxApp::SetInstance(app); 
            wxEntryStart(argc, argv); 
            app->OnInit(); 
        } 

        virtual void TearDown() 
        { 
            //wxTheApp->OnRun(); 
            app->OnExit(); 
            wxEntryCleanup(); 
        } 
};

TEST_F(GuiTest, CopyTest) 
{ 
    wxUIActionSimulator acts; 
    app->frame->textIn->SetFocus(); 
    wxYield(); 
    acts.Text("Text"); 
    wxYield(); 
    app->frame->button->SetFocus(); 
    wxYield(); 
    acts.Char(WXK_RETURN);
    //acts.Char(WXK_SPACE); the result won't change even if I use WXK_SPACE.
    wxYield(); 
    EXPECT_STREQ("Text",app->frame->textOut->GetValue());

}

int main(int argc, char ** argv)
{   
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();  
}
It still says the text box is empty.
matya
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Sep 13, 2022 5:18 pm

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by matya »

I tried the same code on Ubuntu 20.04 with GCC9.4, and macOS 10.12 with AppleClang 9.0.
But they raised the same errors as Windows.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 6222
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by ONEEYEMAN »

Hi,
Can you run wxwidgets test code? Does it succeed for you?
Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 3598
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by PB »

Are you sure you are initializing the wxApp instance correctly, i.e., is the event loop running?
matya
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Sep 13, 2022 5:18 pm

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by matya »

ONEEYEMAN wrote: Fri Sep 23, 2022 11:38 am Hi,
Can you run wxwidgets test code? Does it succeed for you?
Thank you.
No, it failed for tons of tests.
So, am I missing something about building?

Here is the commands I executed.

Code: Select all

curl -OL https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.5/wxWidgets-3.1.5.zip
powershell Expand-Archive -Path wxWidgets-3.1.5.zip

cd C:/wxWidgets-3.1.5
mkdir TestBuild
cd TestBuild

cmake -G "Visual Studio 17 2022"^
 -A x64^
 -D CMAKE_CONFIGURATION_TYPES=Debug^
 -D wxBUILD_TESTS=ALL^
 ../

cmake --build . --config Debug
ctest --verbose -C Debug
And here is the output from tests.
test_output.txt
(49.31 KiB) Downloaded 6 times
matya
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Sep 13, 2022 5:18 pm

Re: How to edit wxTextCtrl with wxUIActionSimulator

Post by matya »

I also tested on Ubuntu 20.04 but it failed.

Here are the commands I executed.

Code: Select all

cd ~/

wget https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.5/wxWidgets-3.1.5.tar.bz2
tar -xvjof wxWidgets-3.1.5.tar.bz2

cd wxWidgets-3.1.5

mkdir TestBuild
cd TestBuild

../configure --enable-debug --disable-shared
make -j$(nproc)

cd tests
make test_gui
./test_gui
and here is the result.
test_ubuntu.txt
(15.05 KiB) Downloaded 2 times
Post Reply