xref: /haiku/src/kits/tracker/Tests.cpp (revision 29f8805f6c70f1c819eb58ac2220647d8e40d6e7)
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 
36 #if DEBUG
37 
38 #include "Tests.h"
39 
40 #include <Debug.h>
41 #include <Locker.h>
42 #include <Path.h>
43 #include <String.h>
44 #include <Window.h>
45 
46 #include <directories.h>
47 
48 #include "EntryIterator.h"
49 #include "IconCache.h"
50 #include "Model.h"
51 #include "NodeWalker.h"
52 #include "StopWatch.h"
53 #include "Thread.h"
54 
55 
56 const char* pathsToSearch[] = {
57 //	"/boot/home/config/settings/NetPositive/Bookmarks/",
58 	kSystemDirectory,
59 	kAppsDirectory,
60 	kUserDirectory,
61 	0
62 };
63 
64 
65 namespace BTrackerPrivate {
66 
67 class IconSpewer : public SimpleThread {
68 public:
69 	IconSpewer(bool newCache = true);
70 	~IconSpewer();
71 	void SetTarget(BWindow* target)
72 		{ this->target = target; }
73 
74 	void Quit();
75 	void Run();
76 
77 protected:
78 	void DrawSomeNew();
79 	void DrawSomeOld();
80 	const entry_ref* NextRef();
81 private:
82 	BLocker locker;
83 	bool quitting;
84 	BWindow* target;
85 	TNodeWalker* walker;
86 	CachedEntryIterator* cachingIterator;
87 	int32 searchPathIndex;
88 	bigtime_t cycleTime;
89 	bigtime_t lastCycleLap;
90 	int32 numDrawn;
91 	BStopWatch watch;
92 	bool newCache;
93 	BPath currentPath;
94 
95 	entry_ref ref;
96 };
97 
98 
99 class IconTestWindow : public BWindow {
100 public:
101 	IconTestWindow();
102 	bool QuitRequested();
103 private:
104 	IconSpewer iconSpewer;
105 };
106 
107 }	// namespace BTrackerPrivate
108 
109 
110 IconSpewer::IconSpewer(bool newCache)
111 	:	quitting(false),
112 		cachingIterator(0),
113 		searchPathIndex(0),
114 		cycleTime(0),
115 		lastCycleLap(0),
116 		numDrawn(0),
117 		watch("", true),
118 		newCache(newCache)
119 {
120 	walker = new TNodeWalker(pathsToSearch[searchPathIndex++]);
121 	if (newCache)
122 		cachingIterator = new CachedEntryIterator(walker, 40);
123 }
124 
125 
126 IconSpewer::~IconSpewer()
127 {
128 	delete walker;
129 	delete cachingIterator;
130 }
131 
132 
133 void
134 IconSpewer::Run()
135 {
136 	BStopWatch watch("", true);
137 	for (;;) {
138 		AutoLock<BLocker> lock(locker);
139 
140 		if (!lock || quitting)
141 			break;
142 
143 		lock.Unlock();
144 		if (newCache)
145 			DrawSomeNew();
146 		else
147 			DrawSomeOld();
148 	}
149 }
150 
151 
152 void
153 IconSpewer::Quit()
154 {
155 	kill_thread(fScanThread);
156 	fScanThread = -1;
157 }
158 
159 
160 const icon_size kIconSize = B_LARGE_ICON;
161 const int32 kRowCount = 10;
162 const int32 kColumnCount = 10;
163 
164 
165 void
166 IconSpewer::DrawSomeNew()
167 {
168 	target->Lock();
169 	BView* view = target->FindView("iconView");
170 	ASSERT(view);
171 
172 	BRect bounds(target->Bounds());
173 	view->SetHighColor(Color(255, 255, 255));
174 	view->FillRect(bounds);
175 
176 	view->SetHighColor(Color(0, 0, 0));
177 	char buffer[256];
178 	if (cycleTime) {
179 		sprintf(buffer, "last cycle time %" B_PRId64 " ms", cycleTime/1000);
180 		view->DrawString(buffer, BPoint(20, bounds.bottom - 20));
181 	}
182 
183 	if (numDrawn) {
184 		sprintf(buffer, "average draw time %" B_PRId64 " us per icon",
185 			watch.ElapsedTime() / numDrawn);
186 		view->DrawString(buffer, BPoint(20, bounds.bottom - 30));
187 	}
188 
189 	sprintf(buffer, "directory: %s", currentPath.Path());
190 	view->DrawString(buffer, BPoint(20, bounds.bottom - 40));
191 
192 	target->Unlock();
193 
194 	for (int32 row = 0; row < kRowCount; row++) {
195 		for (int32 column = 0; column < kColumnCount; column++) {
196 			BEntry entry(NextRef());
197 			Model model(&entry, true);
198 
199 			if (!target->Lock())
200 				return;
201 
202 			if (model.IsDirectory())
203 				entry.GetPath(&currentPath);
204 
205 			IconCache::sIconCache->Draw(&model, view,
206 				BPoint(column * (kIconSize + 2), row * (kIconSize + 2)),
207 				kNormalIcon, kIconSize, true);
208 			target->Unlock();
209 			numDrawn++;
210 		}
211 	}
212 }
213 
214 
215 bool oldIconCacheInited = false;
216 
217 
218 void
219 IconSpewer::DrawSomeOld()
220 {
221 #if 0
222 	if (!oldIconCacheInited)
223 		BIconCache::InitIconCaches();
224 
225 	target->Lock();
226 	target->SetTitle("old cache");
227 	BView* view = target->FindView("iconView");
228 	ASSERT(view);
229 
230 	BRect bounds(target->Bounds());
231 	view->SetHighColor(Color(255, 255, 255));
232 	view->FillRect(bounds);
233 
234 	view->SetHighColor(Color(0, 0, 0));
235 	char buffer[256];
236 	if (cycleTime) {
237 		sprintf(buffer, "last cycle time %Ld ms", cycleTime/1000);
238 		view->DrawString(buffer, BPoint(20, bounds.bottom - 20));
239 	}
240 	if (numDrawn) {
241 		sprintf(buffer, "average draw time %Ld us per icon",
242 			watch.ElapsedTime() / numDrawn);
243 		view->DrawString(buffer, BPoint(20, bounds.bottom - 30));
244 	}
245 	sprintf(buffer, "directory: %s", currentPath.Path());
246 	view->DrawString(buffer, BPoint(20, bounds.bottom - 40));
247 
248 	target->Unlock();
249 
250 	for (int32 row = 0; row < kRowCount; row++) {
251 		for (int32 column = 0; column < kColumnCount; column++) {
252 			BEntry entry(NextRef());
253 			BModel model(&entry, true);
254 
255 			if (!target->Lock())
256 				return;
257 
258 			if (model.IsDirectory())
259 				entry.GetPath(&currentPath);
260 
261 			BIconCache::LockIconCache();
262 			BIconCache* iconCache
263 				= BIconCache::GetIconCache(&model, kIconSize);
264 			iconCache->Draw(view, BPoint(column * (kIconSize + 2),
265 				row * (kIconSize + 2)), B_NORMAL_ICON, kIconSize, true);
266 			BIconCache::UnlockIconCache();
267 
268 			target->Unlock();
269 			numDrawn++;
270 		}
271 	}
272 #endif
273 }
274 
275 
276 const entry_ref*
277 IconSpewer::NextRef()
278 {
279 	status_t result;
280 	if (newCache)
281 		result = cachingIterator->GetNextRef(&ref);
282 	else
283 		result = walker->GetNextRef(&ref);
284 
285 	if (result == B_OK)
286 		return &ref;
287 
288 	delete walker;
289 	if (!pathsToSearch[searchPathIndex]) {
290 		bigtime_t now = watch.ElapsedTime();
291 		cycleTime = now - lastCycleLap;
292 		lastCycleLap = now;
293 		PRINT(("**************************hit end of disk, starting over\n"));
294 		searchPathIndex = 0;
295 	}
296 
297 	walker = new TNodeWalker(pathsToSearch[searchPathIndex++]);
298 	if (newCache) {
299 		cachingIterator->SetTo(walker);
300 		result = cachingIterator->GetNextRef(&ref);
301 	} else
302 		result = walker->GetNextRef(&ref);
303 
304 	ASSERT(result == B_OK);
305 		// we don't expect and cannot deal with any problems here
306 	return &ref;
307 }
308 
309 
310 //	#pragma mark -
311 
312 
313 IconTestWindow::IconTestWindow()
314 	:	BWindow(BRect(100, 100, 500, 600), "icon cache test",
315 			B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 0),
316 		iconSpewer(modifiers() == 0)
317 {
318 	iconSpewer.SetTarget(this);
319 	BView* view = new BView(Bounds(), "iconView", B_FOLLOW_ALL, B_WILL_DRAW);
320 	AddChild(view);
321 	iconSpewer.Go();
322 }
323 
324 
325 bool
326 IconTestWindow::QuitRequested()
327 {
328 	iconSpewer.Quit();
329 	return true;
330 }
331 
332 
333 void
334 RunIconCacheTests()
335 {
336 	(new IconTestWindow())->Show();
337 }
338 
339 #endif
340