From 15db99249ada7b04ea295009cb04eb0e2093f5e5 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 23 Jul 2014 10:14:35 +0200 Subject: [PATCH] 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. --- src/tests/servers/app/harness/harness.cpp | 173 +++++++++++++++ src/tests/servers/app/harness/harness.h | 50 +++++ src/tests/servers/app/transformation/Jamfile | 3 + src/tests/servers/app/transformation/main.cpp | 201 +----------------- 4 files changed, 228 insertions(+), 199 deletions(-) create mode 100644 src/tests/servers/app/harness/harness.cpp create mode 100644 src/tests/servers/app/harness/harness.h diff --git a/src/tests/servers/app/harness/harness.cpp b/src/tests/servers/app/harness/harness.cpp new file mode 100644 index 0000000000..8ae735817e --- /dev/null +++ b/src/tests/servers/app/harness/harness.cpp @@ -0,0 +1,173 @@ +/* + * Copyright 2014 Stephan Aßmus + * All rights reserved. Distributed under the terms of the MIT license. + */ + + +#include "harness.h" + + +#include +#include +#include +#include +#include +#include + + +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); +} + + diff --git a/src/tests/servers/app/harness/harness.h b/src/tests/servers/app/harness/harness.h new file mode 100644 index 0000000000..c4901a38b6 --- /dev/null +++ b/src/tests/servers/app/harness/harness.h @@ -0,0 +1,50 @@ +/* + * Copyright 2014 Stephan Aßmus + * All rights reserved. Distributed under the terms of the MIT license. + */ + + +#include +#include +#include +#include + + +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; +}; + + diff --git a/src/tests/servers/app/transformation/Jamfile b/src/tests/servers/app/transformation/Jamfile index 86d77ff78e..e73b55737b 100644 --- a/src/tests/servers/app/transformation/Jamfile +++ b/src/tests/servers/app/transformation/Jamfile @@ -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 diff --git a/src/tests/servers/app/transformation/main.cpp b/src/tests/servers/app/transformation/main.cpp index 26e733df35..0b3f0145bd 100644 --- a/src/tests/servers/app/transformation/main.cpp +++ b/src/tests/servers/app/transformation/main.cpp @@ -11,51 +11,18 @@ #include #include #include -#include -#include -#include #include -#include #include #include #include -#include #include #include -#include -#include +#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());