| 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/entry.h>
|
|---|
| 36 | #include <ui/resource.h>
|
|---|
| 37 | #include <ui/ui.h>
|
|---|
| 38 | #include <ui/window.h>
|
|---|
| 39 | #include "../private/entry.h"
|
|---|
| 40 |
|
|---|
| 41 | PCUT_INIT;
|
|---|
| 42 |
|
|---|
| 43 | PCUT_TEST_SUITE(entry);
|
|---|
| 44 |
|
|---|
| 45 | /** Create and destroy text entry */
|
|---|
| 46 | PCUT_TEST(create_destroy)
|
|---|
| 47 | {
|
|---|
| 48 | ui_entry_t *entry = NULL;
|
|---|
| 49 | errno_t rc;
|
|---|
| 50 |
|
|---|
| 51 | rc = ui_entry_create(NULL, "Hello", &entry);
|
|---|
| 52 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 53 | PCUT_ASSERT_NOT_NULL(entry);
|
|---|
| 54 |
|
|---|
| 55 | ui_entry_destroy(entry);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /** ui_entry_destroy() can take NULL argument (no-op) */
|
|---|
| 59 | PCUT_TEST(destroy_null)
|
|---|
| 60 | {
|
|---|
| 61 | ui_entry_destroy(NULL);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /** ui_entry_ctl() returns control that has a working virtual destructor */
|
|---|
| 65 | PCUT_TEST(ctl)
|
|---|
| 66 | {
|
|---|
| 67 | ui_entry_t *entry;
|
|---|
| 68 | ui_control_t *control;
|
|---|
| 69 | errno_t rc;
|
|---|
| 70 |
|
|---|
| 71 | rc = ui_entry_create(NULL, "Hello", &entry);
|
|---|
| 72 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 73 |
|
|---|
| 74 | control = ui_entry_ctl(entry);
|
|---|
| 75 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 76 |
|
|---|
| 77 | ui_control_destroy(control);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Set text entry rectangle sets internal field */
|
|---|
| 81 | PCUT_TEST(set_rect)
|
|---|
| 82 | {
|
|---|
| 83 | ui_entry_t *entry;
|
|---|
| 84 | gfx_rect_t rect;
|
|---|
| 85 | errno_t rc;
|
|---|
| 86 |
|
|---|
| 87 | rc = ui_entry_create(NULL, "Hello", &entry);
|
|---|
| 88 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 89 |
|
|---|
| 90 | rect.p0.x = 1;
|
|---|
| 91 | rect.p0.y = 2;
|
|---|
| 92 | rect.p1.x = 3;
|
|---|
| 93 | rect.p1.y = 4;
|
|---|
| 94 |
|
|---|
| 95 | ui_entry_set_rect(entry, &rect);
|
|---|
| 96 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
|
|---|
| 97 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
|
|---|
| 98 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
|
|---|
| 99 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
|
|---|
| 100 |
|
|---|
| 101 | ui_entry_destroy(entry);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /** Set entry text horizontal alignment sets internal field */
|
|---|
| 105 | PCUT_TEST(set_halign)
|
|---|
| 106 | {
|
|---|
| 107 | ui_entry_t *entry;
|
|---|
| 108 | errno_t rc;
|
|---|
| 109 |
|
|---|
| 110 | rc = ui_entry_create(NULL, "Hello", &entry);
|
|---|
| 111 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 112 |
|
|---|
| 113 | ui_entry_set_halign(entry, gfx_halign_left);
|
|---|
| 114 | PCUT_ASSERT_EQUALS(gfx_halign_left, entry->halign);
|
|---|
| 115 | ui_entry_set_halign(entry, gfx_halign_center);
|
|---|
| 116 | PCUT_ASSERT_EQUALS(gfx_halign_center, entry->halign);
|
|---|
| 117 |
|
|---|
| 118 | ui_entry_destroy(entry);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /** Set text entry rectangle sets internal field */
|
|---|
| 122 | PCUT_TEST(set_text)
|
|---|
| 123 | {
|
|---|
| 124 | ui_entry_t *entry;
|
|---|
| 125 | gfx_rect_t rect;
|
|---|
| 126 | errno_t rc;
|
|---|
| 127 |
|
|---|
| 128 | rc = ui_entry_create(NULL, "Hello", &entry);
|
|---|
| 129 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 130 |
|
|---|
| 131 | rect.p0.x = 1;
|
|---|
| 132 | rect.p0.y = 2;
|
|---|
| 133 | rect.p1.x = 3;
|
|---|
| 134 | rect.p1.y = 4;
|
|---|
| 135 |
|
|---|
| 136 | ui_entry_set_rect(entry, &rect);
|
|---|
| 137 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, entry->rect.p0.x);
|
|---|
| 138 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, entry->rect.p0.y);
|
|---|
| 139 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, entry->rect.p1.x);
|
|---|
| 140 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, entry->rect.p1.y);
|
|---|
| 141 |
|
|---|
| 142 | ui_entry_destroy(entry);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Paint text entry */
|
|---|
| 146 | PCUT_TEST(paint)
|
|---|
| 147 | {
|
|---|
| 148 | errno_t rc;
|
|---|
| 149 | ui_t *ui = NULL;
|
|---|
| 150 | ui_window_t *window = NULL;
|
|---|
| 151 | ui_wnd_params_t params;
|
|---|
| 152 | ui_entry_t *entry;
|
|---|
| 153 |
|
|---|
| 154 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 155 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 156 |
|
|---|
| 157 | ui_wnd_params_init(¶ms);
|
|---|
| 158 | params.caption = "Hello";
|
|---|
| 159 |
|
|---|
| 160 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 161 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 162 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 163 |
|
|---|
| 164 | rc = ui_entry_create(window, "Hello", &entry);
|
|---|
| 165 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 166 |
|
|---|
| 167 | rc = ui_entry_paint(entry);
|
|---|
| 168 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 169 |
|
|---|
| 170 | ui_entry_destroy(entry);
|
|---|
| 171 | ui_window_destroy(window);
|
|---|
| 172 | ui_destroy(ui);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | PCUT_EXPORT(entry);
|
|---|