Merge pull request #6 from prashantgupta24/refactor-1

added tests + disabled buttons
This commit is contained in:
Prashant Gupta 2019-03-28 13:40:08 -07:00 committed by GitHub
commit deff22178c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 25 deletions

View File

@ -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")
} }

6
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/go-stack/stack v1.8.0 // indirect github.com/go-stack/stack v1.8.0 // indirect
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.4.0
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.3.0
) )

8
go.sum
View File

@ -4,6 +4,7 @@ github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e h1:4ZrkT/RzpnRO
github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k= github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k=
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 h1:UUppSQnhf4Yc6xGxSkoQpPhb7RVzuv5Nb1mwJ5VId9s= github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 h1:UUppSQnhf4Yc6xGxSkoQpPhb7RVzuv5Nb1mwJ5VId9s=
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/gosx-notifier v0.0.0-20180201035817-e127226297fb h1:6S+TKObz6+Io2c8IOkcbK4Sz7nj6RpEVU7TkvmsZZcw= github.com/deckarep/gosx-notifier v0.0.0-20180201035817-e127226297fb h1:6S+TKObz6+Io2c8IOkcbK4Sz7nj6RpEVU7TkvmsZZcw=
@ -26,6 +27,7 @@ github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-vgo/robotgo v0.0.0-20190311142943-193b4935d4b1/go.mod h1:jRysmxT/HqVgkZAFBsYVjLHRrsrEUUKx19xHLNPVYM0=
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca h1:TUqm5e4QD14kygf1AkXLxcn6sBdhh2SuEjPXaFrwEis= github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca h1:TUqm5e4QD14kygf1AkXLxcn6sBdhh2SuEjPXaFrwEis=
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca/go.mod h1:btNdXfFVS5B5p/iqUKRaaSuJfETYNbMv6wG8fL2onK0= github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca/go.mod h1:btNdXfFVS5B5p/iqUKRaaSuJfETYNbMv6wG8fL2onK0=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
@ -40,6 +42,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantgupta24/activity-tracker v0.3.1 h1:+jKFqnve2316ZgWgrYgd+go+ocphaN3EgfeEMfW7lhA= github.com/prashantgupta24/activity-tracker v0.3.1 h1:+jKFqnve2316ZgWgrYgd+go+ocphaN3EgfeEMfW7lhA=
github.com/prashantgupta24/activity-tracker v0.3.1/go.mod h1:hZjTk+hYxhxGutmSNiFTk3+OebO3RTNki5gA/5hIPkM= github.com/prashantgupta24/activity-tracker v0.3.1/go.mod h1:hZjTk+hYxhxGutmSNiFTk3+OebO3RTNki5gA/5hIPkM=
github.com/prashantgupta24/activity-tracker v0.4.0 h1:wIsVFIo4h8GSe08QTPRbqEfBeAjX16WHdFOUb98MKCw=
github.com/prashantgupta24/activity-tracker v0.4.0/go.mod h1:erO/ZDFDjCvt8BgqWwYkFggJOcrwjGGROcM0WwYRwzE=
github.com/robotn/gohook v0.0.0-20190305192954-bbbbefb5ff85/go.mod h1:YD5RyCnUEY2xqtkkgeQVZ31UAfAnVPwUxpTE5cwSXg4=
github.com/robotn/gohook v0.0.0-20190315174547-bc414970f3d6 h1:XTKPqoPYwgasmAWhtpolldqBpnbpgH6iAtQPReXiywA= github.com/robotn/gohook v0.0.0-20190315174547-bc414970f3d6 h1:XTKPqoPYwgasmAWhtpolldqBpnbpgH6iAtQPReXiywA=
github.com/robotn/gohook v0.0.0-20190315174547-bc414970f3d6/go.mod h1:YD5RyCnUEY2xqtkkgeQVZ31UAfAnVPwUxpTE5cwSXg4= github.com/robotn/gohook v0.0.0-20190315174547-bc414970f3d6/go.mod h1:YD5RyCnUEY2xqtkkgeQVZ31UAfAnVPwUxpTE5cwSXg4=
github.com/shirou/gopsutil v0.0.0-20190131151121-071446942108 h1:XXgDK65TPH+Qbo2sdYHldM5avclwThBXVYZHxroFkTQ= github.com/shirou/gopsutil v0.0.0-20190131151121-071446942108 h1:XXgDK65TPH+Qbo2sdYHldM5avclwThBXVYZHxroFkTQ=
@ -48,9 +53,12 @@ github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+D
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc= github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
github.com/sirupsen/logrus v1.4.0 h1:yKenngtzGh+cUSSh6GWbxW2abRqhYUSR/t/6+2QqNvE= github.com/sirupsen/logrus v1.4.0 h1:yKenngtzGh+cUSSh6GWbxW2abRqhYUSR/t/6+2QqNvE=
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/vcaesar/imgo v0.0.0-20181209162409-13af122cf2fa h1:kOg2u5TN+l2IBl4Q0Xm3bZRQ5gK3zGXRi/24XT8L7FI= github.com/vcaesar/imgo v0.0.0-20181209162409-13af122cf2fa h1:kOg2u5TN+l2IBl4Q0Xm3bZRQ5gK3zGXRi/24XT8L7FI=
github.com/vcaesar/imgo v0.0.0-20181209162409-13af122cf2fa/go.mod h1:D+Ywq+8bsOIg4nUk1lgHtPGxYxknhySi/HfZjG7VN/g= github.com/vcaesar/imgo v0.0.0-20181209162409-13af122cf2fa/go.mod h1:D+Ywq+8bsOIg4nUk1lgHtPGxYxknhySi/HfZjG7VN/g=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=

View File

@ -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,9 +25,12 @@ 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 := 60 //value always in seconds
activityTracker := &tracker.Instance{ activityTracker := &tracker.Instance{
Frequency: frequency, Frequency: frequency,
//LogLevel: "debug", //if we want verbose logging //LogLevel: "debug", //if we want verbose logging
@ -34,29 +39,29 @@ 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 {
case heartbeat := <-heartbeatCh: case heartbeat := <-heartbeatCh:
if !heartbeat.IsActivity { if !heartbeat.WasAnyActivity {
commCh := make(chan bool) commCh := make(chan bool)
go moveMouse(movePixel, commCh) go moveMouse(movePixel, commCh)
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{}{}
} }
} }

View File

@ -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)