1/*
2	$Id: DeskbarGetItemTest.cpp 1236 2002-09-28 07:27:00Z shatty $
3
4	This file implements tests for the following use cases of BDeskbar:
5	  - Count Items
6	  - Has Item 1
7	  - Has Item 2
8	  - Get Item Info 1
9	  - Get Item Info 2
10
11	*/
12
13
14#include "DeskbarGetItemTest.h"
15#include <Deskbar.h>
16
17#include <assert.h>
18
19
20/*
21 *  Method:  DeskbarGetItemTest::DeskbarGetItemTest()
22 *   Descr:  This is the constructor for this class.
23 */
24
25
26	DeskbarGetItemTest::DeskbarGetItemTest(std::string name) :
27		TestCase(name)
28{
29	}
30
31
32/*
33 *  Method:  DeskbarGetItemTest::~DeskbarGetItemTest()
34 *   Descr:  This is the destructor for this class.
35 */
36
37
38	DeskbarGetItemTest::~DeskbarGetItemTest()
39{
40	}
41
42
43/*
44 *  Method:  DeskbarGetItemTest::PerformTest()
45 *   Descr:  This member function iterates over all of the items in the
46 *           deskbar shelf and gets information about each item and confirms
47 *           that all of the information is self-consistent.
48 */
49
50
51	void DeskbarGetItemTest::PerformTest(void)
52{
53	BDeskbar myDeskbar;
54
55	int32 itemCount = myDeskbar.CountItems();
56	assert(itemCount >= 0);
57
58
59	int32 id=0;
60	int32 lastFoundId = -1;
61	char buffer[1024];
62	const char *name = buffer;
63
64	assert(!myDeskbar.HasItem("NameThatDoesNotExistWeHope!!"));
65	assert(myDeskbar.GetItemInfo("NameThatDoesNotExistWeHope!!", &id) == B_NAME_NOT_FOUND);
66
67	for(id = 0; ((id < 10000) && (itemCount > 0)); id++) {
68		int32 tmpId;
69
70		if (myDeskbar.HasItem(id)) {
71			itemCount--;
72
73			/* In Be's implementation, if the char * points to NULL, it
74			   returns B_BAD_VALUE.  However, no matter what it points to
75			   before you start, it is changed by the call to GetItemInfo().
76			   So, if it points to allocated memory, there is a good chance
77			   for a leak.  I would argue that Be should return B_BAD_VALUE
78			   if it points to non-NULL.  The Haiku implementation does
79			   not return B_BAD_VALUE in this case so this assert would fail
80			   from Haiku.  However, this is considered to be an acceptable
81			   deviation from Be's implementation.
82			name = NULL;
83			assert(myDeskbar.GetItemInfo(id, &name) == B_BAD_VALUE); */
84
85			name = buffer;
86			assert(myDeskbar.GetItemInfo(id, &name) == B_OK);
87
88			assert(name != buffer);
89			assert(myDeskbar.HasItem(name));
90			assert(myDeskbar.GetItemInfo(name, &tmpId) == B_OK);
91			delete[] name;
92			name = buffer;
93			assert(tmpId == id);
94			lastFoundId = id;
95		} else {
96			assert(myDeskbar.GetItemInfo(id, &name) == B_NAME_NOT_FOUND);
97			assert(name == buffer);
98		}
99	}
100	assert(itemCount == 0);
101	if (lastFoundId >= 0) {
102		for(id = lastFoundId + 1; id < lastFoundId + 200; id++) {
103			assert(!myDeskbar.HasItem(id));
104			assert(myDeskbar.GetItemInfo(id, &name) == B_NAME_NOT_FOUND);
105			assert(name == buffer);
106		}
107	}
108}
109
110
111/*
112 *  Method:  PropertyConstructionTest::suite()
113 *   Descr:  This static member function returns a test caller for performing
114 *           all combinations of "DeskbarGetItemTest".
115 */
116
117 Test *DeskbarGetItemTest::suite(void)
118{
119	typedef CppUnit::TestCaller<DeskbarGetItemTest>
120		DeskbarGetItemTestCaller;
121
122	return(new DeskbarGetItemTestCaller("BDeskbar::Get Item Test", &DeskbarGetItemTest::PerformTest));
123	}
124