Skip to content

Commit deff221

Browse files
Merge pull request #6 from prashantgupta24/refactor-1
added tests + disabled buttons
2 parents c36f2f3 + 5cc0c44 commit deff221

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

cmd/main.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"fmt"
4+
log "github.com/sirupsen/logrus"
55

66
"github.com/getlantern/systray"
77
"github.com/prashantgupta24/automatic-mouse-mover/assets/icon"
@@ -17,6 +17,7 @@ func onReady() {
1717
systray.SetIcon(icon.Data)
1818
ammStart := systray.AddMenuItem("Start", "start the app")
1919
ammStop := systray.AddMenuItem("Stop", "stop the app")
20+
ammStop.Disable()
2021
systray.AddSeparator()
2122
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
2223
// Sets the icon of a menu item. Only available on Mac.
@@ -25,19 +26,22 @@ func onReady() {
2526
for {
2627
select {
2728
case <-ammStart.ClickedCh:
28-
fmt.Println("starting the app")
29+
log.Infof("starting the app")
2930
mouseMover.Start()
31+
ammStart.Disable()
32+
ammStop.Enable()
3033
//notify.SendMessage("starting the app")
3134

3235
case <-ammStop.ClickedCh:
33-
fmt.Println("stopping the app")
36+
log.Infof("stopping the app")
37+
ammStart.Enable()
38+
ammStop.Disable()
3439
mouseMover.Quit()
3540

3641
case <-mQuit.ClickedCh:
37-
fmt.Println("Requesting quit")
42+
log.Infof("Requesting quit")
3843
mouseMover.Quit()
3944
systray.Quit()
40-
fmt.Println("Finished quitting")
4145
return
4246
}
4347
}
@@ -47,6 +51,5 @@ func onReady() {
4751

4852
func onExit() {
4953
// clean up here
50-
fmt.Println("exiting")
51-
54+
log.Infof("Finished quitting")
5255
}

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/go-stack/stack v1.8.0 // indirect
1313
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca
1414
github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc // indirect
15-
github.com/prashantgupta24/activity-tracker v0.3.1
16-
github.com/sirupsen/logrus v1.4.0 // indirect
17-
github.com/stretchr/testify v1.2.2
15+
github.com/prashantgupta24/activity-tracker v0.4.0
16+
github.com/sirupsen/logrus v1.4.0
17+
github.com/stretchr/testify v1.3.0
1818
)

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e h1:4ZrkT/RzpnRO
44
github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k=
55
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 h1:UUppSQnhf4Yc6xGxSkoQpPhb7RVzuv5Nb1mwJ5VId9s=
66
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
7+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
78
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
89
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
910
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=
2627
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
2728
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
2829
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
30+
github.com/go-vgo/robotgo v0.0.0-20190311142943-193b4935d4b1/go.mod h1:jRysmxT/HqVgkZAFBsYVjLHRrsrEUUKx19xHLNPVYM0=
2931
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca h1:TUqm5e4QD14kygf1AkXLxcn6sBdhh2SuEjPXaFrwEis=
3032
github.com/go-vgo/robotgo v0.0.0-20190321150332-9d92e0f852ca/go.mod h1:btNdXfFVS5B5p/iqUKRaaSuJfETYNbMv6wG8fL2onK0=
3133
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
4042
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4143
github.com/prashantgupta24/activity-tracker v0.3.1 h1:+jKFqnve2316ZgWgrYgd+go+ocphaN3EgfeEMfW7lhA=
4244
github.com/prashantgupta24/activity-tracker v0.3.1/go.mod h1:hZjTk+hYxhxGutmSNiFTk3+OebO3RTNki5gA/5hIPkM=
45+
github.com/prashantgupta24/activity-tracker v0.4.0 h1:wIsVFIo4h8GSe08QTPRbqEfBeAjX16WHdFOUb98MKCw=
46+
github.com/prashantgupta24/activity-tracker v0.4.0/go.mod h1:erO/ZDFDjCvt8BgqWwYkFggJOcrwjGGROcM0WwYRwzE=
47+
github.com/robotn/gohook v0.0.0-20190305192954-bbbbefb5ff85/go.mod h1:YD5RyCnUEY2xqtkkgeQVZ31UAfAnVPwUxpTE5cwSXg4=
4348
github.com/robotn/gohook v0.0.0-20190315174547-bc414970f3d6 h1:XTKPqoPYwgasmAWhtpolldqBpnbpgH6iAtQPReXiywA=
4449
github.com/robotn/gohook v0.0.0-20190315174547-bc414970f3d6/go.mod h1:YD5RyCnUEY2xqtkkgeQVZ31UAfAnVPwUxpTE5cwSXg4=
4550
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
4853
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
4954
github.com/sirupsen/logrus v1.4.0 h1:yKenngtzGh+cUSSh6GWbxW2abRqhYUSR/t/6+2QqNvE=
5055
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
56+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5157
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5258
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
5359
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
60+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
61+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5462
github.com/vcaesar/imgo v0.0.0-20181209162409-13af122cf2fa h1:kOg2u5TN+l2IBl4Q0Xm3bZRQ5gK3zGXRi/24XT8L7FI=
5563
github.com/vcaesar/imgo v0.0.0-20181209162409-13af122cf2fa/go.mod h1:D+Ywq+8bsOIg4nUk1lgHtPGxYxknhySi/HfZjG7VN/g=
5664
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=

pkg/mousemover/mouseMover.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package mousemover
22

33
import (
4-
"fmt"
5-
"log"
4+
"sync"
65
"time"
76

7+
log "github.com/sirupsen/logrus"
8+
89
"github.com/go-vgo/robotgo"
910
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
1011
)
@@ -13,8 +14,9 @@ var instance *MouseMover
1314

1415
//MouseMover is the main struct for the app
1516
type MouseMover struct {
16-
quit chan struct{}
17-
isRunning bool
17+
quit chan struct{}
18+
mutex sync.RWMutex
19+
runningStatus bool
1820
}
1921

2022
const (
@@ -23,9 +25,12 @@ const (
2325

2426
//Start the main app
2527
func (m *MouseMover) Start() {
28+
if m.isRunning() {
29+
return
30+
}
2631
m.quit = make(chan struct{})
2732

28-
frequency := 5 //value always in seconds
33+
frequency := 60 //value always in seconds
2934
activityTracker := &tracker.Instance{
3035
Frequency: frequency,
3136
//LogLevel: "debug", //if we want verbose logging
@@ -34,36 +39,47 @@ func (m *MouseMover) Start() {
3439
heartbeatCh := activityTracker.Start()
3540

3641
go func(m *MouseMover) {
37-
m.isRunning = true
42+
m.updateRunningStatus(true)
3843
movePixel := 10
3944
for {
4045
select {
4146
case heartbeat := <-heartbeatCh:
42-
if !heartbeat.IsActivity {
47+
if !heartbeat.WasAnyActivity {
4348
commCh := make(chan bool)
4449
go moveMouse(movePixel, commCh)
4550
select {
4651
case wasMouseMoveSuccess := <-commCh:
4752
if wasMouseMoveSuccess {
48-
fmt.Printf("\nmoving mouse at : %v\n\n", time.Now())
53+
log.Infof("moving mouse at : %v\n\n", time.Now())
4954
movePixel *= -1
5055
}
5156
case <-time.After(timeout * time.Millisecond):
5257
//timeout, do nothing
53-
log.Printf("timeout happened after %vms while trying to move mouse", timeout)
58+
log.Errorf("timeout happened after %vms while trying to move mouse", timeout)
5459
}
5560

5661
}
5762
case <-m.quit:
58-
fmt.Println("stopping mouse mover")
59-
m.isRunning = false
63+
log.Infof("stopping mouse mover")
64+
m.updateRunningStatus(false)
6065
activityTracker.Quit()
6166
return
6267
}
6368
}
6469
}(m)
6570
}
6671

72+
func (m *MouseMover) isRunning() bool {
73+
m.mutex.RLock()
74+
defer m.mutex.RUnlock()
75+
return m.runningStatus
76+
}
77+
func (m *MouseMover) updateRunningStatus(isRunning bool) {
78+
m.mutex.Lock()
79+
defer m.mutex.Unlock()
80+
m.runningStatus = isRunning
81+
}
82+
6783
func moveMouse(movePixel int, commCh chan bool) {
6884
currentX, currentY := robotgo.GetMousePos()
6985
moveToX := currentX + movePixel
@@ -75,7 +91,7 @@ func moveMouse(movePixel int, commCh chan bool) {
7591
//Quit the app
7692
func (m *MouseMover) Quit() {
7793
//making it idempotent
78-
if m != nil && m.isRunning {
94+
if m != nil && m.isRunning() {
7995
m.quit <- struct{}{}
8096
}
8197
}

pkg/mousemover/mouseMover_test.go

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,51 @@
11
package mousemover
22

33
import (
4-
"fmt"
54
"math"
65
"testing"
6+
"time"
77

88
"github.com/go-vgo/robotgo"
99
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/suite"
1011
)
1112

12-
func TestMouseMove(t *testing.T) {
13-
fmt.Println("starting test")
13+
type TestMover struct {
14+
suite.Suite
15+
}
16+
17+
func TestSuite(t *testing.T) {
18+
suite.Run(t, new(TestMover))
19+
}
20+
21+
//Run once before each test
22+
func (suite *TestMover) SetupTest() {
23+
instance = nil
24+
}
25+
26+
func (suite *TestMover) TestSingleton() {
27+
t := suite.T()
28+
mouseMover1 := GetInstance()
29+
mouseMover1.Start()
30+
31+
time.Sleep(time.Millisecond * 500)
32+
33+
mouseMover2 := GetInstance()
34+
assert.True(t, mouseMover2.isRunning(), "instance should not have started")
35+
}
36+
func (suite *TestMover) TestAppStartAndStop() {
37+
t := suite.T()
38+
mouseMover := GetInstance()
39+
mouseMover.Start()
40+
time.Sleep(time.Millisecond * 500) //wait for app to start
41+
assert.True(t, mouseMover.isRunning(), "app should have started")
42+
43+
mouseMover.Quit()
44+
time.Sleep(time.Millisecond * 500) //wait for app to stop
45+
assert.False(t, mouseMover.isRunning(), "app should have stopped")
46+
}
47+
func (suite *TestMover) TestMouseMove() {
48+
t := suite.T()
1449
movePixel := 10
1550
currentX, _ := robotgo.GetMousePos()
1651
commCh := make(chan bool, 1)

0 commit comments

Comments
 (0)