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 "SpriteButtonEventsTest.swf"
23 
24 #include "MovieTester.h"
25 #include "MovieClip.h"
26 #include "DisplayObject.h"
27 #include "DisplayList.h"
28 #include "TextField.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 covered_red(127,126,0,255); // red, covered by 50% black
43 	rgba yellow(255,255,0,255);
44 	rgba covered_yellow(128,255,0,255); // yellow, covered by 50% black
45 	rgba green(0,255,0,255);
46 
47 	string tmp, tmp2; // to backup text and text2 values before changing them
48 
49 	// roll over the middle of the square, this should change
50 	// the textfield value, if enabled
51 	tmp = text->get_text_value();
52 	tmp2 = text2->get_text_value();
53 	tester.movePointerTo(60, 60);
54 	if ( enabled ) {
55 		check_equals(string(text->get_text_value()), string("RollOver"));
56 		check_equals(string(text2->get_text_value()), tmp2); // would retain last value
57 		check(tester.isMouseOverMouseEntity());
58 		// check that pixel @ 60,60 is yellow !
59 		if ( covered ) { check_pixel(60, 60, 2, covered_yellow, 2);  }
60 		else { check_pixel(60, 60, 2, yellow, 2);  }
61 	} else {
62 		check_equals(string(text->get_text_value()), tmp); // not enabled...
63 		check_equals(string(text2->get_text_value()), tmp2); // would retain last value
64 		check(!tester.isMouseOverMouseEntity());
65 		// check that pixel @ 60,60 is red !
66 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2);  }
67 		else { check_pixel(60, 60, 2, red, 2);  }
68 	}
69 
70 	// press the mouse button, this should change
71 	// the textfield value, if enabled.
72 	tester.pressMouseButton();
73 	if ( enabled ) {
74 		check_equals(string(text->get_text_value()), string("Press"));
75 		check_equals(string(text2->get_text_value()), string("MouseDown"));
76 		check(tester.isMouseOverMouseEntity());
77 		// check that pixel @ 60,60 is green !
78 		check_pixel(60, 60, 2, green, 2);
79 	} else {
80 		check_equals(string(text->get_text_value()), tmp);
81 		check_equals(string(text2->get_text_value()), string("MouseDown")); // no matter .enabled
82 		check(!tester.isMouseOverMouseEntity());
83 		// check that pixel @ 60,60 is red !
84 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2);  }
85 		else { check_pixel(60, 60, 2, red, 2);  }
86 	}
87 
88 	// depress the mouse button, this should change
89 	// the textfield value, if enabled
90 	tester.depressMouseButton();
91 	if ( enabled ) {
92 		check_equals(string(text->get_text_value()), string("Release"));
93 		check_equals(string(text2->get_text_value()), string("MouseUp"));
94 		check(tester.isMouseOverMouseEntity());
95 		// check that pixel @ 60,60 is yellow !
96 		if ( covered ) { check_pixel(60, 60, 2, covered_yellow, 2);  }
97 		else { check_pixel(60, 60, 2, yellow, 2);  }
98 	} else {
99 		check_equals(string(text->get_text_value()), tmp);
100 		check_equals(string(text2->get_text_value()), string("MouseUp")); // no matter .enabled
101 		check(!tester.isMouseOverMouseEntity());
102 		// check that pixel @ 60,60 is red !
103 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2);  }
104 		else { check_pixel(60, 60, 2, red, 2);  }
105 	}
106 
107 	tmp = text->get_text_value();
108 	tmp2 = text2->get_text_value();
109 
110 	// roll off the square, this should change
111 	// the textfield value, if enabled
112 	tester.movePointerTo(39, 60);
113 	if ( enabled ) {
114 		check_equals(string(text->get_text_value()), string("RollOut"));
115 		check_equals(string(text2->get_text_value()), tmp2);
116 	} else {
117 		check_equals(string(text->get_text_value()), tmp);
118 		check_equals(string(text2->get_text_value()), tmp2);
119 	}
120 	check(!tester.isMouseOverMouseEntity());
121 	// check that pixel @ 60,60 is red !
122 	if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
123 	else { check_pixel(60, 60, 2, red, 2); }
124 
125 	tmp = text->get_text_value();
126 	tmp2 = text2->get_text_value();
127 
128 	// press the mouse button, this should not change anything
129 	// as we're outside of the button.
130 	tester.pressMouseButton();
131 	check_equals(string(text->get_text_value()), tmp);
132 	check_equals(string(text2->get_text_value()), string("MouseDown"));
133 	check(!tester.isMouseOverMouseEntity());
134 	// check that pixel @ 60,60 is red !
135 	if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
136 	else { check_pixel(60, 60, 2, red, 2); }
137 
138 	// depress the mouse button, this should not change anything
139 	// as we're outside of the button.
140 	tester.depressMouseButton();
141 	check_equals(string(text->get_text_value()), tmp);
142 	check_equals(string(text2->get_text_value()), string("MouseUp"));
143 	check(!tester.isMouseOverMouseEntity());
144 	// check that pixel @ 60,60 is red !
145 	if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
146 	else { check_pixel(60, 60, 2, red, 2); }
147 
148 	// Now press the mouse inside and release outside
149 
150 	tester.movePointerTo(60, 60);
151 
152 	if ( enabled ) {
153 		check_equals(string(text->get_text_value()), string("RollOver"));
154 		check_equals(string(text2->get_text_value()), tmp2);
155 		check(tester.isMouseOverMouseEntity());
156 		// check that pixel @ 60,60 is yellow !
157 		if ( covered ) { check_pixel(60, 60, 2, covered_yellow, 2);  }
158 		else { check_pixel(60, 60, 2, yellow, 2);  }
159 	} else {
160 		check_equals(string(text->get_text_value()), tmp);
161 		check_equals(string(text2->get_text_value()), tmp2);
162 		check(!tester.isMouseOverMouseEntity());
163 		// check that pixel @ 60,60 is red !
164 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
165 		else { check_pixel(60, 60, 2, red, 2); }
166 	}
167 
168 	tester.pressMouseButton();
169 
170 	if ( enabled ) {
171 		check_equals(string(text->get_text_value()), string("Press"));
172 		check_equals(string(text2->get_text_value()), string("MouseDown"));
173 		check(tester.isMouseOverMouseEntity());
174 		// check that pixel @ 60,60 is green !
175 		check_pixel(60, 60, 2, rgba(0,255,0,255), 2);
176 	} else {
177 		check_equals(string(text->get_text_value()), tmp);
178 		check_equals(string(text2->get_text_value()), string("MouseDown"));
179 		check(!tester.isMouseOverMouseEntity());
180 		// check that pixel @ 60,60 is red !
181 		if ( covered ) { check_pixel(60, 60, 2, covered_red, 2); }
182 		else { check_pixel(60, 60, 2, red, 2); }
183 	}
184 
185 	tester.movePointerTo(39, 60);
186 
187 	// The following might be correct, as the DisplayObject still catches releaseOutside events
188 	//check(tester.isMouseOverMouseEntity());
189 	tester.depressMouseButton();
190 
191 	if ( enabled ) {
192 		check_equals(string(text->get_text_value()), string("ReleaseOutside"));
193 		check_equals(string(text2->get_text_value()), "MouseUp");
194 	} else {
195 		check_equals(string(text->get_text_value()), string("ReleaseOutside"));
196 		check_equals(string(text2->get_text_value()), "MouseUp");
197 	}
198 }
199 
200 TRYMAIN(_runtest);
201 int
trymain(int,char **)202 trymain(int /*argc*/, char** /*argv*/)
203 {
204 	//string filename = INPUT_FILENAME;
205 	string filename = string(TGTDIR) + string("/") + string(INPUT_FILENAME);
206 	MovieTester tester(filename);
207 
208 	std::string idleString = "Idle";
209 
210 	MovieClip* root = tester.getRootMovie();
211 	assert(root);
212 
213 	check_equals(root->get_frame_count(), 5);
214 	check_equals(root->get_current_frame(), 0);
215 
216 	const TextField* text = dynamic_cast<const TextField*>(
217 		tester.findDisplayItemByName(*root, "textfield"));
218 	check(text);
219 
220 	const TextField* text2 = dynamic_cast<const TextField*>(
221 		tester.findDisplayItemByName(*root, "textfield2"));
222 	check(text2);
223 
224 	const TextField* text3 = dynamic_cast<const TextField*>(
225 		tester.findDisplayItemByName(*root, "textfield3"));
226 	check(text3);
227 
228 	tester.advance();
229 	check_equals(root->get_current_frame(), 1);
230 
231 	const DisplayObject* mc1 = tester.findDisplayItemByName(*root, "square1");
232 	check(mc1);
233 	check_equals(mc1->get_depth(), 2+DisplayObject::staticDepthOffset);
234 
235 
236 	check_equals(string(text->get_text_value()), idleString);
237 	check_equals(string(text2->get_text_value()), idleString);
238 	check_equals(string(text3->get_text_value()), idleString);
239 	check(!tester.isMouseOverMouseEntity());
240 	// check that pixel @ 60,60 is red !
241 	rgba red(255,0,0,255);
242 	check_pixel(60, 60, 2, red, 2);
243 
244 	for (size_t fno=1; fno<root->get_frame_count(); fno++)
245 	{
246 		const DisplayObject* square_back = tester.findDisplayItemByDepth(*root, 1+DisplayObject::staticDepthOffset);
247 		const DisplayObject* square_front = tester.findDisplayItemByDepth(*root, 3+DisplayObject::staticDepthOffset);
248 
249 		switch (fno)
250 		{
251 			case 1:
252 				check(!square_back);
253 				check(!square_front);
254 				break;
255 			case 2:
256 				check(square_back);
257 				check(!square_front);
258 				break;
259 			case 3:
260 				check(square_back);
261 				check(square_front);
262 				break;
263 		}
264 
265 		check_equals(root->get_current_frame(), fno);
266 
267 		info (("testing mouse activity in frame %d", root->get_current_frame()));
268 		test_mouse_activity(tester, text, text2,
269 				square_front!=NULL, fno != root->get_frame_count()-1);
270 
271 		// TODO: test key presses !
272 		//       They seem NOT to trigger immediate redraw
273 
274 		tester.advance();
275 
276 	}
277 
278     tester.scrollMouse(-1);
279     check_equals(text->get_text_value(), "onMouseWheel: -1, , 2");
280 
281     tester.scrollMouse(1);
282     check_equals(text->get_text_value(), "onMouseWheel: 1, , 2");
283 
284     tester.movePointerTo(100, 100);
285     tester.scrollMouse(1);
286     check_equals(text->get_text_value(), "onMouseWheel: 1, _level0.textfield2, 2");
287 
288 	// last advance should not restart the loop (it's in STOP mode)
289     check_equals(root->getPlayState(), MovieClip::PLAYSTATE_STOP);
290 	check_equals(root->get_current_frame(), 4);
291 
292     return 0;
293 }
294 
295