mirror of
https://github.com/prashantgupta24/automatic-mouse-mover.git
synced 2024-12-22 00:14:11 +00:00
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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/getlantern/systray"
|
||||
"github.com/prashantgupta24/automatic-mouse-mover/assets/icon"
|
||||
@ -17,6 +17,7 @@ func onReady() {
|
||||
systray.SetIcon(icon.Data)
|
||||
ammStart := systray.AddMenuItem("Start", "start the app")
|
||||
ammStop := systray.AddMenuItem("Stop", "stop the app")
|
||||
ammStop.Disable()
|
||||
systray.AddSeparator()
|
||||
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
|
||||
// Sets the icon of a menu item. Only available on Mac.
|
||||
@ -25,19 +26,22 @@ func onReady() {
|
||||
for {
|
||||
select {
|
||||
case <-ammStart.ClickedCh:
|
||||
fmt.Println("starting the app")
|
||||
log.Infof("starting the app")
|
||||
mouseMover.Start()
|
||||
ammStart.Disable()
|
||||
ammStop.Enable()
|
||||
//notify.SendMessage("starting the app")
|
||||
|
||||
case <-ammStop.ClickedCh:
|
||||
fmt.Println("stopping the app")
|
||||
log.Infof("stopping the app")
|
||||
ammStart.Enable()
|
||||
ammStop.Disable()
|
||||
mouseMover.Quit()
|
||||
|
||||
case <-mQuit.ClickedCh:
|
||||
fmt.Println("Requesting quit")
|
||||
log.Infof("Requesting quit")
|
||||
mouseMover.Quit()
|
||||
systray.Quit()
|
||||
fmt.Println("Finished quitting")
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -47,6 +51,5 @@ func onReady() {
|
||||
|
||||
func onExit() {
|
||||
// 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/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc // indirect
|
||||
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
|
||||
)
|
||||
|
@ -1,10 +1,11 @@
|
||||
package mousemover
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/go-vgo/robotgo"
|
||||
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
|
||||
)
|
||||
@ -13,8 +14,9 @@ var instance *MouseMover
|
||||
|
||||
//MouseMover is the main struct for the app
|
||||
type MouseMover struct {
|
||||
quit chan struct{}
|
||||
isRunning bool
|
||||
quit chan struct{}
|
||||
mutex sync.RWMutex
|
||||
runningStatus bool
|
||||
}
|
||||
|
||||
const (
|
||||
@ -23,6 +25,9 @@ const (
|
||||
|
||||
//Start the main app
|
||||
func (m *MouseMover) Start() {
|
||||
if m.isRunning() {
|
||||
return
|
||||
}
|
||||
m.quit = make(chan struct{})
|
||||
|
||||
frequency := 5 //value always in seconds
|
||||
@ -34,7 +39,7 @@ func (m *MouseMover) Start() {
|
||||
heartbeatCh := activityTracker.Start()
|
||||
|
||||
go func(m *MouseMover) {
|
||||
m.isRunning = true
|
||||
m.updateRunningStatus(true)
|
||||
movePixel := 10
|
||||
for {
|
||||
select {
|
||||
@ -45,18 +50,18 @@ func (m *MouseMover) Start() {
|
||||
select {
|
||||
case wasMouseMoveSuccess := <-commCh:
|
||||
if wasMouseMoveSuccess {
|
||||
fmt.Printf("\nmoving mouse at : %v\n\n", time.Now())
|
||||
log.Infof("moving mouse at : %v\n\n", time.Now())
|
||||
movePixel *= -1
|
||||
}
|
||||
case <-time.After(timeout * time.Millisecond):
|
||||
//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:
|
||||
fmt.Println("stopping mouse mover")
|
||||
m.isRunning = false
|
||||
log.Infof("stopping mouse mover")
|
||||
m.updateRunningStatus(false)
|
||||
activityTracker.Quit()
|
||||
return
|
||||
}
|
||||
@ -64,6 +69,17 @@ func (m *MouseMover) Start() {
|
||||
}(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) {
|
||||
currentX, currentY := robotgo.GetMousePos()
|
||||
moveToX := currentX + movePixel
|
||||
@ -75,7 +91,7 @@ func moveMouse(movePixel int, commCh chan bool) {
|
||||
//Quit the app
|
||||
func (m *MouseMover) Quit() {
|
||||
//making it idempotent
|
||||
if m != nil && m.isRunning {
|
||||
if m != nil && m.isRunning() {
|
||||
m.quit <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,51 @@
|
||||
package mousemover
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-vgo/robotgo"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
func TestMouseMove(t *testing.T) {
|
||||
fmt.Println("starting test")
|
||||
type TestMover struct {
|
||||
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
|
||||
currentX, _ := robotgo.GetMousePos()
|
||||
commCh := make(chan bool, 1)
|
||||
|
Loading…
Reference in New Issue
Block a user