1 /*
2  *   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3  *   Free Software Foundation, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  *
20  */
21 
22 #define INPUT_FILENAME "ButtonEventsTest.swf"
23 
24 #include "MovieTester.h"
25 #include "MovieClip.h"
26 #include "DisplayObject.h"
27 #include "TextField.h"
28 #include "DisplayList.h"
29 #include "log.h"
30 
31 #include "check.h"
32 #include <string>
33 #include <cassert>
34 
35 using namespace gnash;
36 using namespace std;
37 
38 void
test_mouse_activity(MovieTester & tester,const TextField * text,const TextField * text2,bool covered,bool enabled)39 test_mouse_activity(MovieTester& tester, const TextField* text, const TextField* text2, bool covered, bool enabled)
40 {
41 	rgba red(255,0,0,255);
42 	rgba dark_red(128,0,0,255);
43 	rgba covered_red(127,126,0,255); // red, covered by 50% black
44 	rgba covered_dark_red(64,120,0,255); // dark red, covered by 50% black
45 	rgba yellow(255,255,0,255);
46 	rgba dark_yellow(128,128,0,255);
47 	rgba covered_yellow(128,255,0,255); // yellow, covered by 50% black
48 	rgba covered_dark_yellow(64,184,0,255); // dark yellow, covered by 50% black
49 	rgba green(0,255,0,255);
50 
51 	// roll over the middle of the square, this should change
52 	// the textfield value, if enabled
53 	tester.movePointerTo(60, 60);
54 	if ( enabled ) {
55 		check(tester.usingHandCursor());
56 		check_equals(string(text->get_text_value()), string("MouseOver"));
57 		check_equals(string(text2->get_text_value()), string("RollOver"));
58 		check(tester.isMouseOverMouseEntity());
59 		if ( covered )
60 		{
61 			// check that pixel @ 60,60 is yellow (covered)
62 			check_pixel(60, 60, 2, covered_yellow, 2);
63 			// check that pixel @ 72,64 is dark_yellow (covered)
64 			check_pixel(72, 64, 2, covered_dark_yellow, 2);
65 		}
66 		else
67 		{
68 			// check that pixel @ 60,60 is yellow
69 			check_pixel(60, 60, 2, yellow, 2);
70 			// check that pixel @ 72,64 is dark_yellow
71 			check_pixel(72, 64, 2, dark_yellow, 2);
72 		}
73 	} else {
74 		check(!tester.usingHandCursor());
75 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
76 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
77 		check(!tester.isMouseOverMouseEntity());
78 		if ( covered )
79 		{
80 			// check that pixel @ 60,60 is red  (covered)
81 			check_pixel(60, 60, 2, covered_red, 2);
82 			// check that pixel @ 72,64 is dark_red  (covered)
83 			check_pixel(72, 64, 2, covered_dark_red, 2);
84 		}
85 		else
86 		{
87 			// check that pixel @ 60,60 is red
88 			check_pixel(60, 60, 2, red, 2);
89 			// check that pixel @ 72,64 is dark_red
90 			check_pixel(72, 64, 2, dark_red, 2);
91 		}
92 	}
93 
94 	// press the mouse button, this should change
95 	// the textfield value, if enabled.
96 	tester.pressMouseButton();
97 	if ( enabled ) {
98 		check_equals(string(text->get_text_value()), string("MouseDown"));
99 		check_equals(string(text2->get_text_value()), string("Press"));
100 		check(tester.isMouseOverMouseEntity());
101 		// check that pixel @ 60,60 is green !
102 		check_pixel(60, 60, 2, green, 2);
103 	} else {
104 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
105 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
106 		check(!tester.isMouseOverMouseEntity());
107 		// check that pixel @ 60,60 is red !
108 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2);  }
109 		else { check_pixel(60, 60, 2, red, 2);  }
110 	}
111 
112 	// depress the mouse button, this should change
113 	// the textfield value, if enabled
114 	tester.depressMouseButton();
115 	if ( enabled ) {
116 		check_equals(string(text->get_text_value()), string("MouseUp"));
117 		check_equals(string(text2->get_text_value()), string("Release"));
118 		check(tester.isMouseOverMouseEntity());
119 		// check that pixel @ 60,60 is yellow !
120 		if ( covered ) { check_pixel(60, 60, 2, covered_yellow, 2);  }
121 		else { check_pixel(60, 60, 2, yellow, 2);  }
122 	} else {
123 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
124 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
125 		check(!tester.isMouseOverMouseEntity());
126 		// check that pixel @ 60,60 is red !
127 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2);  }
128 		else { check_pixel(60, 60, 2, red, 2);  }
129 	}
130 
131 	// roll off the square, this should change
132 	// the textfield value, if enabled
133 	tester.movePointerTo(39, 60);
134 	if ( enabled ) {
135 		check_equals(string(text->get_text_value()), string("MouseOut"));
136 		check_equals(string(text2->get_text_value()), string("RollOut"));
137 	} else {
138 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
139 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
140 	}
141 	check(!tester.isMouseOverMouseEntity());
142 	// check that pixel @ 60,60 is red !
143 	if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
144 	else { check_pixel(60, 60, 2, red, 2); }
145 
146 	// press the mouse button, this should not change anything
147 	// as we're outside of the button.
148 	tester.pressMouseButton();
149 	if ( enabled ) {
150 		check_equals(string(text->get_text_value()), string("MouseOut"));
151 		check_equals(string(text2->get_text_value()), string("RollOut"));
152 	} else {
153 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
154 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
155 	}
156 	check(!tester.isMouseOverMouseEntity());
157 	// check that pixel @ 60,60 is red !
158 	if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
159 	else { check_pixel(60, 60, 2, red, 2); }
160 
161 	// depress the mouse button, this should not change anything
162 	// as we're outside of the button.
163 	tester.depressMouseButton();
164 	if ( enabled ) {
165 		check_equals(string(text->get_text_value()), string("MouseOut"));
166 		check_equals(string(text2->get_text_value()), string("RollOut"));
167 	} else {
168 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
169 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
170 	}
171 	check(!tester.isMouseOverMouseEntity());
172 	// check that pixel @ 60,60 is red !
173 	if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
174 	else { check_pixel(60, 60, 2, red, 2); }
175 
176 	// Now press the mouse inside and release outside
177 
178 	tester.movePointerTo(60, 60);
179 
180 	if ( enabled ) {
181 		check_equals(string(text->get_text_value()), string("MouseOver"));
182 		check_equals(string(text2->get_text_value()), string("RollOver"));
183 		check(tester.isMouseOverMouseEntity());
184 		// check that pixel @ 60,60 is yellow !
185 		if ( covered ) { check_pixel(60, 60, 2, covered_yellow, 2);  }
186 		else { check_pixel(60, 60, 2, yellow, 2);  }
187 	} else {
188 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
189 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
190 		check(!tester.isMouseOverMouseEntity());
191 		// check that pixel @ 60,60 is red !
192 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
193 		else { check_pixel(60, 60, 2, red, 2); }
194 	}
195 
196 	tester.pressMouseButton();
197 
198 	if ( enabled ) {
199 		check_equals(string(text->get_text_value()), string("MouseDown"));
200 		check_equals(string(text2->get_text_value()), string("Press"));
201 		check(tester.isMouseOverMouseEntity());
202 		// check that pixel @ 60,60 is green !
203 		check_pixel(60, 60, 2, rgba(0,255,0,255), 2);
204 	} else {
205 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
206 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
207 		check(!tester.isMouseOverMouseEntity());
208 		// check that pixel @ 60,60 is red !
209 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
210 		else { check_pixel(60, 60, 2, red, 2); }
211 	}
212 
213 	tester.movePointerTo(39, 60);
214 
215 	// The following might be correct, as the DisplayObject still catches releaseOutside events
216 	//check(tester.isMouseOverMouseEntity());
217 	tester.depressMouseButton();
218 
219 	if ( enabled ) {
220 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
221 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
222 	} else {
223 		check_equals(string(text->get_text_value()), string("MouseUpOutside"));
224 		check_equals(string(text2->get_text_value()), string("ReleaseOutside"));
225 	}
226 }
227 
228 TRYMAIN(_runtest);
229 int
trymain(int,char **)230 trymain(int /*argc*/, char** /*argv*/)
231 {
232 	//string filename = INPUT_FILENAME;
233 	string filename = string(TGTDIR) + string("/") + string(INPUT_FILENAME);
234 	MovieTester tester(filename);
235 
236 	std::string idleString = "Idle";
237 
238 	MovieClip* root = tester.getRootMovie();
239 	assert(root);
240 
241 	check_equals(root->get_frame_count(), 7);
242 
243 	check_equals(root->get_current_frame(), 0);
244 
245 	const TextField* text = dynamic_cast<const TextField*>(
246 		tester.findDisplayItemByName(*root, "textfield"));
247 	check(text);
248 
249 	const TextField* text2 = dynamic_cast<const TextField*>(
250 		tester.findDisplayItemByName(*root, "textfield2"));
251 	check(text2);
252 
253 	const TextField* text3 = dynamic_cast<const TextField*>(
254 		tester.findDisplayItemByName(*root, "textfield3"));
255 	check(text3);
256 
257 	check_equals(string(text->get_text_value()), idleString);
258 	check_equals(string(text2->get_text_value()), idleString);
259 	check_equals(string(text3->get_text_value()), idleString);
260 
261 	tester.advance();
262 	check_equals(root->get_current_frame(), 1);
263 
264 	const DisplayObject* mc1 = tester.findDisplayItemByName(*root, "square1");
265 	check(mc1);
266 	check_equals(mc1->get_depth(), 2+DisplayObject::staticDepthOffset);
267 
268 	check(!tester.isMouseOverMouseEntity());
269 	check(!tester.usingHandCursor());
270 
271 	// check that pixel @ 60,60 is red !
272 	rgba red(255,0,0,255);
273 	check_pixel(60, 60, 2, red, 2);
274 
275 	tester.advance();
276 	check_equals(root->get_current_frame(), 2);
277 	tester.advance();
278 	check_equals(root->get_current_frame(), 2); // need to roll out
279 
280 	tester.movePointerTo(60, 60); // roll over the square
281 	check(tester.isMouseOverMouseEntity());
282 	check(tester.usingHandCursor());
283 	check_equals(root->get_current_frame(), 2); // need to roll out
284 
285 	tester.movePointerTo(0, 0); // roll out, should go to next frame
286 	check(!tester.isMouseOverMouseEntity());
287 	check(!tester.usingHandCursor());
288 	check_equals(root->get_current_frame(), 3);
289 
290 	for (size_t fno=root->get_current_frame(); fno<root->get_frame_count(); fno++)
291 	{
292 		const DisplayObject* square_back = tester.findDisplayItemByDepth(*root, 1+DisplayObject::staticDepthOffset);
293 		const DisplayObject* square_front = tester.findDisplayItemByDepth(*root, 3+DisplayObject::staticDepthOffset);
294 
295 		switch (fno)
296 		{
297 			case 3:
298 				check(!square_back);
299 				check(!square_front);
300 				break;
301 			case 4:
302 				check(square_back);
303 				check(!square_front);
304 				break;
305 			case 5:
306 				check(square_back);
307 				check(square_front);
308 				break;
309 		}
310 
311 		check_equals(root->get_current_frame(), fno);
312 
313 		info (("testing mouse activity in frame %d", root->get_current_frame()));
314 		test_mouse_activity(tester, text, text2, square_front!=NULL, fno != root->get_frame_count()-1);
315 
316 		// TODO: test key presses !
317 		//       They seem NOT to trigger immediate redraw
318 
319 		tester.advance();
320 
321 	}
322 
323 	// last advance should not restart the loop (it's in STOP mode)
324         check_equals(root->getPlayState(), MovieClip::PLAYSTATE_STOP);
325 	check_equals(root->get_current_frame(), 6);
326 
327 	return 0;
328 }
329 
330