xref: /haiku/src/tests/kits/interface/OutlineListViewTest.cpp (revision 0fae96c5a349db3761ac2a4ab4a7fbbf23a3b76c)
1 /*
2  * Copyright 2018, Sean Healy
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 
8 #include "common.h"
9 
10 #include <Application.h>
11 #include <Window.h>
12 #include <TestUtils.h>
13 
14 
15 #include <OutlineListView.h>
16 
17 BListItem* gExpected[16];
18 int gIndex = 0;
19 int gCount = 0;
20 
21 
22 BListItem* eachitemunder(BListItem* item, void* arg) {
23 	BStringItem* str = (BStringItem*)item;
24 	fprintf(stderr, "Item @%d: %s\n", gIndex, str->Text());
25 
26 	CHK(gIndex < gCount);
27 	CPPUNIT_ASSERT_EQUAL(item, gExpected[gIndex]);
28 	gIndex++;
29 	return NULL;
30 }
31 
32 
33 class OutlineListViewTest: public TestCase
34 {
35 	public:
36 		OutlineListViewTest() {}
37 		OutlineListViewTest(std::string name) : TestCase(name) {}
38 
39 		void EachItemUnder();
40 
41 		static Test* Suite();
42 };
43 
44 
45 void OutlineListViewTest::EachItemUnder() {
46 	BApplication app(
47 		"application/x-vnd.OutlineListView_EachItemUnder.test");
48 	BWindow* window = new BWindow(BRect(50,50,550,550),
49 		"OutlineListView_EachItemUnder", B_TITLED_WINDOW,
50 		B_QUIT_ON_WINDOW_CLOSE, 0);
51 	BOutlineListView* view = new BOutlineListView(BRect(5,5,495,495), "View",
52 		B_MULTIPLE_SELECTION_LIST, B_FOLLOW_ALL);
53 	window->AddChild(view);
54 
55 	view->AddItem(new BStringItem("One", 0));
56 	view->AddItem(new BStringItem("One-A", 1));
57 	view->AddItem(new BStringItem("One-A-1", 2));
58 	view->AddItem(new BStringItem("One-B", 1));
59 	view->AddItem(new BStringItem("One-C", 1));
60 
61 	view->AddItem(new BStringItem("Two", 0));
62 	view->AddItem(new BStringItem("Two-A", 1));
63 	view->AddItem(new BStringItem("Two-A-1", 2));
64 	view->AddItem(new BStringItem("Two-B", 1));
65 	view->AddItem(new BStringItem("Two-C", 1));
66 
67 	view->AddItem(new BStringItem("Three", 0));
68 	view->AddItem(new BStringItem("Three-A", 1));
69 	view->AddItem(new BStringItem("Three-A-1", 2));
70 	view->AddItem(new BStringItem("Three-B", 1));
71 	view->AddItem(new BStringItem("Three-C", 1));
72 
73 	// First test is easy
74 	gExpected[0] = view->FullListItemAt(6);
75 	gExpected[1] = view->FullListItemAt(8);
76 	gExpected[2] = view->FullListItemAt(9);
77 	gCount = 3;
78 	gIndex = 0;
79 
80 	fprintf(stderr, "Easy test\n");
81 	view->EachItemUnder(view->FullListItemAt(5), true, eachitemunder, NULL);
82 
83 	// Check that collapsing an item does not change the outcome
84 	gIndex = 0;
85 	view->Collapse(view->FullListItemAt(0));
86 
87 	fprintf(stderr, "One collapsed\n");
88 	view->EachItemUnder(view->FullListItemAt(5), true, eachitemunder, NULL);
89 
90 	gIndex = 0;
91 	view->Collapse(view->FullListItemAt(5));
92 
93 	fprintf(stderr, "Two collapsed\n");
94 	view->EachItemUnder(view->FullListItemAt(5), true, eachitemunder, NULL);
95 
96 	// Don't actually run anything
97 	delete window;
98 }
99 
100 
101 Test* OutlineListViewTest::Suite()
102 {
103 	TestSuite* SuiteOfTests = new TestSuite;
104 
105 	ADD_TEST4(BOutlineListView, SuiteOfTests, OutlineListViewTest,
106 		EachItemUnder);
107 
108 	return SuiteOfTests;
109 }
110 
111 
112 CppUnit::Test* OutlineListViewTestSuite()
113 {
114 	CppUnit::TestSuite* testSuite = new CppUnit::TestSuite();
115 
116 	testSuite->addTest(OutlineListViewTest::Suite());
117 
118 	return testSuite;
119 }
120