1 
2 
3 #ifdef AVTK_TESTER
4 
5 #include "tester.hxx"
6 
7 #include "ui.hxx"
8 //#include "picojson.hxx"
9 
10 // for converting events -> JSON
11 #include "pugl/pugl.h"
12 
13 #include <list>
14 #include <vector>
15 
16 #ifdef _WIN32
17 #include <windows.h>
18 #else
19 #include <sys/time.h>
20 #endif
21 
22 
23 namespace Avtk
24 {
25 
Tester(Avtk::UI * ui_)26 Tester::Tester( Avtk::UI* ui_ ) :
27 	ui( ui_ ),
28 	playing_( false ),
29 	recording_( false ),
30 	startRecTime( 0 ),
31 	startPlayTime( 0 ),
32 	playEventNum( 0 )
33 {
34 }
35 
record(const char * n)36 void Tester::record( const char* n )
37 {
38 	playing_ = false;
39 	recording_ = true;
40 	name = n;
41 	startRecTime = getTime();
42 	printf("%s , %lf\n", __PRETTY_FUNCTION__, startRecTime );
43 	events.clear();
44 }
45 
handle(const PuglEvent * event)46 void Tester::handle( const PuglEvent* event )
47 {
48 	if( !playing_ ) {
49 		if( event->type != PUGL_MOTION_NOTIFY ) {
50 			events.push_back( AvtkEvent( event, getTime() - startRecTime ) );
51 		}
52 	}
53 }
54 
writeTest(const char * filename)55 void Tester::writeTest( const char* filename )
56 {
57 	printf("%s %s : STUB\n", __FILE__, __PRETTY_FUNCTION__ );
58 	/*
59 	picojson::object test;
60 	picojson::object event;
61 
62 	test["name"] = picojson::value("Test name");
63 	test["avtkVersion"] = picojson::value( AVTK_VERSION_STRING );
64 
65 	picojson::array list = picojson::array();
66 
67 	AvtkEvent* e = &events.at(0);
68 	for(int i = 0; i < events.size(); i++ )
69 	{
70 	  event["eventType"] =  picojson::value( puglEventGetTypeString( e->event.type ) );
71 	  event["integer"] =  picojson::value( 1.0 );
72 
73 
74 	  list.push_back( picojson::value(event) );
75 
76 	  //printf("Event %i, type %i\n", i, e->event.type );
77 	  e++;
78 	}
79 
80 	test["events"] =  picojson::value( list );
81 
82 	std::string str = picojson::value(test).serialize();
83 	//printf("Tester::writeTest() %s\n", str.c_str() );
84 
85 	std::ofstream out;
86 	out.open ( filename );
87 	out << str << "\n";
88 	out.close();
89 	*/
90 }
91 
recordStop()92 void Tester::recordStop()
93 {
94 	if( !events.size() ) {
95 		printf("No events recorded.\n" );
96 		return;
97 	}
98 	AvtkEvent* e = &events.at(0);
99 
100 	for(int i = 0; i < events.size(); i++ ) {
101 		printf("Event %i, type %i\n", i, e->event.type );
102 		e++;
103 	}
104 
105 	writeTest( "avtk_test.json" );
106 
107 	printf("%s stopping AVTK TEST %s\n", __PRETTY_FUNCTION__, name.c_str() );
108 	recording_ = false;
109 }
110 
process()111 void Tester::process()
112 {
113 	if( playing_ ) {
114 		if ( playEventNum < events.size() ) {
115 			if( events.at( playEventNum ).timestamp < getTime() - startPlayTime ) {
116 				ui->event( &events.at( playEventNum ).event );
117 				playEventNum++;
118 			}
119 		} else {
120 			playing_   = 0;
121 			recording_ = 0;
122 		}
123 	}
124 }
125 
runTest(const char * testName,bool ignoreTimestamps)126 int Tester::runTest( const char* testName, bool ignoreTimestamps )
127 {
128 	playing_ = true;
129 
130 	// get the time of the play start
131 	startPlayTime = getTime();
132 
133 	if( recording_ )
134 		recordStop();
135 
136 	playEventNum = 0;
137 
138 	/*
139 	printf("%s running test %s : num events %i\n", __PRETTY_FUNCTION__, testName, events.size() );
140 
141 	if( events.size() > 0 )
142 	{
143 	  //AvtkEvent* e = &events.at(0);
144 	  for(int i = 0; i < events.size(); i++ )
145 	  {
146 	    //printf("Event %i, type %i\n", i, events.at(0).event.type );
147 
148 
149 	  }
150 	}
151 	*/
152 
153 	return 0;
154 
155 	/*
156 	try
157 	{
158 	  std::ifstream ifs;
159 	  ifs.open ( testName, std::ifstream::in);
160 
161 	  picojson::value v;
162 	  ifs >> v;
163 
164 	  if( ifs.fail() )
165 	  {
166 	    printf("Theme::load() %s : File doesn't exist, abort.\n", testName );
167 	    //std::cerr << picojson::get_last_error() << std::endl;
168 	    return -1;
169 	  }
170 
171 	  const char* items[5] =
172 	  {
173 	    "bg",
174 	    "bg-dark",
175 	    "fg",
176 	    "fg-dark",
177 	    "highlight"
178 	  };
179 
180 	  for( int i = 0; i < 5; i++ )
181 	  {
182 	    // extract the 3 ints from the array, and store into Color array
183 	    int colNum = 0;
184 	    picojson::array list = v.get( items[i] ).get<picojson::array>();
185 	    for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter)
186 	    {
187 	      int tmp = (int)(*iter).get("c").get<double>();
188 	      //printf("%s = %lf\r\n", items[i], tmp );
189 	      ///colors[i].c[colNum++] = tmp;
190 	    }
191 	  }
192 	}
193 	catch( ... )
194 	{
195 	  printf("Theme::load() Error loading theme from %s : falling back to default.Double check file-exists and JSON contents valid.\n", testName );
196 	  // *any* error, and we don't use the theme
197 	  return -1;
198 	}
199 	*/
200 }
201 
getTime()202 double Tester::getTime()
203 {
204 #ifdef _WIN32
205 	LARGE_INTEGER count;
206 	QueryPerformanceCounter(&count);
207 	double time = count.QuadPart * m_reciprocalFrequency;
208 #else
209 	struct timespec interval;
210 	clock_gettime(CLOCK_MONOTONIC, &interval);
211 	double time = interval.tv_sec + interval.tv_nsec * 0.000000001f;
212 #endif
213 
214 	return time;
215 };
216 
217 }; // Avtk
218 
219 #endif // AVTK_TESTER
220