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

googletest and wxuiactionsimulator

Post by rafae11 » Wed Feb 28, 2018 3:44 pm

I am trying to create a unit test with some user interface simulations using google test.
The code under test works in my main application the problem i am encountering is that on my test code I want to extract some data that the user input in the text field. I think the message loop isn't working properly when i run my unit test.
I am using wxwidgets 3.1.0 and visual studio 2015.
What would be the proper way to initialize the test so that the message loop is working?

Code: Select all

#ifndef  _FRAME
#define  _FRAME

#include "Form.h"
#include "Source.h"
#include "gtest\gtest.h"


class IFrame : public Frame
{	
public:
	IFrame(wxWindow* parent);

	FRIEND_TEST(GuiTest, CopyTest);	
	
	~IFrame();

protected:
	void m_button1OnButtonClick(wxCommandEvent& event);
	void m_button2OnButtonClick(wxCommandEvent& event);
	bool testff();

};

class TestApp : public wxApp
{
	public: IFrame *frame; 
	
	virtual bool OnInit()
	{
		frame = new IFrame(NULL);
		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();		
		
		// I have a feeling something is wrong with this section.
	}

	virtual void TearDown()
	{
		// if i have app on run uncommented the window stays open the whole time.
		//app->OnRun();
		app->OnExit();
		wxEntryCleanup();
	}
};

#endif // ! FRAME

#include "stdafx.h"
#include "main.h"
#include "Source.h"
#include "Frame.h"

TEST(t1, check)
{
	LSource l;
	EXPECT_EQ(l.FirstFunction(), false);
	EXPECT_EQ(2, 2);
}

TEST(t1, check2)
{
	EXPECT_EQ(2, 2);
}

TEST_F(GuiTest, CopyTest)
{
	wxUIActionSimulator acts;
	app->frame->m_textCtrl1->SetFocus();
	wxYield();
	acts.Text("Text");
	wxYield();
	app->frame->m_textCtrl2->SetFocus();
	acts.Text("Text");
	wxYield();

	wxString val1 = app->frame->m_textCtrl1->GetValue();
	wxString val2 = app->frame->m_textCtrl2->GetValue();

	wxYield();
	app->frame->Close();
	wxYield();

	wxString x("Text");
	std::string y("Text");

	ASSERT_STREQ(x.ToStdString().c_str(), val1.ToStdString().c_str());

	//EXPECT_EQ("Text", val1.c_str());
	//EXPECT_EQ("Text", val2.c_str());
}

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

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3461
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: googletest and wxuiactionsimulator

Post by ONEEYEMAN » Wed Feb 28, 2018 4:18 pm

Hi,
Take a look at the test folder of the wxWidgets distribution.
It does not use GoogleTest, but yoiu should get the idea.

Thank you.

Post Reply