awtk/tests/animator_move_test.cc

229 lines
6.4 KiB
C++
Raw Normal View History

2018-06-27 13:46:42 +08:00
#include <string>
2018-05-17 10:26:18 +08:00
#include "base/timer.h"
#include "gtest/gtest.h"
#include "widgets/button.h"
2018-05-17 10:26:18 +08:00
#include "widget_animators/widget_animator_move.h"
using std::string;
TEST(AnimatorMove, basic) {
widget_t* button = button_create(NULL, 0, 0, 100, 30);
2018-05-31 15:47:42 +08:00
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
2018-05-17 11:14:41 +08:00
2018-05-17 10:26:18 +08:00
ASSERT_EQ(wa->yoyo_times, 0);
ASSERT_EQ(widget_animator_set_yoyo(wa, 12), RET_OK);
ASSERT_EQ(wa->yoyo_times, 12);
2018-05-17 11:14:41 +08:00
2018-05-17 10:26:18 +08:00
ASSERT_EQ(wa->repeat_times, 0);
ASSERT_EQ(widget_animator_set_repeat(wa, 10), RET_OK);
ASSERT_EQ(wa->repeat_times, 10);
2018-05-17 11:14:41 +08:00
2018-05-17 10:26:18 +08:00
ASSERT_EQ(wa->reversed, FALSE);
ASSERT_EQ(widget_animator_set_reversed(wa, TRUE), RET_OK);
ASSERT_EQ(wa->reversed, TRUE);
2018-05-17 11:14:41 +08:00
widget_animator_destroy(wa);
2018-08-04 11:58:43 +08:00
widget_destroy(button);
2018-05-17 10:26:18 +08:00
}
2018-11-05 17:32:23 +08:00
TEST(AnimatorMove, state) {
widget_t* button = button_create(NULL, 0, 0, 100, 30);
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
ASSERT_EQ(wa->repeat_times, 0);
ASSERT_EQ(widget_animator_set_repeat(wa, 10), RET_OK);
ASSERT_EQ(wa->repeat_times, 10);
ASSERT_EQ(widget_animator_start(wa), RET_OK);
ASSERT_EQ(wa->state, ANIMATOR_RUNNING);
ASSERT_EQ(widget_animator_pause(wa), RET_OK);
ASSERT_EQ(wa->state, ANIMATOR_PAUSED);
ASSERT_EQ(widget_animator_start(wa), RET_OK);
ASSERT_EQ(wa->state, ANIMATOR_RUNNING);
ASSERT_EQ(widget_animator_stop(wa), RET_OK);
ASSERT_EQ(wa->state, ANIMATOR_STOPPED);
widget_animator_destroy(wa);
widget_destroy(button);
}
2018-05-17 10:26:18 +08:00
static string s_log;
static ret_t on_animator_event(void* ctx, event_t* e) {
2018-05-17 11:14:41 +08:00
switch (e->type) {
2018-05-17 10:26:18 +08:00
case EVT_ANIM_START: {
s_log += "start:";
break;
}
case EVT_ANIM_STOP: {
s_log += "stop:";
break;
}
case EVT_ANIM_ONCE: {
s_log += "once:";
break;
}
case EVT_ANIM_END: {
s_log += "end:";
break;
}
}
return RET_OK;
}
TEST(AnimatorMove, once) {
2018-11-05 17:32:23 +08:00
widget_t* button = button_create(NULL, 0, 0, 100, 30);
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
widget_animator_move_set_params(wa, 0, 0, 100, 200);
widget_animator_on(wa, EVT_ANIM_START, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_ONCE, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_STOP, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_END, on_animator_event, NULL);
s_log = "";
widget_animator_start(wa);
widget_animator_time_elapse(wa, 0);
ASSERT_EQ(s_log, "start:");
ASSERT_EQ(button->x, 0);
ASSERT_EQ(button->y, 0);
widget_animator_time_elapse(wa, 500);
ASSERT_EQ(button->x, 50);
ASSERT_EQ(button->y, 100);
widget_animator_time_elapse(wa, 500);
ASSERT_EQ(button->x, 100);
ASSERT_EQ(button->y, 200);
ASSERT_EQ(s_log, "start:end:");
2018-05-17 10:26:18 +08:00
2018-11-05 17:32:23 +08:00
widget_destroy(button);
}
TEST(AnimatorMove, time_scale) {
2018-05-17 10:26:18 +08:00
widget_t* button = button_create(NULL, 0, 0, 100, 30);
2018-05-31 15:47:42 +08:00
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
2018-05-17 10:26:18 +08:00
widget_animator_move_set_params(wa, 0, 0, 100, 200);
widget_animator_on(wa, EVT_ANIM_START, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_ONCE, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_STOP, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_END, on_animator_event, NULL);
s_log = "";
widget_animator_start(wa);
2018-11-05 17:32:23 +08:00
widget_animator_set_time_scale(wa, 0.5);
widget_animator_time_elapse(wa, 0);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(s_log, "start:");
ASSERT_EQ(button->x, 0);
ASSERT_EQ(button->y, 0);
2018-05-17 11:14:41 +08:00
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 1000);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 50);
ASSERT_EQ(button->y, 100);
2018-05-17 11:14:41 +08:00
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 1000);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 100);
ASSERT_EQ(button->y, 200);
ASSERT_EQ(s_log, "start:end:");
2018-05-31 15:47:42 +08:00
widget_destroy(button);
2018-05-17 10:26:18 +08:00
}
TEST(AnimatorMove, reversed) {
widget_t* button = button_create(NULL, 0, 0, 100, 30);
2018-05-31 15:47:42 +08:00
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
2018-05-17 10:26:18 +08:00
widget_animator_move_set_params(wa, 0, 0, 100, 200);
widget_animator_on(wa, EVT_ANIM_START, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_ONCE, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_STOP, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_END, on_animator_event, NULL);
widget_animator_set_reversed(wa, TRUE);
s_log = "";
widget_animator_start(wa);
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 0);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(s_log, "start:");
ASSERT_EQ(button->x, 100);
ASSERT_EQ(button->y, 200);
2018-05-17 11:14:41 +08:00
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 500);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 50);
ASSERT_EQ(button->y, 100);
2018-05-17 11:14:41 +08:00
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 500);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 0);
ASSERT_EQ(button->y, 0);
ASSERT_EQ(s_log, "start:end:");
2018-05-31 15:47:42 +08:00
widget_destroy(button);
2018-05-17 10:26:18 +08:00
}
TEST(AnimatorMove, repeat) {
uint32_t i = 0;
uint32_t nr = 3;
widget_t* button = button_create(NULL, 0, 0, 100, 30);
2018-05-31 15:47:42 +08:00
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
2018-05-17 10:26:18 +08:00
widget_animator_move_set_params(wa, 0, 0, 100, 200);
widget_animator_set_repeat(wa, 3);
widget_animator_on(wa, EVT_ANIM_START, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_ONCE, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_STOP, on_animator_event, NULL);
widget_animator_on(wa, EVT_ANIM_END, on_animator_event, NULL);
s_log = "";
string log = "start:";
2018-05-17 11:14:41 +08:00
2018-05-17 10:26:18 +08:00
widget_animator_start(wa);
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 0);
2018-05-17 10:26:18 +08:00
2018-05-17 11:14:41 +08:00
for (i = 0; i < nr; i++) {
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 0);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 0);
ASSERT_EQ(button->y, 0);
2018-05-17 11:14:41 +08:00
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 500);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 50);
ASSERT_EQ(button->y, 100);
2018-05-17 11:14:41 +08:00
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 500);
2018-05-17 10:26:18 +08:00
ASSERT_EQ(button->x, 100);
ASSERT_EQ(button->y, 200);
log += "once:";
2018-12-26 10:27:11 +08:00
if (i == 2) {
log += "end:";
}
2018-05-17 10:26:18 +08:00
ASSERT_EQ(s_log, log);
}
2018-05-31 15:47:42 +08:00
widget_destroy(button);
2018-05-17 10:26:18 +08:00
}
2018-10-23 15:08:08 +08:00
TEST(AnimatorMove, repeat_forever) {
widget_t* button = button_create(NULL, 0, 0, 100, 30);
widget_animator_t* wa = widget_animator_move_create(button, 1000, 0, EASING_LINEAR);
widget_animator_move_set_params(wa, 0, 0, 100, 200);
widget_animator_set_repeat(wa, 0);
widget_animator_start(wa);
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 0);
2018-10-23 15:08:08 +08:00
ASSERT_EQ(wa->forever, TRUE);
ASSERT_EQ(wa->repeat_times, TK_UINT32_MAX);
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 1000);
2018-10-23 15:08:08 +08:00
ASSERT_EQ(wa->repeat_times, TK_UINT32_MAX - 1);
2018-10-23 15:08:08 +08:00
wa->repeat_times = 1;
2018-11-05 17:32:23 +08:00
widget_animator_time_elapse(wa, 1000);
2018-10-23 15:08:08 +08:00
ASSERT_EQ(wa->repeat_times, TK_UINT32_MAX);
widget_destroy(button);
}