added tests
This commit is contained in:
parent
c36f2f3352
commit
eb49206c5e
17
cmd/main.go
17
cmd/main.go
|
@ -1,7 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/getlantern/systray"
|
"github.com/getlantern/systray"
|
||||||
"github.com/prashantgupta24/automatic-mouse-mover/assets/icon"
|
"github.com/prashantgupta24/automatic-mouse-mover/assets/icon"
|
||||||
|
@ -17,6 +17,7 @@ func onReady() {
|
||||||
systray.SetIcon(icon.Data)
|
systray.SetIcon(icon.Data)
|
||||||
ammStart := systray.AddMenuItem("Start", "start the app")
|
ammStart := systray.AddMenuItem("Start", "start the app")
|
||||||
ammStop := systray.AddMenuItem("Stop", "stop the app")
|
ammStop := systray.AddMenuItem("Stop", "stop the app")
|
||||||
|
ammStop.Disable()
|
||||||
systray.AddSeparator()
|
systray.AddSeparator()
|
||||||
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
|
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
|
||||||
// Sets the icon of a menu item. Only available on Mac.
|
// Sets the icon of a menu item. Only available on Mac.
|
||||||
|
@ -25,19 +26,22 @@ func onReady() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ammStart.ClickedCh:
|
case <-ammStart.ClickedCh:
|
||||||
fmt.Println("starting the app")
|
log.Infof("starting the app")
|
||||||
mouseMover.Start()
|
mouseMover.Start()
|
||||||
|
ammStart.Disable()
|
||||||
|
ammStop.Enable()
|
||||||
//notify.SendMessage("starting the app")
|
//notify.SendMessage("starting the app")
|
||||||
|
|
||||||
case <-ammStop.ClickedCh:
|
case <-ammStop.ClickedCh:
|
||||||
fmt.Println("stopping the app")
|
log.Infof("stopping the app")
|
||||||
|
ammStart.Enable()
|
||||||
|
ammStop.Disable()
|
||||||
mouseMover.Quit()
|
mouseMover.Quit()
|
||||||
|
|
||||||
case <-mQuit.ClickedCh:
|
case <-mQuit.ClickedCh:
|
||||||
fmt.Println("Requesting quit")
|
log.Infof("Requesting quit")
|
||||||
mouseMover.Quit()
|
mouseMover.Quit()
|
||||||
systray.Quit()
|
systray.Quit()
|
||||||
fmt.Println("Finished quitting")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +51,5 @@ func onReady() {
|
||||||
|
|
||||||
func onExit() {
|
func onExit() {
|
||||||
// clean up here
|
// clean up here
|
||||||
fmt.Println("exiting")
|
log.Infof("Finished quitting")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -13,6 +13,6 @@ require (
|
||||||
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca
|
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca
|
||||||
github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc // indirect
|
github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc // indirect
|
||||||
github.com/prashantgupta24/activity-tracker v0.3.1
|
github.com/prashantgupta24/activity-tracker v0.3.1
|
||||||
github.com/sirupsen/logrus v1.4.0 // indirect
|
github.com/sirupsen/logrus v1.4.0
|
||||||
github.com/stretchr/testify v1.2.2
|
github.com/stretchr/testify v1.2.2
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package mousemover
|
package mousemover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"sync"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/go-vgo/robotgo"
|
"github.com/go-vgo/robotgo"
|
||||||
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
|
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
|
||||||
)
|
)
|
||||||
|
@ -14,7 +15,8 @@ var instance *MouseMover
|
||||||
//MouseMover is the main struct for the app
|
//MouseMover is the main struct for the app
|
||||||
type MouseMover struct {
|
type MouseMover struct {
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
isRunning bool
|
mutex sync.RWMutex
|
||||||
|
runningStatus bool
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -23,6 +25,9 @@ const (
|
||||||
|
|
||||||
//Start the main app
|
//Start the main app
|
||||||
func (m *MouseMover) Start() {
|
func (m *MouseMover) Start() {
|
||||||
|
if m.isRunning() {
|
||||||
|
return
|
||||||
|
}
|
||||||
m.quit = make(chan struct{})
|
m.quit = make(chan struct{})
|
||||||
|
|
||||||
frequency := 5 //value always in seconds
|
frequency := 5 //value always in seconds
|
||||||
|
@ -34,7 +39,7 @@ func (m *MouseMover) Start() {
|
||||||
heartbeatCh := activityTracker.Start()
|
heartbeatCh := activityTracker.Start()
|
||||||
|
|
||||||
go func(m *MouseMover) {
|
go func(m *MouseMover) {
|
||||||
m.isRunning = true
|
m.updateRunningStatus(true)
|
||||||
movePixel := 10
|
movePixel := 10
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -45,18 +50,18 @@ func (m *MouseMover) Start() {
|
||||||
select {
|
select {
|
||||||
case wasMouseMoveSuccess := <-commCh:
|
case wasMouseMoveSuccess := <-commCh:
|
||||||
if wasMouseMoveSuccess {
|
if wasMouseMoveSuccess {
|
||||||
fmt.Printf("\nmoving mouse at : %v\n\n", time.Now())
|
log.Infof("moving mouse at : %v\n\n", time.Now())
|
||||||
movePixel *= -1
|
movePixel *= -1
|
||||||
}
|
}
|
||||||
case <-time.After(timeout * time.Millisecond):
|
case <-time.After(timeout * time.Millisecond):
|
||||||
//timeout, do nothing
|
//timeout, do nothing
|
||||||
log.Printf("timeout happened after %vms while trying to move mouse", timeout)
|
log.Errorf("timeout happened after %vms while trying to move mouse", timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
case <-m.quit:
|
case <-m.quit:
|
||||||
fmt.Println("stopping mouse mover")
|
log.Infof("stopping mouse mover")
|
||||||
m.isRunning = false
|
m.updateRunningStatus(false)
|
||||||
activityTracker.Quit()
|
activityTracker.Quit()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -64,6 +69,17 @@ func (m *MouseMover) Start() {
|
||||||
}(m)
|
}(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MouseMover) isRunning() bool {
|
||||||
|
m.mutex.RLock()
|
||||||
|
defer m.mutex.RUnlock()
|
||||||
|
return m.runningStatus
|
||||||
|
}
|
||||||
|
func (m *MouseMover) updateRunningStatus(isRunning bool) {
|
||||||
|
m.mutex.Lock()
|
||||||
|
defer m.mutex.Unlock()
|
||||||
|
m.runningStatus = isRunning
|
||||||
|
}
|
||||||
|
|
||||||
func moveMouse(movePixel int, commCh chan bool) {
|
func moveMouse(movePixel int, commCh chan bool) {
|
||||||
currentX, currentY := robotgo.GetMousePos()
|
currentX, currentY := robotgo.GetMousePos()
|
||||||
moveToX := currentX + movePixel
|
moveToX := currentX + movePixel
|
||||||
|
@ -75,7 +91,7 @@ func moveMouse(movePixel int, commCh chan bool) {
|
||||||
//Quit the app
|
//Quit the app
|
||||||
func (m *MouseMover) Quit() {
|
func (m *MouseMover) Quit() {
|
||||||
//making it idempotent
|
//making it idempotent
|
||||||
if m != nil && m.isRunning {
|
if m != nil && m.isRunning() {
|
||||||
m.quit <- struct{}{}
|
m.quit <- struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,51 @@
|
||||||
package mousemover
|
package mousemover
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-vgo/robotgo"
|
"github.com/go-vgo/robotgo"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMouseMove(t *testing.T) {
|
type TestMover struct {
|
||||||
fmt.Println("starting test")
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(TestMover))
|
||||||
|
}
|
||||||
|
|
||||||
|
//Run once before each test
|
||||||
|
func (suite *TestMover) SetupTest() {
|
||||||
|
instance = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *TestMover) TestSingleton() {
|
||||||
|
t := suite.T()
|
||||||
|
mouseMover1 := GetInstance()
|
||||||
|
mouseMover1.Start()
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond * 500)
|
||||||
|
|
||||||
|
mouseMover2 := GetInstance()
|
||||||
|
assert.True(t, mouseMover2.isRunning(), "instance should not have started")
|
||||||
|
}
|
||||||
|
func (suite *TestMover) TestAppStartAndStop() {
|
||||||
|
t := suite.T()
|
||||||
|
mouseMover := GetInstance()
|
||||||
|
mouseMover.Start()
|
||||||
|
time.Sleep(time.Millisecond * 500) //wait for app to start
|
||||||
|
assert.True(t, mouseMover.isRunning(), "app should have started")
|
||||||
|
|
||||||
|
mouseMover.Quit()
|
||||||
|
time.Sleep(time.Millisecond * 500) //wait for app to stop
|
||||||
|
assert.False(t, mouseMover.isRunning(), "app should have stopped")
|
||||||
|
}
|
||||||
|
func (suite *TestMover) TestMouseMove() {
|
||||||
|
t := suite.T()
|
||||||
movePixel := 10
|
movePixel := 10
|
||||||
currentX, _ := robotgo.GetMousePos()
|
currentX, _ := robotgo.GetMousePos()
|
||||||
commCh := make(chan bool, 1)
|
commCh := make(chan bool, 1)
|
||||||
|
|
Loading…
Reference in New Issue