我正在尝试遵循本教程。 http://www.remy.org.uk/tech.php?tech=1407951209
该测试会编译并运行,除了无法获得正确的结果。由于某种原因,测试GUI无法获得关注。
我试图逐步完成将代码添加到当前窗口的代码,我将焦点放在文本编辑器上。
示例中我缺少什么?
#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();
ASSERT_EQ("Text", app->frame->textOut->GetValue());
}
// 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();
}