automatic-mouse-mover/pkg/mousemover/mouseMover_test.go

150 lines
4.6 KiB
Go
Raw Normal View History

2019-03-27 02:50:09 +00:00
package mousemover
import (
"testing"
2019-03-28 19:52:25 +00:00
"time"
2019-03-27 02:50:09 +00:00
"github.com/stretchr/testify/assert"
2019-03-28 19:52:25 +00:00
"github.com/stretchr/testify/suite"
2019-04-27 01:05:16 +00:00
2019-05-01 01:53:53 +00:00
"github.com/prashantgupta24/activity-tracker/pkg/activity"
2019-04-27 01:05:16 +00:00
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
2019-03-27 02:50:09 +00:00
)
2019-03-28 19:52:25 +00:00
type TestMover struct {
suite.Suite
2019-04-27 01:05:16 +00:00
activityTracker *tracker.Instance
2019-05-01 01:53:53 +00:00
heartbeatCh chan *tracker.Heartbeat
2019-03-28 19:52:25 +00:00
}
func TestSuite(t *testing.T) {
suite.Run(t, new(TestMover))
}
2019-05-01 01:53:53 +00:00
2019-04-27 01:05:16 +00:00
//Run once before all tests
func (suite *TestMover) SetupSuite() {
2019-05-01 01:53:53 +00:00
heartbeatInterval := 60
2019-04-27 01:05:16 +00:00
workerInterval := 10
suite.activityTracker = &tracker.Instance{
HeartbeatInterval: heartbeatInterval,
WorkerInterval: workerInterval,
}
2019-05-01 01:53:53 +00:00
suite.heartbeatCh = make(chan *tracker.Heartbeat)
2019-04-27 01:05:16 +00:00
}
2019-03-28 19:52:25 +00:00
//Run once before each test
func (suite *TestMover) SetupTest() {
instance = nil
}
2019-05-01 01:53:53 +00:00
func (suite *TestMover) TestAppStart() {
t := suite.T()
mouseMover := GetInstance()
mouseMover.run(suite.heartbeatCh, suite.activityTracker)
time.Sleep(time.Millisecond * 500) //wait for app to start
assert.True(t, mouseMover.state.isRunning(), "app should have started")
}
2019-03-28 19:52:25 +00:00
func (suite *TestMover) TestSingleton() {
t := suite.T()
2019-04-27 01:05:16 +00:00
2019-03-28 19:52:25 +00:00
mouseMover1 := GetInstance()
2019-04-27 01:05:16 +00:00
mouseMover1.run(suite.heartbeatCh, suite.activityTracker)
2019-03-28 19:52:25 +00:00
time.Sleep(time.Millisecond * 500)
mouseMover2 := GetInstance()
2019-05-01 01:53:53 +00:00
assert.True(t, mouseMover2.state.isRunning(), "instance should have started")
2019-03-28 19:52:25 +00:00
}
2019-05-01 01:53:53 +00:00
2019-05-01 02:45:09 +00:00
func (suite *TestMover) TestSystemSleepAndWake() {
2019-03-28 19:52:25 +00:00
t := suite.T()
mouseMover := GetInstance()
2019-05-01 01:53:53 +00:00
heartbeatCh := make(chan *tracker.Heartbeat)
mouseMover.run(heartbeatCh, suite.activityTracker)
time.Sleep(time.Millisecond * 500) //wait for app to start
assert.True(t, mouseMover.state.isRunning(), "instance should have started")
assert.False(t, mouseMover.state.isSystemSleeping(), "machine should not be sleeping")
//fake a machine-sleep activity
machineSleepActivityMap := make(map[activity.Type][]time.Time)
2019-05-01 02:45:09 +00:00
var sleepTimeArray []time.Time
sleepTimeArray = append(sleepTimeArray, time.Now())
machineSleepActivityMap[activity.MachineSleep] = sleepTimeArray
2019-05-01 01:53:53 +00:00
heartbeatCh <- &tracker.Heartbeat{
WasAnyActivity: true,
ActivityMap: machineSleepActivityMap,
}
time.Sleep(time.Millisecond * 500) //wait for it to be registered
assert.True(t, mouseMover.state.isSystemSleeping(), "machine should be sleeping now")
2019-05-01 02:45:09 +00:00
//fake a machine-wake activity
machineWakeActivityMap := make(map[activity.Type][]time.Time)
var wakeTimeArray []time.Time
wakeTimeArray = append(wakeTimeArray, time.Now())
machineWakeActivityMap[activity.MachineWake] = wakeTimeArray
heartbeatCh <- &tracker.Heartbeat{
WasAnyActivity: true,
ActivityMap: machineWakeActivityMap,
}
time.Sleep(time.Millisecond * 500) //wait for it to be registered
assert.False(t, mouseMover.state.isSystemSleeping(), "machine should be awake now")
2019-05-01 01:53:53 +00:00
}
func (suite *TestMover) TestMouseMoveSuccess() {
t := suite.T()
mouseMover := GetInstance()
state := &state{
override: &override{
valueToReturn: true,
},
}
mouseMover.state = state
heartbeatCh := make(chan *tracker.Heartbeat)
mouseMover.run(heartbeatCh, suite.activityTracker)
2019-03-28 19:52:25 +00:00
time.Sleep(time.Millisecond * 500) //wait for app to start
2019-05-01 01:53:53 +00:00
assert.True(t, state.isRunning(), "instance should have started")
assert.False(t, state.isSystemSleeping(), "machine should not be sleeping")
assert.True(t, time.Time.IsZero(state.getLastMouseMovedTime()), "should be default")
assert.Equal(t, state.getDidNotMoveCount(), 0, "should be 0")
heartbeatCh <- &tracker.Heartbeat{
WasAnyActivity: false,
}
time.Sleep(time.Millisecond * 500) //wait for it to be registered
assert.False(t, time.Time.IsZero(state.getLastMouseMovedTime()), "should be default but is ", state.getLastMouseMovedTime())
}
func (suite *TestMover) TestMouseMoveFailure() {
t := suite.T()
mouseMover := GetInstance()
state := &state{
override: &override{
valueToReturn: false,
},
}
mouseMover.state = state
heartbeatCh := make(chan *tracker.Heartbeat)
mouseMover.run(heartbeatCh, suite.activityTracker)
time.Sleep(time.Millisecond * 500) //wait for app to start
assert.True(t, state.isRunning(), "instance should have started")
assert.False(t, state.isSystemSleeping(), "machine should not be sleeping")
assert.True(t, time.Time.IsZero(state.getLastMouseMovedTime()), "should be default")
assert.Equal(t, state.getDidNotMoveCount(), 0, "should be 0")
heartbeatCh <- &tracker.Heartbeat{
WasAnyActivity: false,
}
time.Sleep(time.Millisecond * 500) //wait for it to be registered
assert.True(t, time.Time.IsZero(state.getLastMouseMovedTime()), "should be default but is ", state.getLastMouseMovedTime())
assert.NotEqual(t, state.getDidNotMoveCount(), 0, "should not be 0")
2019-03-28 19:52:25 +00:00
}