~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/emscripten_fs_api_browser.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include<stdio.h>
 
2
#include<emscripten.h>
 
3
#include<assert.h>
 
4
#include<string.h>
 
5
#include<SDL/SDL.h>
 
6
#include"SDL/SDL_image.h"
 
7
 
 
8
extern "C" {
 
9
 
 
10
int result = 1;
 
11
int get_count = 0;
 
12
int data_ok = 0;
 
13
int data_bad = 0;
 
14
 
 
15
void onLoadedData(void *arg, void *buffer, int size) {
 
16
  printf("onLoadedData %d\n", (int)arg);
 
17
  get_count++;
 
18
  assert(size == 329895);
 
19
  assert((int)arg == 135);
 
20
  unsigned char *b = (unsigned char*)buffer;
 
21
  assert(b[0] == 137);
 
22
  assert(b[1122] == 128);
 
23
  assert(b[1123] == 201);
 
24
  assert(b[202125] == 218);
 
25
  data_ok = 1;
 
26
}
 
27
 
 
28
void onErrorData(void *arg) {
 
29
  printf("onErrorData %d\n", (int)arg);
 
30
  get_count++;
 
31
  assert((int)arg == 246);
 
32
  data_bad = 1;
 
33
}
 
34
 
 
35
int counter = 0;
 
36
void wait_wgets() {
 
37
  if (counter++ == 60) {
 
38
    printf("%d\n", get_count);
 
39
    counter = 0;
 
40
  }
 
41
 
 
42
  if (get_count == 3) {
 
43
    static bool fired = false;
 
44
    if (!fired) {
 
45
      fired = true;
 
46
      emscripten_async_wget_data(
 
47
        "http://localhost:8888/screenshot.png",
 
48
        (void*)135,
 
49
        onLoadedData,
 
50
        onErrorData);
 
51
      emscripten_async_wget_data(
 
52
        "http://localhost:8888/fail_me",
 
53
        (void*)246,
 
54
        onLoadedData,
 
55
        onErrorData);
 
56
    }
 
57
  } else if (get_count == 5) {
 
58
    assert(IMG_Load("/tmp/screen_shot.png"));
 
59
    assert(data_ok == 1 && data_bad == 1);
 
60
    emscripten_cancel_main_loop();
 
61
    REPORT_RESULT();
 
62
  }
 
63
  assert(get_count <= 5);
 
64
}
 
65
 
 
66
void onLoaded(const char* file) {
 
67
  if (strcmp(file, "/tmp/test.html") && strcmp(file, "/tmp/screen_shot.png")) {
 
68
    result = 0;
 
69
  }
 
70
 
 
71
  if (FILE * f = fopen(file, "r")) {
 
72
      printf("exists: %s\n", file);
 
73
      int c = fgetc (f);
 
74
      if (c == EOF) {
 
75
        printf("file empty: %s\n", file);
 
76
        result = 0;
 
77
      }
 
78
      fclose(f);
 
79
  } else {
 
80
    result = 0;
 
81
    printf("!exists: %s\n", file);
 
82
  }
 
83
  
 
84
  get_count++;
 
85
  printf("onLoaded %s\n", file);
 
86
}
 
87
 
 
88
void onError(const char* file) {
 
89
  if (strcmp(file, "/tmp/null")) {
 
90
    result = 0;
 
91
  }
 
92
 
 
93
  get_count++;
 
94
  printf("onError %s\n", file);
 
95
}
 
96
 
 
97
int main() {
 
98
  emscripten_async_wget(
 
99
    "http://localhost:8888/this_is_not_a_file", 
 
100
    "/tmp/null",
 
101
    onLoaded,
 
102
    onError);
 
103
 
 
104
  emscripten_async_wget(
 
105
    "http://localhost:8888/test.html", 
 
106
    "/tmp/test.html",
 
107
    onLoaded,
 
108
    onError);
 
109
 
 
110
  emscripten_async_wget(
 
111
    "http://localhost:8888/screenshot.png", 
 
112
    "/tmp/screen_shot.png",
 
113
    onLoaded,
 
114
    onError);
 
115
 
 
116
  emscripten_set_main_loop(wait_wgets, 0, 0);
 
117
 
 
118
  return 0;
 
119
}
 
120
 
 
121
}
 
122