How do i write a unit test for testing gui

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
rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

How do i write a unit test for testing gui

Post by rafae11 » Wed Jul 18, 2018 9:10 am

I am trying to follow this tutorial. http://www.remy.org.uk/tech.php?tech=1407951209

the test compiles and runs apart from not getting the correct result. for some reason the test gui is not getting focus.

I tried to step through the code it appends the text on the current window i have focus which is the text editor.

What am i missing from the example?

Code: Select all

#pragma once

#include <wx/wx.h>
#include <wx/uiaction.h>
#include "gtest/gtest.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); 
    wxYield(); 
    EXPECT_EQ(0,app->frame->textOut->GetValue().Cmp("Text"));

}

// Main.cpp
#include "stdafx.h"
#include "Main.h"
#include "tst_guitest.h"

#include "wx/app.h"

int main(int argc, char ** argv)
{   
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();  
}

catalin
Moderator
Moderator
Posts: 1525
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How do i write a unit test for testing gui

Post by catalin » Wed Jul 18, 2018 9:40 am

Did you look at how-to-write-unit-tests?

rafae11
Experienced Solver
Experienced Solver
Posts: 85
Joined: Sat Jun 02, 2012 8:41 am

Re: How do i write a unit test for testing gui

Post by rafae11 » Wed Jul 18, 2018 10:41 am

I managed to get the test to work, but i don't understand why the test works with a space but not with enter.

Code: Select all

	acts.Char(WXK_RETURN);
with

Code: Select all

acts.Char(WXK_SPACE);

Post Reply