|
| 1 | +# |
| 2 | +# Created by Boyd Multerer on September 18, 2018 |
| 3 | +# Copyright © 2018 Kry10 Industries. All rights reserved. |
| 4 | +# |
| 5 | + |
| 6 | +defmodule Scenic.Component.Input.TextFieldTest do |
| 7 | + use ExUnit.Case, async: true |
| 8 | + doctest Scenic |
| 9 | + |
| 10 | + # alias Scenic.Component |
| 11 | + alias Scenic.Graph |
| 12 | + alias Scenic.Primitive |
| 13 | + alias Scenic.ViewPort |
| 14 | + alias Scenic.Component.Input.TextField |
| 15 | + |
| 16 | + @initial_value "Initial value" |
| 17 | + @initial_password "*************" |
| 18 | + |
| 19 | + @state %{ |
| 20 | + graph: Graph.build(), |
| 21 | + theme: Primitive.Style.Theme.preset(:primary), |
| 22 | + width: 100, |
| 23 | + height: 30, |
| 24 | + value: @initial_value, |
| 25 | + display: @initial_value, |
| 26 | + hint: "hint", |
| 27 | + index: 2, |
| 28 | + char_width: 12, |
| 29 | + focused: false, |
| 30 | + type: :text, |
| 31 | + filter: :all, |
| 32 | + id: :test_id |
| 33 | + } |
| 34 | + |
| 35 | + # ============================================================================ |
| 36 | + # info |
| 37 | + |
| 38 | + test "info works" do |
| 39 | + assert is_bitstring(TextField.info(:bad_data)) |
| 40 | + assert TextField.info(:bad_data) =~ ":bad_data" |
| 41 | + end |
| 42 | + |
| 43 | + # ============================================================================ |
| 44 | + # verify |
| 45 | + |
| 46 | + test "verify passes valid data" do |
| 47 | + assert TextField.verify("Title") == {:ok, "Title"} |
| 48 | + end |
| 49 | + |
| 50 | + test "verify fails invalid data" do |
| 51 | + assert TextField.verify(:banana) == :invalid_data |
| 52 | + end |
| 53 | + |
| 54 | + # ============================================================================ |
| 55 | + # init |
| 56 | + |
| 57 | + test "init works with simple data" do |
| 58 | + {:ok, state} = TextField.init(@initial_value, styles: %{}, id: :test_id) |
| 59 | + %Graph{} = state.graph |
| 60 | + assert is_map(state.theme) |
| 61 | + assert state.value == @initial_value |
| 62 | + assert state.display == @initial_value |
| 63 | + assert state.focused == false |
| 64 | + assert state.type == :text |
| 65 | + assert state.id == :test_id |
| 66 | + |
| 67 | + {:ok, state} = TextField.init(@initial_value, styles: %{type: :password} ) |
| 68 | + assert state.value == @initial_value |
| 69 | + assert state.display == @initial_password |
| 70 | + assert state.type == :password |
| 71 | + end |
| 72 | + |
| 73 | + # ============================================================================ |
| 74 | + # handle_input |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + # ============================================================================ |
| 79 | + # control keys |
| 80 | + |
| 81 | + test "handle_input {:key \"left\" moves the cursor to the left" do |
| 82 | + self = self() |
| 83 | + scene_ref = make_ref() |
| 84 | + Process.put(:parent_pid, self) |
| 85 | + Process.put(:scene_ref, scene_ref) |
| 86 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 87 | + context = %ViewPort.Context{viewport: self} |
| 88 | + |
| 89 | + assert @state.index == 2 |
| 90 | + {:noreply, state} = TextField.handle_input({:key, {"left", :press, 0}}, context, @state) |
| 91 | + # confirm the graph was pushed |
| 92 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 93 | + assert state.index == 1 |
| 94 | + |
| 95 | + {:noreply, state} = TextField.handle_input({:key, {"left", :press, 0}}, context, state) |
| 96 | + assert state.index == 0 |
| 97 | + |
| 98 | + # does not keep going below 0 |
| 99 | + {:noreply, state} = TextField.handle_input({:key, {"left", :press, 0}}, context, state) |
| 100 | + assert state.index == 0 |
| 101 | + |
| 102 | + # cleanup |
| 103 | + Process.exit(tables_pid, :shutdown) |
| 104 | + end |
| 105 | + |
| 106 | + test "handle_input {:key \"right\" moves the cursor to the right" do |
| 107 | + self = self() |
| 108 | + scene_ref = make_ref() |
| 109 | + Process.put(:parent_pid, self) |
| 110 | + Process.put(:scene_ref, scene_ref) |
| 111 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 112 | + context = %ViewPort.Context{viewport: self} |
| 113 | + |
| 114 | + length = String.length( @initial_value ) |
| 115 | + state = %{@state | index: length - 2} |
| 116 | + |
| 117 | + {:noreply, state} = TextField.handle_input({:key, {"right", :press, 0}}, context, state) |
| 118 | + # confirm the graph was pushed |
| 119 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 120 | + assert state.index == length - 1 |
| 121 | + |
| 122 | + {:noreply, state} = TextField.handle_input({:key, {"right", :press, 0}}, context, state) |
| 123 | + assert state.index == length |
| 124 | + |
| 125 | + # does not keep going past the end |
| 126 | + {:noreply, state} = TextField.handle_input({:key, {"right", :press, 0}}, context, state) |
| 127 | + assert state.index == length |
| 128 | + |
| 129 | + # cleanup |
| 130 | + Process.exit(tables_pid, :shutdown) |
| 131 | + end |
| 132 | + |
| 133 | + test "handle_input {:key \"home\" and \"page_up\" move the cursor all the way to the left" do |
| 134 | + self = self() |
| 135 | + scene_ref = make_ref() |
| 136 | + Process.put(:parent_pid, self) |
| 137 | + Process.put(:scene_ref, scene_ref) |
| 138 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 139 | + context = %ViewPort.Context{viewport: self} |
| 140 | + |
| 141 | + state = %{@state | index: 4} |
| 142 | + {:noreply, state} = TextField.handle_input({:key, {"home", :press, 0}}, context, state) |
| 143 | + # confirm the graph was pushed |
| 144 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 145 | + assert state.index == 0 |
| 146 | + |
| 147 | + state = %{@state | index: 4} |
| 148 | + {:noreply, state} = TextField.handle_input({:key, {"page_up", :press, 0}}, context, state) |
| 149 | + # confirm the graph was pushed |
| 150 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 151 | + assert state.index == 0 |
| 152 | + |
| 153 | + # cleanup |
| 154 | + Process.exit(tables_pid, :shutdown) |
| 155 | + end |
| 156 | + |
| 157 | + test "handle_input {:key \"end\" and \"page_down\" move the cursor all the way to the right" do |
| 158 | + self = self() |
| 159 | + scene_ref = make_ref() |
| 160 | + Process.put(:parent_pid, self) |
| 161 | + Process.put(:scene_ref, scene_ref) |
| 162 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 163 | + context = %ViewPort.Context{viewport: self} |
| 164 | + |
| 165 | + length = String.length( @initial_value ) |
| 166 | + |
| 167 | + state = %{@state | index: 4} |
| 168 | + {:noreply, state} = TextField.handle_input({:key, {"end", :press, 0}}, context, state) |
| 169 | + # confirm the graph was pushed |
| 170 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 171 | + assert state.index == length |
| 172 | + |
| 173 | + state = %{@state | index: 4} |
| 174 | + {:noreply, state} = TextField.handle_input({:key, {"page_down", :press, 0}}, context, state) |
| 175 | + # confirm the graph was pushed |
| 176 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 177 | + assert state.index == length |
| 178 | + |
| 179 | + # cleanup |
| 180 | + Process.exit(tables_pid, :shutdown) |
| 181 | + end |
| 182 | + |
| 183 | + test "handle_input {:key \"backspace\" deletes to the left" do |
| 184 | + self = self() |
| 185 | + scene_ref = make_ref() |
| 186 | + Process.put(:parent_pid, self) |
| 187 | + Process.put(:scene_ref, scene_ref) |
| 188 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 189 | + context = %ViewPort.Context{viewport: self} |
| 190 | + |
| 191 | + {:noreply, state} = TextField.handle_input({:key, {"backspace", :press, 0}}, context, @state) |
| 192 | + # confirm the graph was pushed |
| 193 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 194 | + assert state.index == 1 |
| 195 | + assert state.value == "Iitial value" |
| 196 | + |
| 197 | + {:noreply, state} = TextField.handle_input({:key, {"backspace", :press, 0}}, context, state) |
| 198 | + assert state.index == 0 |
| 199 | + assert state.value == "itial value" |
| 200 | + |
| 201 | + # does nothing if already at position 0 |
| 202 | + {:noreply, state} = TextField.handle_input({:key, {"backspace", :press, 0}}, context, state) |
| 203 | + assert state.index == 0 |
| 204 | + assert state.value == "itial value" |
| 205 | + |
| 206 | + # cleanup |
| 207 | + Process.exit(tables_pid, :shutdown) |
| 208 | + end |
| 209 | + |
| 210 | + test "handle_input {:key \"delete\" deletes to the right" do |
| 211 | + self = self() |
| 212 | + scene_ref = make_ref() |
| 213 | + Process.put(:parent_pid, self) |
| 214 | + Process.put(:scene_ref, scene_ref) |
| 215 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 216 | + context = %ViewPort.Context{viewport: self} |
| 217 | + |
| 218 | + length = String.length( @initial_value ) |
| 219 | + pos = length - 2 |
| 220 | + state = %{@state | index: pos} |
| 221 | + |
| 222 | + {:noreply, state} = TextField.handle_input({:key, {"delete", :press, 0}}, context, state) |
| 223 | + # confirm the graph was pushed |
| 224 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 225 | + assert state.index == pos |
| 226 | + assert state.value == "Initial vale" |
| 227 | + |
| 228 | + {:noreply, state} = TextField.handle_input({:key, {"delete", :press, 0}}, context, state) |
| 229 | + assert state.index == pos |
| 230 | + assert state.value == "Initial val" |
| 231 | + |
| 232 | + # does nothing if already at position 0 |
| 233 | + {:noreply, state} = TextField.handle_input({:key, {"delete", :press, 0}}, context, state) |
| 234 | + assert state.index == pos |
| 235 | + assert state.value == "Initial val" |
| 236 | + |
| 237 | + # cleanup |
| 238 | + Process.exit(tables_pid, :shutdown) |
| 239 | + end |
| 240 | + |
| 241 | + |
| 242 | + test "handle_input {:codepoint adds and moves cursor to right" do |
| 243 | + self = self() |
| 244 | + scene_ref = make_ref() |
| 245 | + Process.put(:parent_pid, self) |
| 246 | + Process.put(:scene_ref, scene_ref) |
| 247 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 248 | + context = %ViewPort.Context{viewport: self} |
| 249 | + |
| 250 | + state = %{@state | index: 2} |
| 251 | + |
| 252 | + {:noreply, state} = TextField.handle_input({:codepoint, {"a", 0}}, context, state) |
| 253 | + # confirm the graph was pushed |
| 254 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 255 | + assert state.index == 3 |
| 256 | + assert state.value == "Inaitial value" |
| 257 | + assert state.display == "Inaitial value" |
| 258 | + |
| 259 | + # can also add strings |
| 260 | + {:noreply, state} = TextField.handle_input({:codepoint, {".com", 0}}, context, state) |
| 261 | + assert state.index == 7 |
| 262 | + assert state.value == "Ina.comitial value" |
| 263 | + |
| 264 | + # rejects filtered characters |
| 265 | + state = %{state | filter: :number} |
| 266 | + {:noreply, state} = TextField.handle_input({:codepoint, {"a", 0}}, context, state) |
| 267 | + assert state.index == 7 |
| 268 | + assert state.value == "Ina.comitial value" |
| 269 | + |
| 270 | + # cleanup |
| 271 | + Process.exit(tables_pid, :shutdown) |
| 272 | + end |
| 273 | + |
| 274 | + test "handle_input {:codepoint displays password * chars" do |
| 275 | + self = self() |
| 276 | + scene_ref = make_ref() |
| 277 | + Process.put(:parent_pid, self) |
| 278 | + Process.put(:scene_ref, scene_ref) |
| 279 | + {:ok, tables_pid} = Scenic.ViewPort.Tables.start_link(nil) |
| 280 | + context = %ViewPort.Context{viewport: self} |
| 281 | + |
| 282 | + state = %{@state | index: 2, type: :password, display: @initial_password} |
| 283 | + |
| 284 | + {:noreply, state} = TextField.handle_input({:codepoint, {"a", 0}}, context, state) |
| 285 | + # confirm the graph was pushed |
| 286 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 287 | + assert state.index == 3 |
| 288 | + assert state.value == "Inaitial value" |
| 289 | + assert state.display == @initial_password <> "*" |
| 290 | + |
| 291 | + # cleanup |
| 292 | + Process.exit(tables_pid, :shutdown) |
| 293 | + end |
| 294 | + |
| 295 | + |
| 296 | + test "handle_input does nothing on unknown input" do |
| 297 | + context = %ViewPort.Context{viewport: self()} |
| 298 | + {:noreply, state} = TextField.handle_input(:unknown, context, @state) |
| 299 | + assert state == @state |
| 300 | + end |
| 301 | + |
| 302 | +end |
| 303 | + |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | + |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
0 commit comments