gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-bootstrap/triggerwatch/export_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  package triggerwatch
    20  
    21  import (
    22  	"time"
    23  
    24  	"gitee.com/mysnapcore/mysnapd/osutil/udev/netlink"
    25  )
    26  
    27  func MockInput(newInput TriggerProvider) (restore func()) {
    28  	oldInput := trigger
    29  	trigger = newInput
    30  	return func() {
    31  		trigger = oldInput
    32  	}
    33  }
    34  
    35  type TriggerProvider = triggerProvider
    36  type TriggerDevice = triggerDevice
    37  type TriggerCapabilityFilter = triggerEventFilter
    38  type KeyEvent = keyEvent
    39  
    40  type mockUEventConnection struct {
    41  	events []netlink.UEvent
    42  }
    43  
    44  func (m *mockUEventConnection) Connect(mode netlink.Mode) error {
    45  	return nil
    46  }
    47  
    48  func (m *mockUEventConnection) Close() error {
    49  	return nil
    50  }
    51  
    52  func (m *mockUEventConnection) Monitor(queue chan netlink.UEvent, errors chan error, matcher netlink.Matcher) func(time.Duration) bool {
    53  	go func() {
    54  		for _, event := range m.events {
    55  			queue <- event
    56  		}
    57  	}()
    58  	return func(time.Duration) bool {
    59  		return true
    60  	}
    61  }
    62  
    63  func MockUEvent(events []netlink.UEvent) (restore func()) {
    64  	oldGetUEventConn := getUEventConn
    65  	getUEventConn = func() ueventConnection {
    66  		return &mockUEventConnection{events}
    67  	}
    68  
    69  	return func() {
    70  		getUEventConn = oldGetUEventConn
    71  	}
    72  }