Simple test harness for app_server tests

This was developped for the transformations test, but can be used for
other tests too. Extract the relevant classes to a separate file.
This commit is contained in:
Adrien Destugues 2014-07-23 10:14:35 +02:00
parent c5ed108884
commit 15db99249a
4 changed files with 228 additions and 199 deletions

View File

@ -0,0 +1,173 @@
/*
* Copyright 2014 Stephan Aßmus <superstippi@gmx.de>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "harness.h"
#include <Application.h>
#include <Bitmap.h>
#include <LayoutBuilder.h>
#include <Message.h>
#include <PopUpMenu.h>
#include <ScrollView.h>
Test::Test(const char* name)
:
fName(name)
{
}
Test::~Test()
{
}
// #pragma mark - TestView
class TestView : public BView {
public:
TestView();
virtual ~TestView();
virtual void Draw(BRect updateRect);
void SetTest(Test* test);
private:
Test* fTest;
};
TestView::TestView()
:
BView(NULL, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fTest(NULL)
{
}
TestView::~TestView()
{
}
void
TestView::Draw(BRect updateRect)
{
if (fTest != NULL)
fTest->Draw(this, updateRect);
}
void
TestView::SetTest(Test* test)
{
fTest = test;
Invalidate();
}
// #pragma mark - TestWindow
enum {
MSG_SELECT_TEST = 'stst'
};
TestWindow::TestWindow(const char* title)
:
BWindow(BRect(50.0, 50.0, 450.0, 250.0), title,
B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE
| B_AUTO_UPDATE_SIZE_LIMITS)
{
fTestView = new TestView();
BScrollView* scrollView = new BScrollView("scroll", fTestView, 0, true,
true);
fTestSelectionField = new BMenuField("test selection",
"Select test:", new BPopUpMenu("select"));
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
.AddGroup(B_HORIZONTAL)
.Add(fTestSelectionField)
.AddGlue()
.SetInsets(B_USE_DEFAULT_SPACING)
.End()
.Add(scrollView)
;
}
TestWindow::~TestWindow()
{
for (int32 i = fTests.CountItems() - 1; i >= 0; i++)
delete (Test*)fTests.ItemAt(i);
}
void
TestWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_SELECT_TEST:
{
int32 index;
if (message->FindInt32("index", &index) == B_OK)
SetToTest(index);
break;
}
default:
BWindow::MessageReceived(message);
}
}
void
TestWindow::AddTest(Test* test)
{
if (test == NULL || fTests.HasItem(test))
return;
if (!fTests.AddItem(test)) {
delete test;
return;
}
BMessage* message = new BMessage(MSG_SELECT_TEST);
message->AddInt32("index", fTests.CountItems() - 1);
BMenuItem* item = new BMenuItem(test->Name(), message);
if (!fTestSelectionField->Menu()->AddItem(item)) {
fTests.RemoveItem(fTests.CountItems() - 1);
delete test;
delete item;
return;
}
if (fTests.CountItems() == 1)
SetToTest(0);
}
void
TestWindow::SetToTest(int32 index)
{
Test* test = (Test*)fTests.ItemAt(index);
if (test == NULL)
return;
fTestSelectionField->Menu()->ItemAt(index)->SetMarked(true);
fTestView->SetTest(test);
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2014 Stephan Aßmus <superstippi@gmx.de>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include <List.h>
#include <String.h>
#include <View.h>
#include <Window.h>
class BMenuField;
class TestView;
class Test {
public:
Test(const char* name);
virtual ~Test();
const char* Name() const
{ return fName.String(); }
virtual void Draw(BView* view, BRect updateRect) = 0;
private:
BString fName;
};
class TestWindow : public BWindow {
public:
TestWindow(const char* title);
virtual ~TestWindow();
virtual void MessageReceived(BMessage* message);
void AddTest(Test* test);
void SetToTest(int32 index);
private:
TestView* fTestView;
BMenuField* fTestSelectionField;
BList fTests;
};

View File

@ -6,7 +6,10 @@ AddSubDirSupportedPlatforms libbe_test ;
UseHeaders [ FDirName os app ] ;
UseHeaders [ FDirName os interface ] ;
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src tests servers app harness ] ;
SimpleTest Transformation :
harness.cpp
main.cpp
: be $(TARGET_LIBSUPC++)
: Transformation.rdef

View File

@ -11,51 +11,18 @@
#include <Application.h>
#include <Bitmap.h>
#include <GradientLinear.h>
#include <LayoutBuilder.h>
#include <List.h>
#include <Message.h>
#include <Picture.h>
#include <PopUpMenu.h>
#include <Region.h>
#include <Resources.h>
#include <Roster.h>
#include <ScrollView.h>
#include <String.h>
#include <StringView.h>
#include <View.h>
#include <Window.h>
#include "harness.h"
static const char* kAppSignature = "application/x-vnd.Haiku-Transformation";
class Test {
public:
Test(const char* name);
virtual ~Test();
const char* Name() const
{ return fName.String(); }
virtual void Draw(BView* view, BRect updateRect) = 0;
private:
BString fName;
};
Test::Test(const char* name)
:
fName(name)
{
}
Test::~Test()
{
}
class BitmapTest : public Test {
public:
BitmapTest(const char* name)
@ -115,170 +82,6 @@ protected:
};
// #pragma mark - TestView
class TestView : public BView {
public:
TestView();
virtual ~TestView();
virtual void Draw(BRect updateRect);
void SetTest(Test* test);
private:
Test* fTest;
};
TestView::TestView()
:
BView(NULL, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fTest(NULL)
{
}
TestView::~TestView()
{
}
void
TestView::Draw(BRect updateRect)
{
if (fTest != NULL)
fTest->Draw(this, updateRect);
}
void
TestView::SetTest(Test* test)
{
fTest = test;
Invalidate();
}
// #pragma mark - TestWindow
enum {
MSG_SELECT_TEST = 'stst'
};
class TestWindow : public BWindow {
public:
TestWindow();
virtual ~TestWindow();
virtual void MessageReceived(BMessage* message);
void AddTest(Test* test);
void SetToTest(int32 index);
private:
TestView* fTestView;
BMenuField* fTestSelectionField;
BList fTests;
};
TestWindow::TestWindow()
:
BWindow(BRect(50.0, 50.0, 450.0, 250.0), "Transformations Test",
B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE
| B_AUTO_UPDATE_SIZE_LIMITS)
{
fTestView = new TestView();
BScrollView* scrollView = new BScrollView("scroll", fTestView, 0, true,
true);
fTestSelectionField = new BMenuField("test selection",
"Select test:", new BPopUpMenu("select"));
BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
.AddGroup(B_HORIZONTAL)
.Add(fTestSelectionField)
.AddGlue()
.SetInsets(B_USE_DEFAULT_SPACING)
.End()
.Add(scrollView)
;
}
TestWindow::~TestWindow()
{
for (int32 i = fTests.CountItems() - 1; i >= 0; i++)
delete (Test*)fTests.ItemAt(i);
}
void
TestWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_SELECT_TEST:
{
int32 index;
if (message->FindInt32("index", &index) == B_OK)
SetToTest(index);
break;
}
default:
BWindow::MessageReceived(message);
}
}
void
TestWindow::AddTest(Test* test)
{
if (test == NULL || fTests.HasItem(test))
return;
if (!fTests.AddItem(test)) {
delete test;
return;
}
BMessage* message = new BMessage(MSG_SELECT_TEST);
message->AddInt32("index", fTests.CountItems() - 1);
BMenuItem* item = new BMenuItem(test->Name(), message);
if (!fTestSelectionField->Menu()->AddItem(item)) {
fTests.RemoveItem(fTests.CountItems() - 1);
delete test;
delete item;
return;
}
if (fTests.CountItems() == 1)
SetToTest(0);
}
void
TestWindow::SetToTest(int32 index)
{
Test* test = (Test*)fTests.ItemAt(index);
if (test == NULL)
return;
fTestSelectionField->Menu()->ItemAt(index)->SetMarked(true);
fTestView->SetTest(test);
}
// #pragma mark - Test1
@ -633,7 +436,7 @@ main(int argc, char** argv)
{
BApplication app(kAppSignature);
TestWindow* window = new TestWindow();
TestWindow* window = new TestWindow("Transformation tests");
window->AddTest(new RectsTest());
window->AddTest(new BitmapClipTest());