aboutsummaryrefslogtreecommitdiff
path: root/tests/sdl_touch.c
diff options
context:
space:
mode:
authorJukka Jylänki <jujjyl@gmail.com>2014-04-14 01:22:19 +0300
committerJukka Jylänki <jujjyl@gmail.com>2014-04-14 01:22:19 +0300
commit2faa69a865c3d6046d7d5202f2dc747dea3229f1 (patch)
tree99f3fa6eca209e2786eeaecc40b4656ee1d109b9 /tests/sdl_touch.c
parent43ddbe9ecd05afac7674a25097b2799be1f7b6ce (diff)
Add interactive and automated testing for SDL touch events.
Diffstat (limited to 'tests/sdl_touch.c')
-rw-r--r--tests/sdl_touch.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/sdl_touch.c b/tests/sdl_touch.c
new file mode 100644
index 00000000..1fce1df1
--- /dev/null
+++ b/tests/sdl_touch.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_ttf.h>
+#include <assert.h>
+#include <emscripten.h>
+
+int result = 1;
+
+static char *TouchFingerTypeToString(int type) {
+ if (type == SDL_FINGERMOTION) return "SDL_FINGERMOTION";
+ if (type == SDL_FINGERDOWN) return "SDL_FINGERDOWN";
+ if (type == SDL_FINGERUP) return "SDL_FINGERUP";
+ return "UNKNOWN";
+}
+
+int got_down = 0;
+int got_move = 0;
+int got_up = 0;
+
+void progress() {
+ if (!got_down) printf("Hold down a finger to generate a touch down event.\n");
+ else if (!got_move) printf("Drag a finger to generate a touch move event.\n");
+ else if (!got_up) printf("Release a finger to generate a touch up event.\n");
+ else
+ {
+ int result = 0;
+#ifdef REPORT_RESULT
+ REPORT_RESULT();
+#endif
+ }
+}
+
+void loop() {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ switch(event.type) {
+ case SDL_FINGERMOTION:
+ case SDL_FINGERDOWN:
+ case SDL_FINGERUP: {
+ SDL_TouchFingerEvent *t = (SDL_TouchFingerEvent*)&event;
+ printf("type: %s, timestamp: %u, touchId: %llu, fingerId: %llu, x: %f, y: %f, dx: %f, dy: %f, pressure: %f\n",
+ TouchFingerTypeToString(event.type), t->timestamp, t->touchId, t->fingerId, t->x, t->y, t->dx, t->dy, t->pressure);
+
+ if (t->timestamp != 0 && t->x >= 0.f && t->x <= 1.f && t->y >= 0.f && t->y <= 1.f && t->pressure >= 0.f && t->pressure <= 1.f) {
+ if (event.type == SDL_FINGERDOWN) { got_down = 1; progress(); }
+ if (event.type == SDL_FINGERMOTION) { got_move = 1; progress(); }
+ if (event.type == SDL_FINGERDOWN) { got_up = 1; progress(); }
+ }
+ break;
+ }
+ }
+ }
+}
+
+int main() {
+ SDL_Init(SDL_INIT_VIDEO);
+ SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
+
+ progress();
+
+#ifdef AUTOMATE_SUCCESS
+ EM_ASM(
+ function sendEvent(type, data) {
+ var event = document.createEvent('Event');
+ event.initEvent(type, true, true);
+ for(var d in data) event[d] = data[d];
+ Module['canvas'].dispatchEvent(event);
+ }
+ sendEvent('touchstart', { touches: [ { pageX: 300, pageY: 400, deviceID: 1, identifier: 1, force: 1 } ] });
+ sendEvent('touchmove', { touches: [ { pageX: 350, pageY: 400, deviceID: 1, identifier: 1, force: 1 } ] });
+ sendEvent('touchend', { changedTouches: [ { pageX: 350, pageY: 400, deviceID: 1, identifier: 1, force: 1 } ] });
+ );
+#endif
+
+ emscripten_set_main_loop(loop, 0, 0);
+
+ return 0;
+}