|
| 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.RadioButtonTest 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.RadioButton |
| 15 | + |
| 16 | + @state %{ |
| 17 | + graph: Graph.build(), |
| 18 | + theme: Primitive.Style.Theme.preset(:primary), |
| 19 | + pressed: false, |
| 20 | + contained: false, |
| 21 | + checked: false, |
| 22 | + id: :test_id |
| 23 | + } |
| 24 | + |
| 25 | + # ============================================================================ |
| 26 | + # info |
| 27 | + |
| 28 | + test "info works" do |
| 29 | + assert is_bitstring(RadioButton.info(:bad_data)) |
| 30 | + assert RadioButton.info(:bad_data) =~ ":bad_data" |
| 31 | + end |
| 32 | + |
| 33 | + # ============================================================================ |
| 34 | + # verify |
| 35 | + |
| 36 | + test "verify passes valid data" do |
| 37 | + assert RadioButton.verify({"Title", :abc}) == {:ok, {"Title", :abc}} |
| 38 | + assert RadioButton.verify({"Title", :abc, true}) == {:ok, {"Title", :abc, true}} |
| 39 | + assert RadioButton.verify({"Title", :abc, false}) == {:ok, {"Title", :abc, false}} |
| 40 | + end |
| 41 | + |
| 42 | + test "verify fails invalid data" do |
| 43 | + assert RadioButton.verify(:banana) == :invalid_data |
| 44 | + assert RadioButton.verify({"Title", :abc, :banana}) == :invalid_data |
| 45 | + end |
| 46 | + |
| 47 | + # ============================================================================ |
| 48 | + # init |
| 49 | + |
| 50 | + test "init works with simple data" do |
| 51 | + {:ok, state} = RadioButton.init({"Title", :test_id}, styles: %{}) |
| 52 | + %Graph{} = state.graph |
| 53 | + assert is_map(state.theme) |
| 54 | + assert state.contained == false |
| 55 | + assert state.pressed == false |
| 56 | + assert state.checked == false |
| 57 | + assert state.id == :test_id |
| 58 | + |
| 59 | + {:ok, state} = RadioButton.init({"Title", :test_id, false}, styles: %{}, id: :test_id) |
| 60 | + assert state.checked == false |
| 61 | + |
| 62 | + {:ok, state} = RadioButton.init({"Title", :test_id, true}, styles: %{}, id: :test_id) |
| 63 | + assert state.checked == true |
| 64 | + end |
| 65 | + |
| 66 | + # ============================================================================ |
| 67 | + # handle_input |
| 68 | + |
| 69 | + test "handle_input {:cursor_enter, _uid} sets contained" do |
| 70 | + {:noreply, state} = RadioButton.handle_input({:cursor_enter, 1}, %{}, %{@state | pressed: true}) |
| 71 | + assert state.contained |
| 72 | + # confirm the graph was pushed |
| 73 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 74 | + end |
| 75 | + |
| 76 | + test "handle_input {:cursor_exit, _uid} clears contained" do |
| 77 | + {:noreply, state} = RadioButton.handle_input({:cursor_exit, 1}, %{}, %{@state | pressed: true}) |
| 78 | + refute state.contained |
| 79 | + # confirm the graph was pushed |
| 80 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 81 | + end |
| 82 | + |
| 83 | + test "handle_input {:cursor_button, :press" do |
| 84 | + context = %ViewPort.Context{viewport: self()} |
| 85 | + |
| 86 | + {:noreply, state} = |
| 87 | + RadioButton.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{ |
| 88 | + @state |
| 89 | + | pressed: false, |
| 90 | + contained: true |
| 91 | + }) |
| 92 | + |
| 93 | + assert state.pressed |
| 94 | + |
| 95 | + # confirm the input was captured |
| 96 | + assert_receive({:"$gen_cast", {:capture_input, ^context, [:cursor_button, :cursor_pos]}}) |
| 97 | + # confirm the graph was pushed |
| 98 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 99 | + end |
| 100 | + |
| 101 | + test "handle_input {:cursor_button, :release" do |
| 102 | + context = %ViewPort.Context{viewport: self()} |
| 103 | + |
| 104 | + {:noreply, state} = |
| 105 | + RadioButton.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{ |
| 106 | + @state |
| 107 | + | pressed: true, |
| 108 | + contained: true |
| 109 | + }) |
| 110 | + |
| 111 | + refute state.pressed |
| 112 | + |
| 113 | + # confirm the input was released |
| 114 | + assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}}) |
| 115 | + |
| 116 | + # confirm the graph was pushed |
| 117 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 118 | + end |
| 119 | + |
| 120 | + test "handle_input {:cursor_button, :release sends a message if contained" do |
| 121 | + self = self() |
| 122 | + Process.put(:parent_pid, self) |
| 123 | + context = %ViewPort.Context{viewport: self()} |
| 124 | + |
| 125 | + {:noreply, _} = |
| 126 | + RadioButton.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{ |
| 127 | + @state |
| 128 | + | pressed: true, |
| 129 | + contained: true |
| 130 | + }) |
| 131 | + |
| 132 | + # confirm the graph was pushed |
| 133 | + assert_receive({:"$gen_cast", {:event, {:click, :test_id}, ^self}}) |
| 134 | + end |
| 135 | + |
| 136 | + test "handle_input does nothing on unknown input" do |
| 137 | + context = %ViewPort.Context{viewport: self()} |
| 138 | + {:noreply, state} = RadioButton.handle_input(:unknown, context, @state) |
| 139 | + assert state == @state |
| 140 | + end |
| 141 | +end |
0 commit comments