xref: /haiku/src/kits/tracker/Tests.cpp (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 #if DEBUG
36 
37 #include "Tests.h"
38 
39 #include <Debug.h>
40 #include <Locker.h>
41 #include <Path.h>
42 #include <String.h>
43 #include <Window.h>
44 
45 
46 #include "EntryIterator.h"
47 #include "IconCache.h"
48 #include "Model.h"
49 #include "NodeWalker.h"
50 #include "StopWatch.h"
51 #include "Thread.h"
52 
53 
54 
55 
56 const char *pathsToSearch[] = {
57 //	"/boot/home/config/settings/NetPositive/Bookmarks/",
58 	"/boot/beos",
59 	"/boot/apps",
60 	"/boot/home",
61 	0
62 };
63 
64 namespace BTrackerPrivate {
65 
66 class IconSpewer : public SimpleThread {
67 public:
68 	IconSpewer(bool newCache = true);
69 	~IconSpewer();
70 	void SetTarget(BWindow *target)
71 		{ this->target = target; }
72 
73 	void Quit();
74 	void Run();
75 
76 protected:
77 	void DrawSomeNew();
78 	void DrawSomeOld();
79 	const entry_ref *NextRef();
80 private:
81 	BLocker locker;
82 	bool quitting;
83 	BWindow *target;
84 	TNodeWalker *walker;
85 	CachedEntryIterator *cachingIterator;
86 	int32 searchPathIndex;
87 	bigtime_t cycleTime;
88 	bigtime_t lastCycleLap;
89 	int32 numDrawn;
90 	BStopWatch watch;
91 	bool newCache;
92 	BPath currentPath;
93 
94 	entry_ref ref;
95 };
96 
97 class IconTestWindow : public BWindow {
98 public:
99 	IconTestWindow();
100 	bool QuitRequested();
101 private:
102 	IconSpewer iconSpewer;
103 };
104 
105 }	// namespace BTrackerPrivate
106 
107 
108 IconSpewer::IconSpewer(bool newCache)
109 	:	quitting(false),
110 		cachingIterator(0),
111 		searchPathIndex(0),
112 		cycleTime(0),
113 		lastCycleLap(0),
114 		numDrawn(0),
115 		watch("", true),
116 		newCache(newCache)
117 {
118 	walker = new TNodeWalker(pathsToSearch[searchPathIndex++]);
119 	if (newCache)
120 		cachingIterator = new CachedEntryIterator(walker, 40);
121 }
122 
123 
124 IconSpewer::~IconSpewer()
125 {
126 	delete walker;
127 	delete cachingIterator;
128 }
129 
130 void
131 IconSpewer::Run()
132 {
133 	BStopWatch watch("", true);
134 	for (;;) {
135 		AutoLock<BLocker> lock(locker);
136 
137 		if (!lock || quitting)
138 			break;
139 
140 		lock.Unlock();
141 		if (newCache)
142 			DrawSomeNew();
143 		else
144 			DrawSomeOld();
145 	}
146 }
147 
148 void
149 IconSpewer::Quit()
150 {
151 	kill_thread(fScanThread);
152 	fScanThread = -1;
153 }
154 
155 const icon_size kIconSize = B_LARGE_ICON;
156 const int32 kRowCount = 10;
157 const int32 kColumnCount = 10;
158 
159 void
160 IconSpewer::DrawSomeNew()
161 {
162 	target->Lock();
163 	BView *view = target->FindView("iconView");
164 	ASSERT(view);
165 
166 	BRect bounds(target->Bounds());
167 	view->SetHighColor(Color(255, 255, 255));
168 	view->FillRect(bounds);
169 
170 	view->SetHighColor(Color(0, 0, 0));
171 	char buffer[256];
172 	if (cycleTime) {
173 		sprintf(buffer, "last cycle time %Ld ms", cycleTime/1000);
174 		view->DrawString(buffer, BPoint(20, bounds.bottom - 20));
175 	}
176 	if (numDrawn) {
177 		sprintf(buffer, "average draw time %Ld us per icon", watch.ElapsedTime() / numDrawn);
178 		view->DrawString(buffer, BPoint(20, bounds.bottom - 30));
179 	}
180 	sprintf(buffer, "directory: %s", currentPath.Path());
181 	view->DrawString(buffer, BPoint(20, bounds.bottom - 40));
182 
183 	target->Unlock();
184 
185 	for (int32 row = 0; row < kRowCount; row++) {
186 		for (int32 column = 0; column < kColumnCount; column++) {
187 			BEntry entry(NextRef());
188 			Model model(&entry, true);
189 
190 			if (!target->Lock())
191 				return;
192 
193 			if (model.IsDirectory())
194 				entry.GetPath(&currentPath);
195 
196 			IconCache::sIconCache->Draw(&model, view, BPoint(column * (kIconSize + 2),
197 				row * (kIconSize + 2)), kNormalIcon, kIconSize, true);
198 			target->Unlock();
199 			numDrawn++;
200 		}
201 	}
202 }
203 
204 bool oldIconCacheInited = false;
205 void
206 IconSpewer::DrawSomeOld()
207 {
208 #if 0
209 	if (!oldIconCacheInited)
210 		BIconCache::InitIconCaches();
211 
212 	target->Lock();
213 	target->SetTitle("old cache");
214 	BView *view = target->FindView("iconView");
215 	ASSERT(view);
216 
217 	BRect bounds(target->Bounds());
218 	view->SetHighColor(Color(255, 255, 255));
219 	view->FillRect(bounds);
220 
221 	view->SetHighColor(Color(0, 0, 0));
222 	char buffer[256];
223 	if (cycleTime) {
224 		sprintf(buffer, "last cycle time %Ld ms", cycleTime/1000);
225 		view->DrawString(buffer, BPoint(20, bounds.bottom - 20));
226 	}
227 	if (numDrawn) {
228 		sprintf(buffer, "average draw time %Ld us per icon", watch.ElapsedTime() / numDrawn);
229 		view->DrawString(buffer, BPoint(20, bounds.bottom - 30));
230 	}
231 	sprintf(buffer, "directory: %s", currentPath.Path());
232 	view->DrawString(buffer, BPoint(20, bounds.bottom - 40));
233 
234 	target->Unlock();
235 
236 	for (int32 row = 0; row < kRowCount; row++) {
237 		for (int32 column = 0; column < kColumnCount; column++) {
238 			BEntry entry(NextRef());
239 			BModel model(&entry, true);
240 
241 			if (!target->Lock())
242 				return;
243 
244 			if (model.IsDirectory())
245 				entry.GetPath(&currentPath);
246 
247 			BIconCache::LockIconCache();
248 			BIconCache *iconCache = BIconCache::GetIconCache(&model, kIconSize);
249 			iconCache->Draw(view, BPoint(column * (kIconSize + 2),
250 				row * (kIconSize + 2)), B_NORMAL_ICON, kIconSize, true);
251 			BIconCache::UnlockIconCache();
252 
253 			target->Unlock();
254 			numDrawn++;
255 		}
256 	}
257 #endif
258 }
259 
260 const entry_ref *
261 IconSpewer::NextRef()
262 {
263 	status_t result;
264 	if (newCache)
265 		result = cachingIterator->GetNextRef(&ref);
266 	else
267 		result = walker->GetNextRef(&ref);
268 
269 	if (result == B_OK)
270 		return &ref;
271 
272 	delete walker;
273 	if (!pathsToSearch[searchPathIndex]) {
274 		bigtime_t now = watch.ElapsedTime();
275 		cycleTime = now - lastCycleLap;
276 		lastCycleLap = now;
277 		PRINT(("**************************hit end of disk, starting over\n"));
278 		searchPathIndex = 0;
279 	}
280 
281 	walker = new TNodeWalker(pathsToSearch[searchPathIndex++]);
282 	if (newCache) {
283 		cachingIterator->SetTo(walker);
284 		result = cachingIterator->GetNextRef(&ref);
285 	} else
286 		result = walker->GetNextRef(&ref);
287 
288 	ASSERT(result == B_OK);
289 		// we don't expect and cannot deal with any problems here
290 	return &ref;
291 }
292 
293 
294 
295 IconTestWindow::IconTestWindow()
296 	:	BWindow(BRect(100, 100, 500, 600), "icon cache test", B_TITLED_WINDOW_LOOK,
297 			B_NORMAL_WINDOW_FEEL, 0),
298 		iconSpewer(modifiers() == 0)
299 {
300 	iconSpewer.SetTarget(this);
301 	BView *view = new BView(Bounds(), "iconView", B_FOLLOW_ALL, B_WILL_DRAW);
302 	AddChild(view);
303 	iconSpewer.Go();
304 }
305 
306 bool
307 IconTestWindow::QuitRequested()
308 {
309 	iconSpewer.Quit();
310 	return true;
311 }
312 
313 void
314 RunIconCacheTests()
315 {
316 	(new IconTestWindow())->Show();
317 }
318 
319 #endif
320