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.
This commit is contained in:
Adrien Destugues
2014-09-19 09:59:38 +02:00
parent 0bbed9620a
commit 5fae0bc1a2
5 changed files with 214 additions and 164 deletions
@@ -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());
+6 -2
View File
@@ -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 ;
@@ -1,162 +1,155 @@
/*
* Copyright 2009, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <Application.h>
#include <Button.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <ScrollView.h>
#include <String.h>
#include <TextControl.h>
#include <TextView.h>
#include <Window.h>
#include <stdio.h>
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 <Application.h>
#include <Button.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <ScrollView.h>
#include <String.h>
#include <TextControl.h>
#include <TextView.h>
#include <Window.h>
#include <stdio.h>
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;
}
@@ -0,0 +1,41 @@
#include "../common.h"
#include <Application.h>
#include <String.h>
#include <TextView.h>
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<TextViewTestcase>(
"BTextView_Size", &TextViewTestcase::SizeTest));
testSuite->addTest(new CppUnit::TestCaller<TextViewTestcase>(
"BTextView_GetText", &TextViewTestcase::GetTextTest));
return testSuite;
}
@@ -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_