| 1 | /*
|
|---|
| 2 | * Copyright (c) 2021 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <gfx/context.h>
|
|---|
| 30 | #include <gfx/coord.h>
|
|---|
| 31 | #include <mem.h>
|
|---|
| 32 | #include <pcut/pcut.h>
|
|---|
| 33 | #include <stdbool.h>
|
|---|
| 34 | #include <ui/control.h>
|
|---|
| 35 | #include <ui/label.h>
|
|---|
| 36 | #include <ui/resource.h>
|
|---|
| 37 | #include "../private/label.h"
|
|---|
| 38 | #include "../private/testgc.h"
|
|---|
| 39 |
|
|---|
| 40 | PCUT_INIT;
|
|---|
| 41 |
|
|---|
| 42 | PCUT_TEST_SUITE(label);
|
|---|
| 43 |
|
|---|
| 44 | typedef struct {
|
|---|
| 45 | bool clicked;
|
|---|
| 46 | } test_cb_resp_t;
|
|---|
| 47 |
|
|---|
| 48 | /** Create and destroy label */
|
|---|
| 49 | PCUT_TEST(create_destroy)
|
|---|
| 50 | {
|
|---|
| 51 | ui_label_t *label = NULL;
|
|---|
| 52 | errno_t rc;
|
|---|
| 53 |
|
|---|
| 54 | rc = ui_label_create(NULL, "Hello", &label);
|
|---|
| 55 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 56 | PCUT_ASSERT_NOT_NULL(label);
|
|---|
| 57 |
|
|---|
| 58 | ui_label_destroy(label);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /** ui_label_destroy() can take NULL argument (no-op) */
|
|---|
| 62 | PCUT_TEST(destroy_null)
|
|---|
| 63 | {
|
|---|
| 64 | ui_label_destroy(NULL);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /** ui_label_ctl() returns control that has a working virtual destructor */
|
|---|
| 68 | PCUT_TEST(ctl)
|
|---|
| 69 | {
|
|---|
| 70 | ui_label_t *label;
|
|---|
| 71 | ui_control_t *control;
|
|---|
| 72 | errno_t rc;
|
|---|
| 73 |
|
|---|
| 74 | rc = ui_label_create(NULL, "Hello", &label);
|
|---|
| 75 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 76 |
|
|---|
| 77 | control = ui_label_ctl(label);
|
|---|
| 78 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 79 |
|
|---|
| 80 | ui_control_destroy(control);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /** Set label rectangle sets internal field */
|
|---|
| 84 | PCUT_TEST(set_rect)
|
|---|
| 85 | {
|
|---|
| 86 | ui_label_t *label;
|
|---|
| 87 | gfx_rect_t rect;
|
|---|
| 88 | errno_t rc;
|
|---|
| 89 |
|
|---|
| 90 | rc = ui_label_create(NULL, "Hello", &label);
|
|---|
| 91 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 92 |
|
|---|
| 93 | rect.p0.x = 1;
|
|---|
| 94 | rect.p0.y = 2;
|
|---|
| 95 | rect.p1.x = 3;
|
|---|
| 96 | rect.p1.y = 4;
|
|---|
| 97 |
|
|---|
| 98 | ui_label_set_rect(label, &rect);
|
|---|
| 99 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
|
|---|
| 100 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
|
|---|
| 101 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
|
|---|
| 102 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
|
|---|
| 103 |
|
|---|
| 104 | ui_label_destroy(label);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /** Set label text horizontal alignment sets internal field */
|
|---|
| 108 | PCUT_TEST(set_halign)
|
|---|
| 109 | {
|
|---|
| 110 | ui_label_t *label;
|
|---|
| 111 | errno_t rc;
|
|---|
| 112 |
|
|---|
| 113 | rc = ui_label_create(NULL, "Hello", &label);
|
|---|
| 114 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 115 |
|
|---|
| 116 | ui_label_set_halign(label, gfx_halign_left);
|
|---|
| 117 | PCUT_ASSERT_EQUALS(gfx_halign_left, label->halign);
|
|---|
| 118 | ui_label_set_halign(label, gfx_halign_center);
|
|---|
| 119 | PCUT_ASSERT_EQUALS(gfx_halign_center, label->halign);
|
|---|
| 120 |
|
|---|
| 121 | ui_label_destroy(label);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Set label rectangle sets internal field */
|
|---|
| 125 | PCUT_TEST(set_text)
|
|---|
| 126 | {
|
|---|
| 127 | ui_label_t *label;
|
|---|
| 128 | gfx_rect_t rect;
|
|---|
| 129 | errno_t rc;
|
|---|
| 130 |
|
|---|
| 131 | rc = ui_label_create(NULL, "Hello", &label);
|
|---|
| 132 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 133 |
|
|---|
| 134 | rect.p0.x = 1;
|
|---|
| 135 | rect.p0.y = 2;
|
|---|
| 136 | rect.p1.x = 3;
|
|---|
| 137 | rect.p1.y = 4;
|
|---|
| 138 |
|
|---|
| 139 | ui_label_set_rect(label, &rect);
|
|---|
| 140 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
|
|---|
| 141 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
|
|---|
| 142 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
|
|---|
| 143 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
|
|---|
| 144 |
|
|---|
| 145 | ui_label_destroy(label);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /** Paint label */
|
|---|
| 149 | PCUT_TEST(paint)
|
|---|
| 150 | {
|
|---|
| 151 | errno_t rc;
|
|---|
| 152 | gfx_context_t *gc = NULL;
|
|---|
| 153 | test_gc_t tgc;
|
|---|
| 154 | ui_resource_t *resource = NULL;
|
|---|
| 155 | ui_label_t *label;
|
|---|
| 156 |
|
|---|
| 157 | memset(&tgc, 0, sizeof(tgc));
|
|---|
| 158 | rc = gfx_context_new(&ops, &tgc, &gc);
|
|---|
| 159 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 160 |
|
|---|
| 161 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 162 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 163 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 164 |
|
|---|
| 165 | rc = ui_label_create(resource, "Hello", &label);
|
|---|
| 166 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 167 |
|
|---|
| 168 | rc = ui_label_paint(label);
|
|---|
| 169 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 170 |
|
|---|
| 171 | ui_label_destroy(label);
|
|---|
| 172 | ui_resource_destroy(resource);
|
|---|
| 173 |
|
|---|
| 174 | rc = gfx_context_delete(gc);
|
|---|
| 175 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | PCUT_EXPORT(label);
|
|---|