1/*
2	$Id: DeskbarAddItemTest.cpp 1236 2002-09-28 07:27:00Z shatty $
3
4	This file implements tests for the following use cases of BDeskbar:
5	  - Add Item 1
6	  - Add Item 2
7	  - Remove Item 1
8	  - Remove Item 2
9
10	*/
11
12
13#include "DeskbarAddItemTest.h"
14#include <Deskbar.h>
15#include <View.h>
16#include <Application.h>
17#include <Entry.h>
18#include <image.h>
19
20#include <assert.h>
21
22
23const char *appName = "application/x-vnd.jsr-additemtest";
24const char *pulsePath = "/boot/system/apps/ProcessController";
25
26
27/*
28 *  Method:  DeskbarAddItemTest::DeskbarAddItemTest()
29 *   Descr:  This is the constructor for this class.
30 */
31
32
33	DeskbarAddItemTest::DeskbarAddItemTest(std::string name) :
34		TestCase(name)
35{
36	}
37
38
39/*
40 *  Method:  DeskbarAddItemTest::~DeskbarAddItemTest()
41 *   Descr:  This is the destructor for this class.
42 */
43
44
45	DeskbarAddItemTest::~DeskbarAddItemTest()
46{
47	}
48
49
50/*
51 *  Method:  DeskbarAddItemTest::PerformTest()
52 *   Descr:  This member function tests the ability of the BDeskbar class
53 *           when adding and removing an item from the shelf.  It does so by
54 *           adding and removing the pulse item.  It is a good candidate
55 *           it can be added both ways (Add Item 1 and Add Item 2).
56 *
57 *           The code does the following:
58 *             - loads the code for Pulse as an add-on
59 *             - gets the "instantiate_deskbar_item()" function from the add-on
60 *             - calls this function to get a pointer to a BView which can be
61 *               used to test Add Item 1 and in order to get the name of the
62 *               item so it can be removed etc.
63 *             - it gets an entry_ref to the Pulse app in order to test Add
64 *               Item 2
65 *             - it stores in a boolean whether Pulse is in the deskbar at the
66 *               start of the test in order to restore the users config when
67 *               the test completes
68 *             - it adds the item each different way and tests that it exists
69 *             - between each, it removes the item and shows that it has
70 *               been removed
71 */
72
73
74	void DeskbarAddItemTest::PerformTest(void)
75{
76	BApplication theApp(appName);
77
78	BView *(*funcPtr)(void);
79	image_id theImage;
80	assert((theImage = load_add_on(pulsePath)) != B_ERROR);
81	assert(get_image_symbol(theImage, "instantiate_deskbar_item",
82	                        B_SYMBOL_TYPE_TEXT, (void **)&funcPtr) == B_OK);
83	BView *theView = funcPtr();
84	assert(theView != NULL);
85
86	BEntry entry(pulsePath);
87	entry_ref ref;
88	assert(entry.GetRef(&ref) == B_OK);
89
90	int32 theId1, theId2;
91	BDeskbar myDeskbar;
92	bool restorePulse = myDeskbar.HasItem(theView->Name());
93
94	assert(myDeskbar.RemoveItem(theView->Name()) == B_OK);
95	assert(!myDeskbar.HasItem(theView->Name()));
96
97	assert(myDeskbar.AddItem(theView, &theId1) == B_OK);
98	assert(myDeskbar.HasItem(theView->Name()));
99	assert(myDeskbar.GetItemInfo(theView->Name(), &theId2) == B_OK);
100	assert(theId1 == theId2);
101
102	assert(myDeskbar.RemoveItem(theView->Name()) == B_OK);
103	assert(!myDeskbar.HasItem(theView->Name()));
104
105	assert(myDeskbar.AddItem(theView) == B_OK);
106	assert(myDeskbar.HasItem(theView->Name()));
107
108	assert(myDeskbar.RemoveItem(theView->Name()) == B_OK);
109	assert(!myDeskbar.HasItem(theView->Name()));
110
111	assert(myDeskbar.AddItem(&ref, &theId1) == B_OK);
112	assert(myDeskbar.HasItem(theView->Name()));
113	assert(myDeskbar.GetItemInfo(theView->Name(), &theId2) == B_OK);
114	assert(theId1 == theId2);
115
116	assert(myDeskbar.RemoveItem(theView->Name()) == B_OK);
117	assert(!myDeskbar.HasItem(theView->Name()));
118
119	assert(myDeskbar.AddItem(&ref) == B_OK);
120	assert(myDeskbar.HasItem(theView->Name()));
121
122	if (!restorePulse) {
123		assert(myDeskbar.RemoveItem(theView->Name()) == B_OK);
124		assert(!myDeskbar.HasItem(theView->Name()));
125	}
126}
127
128
129/*
130 *  Method:  PropertyConstructionTest::suite()
131 *   Descr:  This static member function returns a test caller for performing
132 *           all combinations of "DeskbarAddItemTest".
133 */
134
135 Test *DeskbarAddItemTest::suite(void)
136{
137	typedef CppUnit::TestCaller<DeskbarAddItemTest>
138		DeskbarAddItemTestCaller;
139
140	return(new DeskbarAddItemTestCaller("BDeskbar::Add Item Test", &DeskbarAddItemTest::PerformTest));
141	}
142