From 5fae0bc1a2f74ccf56b7e3958149317d6af2cccc Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Fri, 19 Sep 2014 09:56:14 +0200 Subject: [PATCH] Add some cppunit-based tests for BTextView. Note that this does not reproduce #8447. I could not get more precise details from involved people about how to reproduce this, and the patch was already applied anyway, so I'm stopping my investigation for now. Feel free to complete this with more test cases. --- .../kits/interface/InterfaceKitTestAddon.cpp | 2 + src/tests/kits/interface/Jamfile | 8 +- ...extViewTest.cpp => TextViewTestManual.cpp} | 317 +++++++++--------- .../kits/interface/btextview/TextViewTest.cpp | 41 +++ .../kits/interface/btextview/TextViewTest.h | 10 + 5 files changed, 214 insertions(+), 164 deletions(-) rename src/tests/kits/interface/{TextViewTest.cpp => TextViewTestManual.cpp} (89%) create mode 100644 src/tests/kits/interface/btextview/TextViewTest.cpp create mode 100644 src/tests/kits/interface/btextview/TextViewTest.h diff --git a/src/tests/kits/interface/InterfaceKitTestAddon.cpp b/src/tests/kits/interface/InterfaceKitTestAddon.cpp index 6ecf730a5b..da48c61c0f 100644 --- a/src/tests/kits/interface/InterfaceKitTestAddon.cpp +++ b/src/tests/kits/interface/InterfaceKitTestAddon.cpp @@ -7,6 +7,7 @@ #include "bdeskbar/DeskbarTest.h" #include "bpolygon/PolygonTest.h" #include "bregion/RegionTest.h" +#include "btextview/TextViewTest.h" //#include "bwidthbuffer/WidthBufferTest.h" #include "GraphicsDefsTest.h" @@ -22,6 +23,7 @@ getTestSuite() suite->addTest("BDeskbar", DeskbarTestSuite()); suite->addTest("BPolygon", PolygonTestSuite()); suite->addTest("BRegion", RegionTestSuite()); + suite->addTest("BTextView", TextViewTestSuite()); //suite->addTest("_BWidthBuffer_", WidthBufferTestSuite()); suite->addTest("GraphicsDefs", GraphicsDefsTestSuite()); diff --git a/src/tests/kits/interface/Jamfile b/src/tests/kits/interface/Jamfile index c6e81493ea..1e410595f9 100644 --- a/src/tests/kits/interface/Jamfile +++ b/src/tests/kits/interface/Jamfile @@ -11,6 +11,7 @@ SEARCH_SOURCE += [ FDirName $(SUBDIR) bbitmap ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) bdeskbar ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) bpolygon ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) bregion ] ; +SEARCH_SOURCE += [ FDirName $(SUBDIR) btextview ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) bwindowstack ] ; SEARCH_SOURCE += [ FDirName $(TOP) src kits interface ] ; @@ -49,6 +50,8 @@ UnitTestLib libinterfacetest.so RegionIntersect.cpp RegionOffsetBy.cpp + TextViewTest.cpp + : be [ TargetLibstdc++ ] ; @@ -164,7 +167,7 @@ SimpleTest SetBorderScrollViewTest : ; SimpleTest TextViewTest : - TextViewTest.cpp + TextViewTestManual.cpp : be [ TargetLibsupc++ ] ; @@ -174,7 +177,8 @@ SimpleTest WindowStackTest : ; SEARCH on [ FGristFiles - ScrollView.cpp CheckBox.cpp ChannelSlider.cpp ChannelControl.cpp Slider.cpp Control.cpp + ScrollView.cpp CheckBox.cpp ChannelSlider.cpp ChannelControl.cpp + Slider.cpp Control.cpp ] = [ FDirName $(HAIKU_TOP) src kits interface ] ; SubInclude HAIKU_TOP src tests kits interface bprintjob ; diff --git a/src/tests/kits/interface/TextViewTest.cpp b/src/tests/kits/interface/TextViewTestManual.cpp similarity index 89% rename from src/tests/kits/interface/TextViewTest.cpp rename to src/tests/kits/interface/TextViewTestManual.cpp index 0b5b218beb..0b1462593d 100644 --- a/src/tests/kits/interface/TextViewTest.cpp +++ b/src/tests/kits/interface/TextViewTestManual.cpp @@ -1,162 +1,155 @@ -/* - * Copyright 2009, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved. - * Distributed under the terms of the MIT License. - */ - - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - - -const static uint32 kMsgAlignLeft = 'alle'; -const static uint32 kMsgAlignCenter = 'alce'; -const static uint32 kMsgAlignRight = 'alri'; - - -class Window : public BWindow { - public: - Window(); - - virtual bool QuitRequested(); - virtual void MessageReceived(BMessage *message); - - private: - BTextControl* fTextControl; - BTextView* fTextView; -}; - - -// #pragma mark - - - -Window::Window() - : BWindow(BRect(100, 100, 800, 500), "TextView-Test", - B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS) -{ - fTextControl = new BTextControl("text-contr-O", - "a single line of text - (c) Conglom-O", NULL); - fTextView = new BTextView("text-O"); - BScrollView* scrollView = new BScrollView("scroll-O", fTextView, 0, true, - true, B_FANCY_BORDER); - - SetLayout(new BGroupLayout(B_HORIZONTAL)); - AddChild(BGroupLayoutBuilder(B_VERTICAL, 10) - .Add(fTextControl) - .Add(scrollView) - .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10) - .Add(new BButton("Align Left", new BMessage(kMsgAlignLeft))) - .AddGlue() - .Add(new BButton("Align Center", new BMessage(kMsgAlignCenter))) - .AddGlue() - .Add(new BButton("Align Right", new BMessage(kMsgAlignRight))) - ) - .SetInsets(5, 5, 5, 5) - ); - - // generate some lines of content - const int32 kLineCount = 10; - const int32 kLineNoSize = 6; - BString line = ": just some text here - nothing special to see\n"; - BString format = BString("%*d") << line; - BString content; - int32 lineLength = line.Length() + kLineNoSize; - int32 contentLength = lineLength * kLineCount; - char* currLine = content.LockBuffer(contentLength); - if (currLine) { - int32 lineNo = 0; - for ( ; lineNo < kLineCount; currLine += lineLength) - sprintf(currLine, format.String(), kLineNoSize, lineNo++); - content.UnlockBuffer(contentLength); - } - fTextView->SetInsets(2,2,2,2); - fTextView->SetText(content.String()); -} - - -bool -Window::QuitRequested() -{ - be_app->PostMessage(B_QUIT_REQUESTED); - return true; -} - - -void -Window::MessageReceived(BMessage *message) -{ - switch (message->what) { - case kMsgAlignLeft: - fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_LEFT); - fTextView->SetAlignment(B_ALIGN_LEFT); - break; - - case kMsgAlignCenter: - fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER); - fTextView->SetAlignment(B_ALIGN_CENTER); - break; - - case kMsgAlignRight: - fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT); - fTextView->SetAlignment(B_ALIGN_RIGHT); - break; - - default: - BWindow::MessageReceived(message); - break; - } -} - - -// #pragma mark - - - -class Application : public BApplication { - public: - Application(); - - virtual void ReadyToRun(void); -}; - - -Application::Application() - : BApplication("application/x-vnd.haiku-test") -{ -} - - -void -Application::ReadyToRun(void) -{ - BWindow *window = new Window(); - window->Show(); -} - - -// #pragma mark - - - -int -main(int argc, char **argv) -{ - Application app; - - const int kExpectedTextViewSize = 356; - if (sizeof(BTextView) != kExpectedTextViewSize) { - fprintf(stderr, "sizeof(BTextView) is %ld instead of %d!\n", - sizeof(BTextView), kExpectedTextViewSize); - return 1; - } - - app.Run(); - return 0; -} - +/* + * Copyright 2009, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +const static uint32 kMsgAlignLeft = 'alle'; +const static uint32 kMsgAlignCenter = 'alce'; +const static uint32 kMsgAlignRight = 'alri'; + + +class Window : public BWindow { + public: + Window(); + + virtual bool QuitRequested(); + virtual void MessageReceived(BMessage *message); + + private: + BTextControl* fTextControl; + BTextView* fTextView; +}; + + +// #pragma mark - + + +Window::Window() + : BWindow(BRect(100, 100, 800, 500), "TextView-Test", + B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS) +{ + fTextControl = new BTextControl("text-contr-O", + "a single line of text - (c) Conglom-O", NULL); + fTextView = new BTextView("text-O"); + BScrollView* scrollView = new BScrollView("scroll-O", fTextView, 0, true, + true, B_FANCY_BORDER); + + SetLayout(new BGroupLayout(B_HORIZONTAL)); + AddChild(BGroupLayoutBuilder(B_VERTICAL, 10) + .Add(fTextControl) + .Add(scrollView) + .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10) + .Add(new BButton("Align Left", new BMessage(kMsgAlignLeft))) + .AddGlue() + .Add(new BButton("Align Center", new BMessage(kMsgAlignCenter))) + .AddGlue() + .Add(new BButton("Align Right", new BMessage(kMsgAlignRight))) + ) + .SetInsets(5, 5, 5, 5) + ); + + // generate some lines of content + const int32 kLineCount = 10; + const int32 kLineNoSize = 6; + BString line = ": just some text here - nothing special to see\n"; + BString format = BString("%*d") << line; + BString content; + int32 lineLength = line.Length() + kLineNoSize; + int32 contentLength = lineLength * kLineCount; + char* currLine = content.LockBuffer(contentLength); + if (currLine) { + int32 lineNo = 0; + for ( ; lineNo < kLineCount; currLine += lineLength) + sprintf(currLine, format.String(), kLineNoSize, lineNo++); + content.UnlockBuffer(contentLength); + } + fTextView->SetInsets(2,2,2,2); + fTextView->SetText(content.String()); +} + + +bool +Window::QuitRequested() +{ + be_app->PostMessage(B_QUIT_REQUESTED); + return true; +} + + +void +Window::MessageReceived(BMessage *message) +{ + switch (message->what) { + case kMsgAlignLeft: + fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_LEFT); + fTextView->SetAlignment(B_ALIGN_LEFT); + break; + + case kMsgAlignCenter: + fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER); + fTextView->SetAlignment(B_ALIGN_CENTER); + break; + + case kMsgAlignRight: + fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT); + fTextView->SetAlignment(B_ALIGN_RIGHT); + break; + + default: + BWindow::MessageReceived(message); + break; + } +} + + +// #pragma mark - + + +class Application : public BApplication { + public: + Application(); + + virtual void ReadyToRun(void); +}; + + +Application::Application() + : BApplication("application/x-vnd.haiku-test") +{ +} + + +void +Application::ReadyToRun(void) +{ + BWindow *window = new Window(); + window->Show(); +} + + +// #pragma mark - + + +int +main(int argc, char **argv) +{ + Application app; + + app.Run(); + return 0; +} + diff --git a/src/tests/kits/interface/btextview/TextViewTest.cpp b/src/tests/kits/interface/btextview/TextViewTest.cpp new file mode 100644 index 0000000000..607f4404b1 --- /dev/null +++ b/src/tests/kits/interface/btextview/TextViewTest.cpp @@ -0,0 +1,41 @@ +#include "../common.h" + +#include +#include +#include + +class TextViewTestcase: public TestCase { +public: + void + SizeTest() + { + CPPUNIT_ASSERT_EQUAL(356, sizeof(BTextView)); + } + + void + GetTextTest() + { + BApplication app("application/x-vnd.Haiku-interfacekit-textviewtest"); + BRect textRect(0, 0, 100, 100); + BTextView* v = new BTextView(textRect, "test", textRect, 0, 0); + v->SetText("Initial text"); + v->Insert(8, "(inserted) ", 10); + char buffer[12]; + v->GetText(2, 11, buffer); + CPPUNIT_ASSERT_EQUAL(BString("itial (inse"), buffer); + } +}; + + +Test* +TextViewTestSuite() +{ + TestSuite *testSuite = new TestSuite(); + + testSuite->addTest(new CppUnit::TestCaller( + "BTextView_Size", &TextViewTestcase::SizeTest)); + testSuite->addTest(new CppUnit::TestCaller( + "BTextView_GetText", &TextViewTestcase::GetTextTest)); + + return testSuite; +} diff --git a/src/tests/kits/interface/btextview/TextViewTest.h b/src/tests/kits/interface/btextview/TextViewTest.h new file mode 100644 index 0000000000..e3e1549e8f --- /dev/null +++ b/src/tests/kits/interface/btextview/TextViewTest.h @@ -0,0 +1,10 @@ +#ifndef _text_view_test_h_ +#define _text_view_test_h_ + +class CppUnit::Test; + +CppUnit::Test *TextViewTestSuite(); + +#endif // text_view_test_h_ + +