github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/cmd/snap-bootstrap/triggerwatch/triggerwatch_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package triggerwatch_test 21 22 import ( 23 "fmt" 24 "testing" 25 "time" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/cmd/snap-bootstrap/triggerwatch" 30 ) 31 32 // Hook up check.v1 into the "go test" runner 33 func Test(t *testing.T) { TestingT(t) } 34 35 type triggerwatchSuite struct{} 36 37 var _ = Suite(&triggerwatchSuite{}) 38 39 type mockTriggerDevice struct { 40 waitForTriggerCalls int 41 closeCalls int 42 ev *triggerwatch.KeyEvent 43 } 44 45 func (m *mockTriggerDevice) WaitForTrigger(n chan triggerwatch.KeyEvent) { 46 m.waitForTriggerCalls++ 47 if m.ev != nil { 48 ev := *m.ev 49 ev.Dev = m 50 n <- ev 51 } 52 } 53 54 func (m *mockTriggerDevice) String() string { return "mock-device" } 55 func (m *mockTriggerDevice) Close() { m.closeCalls++ } 56 57 type mockTrigger struct { 58 f triggerwatch.TriggerCapabilityFilter 59 d *mockTriggerDevice 60 err error 61 62 findMatchingCalls int 63 } 64 65 func (m *mockTrigger) FindMatchingDevices(f triggerwatch.TriggerCapabilityFilter) ([]triggerwatch.TriggerDevice, error) { 66 m.findMatchingCalls++ 67 68 m.f = f 69 if m.err != nil { 70 return nil, m.err 71 } 72 if m.d != nil { 73 return []triggerwatch.TriggerDevice{m.d}, nil 74 } 75 return nil, nil 76 } 77 78 const testTriggerTimeout = 5 * time.Millisecond 79 80 func (s *triggerwatchSuite) TestNoDevsWaitKey(c *C) { 81 md := &mockTriggerDevice{ev: &triggerwatch.KeyEvent{}} 82 mi := &mockTrigger{d: md} 83 restore := triggerwatch.MockInput(mi) 84 defer restore() 85 86 err := triggerwatch.Wait(testTriggerTimeout) 87 c.Assert(err, IsNil) 88 c.Assert(mi.findMatchingCalls, Equals, 1) 89 c.Assert(md.waitForTriggerCalls, Equals, 1) 90 c.Assert(md.closeCalls, Equals, 1) 91 } 92 93 func (s *triggerwatchSuite) TestNoDevsWaitKeyTimeout(c *C) { 94 md := &mockTriggerDevice{} 95 mi := &mockTrigger{d: md} 96 restore := triggerwatch.MockInput(mi) 97 defer restore() 98 99 err := triggerwatch.Wait(testTriggerTimeout) 100 c.Assert(err, Equals, triggerwatch.ErrTriggerNotDetected) 101 c.Assert(mi.findMatchingCalls, Equals, 1) 102 c.Assert(md.waitForTriggerCalls, Equals, 1) 103 c.Assert(md.closeCalls, Equals, 1) 104 } 105 106 func (s *triggerwatchSuite) TestNoDevsWaitNoMatching(c *C) { 107 mi := &mockTrigger{} 108 restore := triggerwatch.MockInput(mi) 109 defer restore() 110 111 err := triggerwatch.Wait(testTriggerTimeout) 112 c.Assert(err, Equals, triggerwatch.ErrNoMatchingInputDevices) 113 } 114 115 func (s *triggerwatchSuite) TestNoDevsWaitMatchingError(c *C) { 116 mi := &mockTrigger{err: fmt.Errorf("failed")} 117 restore := triggerwatch.MockInput(mi) 118 defer restore() 119 120 err := triggerwatch.Wait(testTriggerTimeout) 121 c.Assert(err, ErrorMatches, "cannot list trigger devices: failed") 122 } 123 124 func (s *triggerwatchSuite) TestChecksInput(c *C) { 125 restore := triggerwatch.MockInput(nil) 126 defer restore() 127 128 c.Assert(func() { triggerwatch.Wait(testTriggerTimeout) }, 129 Panics, "trigger is unset") 130 }