awtk/tests/check_button_test.cc

152 lines
4.5 KiB
C++
Raw Normal View History

2018-11-08 17:12:40 +08:00
#include <string>
#include "gtest/gtest.h"
2019-02-26 09:40:40 +08:00
#include "widgets/button.h"
#include "widgets/check_button.h"
2020-06-18 18:06:58 +08:00
#include "widgets/group_box.h"
2018-03-26 12:16:52 +08:00
2018-11-08 17:12:40 +08:00
using std::string;
2018-03-26 12:16:52 +08:00
static ret_t on_changed(void* ctx, event_t* e) {
int* count = (int*)ctx;
2018-03-30 22:15:45 +08:00
if (e->type == EVT_VALUE_CHANGED) {
2018-03-26 12:16:52 +08:00
*count += 1;
}
return RET_OK;
}
TEST(CheckButton, basic) {
uint32_t id = 0;
int count = 0;
widget_t* button = check_button_create(NULL, 0, 0, 100, 100);
ASSERT_EQ(CHECK_BUTTON(button)->radio, FALSE);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
id = widget_on(button, EVT_VALUE_CHANGED, on_changed, &count);
ASSERT_EQ(id > 0, TRUE);
ASSERT_EQ(check_button_set_value(button, TRUE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, TRUE);
2018-03-30 22:15:45 +08:00
ASSERT_EQ(count, 1);
2018-03-26 12:16:52 +08:00
ASSERT_EQ(check_button_set_value(button, FALSE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
2018-03-30 22:15:45 +08:00
ASSERT_EQ(count, 2);
2018-03-26 12:16:52 +08:00
ASSERT_EQ(widget_off(button, id), RET_OK);
ASSERT_EQ(check_button_set_value(button, TRUE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, TRUE);
2018-03-30 22:15:45 +08:00
ASSERT_EQ(count, 2);
2018-03-26 12:16:52 +08:00
ASSERT_EQ(check_button_set_value(button, FALSE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
2018-03-30 22:15:45 +08:00
ASSERT_EQ(count, 2);
2018-07-14 07:01:13 +08:00
widget_destroy(button);
2018-03-26 12:16:52 +08:00
}
TEST(CheckButton, radio) {
widget_t* button = check_button_create_radio(NULL, 0, 0, 100, 100);
2018-08-08 17:58:03 +08:00
ASSERT_EQ(check_button_cast(button), button);
2018-03-26 12:16:52 +08:00
ASSERT_EQ(CHECK_BUTTON(button)->radio, TRUE);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
2018-07-14 11:35:40 +08:00
2018-07-14 07:01:13 +08:00
widget_destroy(button);
2018-03-26 12:16:52 +08:00
}
2018-11-08 17:12:40 +08:00
#include "log_change_events.inc"
TEST(CheckButton, event) {
widget_t* w = check_button_create_radio(NULL, 0, 0, 100, 100);
check_button_set_value(w, TRUE);
s_log = "";
widget_on(w, EVT_VALUE_WILL_CHANGE, on_change_events, NULL);
widget_on(w, EVT_VALUE_CHANGED, on_change_events, NULL);
check_button_set_value(w, TRUE);
ASSERT_EQ(s_log, "");
check_button_set_value(w, FALSE);
ASSERT_EQ(s_log, "will_change;change;");
widget_destroy(w);
}
2019-02-26 09:40:40 +08:00
TEST(CheckButton, radio_cast) {
widget_t* w = check_button_create_radio(NULL, 0, 0, 100, 100);
ASSERT_EQ(w, check_button_cast(w));
widget_destroy(w);
}
TEST(CheckButton, check_cast) {
widget_t* w = check_button_create(NULL, 0, 0, 100, 100);
ASSERT_EQ(w, check_button_cast(w));
ASSERT_NE(w, button_cast(w));
widget_destroy(w);
}
2020-06-18 18:06:58 +08:00
TEST(CheckButton, radio1) {
widget_t* g = group_box_create(NULL, 0, 0, 100, 100);
widget_t* b1 = check_button_create_radio(g, 0, 0, 100, 100);
widget_t* b2 = check_button_create_radio(g, 0, 0, 100, 100);
widget_t* b3 = check_button_create_radio(g, 0, 0, 100, 100);
ASSERT_EQ(check_button_set_value(b3, TRUE), RET_OK);
ASSERT_EQ(check_button_get_checked_button(b1), b3);
ASSERT_EQ(check_button_get_checked_button(b2), b3);
ASSERT_EQ(check_button_get_checked_button(b3), b3);
2020-06-19 17:54:43 +08:00
2020-06-18 18:06:58 +08:00
ASSERT_EQ(check_button_set_value(b1, TRUE), RET_OK);
ASSERT_EQ(check_button_get_checked_button(b1), b1);
ASSERT_EQ(check_button_get_checked_button(b2), b1);
ASSERT_EQ(check_button_get_checked_button(b3), b1);
2020-06-19 17:54:43 +08:00
2020-06-18 18:06:58 +08:00
ASSERT_EQ(check_button_set_value(b2, TRUE), RET_OK);
ASSERT_EQ(check_button_get_checked_button(b1), b2);
ASSERT_EQ(check_button_get_checked_button(b2), b2);
ASSERT_EQ(check_button_get_checked_button(b3), b2);
widget_destroy(g);
}
TEST(CheckButton, change_value) {
widget_t* w = check_button_create(NULL, 0, 0, 100, 100);
value_change_event_t evt;
memset(&evt, 0x00, sizeof(evt));
ASSERT_EQ(widget_set_prop_bool(w, WIDGET_PROP_VALUE, FALSE), RET_OK);
widget_on(w, EVT_VALUE_WILL_CHANGE, on_value_will_changed_accept, NULL);
widget_on(w, EVT_VALUE_CHANGED, on_value_changed, &evt);
ASSERT_EQ(widget_set_prop_bool(w, WIDGET_PROP_VALUE, TRUE), RET_OK);
ASSERT_EQ(widget_get_prop_bool(w, WIDGET_PROP_VALUE, FALSE), TRUE);
ASSERT_EQ(value_bool(&(evt.old_value)), FALSE);
ASSERT_EQ(value_bool(&(evt.new_value)), TRUE);
widget_destroy(w);
}
TEST(CheckButton, change_value_abort) {
widget_t* w = check_button_create(NULL, 0, 0, 100, 100);
value_change_event_t evt;
memset(&evt, 0x00, sizeof(evt));
ASSERT_EQ(widget_set_prop_bool(w, WIDGET_PROP_VALUE, FALSE), RET_OK);
widget_on(w, EVT_VALUE_WILL_CHANGE, on_value_will_changed_abort, NULL);
ASSERT_EQ(widget_set_prop_bool(w, WIDGET_PROP_VALUE, TRUE), RET_OK);
ASSERT_EQ(widget_get_prop_bool(w, WIDGET_PROP_VALUE, TRUE), FALSE);
widget_destroy(w);
}
2021-08-16 18:42:30 +08:00
TEST(CheckButton, radio_prop) {
widget_t* w = check_button_create(NULL, 0, 0, 100, 100);
ASSERT_EQ(widget_set_prop_bool(w, WIDGET_PROP_RADIO, TRUE), RET_OK);
ASSERT_EQ(widget_get_prop_bool(w, WIDGET_PROP_RADIO, FALSE), TRUE);
widget_destroy(w);
}