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