|
| 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.DropdownTest 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.Dropdown |
| 15 | + |
| 16 | + @items [{"a", 1}, {"b", 2}] |
| 17 | + @initial_item 2 |
| 18 | + @data {@items, @initial_item} |
| 19 | + |
| 20 | + @state %{ |
| 21 | + graph: Graph.build(), |
| 22 | + selected_id: @initial_item, |
| 23 | + theme: Primitive.Style.Theme.preset(:primary), |
| 24 | + id: :test_id, |
| 25 | + down: false, |
| 26 | + hover_id: nil, |
| 27 | + items: @items, |
| 28 | + drop_time: 0, |
| 29 | + rotate_caret: 0 |
| 30 | + } |
| 31 | + |
| 32 | + @button_id :__dropbox_btn__ |
| 33 | + |
| 34 | + # ============================================================================ |
| 35 | + # info |
| 36 | + |
| 37 | + test "info works" do |
| 38 | + assert is_bitstring(Dropdown.info(:bad_data)) |
| 39 | + assert Dropdown.info(:bad_data) =~ ":bad_data" |
| 40 | + end |
| 41 | + |
| 42 | + # ============================================================================ |
| 43 | + # verify |
| 44 | + |
| 45 | + test "verify passes valid data" do |
| 46 | + assert Dropdown.verify(@data) == {:ok, @data} |
| 47 | + end |
| 48 | + |
| 49 | + test "verify fails invalid data" do |
| 50 | + assert Dropdown.verify(:banana) == :invalid_data |
| 51 | + |
| 52 | + # invalid item in list |
| 53 | + data = {[{:a, 1}, {"b", 2}], 2} |
| 54 | + assert Dropdown.verify(data) == :invalid_data |
| 55 | + |
| 56 | + # selected is not in list |
| 57 | + data = {[{"a", 1}, {"b", 2}], 3} |
| 58 | + assert Dropdown.verify(data) == :invalid_data |
| 59 | + end |
| 60 | + |
| 61 | + # ============================================================================ |
| 62 | + # init |
| 63 | + |
| 64 | + test "init works with simple data" do |
| 65 | + {:ok, state} = Dropdown.init(@data, styles: %{}, id: :test_id) |
| 66 | + %Graph{} = state.graph |
| 67 | + assert state.selected_id == @initial_item |
| 68 | + assert is_map(state.theme) |
| 69 | + assert state.down == false |
| 70 | + assert state.hover_id == nil |
| 71 | + assert state.items == @items |
| 72 | + assert state.id == :test_id |
| 73 | + end |
| 74 | + |
| 75 | + # ============================================================================ |
| 76 | + # handle_input - up |
| 77 | + |
| 78 | + test "handle_input {:cursor_enter, _uid} - up" do |
| 79 | + {:noreply, state} = |
| 80 | + Dropdown.handle_input({:cursor_enter, 1}, %{id: 123}, %{@state | down: false}) |
| 81 | + |
| 82 | + assert state.hover_id == 123 |
| 83 | + end |
| 84 | + |
| 85 | + test "handle_input {:cursor_exit, _uid} - up" do |
| 86 | + {:noreply, state} = |
| 87 | + Dropdown.handle_input({:cursor_exit, 1}, %{id: 123}, %{@state | down: false}) |
| 88 | + |
| 89 | + assert state.hover_id == nil |
| 90 | + end |
| 91 | + |
| 92 | + test "handle_input {:cursor_button, :press - up" do |
| 93 | + context = %ViewPort.Context{viewport: self(), id: @button_id} |
| 94 | + |
| 95 | + {:noreply, state} = |
| 96 | + Dropdown.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{ |
| 97 | + @state |
| 98 | + | down: false |
| 99 | + }) |
| 100 | + assert state.down == true |
| 101 | + assert (is_integer( state.drop_time ) && state.drop_time > 0) |
| 102 | + |
| 103 | + # confirm the input was captured |
| 104 | + assert_receive({:"$gen_cast", {:capture_input, ^context, [:cursor_button, :cursor_pos]}}) |
| 105 | + |
| 106 | + # confirm the graph was pushed |
| 107 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 108 | + end |
| 109 | + |
| 110 | + # ============================================================================ |
| 111 | + # handle_input - down |
| 112 | + |
| 113 | + test "handle_input {:cursor_enter, _uid} - down" do |
| 114 | + context = %ViewPort.Context{viewport: self(), id: 1} |
| 115 | + |
| 116 | + {:noreply, state} = |
| 117 | + Dropdown.handle_input({:cursor_enter, 1}, context, %{@state | down: true}) |
| 118 | + |
| 119 | + assert state.hover_id == 1 |
| 120 | + |
| 121 | + # confirm the graph was pushed |
| 122 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 123 | + end |
| 124 | + |
| 125 | + test "handle_input {:cursor_exit, _uid} - down" do |
| 126 | + context = %ViewPort.Context{viewport: self(), id: 1} |
| 127 | + |
| 128 | + {:noreply, state} = |
| 129 | + Dropdown.handle_input({:cursor_exit, 1}, context, %{@state | down: true}) |
| 130 | + |
| 131 | + assert state.hover_id == nil |
| 132 | + |
| 133 | + # confirm the graph was pushed |
| 134 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 135 | + end |
| 136 | + |
| 137 | + # mouse down outside menu |
| 138 | + test "handle_input {:cursor_button, :press nil - down" do |
| 139 | + context = %ViewPort.Context{viewport: self(), id: nil} |
| 140 | + |
| 141 | + {:noreply, state} = |
| 142 | + Dropdown.handle_input({:cursor_button, {:left, :press, nil, nil}}, context, %{ |
| 143 | + @state |
| 144 | + | down: true |
| 145 | + }) |
| 146 | + assert state.down == false |
| 147 | + |
| 148 | + # confirm the input was released |
| 149 | + assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}}) |
| 150 | + |
| 151 | + # confirm the graph was pushed |
| 152 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 153 | + end |
| 154 | + |
| 155 | + # mouse down inside button - slow |
| 156 | + test "handle_input {:cursor_button, :press button - down - slow - should close" do |
| 157 | + context = %ViewPort.Context{viewport: self(), id: @button_id} |
| 158 | + |
| 159 | + {:noreply, state} = |
| 160 | + Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{ |
| 161 | + @state |
| 162 | + | down: true |
| 163 | + }) |
| 164 | + assert state.down == false |
| 165 | + |
| 166 | + # confirm the input was released |
| 167 | + assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}}) |
| 168 | + |
| 169 | + # confirm the graph was pushed |
| 170 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 171 | + end |
| 172 | + |
| 173 | + # mouse down inside button - fast |
| 174 | + test "handle_input {:cursor_button, :press button - down - fast - should stay down" do |
| 175 | + context = %ViewPort.Context{viewport: self(), id: @button_id} |
| 176 | + |
| 177 | + {:noreply, state} = |
| 178 | + Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{ |
| 179 | + @state |
| 180 | + | down: true, |
| 181 | + drop_time: :os.system_time(:milli_seconds) |
| 182 | + }) |
| 183 | + assert state.down == true |
| 184 | + |
| 185 | + # confirm the input was not released |
| 186 | + refute_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}}) |
| 187 | + |
| 188 | + # confirm the graph was not pushed |
| 189 | + refute_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 190 | + end |
| 191 | + |
| 192 | + # mouse released outside dropdown space |
| 193 | + test "handle_input {:cursor_button, :release button - outside menu" do |
| 194 | + context = %ViewPort.Context{viewport: self(), id: nil} |
| 195 | + |
| 196 | + {:noreply, state} = |
| 197 | + Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{ |
| 198 | + @state |
| 199 | + | down: true, |
| 200 | + drop_time: :os.system_time(:milli_seconds) |
| 201 | + }) |
| 202 | + assert state.down == false |
| 203 | + assert state.selected_id == @initial_item |
| 204 | + |
| 205 | + # confirm the input was released |
| 206 | + assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}}) |
| 207 | + |
| 208 | + # confirm the value change was not sent |
| 209 | + refute_receive({:"$gen_cast", {:event, {:value_changed, _, _}, _}}) |
| 210 | + |
| 211 | + # confirm the graph was pushed |
| 212 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 213 | + end |
| 214 | + |
| 215 | + # mouse released inside dropdown space |
| 216 | + test "handle_input {:cursor_button, :release button - inside menu" do |
| 217 | + self = self() |
| 218 | + Process.put(:parent_pid, self) |
| 219 | + context = %ViewPort.Context{viewport: self, id: 1} |
| 220 | + |
| 221 | + {:noreply, state} = |
| 222 | + Dropdown.handle_input({:cursor_button, {:left, :release, nil, nil}}, context, %{ |
| 223 | + @state |
| 224 | + | down: true |
| 225 | + }) |
| 226 | + assert state.down == false |
| 227 | + assert state.selected_id == 1 |
| 228 | + |
| 229 | + # confirm the input was released |
| 230 | + assert_receive({:"$gen_cast", {:release_input, [:cursor_button, :cursor_pos]}}) |
| 231 | + |
| 232 | + # confirm the value change was not sent |
| 233 | + assert_receive({:"$gen_cast", {:event, {:value_changed, :test_id, 1}, ^self}}) |
| 234 | + |
| 235 | + # confirm the graph was pushed |
| 236 | + assert_receive({:"$gen_cast", {:push_graph, _, _, _}}) |
| 237 | + end |
| 238 | + |
| 239 | + test "handle_input does nothing on unknown input" do |
| 240 | + context = %ViewPort.Context{viewport: self()} |
| 241 | + {:noreply, state} = Dropdown.handle_input(:unknown, context, @state) |
| 242 | + assert state == @state |
| 243 | + end |
| 244 | + |
| 245 | +end |
0 commit comments