forked from gnome-pomodoro/gnome-pomodoro
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timer.vala
597 lines (481 loc) · 19.3 KB
/
test-timer.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* This file is part of GNOME Pomodoro
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Kamil Prusko <[email protected]>
*
*/
namespace Pomodoro
{
public class TimerTest : Pomodoro.TestSuite
{
private const double POMODORO_DURATION = 25.0;
private const double SHORT_BREAK_DURATION = 5.0;
private const double LONG_BREAK_DURATION = 15.0;
private const double LONG_BREAK_INTERVAL = 4.0;
public TimerTest ()
{
this.add_test ("state_duration_setting",
this.test_state_duration_setting);
this.add_test ("set_state",
this.test_set_state);
this.add_test ("start",
this.test_start);
this.add_test ("stop",
this.test_stop);
this.add_test ("update",
this.test_update);
this.add_test ("update_offset",
this.test_update_offset);
this.add_test ("disabled_state",
this.test_disabled_state);
this.add_test ("short_break_state",
this.test_short_break_state);
this.add_test ("long_break_state",
this.test_long_break_state);
this.add_test ("long_break_state_postponed",
this.test_long_break_state_postponed);
this.add_test ("pomodoro_state_create_next_state",
this.test_pomodoro_state_create_next_state);
this.add_test ("pause_1",
this.test_pause_1);
this.add_test ("pause_2",
this.test_pause_2);
this.add_test ("is_running",
this.test_is_running);
this.add_test ("restore_1",
this.test_restore_1);
this.add_test ("restore_2",
this.test_restore_2);
this.add_test ("restore_3",
this.test_restore_3);
this.add_test ("restore_4",
this.test_restore_4);
this.add_test ("score_1",
this.test_score_1);
this.add_test ("score_2",
this.test_score_2);
this.add_test ("score_3",
this.test_score_3);
this.add_test ("score_4",
this.test_score_4);
// this.add_test ("state_duration_change",
// this.test_state_duration_change);
// this.add_test ("state_changed_signal",
// this.test_state_changed_signal);
}
public override void setup ()
{
var settings = Pomodoro.get_settings ()
.get_child ("preferences");
settings.set_double ("pomodoro-duration", POMODORO_DURATION);
settings.set_double ("short-break-duration", SHORT_BREAK_DURATION);
settings.set_double ("long-break-duration", LONG_BREAK_DURATION);
settings.set_double ("long-break-interval", LONG_BREAK_INTERVAL);
settings.set_boolean ("pause-when-idle", false);
}
public override void teardown ()
{
var settings = Pomodoro.get_settings ();
settings.revert ();
}
/**
* Unit test for Pomodoro.Timer.set_state_full() method.
*
* Check changing timer state.
*/
public void test_set_state ()
{
var timer = new Pomodoro.Timer ();
var timestamp = Pomodoro.get_current_time ();
timer.state_changed.connect ((new_state, previous_state) => {
});
timer.state = new PomodoroState.with_timestamp (timestamp);
timestamp += timer.state.duration;
timer.state = new ShortBreakState.with_timestamp (timestamp);
timestamp += timer.state.duration;
timer.state = new PomodoroState.with_timestamp (timestamp);
}
public void test_start ()
{
var timer = new Pomodoro.Timer ();
timer.start ();
assert (timer.state is PomodoroState);
assert (timer.is_running ());
timer.pause ();
timer.start ();
assert (timer.state is PomodoroState);
assert (!timer.is_paused);
assert (timer.is_running ());
}
public void test_stop ()
{
var timer = new Pomodoro.Timer ();
timer.state = new PomodoroState ();
timer.stop ();
assert (timer.state is DisabledState);
assert (!timer.is_running ());
}
public void test_update ()
{
var timer = new Pomodoro.Timer ();
timer.start ();
timer.update (timer.state.timestamp + 0.5);
assert (timer.state is PomodoroState);
assert (timer.elapsed == 0.5);
}
public void test_update_offset ()
{
var timer = new Pomodoro.Timer ();
var initial_timestamp = timer.timestamp;
var state1 = new PomodoroState.with_timestamp (initial_timestamp);
state1.elapsed = 0.5;
timer.state = state1;
assert (timer.elapsed == 0.5);
var state2 = new PomodoroState.with_timestamp (initial_timestamp - 2.0);
state2.elapsed = 0.5;
timer.state = state2;
timer.update (initial_timestamp);
assert (timer.elapsed == 2.5);
}
public void test_disabled_state ()
{
var timer = new Pomodoro.Timer ();
var initial_timestamp = timer.state.timestamp;
timer.update (initial_timestamp + 2.0);
assert (timer.state is Pomodoro.DisabledState);
assert (!timer.is_running ());
assert (timer.state.duration == 0.0);
assert (timer.state.timestamp == initial_timestamp);
}
/**
* Unit test for Pomodoro.Timer.update() method.
*
* Check whether states change properly with time.
*/
public void test_short_break_state ()
{
var timer = new Pomodoro.Timer ();
timer.state = new PomodoroState ();
timer.score = 0.0;
timer.update (timer.state.timestamp + timer.state.duration);
assert (timer.state is ShortBreakState);
assert (timer.score == 1.0);
timer.update (timer.state.timestamp + timer.state.duration);
}
public void test_long_break_state ()
{
var timer = new Pomodoro.Timer ();
timer.state = new PomodoroState ();
timer.score = 3.0;
timer.update (timer.state.timestamp + timer.state.duration);
assert (timer.state is LongBreakState);
assert (timer.score == 4.0);
timer.update (timer.state.timestamp + timer.state.duration);
assert (timer.state is PomodoroState);
assert (timer.score == 0.0);
}
/**
* Timer should not reset session count if a long break hasn't completed.
*/
public void test_long_break_state_postponed ()
{
var timer = new Pomodoro.Timer ();
timer.state = new PomodoroState ();
timer.score = 3.0;
timer.update (timer.state.timestamp + timer.state.duration);
assert (timer.state is LongBreakState);
assert (timer.score == 4.0);
var state = new PomodoroState.with_timestamp (timer.state.timestamp + 1.0);
timer.state = state;
assert (timer.state is PomodoroState);
assert (timer.score == 4.0);
}
/**
* Extra time from pomodoro should be passed on to a break. If interruption happens
* (a reboot for instance) we can assume that user is not straining himself/herself.
*/
public void test_pomodoro_state_create_next_state ()
{
var timer = new Pomodoro.Timer ();
timer.start ();
timer.update (timer.state.timestamp + timer.state.duration + 2.0);
assert (timer.state is ShortBreakState);
assert (timer.elapsed == 2.0);
timer.update (timer.state.timestamp + timer.state.duration + 2.0);
assert (timer.state is PomodoroState);
assert (timer.elapsed == 0.0);
}
public void test_reset ()
{
// TODO
}
public void test_pause_1 ()
{
var timer = new Pomodoro.Timer ();
timer.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer.start (0.0);
timer.pause (0.0);
timer.resume (2.0);
timer.update (2.0 + 1.0);
assert (timer.elapsed == 1.0);
assert (timer.offset == 2.0);
}
/**
* Long pauses or interruptions should not affect score.
*/
public void test_pause_2 ()
{
var timer1 = new Pomodoro.Timer ();
timer1.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer1.start (0.0);
timer1.pause (0.0);
timer1.resume (15.0);
timer1.update (15.0 + 25.0);
assert (timer1.state is Pomodoro.ShortBreakState);
assert (timer1.score == 1.0);
var timer2 = new Pomodoro.Timer ();
timer2.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer2.start (0.0);
timer2.update (20.0);
timer2.pause (20.0);
timer2.resume (20.0 + 15.0);
timer2.update (20.0 + 15.0 + 5.0);
assert (timer2.state is Pomodoro.ShortBreakState);
assert (timer2.score == 1.0);
}
public void test_is_running ()
{
var timer = new Pomodoro.Timer ();
timer.pause ();
assert (!timer.is_running ());
timer.start ();
timer.pause ();
assert (!timer.is_running ());
}
public void test_state_duration_setting ()
{
Pomodoro.TimerState state;
state = new Pomodoro.DisabledState ();
assert (state.duration == 0.0);
state = new Pomodoro.PomodoroState ();
assert (state.duration == POMODORO_DURATION);
state = new Pomodoro.ShortBreakState ();
assert (state.duration == SHORT_BREAK_DURATION);
state = new Pomodoro.LongBreakState ();
assert (state.duration == LONG_BREAK_DURATION);
}
// /** TODO
// * Unit test for pomodoro duration.
// *
// * Shortening pomodoro_duration shouldn't result in immediate long_break,
// */
// public void test_state_duration_change ()
// {
// var timer = new Pomodoro.Timer ();
// timer.start ();
//
// /* shorten pomodoro duration */
// timer.state.duration = POMODORO_DURATION / 10.0;
//
// timer.update (timer.state.timestamp + timer.state.duration);
//
//// print_timer_state (timer);
//
// assert (timer.state is Pomodoro.ShortBreakState);
// assert (timer.session == 1.0);
// }
/**
* Test 1:1 save and restore
*/
public void test_restore_1 ()
{
var settings = Pomodoro.get_settings ()
.get_child ("state");
var timer1 = new Pomodoro.Timer ();
timer1.score = 1.0;
timer1.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer1.state.duration = 20.0; // custom duration
timer1.pause (0.0);
timer1.resume (5.0);
timer1.update (15.0);
timer1.pause (15.0);
timer1.save (settings);
assert (settings.get_string ("timer-state") == "pomodoro");
assert (settings.get_double ("timer-state-duration") == 20.0);
assert (settings.get_double ("timer-elapsed") == 10.0);
assert (settings.get_double ("timer-score") == 1.0);
assert (settings.get_boolean ("timer-paused") == true);
assert (
settings.get_string ("timer-state-date") == "1970-01-01T00:00:00Z" ||
settings.get_string ("timer-state-date") == "1970-01-01T00:00:00+0000"
);
assert (
settings.get_string ("timer-date") == "1970-01-01T00:00:15Z" ||
settings.get_string ("timer-date") == "1970-01-01T00:00:15+0000"
);
var timer2 = new Pomodoro.Timer ();
timer2.restore (settings, timer1.timestamp);
assert (timer2.state.name == timer1.state.name);
assert (timer2.state.elapsed == timer1.state.elapsed);
assert (timer2.state.duration == timer1.state.duration);
assert (timer2.state.timestamp == timer1.state.timestamp);
assert (timer2.score == timer1.score);
assert (timer2.timestamp == timer1.timestamp);
assert (timer2.offset == timer1.offset);
assert (timer2.is_paused == timer1.is_paused);
}
/**
* Test wether we go to next state during restore or continue where we left
*/
public void test_restore_2 ()
{
var settings = Pomodoro.get_settings ()
.get_child ("state");
var timer1 = new Pomodoro.Timer ();
timer1.score = 1.0;
timer1.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer1.start (0.0);
timer1.update (10.0);
timer1.pause (10.0);
timer1.update (15.0);
timer1.save (settings);
var timer2 = new Pomodoro.Timer ();
timer2.restore (settings, 20.0);
assert (timer2.state.name == timer1.state.name);
assert (timer2.state.elapsed == timer1.state.elapsed);
assert (timer2.state.duration == timer1.state.duration);
assert (timer2.state.timestamp == timer1.state.timestamp);
assert (timer2.score == timer1.score);
assert (timer2.is_paused == timer1.is_paused);
assert (timer2.timestamp == 20.0);
assert (timer2.offset == 10.0);
}
/**
* Test whether timer is reset during restore after 1h
*/
public void test_restore_3 ()
{
var settings = Pomodoro.get_settings ()
.get_child ("state");
var timer1 = new Pomodoro.Timer ();
timer1.score = 1.0;
timer1.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer1.start (0.0);
timer1.update (10.0);
timer1.pause (10.0);
timer1.update (15.0);
timer1.save (settings);
var timer2 = new Pomodoro.Timer ();
timer2.restore (settings, 20.0 + 3600.0);
assert (timer2.state is Pomodoro.DisabledState);
assert (timer2.state.elapsed == 0.0);
assert (timer2.state.timestamp == 3620.0);
assert (timer2.score == 0.0);
assert (timer2.timestamp == 3620.0);
assert (timer2.offset == 0.0);
assert (timer2.is_paused == false);
}
/**
* Test against bad values
*/
public void test_restore_4 ()
{
var settings = Pomodoro.get_settings ()
.get_child ("state");
settings.set_string ("timer-state", "pomodoro");
settings.set_double ("timer-state-duration", 10.0);
settings.set_double ("timer-elapsed", 1.0);
settings.set_double ("timer-score", 1.0);
settings.set_boolean ("timer-paused", false);
settings.set_string ("timer-state-date", "invalid value");
settings.set_string ("timer-date", "invalid value");
var timer = new Pomodoro.Timer ();
timer.restore (settings, 3600.0);
assert (timer.state is Pomodoro.DisabledState);
}
/**
* Test whether score is counted after pomodoro
*/
public void test_score_1 ()
{
var timer = new Pomodoro.Timer ();
timer.state = new Pomodoro.PomodoroState.with_timestamp (0.0);
timer.update (timer.state.duration);
assert (timer.score == 1.0);
}
/**
* Test whether score is reset after a long break
*/
public void test_score_2 ()
{
var timer = new Pomodoro.Timer ();
timer.state = new Pomodoro.LongBreakState.with_timestamp (0.0);
timer.score = 4.0;
timer.update (timer.state.duration);
assert (timer.state is Pomodoro.PomodoroState);
assert (timer.score == 0.0);
}
/**
* Test whether score kept when stopping timer
*/
public void test_score_3 ()
{
var timer = new Pomodoro.Timer ();
timer.score = 1.0;
timer.start ();
timer.stop ();
assert (timer.score == 1.0);
}
/**
* Timer should reset score after 1h of inactivity.
*/
public void test_score_4 ()
{
var timer = new Pomodoro.Timer ();
timer.state = new Pomodoro.DisabledState.with_timestamp (0.0);
timer.score = 1.0;
timer.state_leave.connect ((old_state) => {
message ("*** state_leave %g", old_state.timestamp);
});
timer.state_enter.connect ((new_state) => {
message ("*** state_enter %g", new_state.timestamp);
});
timer.start (timer.state.timestamp + 3600.0);
assert (timer.score == 0.0);
}
// private static void print_timer_state (Pomodoro.Timer timer)
// {
// stdout.printf ("""
// %s
// state.name = %s
// state.timestamp = %.2f
// state.duration = %.2f
// score = %.2f
// elapsed = %.2f
// offset = %.2f
// timestamp = %.2f
// """,
// timer.state.get_type ().name (),
// timer.state.name,
// timer.state.timestamp,
// timer.state.duration,
// timer.score,
// timer.elapsed,
// timer.offset,
// timer.timestamp);
// }
}
}