Skip to content

Commit

Permalink
#38 - Add test for click handler
Browse files Browse the repository at this point in the history
  • Loading branch information
piratax007 committed Jun 28, 2022
1 parent 44d80b2 commit 4556585
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 16 deletions.
24 changes: 20 additions & 4 deletions gui/key_details.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package gui

import "github.com/coyim/gotk3adapter/gtki"
import (
"github.com/coyim/gotk3adapter/gtki"
"github.com/digitalautonomy/keymirror/api"
)

func (u *ui) populateKeyDetails(box gtki.Box) {
b, _ := buildObjectFrom[gtki.Box](u, "KeyDetails")
box.Add(b)
type clearable[T any] interface {
GetChildren() []T
Remove(T)
}

func clearAllChildrenOf[T any](b clearable[T]) {
for _, c := range b.GetChildren() {
b.Remove(c)
}
}

func (u *ui) populateKeyDetails(key api.KeyEntry, into gtki.Box) {
b, builder := buildObjectFrom[gtki.Box](u, "KeyDetails")
builder.get("publicKeyPath").(gtki.Label).SetLabel(key.PublicKeyLocations()[0])
clearAllChildrenOf[gtki.Widget](into)
into.Add(b)
}
43 changes: 43 additions & 0 deletions gui/key_details_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
package gui

import (
"github.com/coyim/gotk3adapter/gtki"
"github.com/coyim/gotk3mocks/gtk"
)

func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThePublicKeyPath() {
keyDetailsBoxMock := &gtk.MockBox{}
builder := s.setupBuildingOfObject(keyDetailsBoxMock, "KeyDetails")

keyDetailsHolder := &gtk.MockBox{}
keyDetailsHolder.On("Add", keyDetailsBoxMock).Return().Once()
keyDetailsHolder.On("GetChildren").Return(nil).Once()

publicKeyPathLabel := &gtk.MockLabel{}
builder.On("GetObject", "publicKeyPath").Return(publicKeyPathLabel, nil).Once()

keMock := &keyEntryMock{}
keMock.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()

publicKeyPathLabel.On("SetLabel", "/a/path/to/a/public/key").Return().Once()

u := &ui{gtk: s.gtkMock}
u.populateKeyDetails(keMock, keyDetailsHolder)

keyDetailsHolder.AssertExpectations(s.T())
keMock.AssertExpectations(s.T())
publicKeyPathLabel.AssertExpectations(s.T())
}

func (s *guiSuite) Test_clearAllChildrenOf_removeEachOneOfTheChildrenOfTheBox() {
boxMock := &gtk.MockBox{}
child1 := &gtk.MockWidget{}
child2 := &gtk.MockWidget{}

boxMock.On("GetChildren").Return([]gtki.Widget{child1, child2}).Once()
boxMock.On("Remove", child1).Return().Once()
boxMock.On("Remove", child2).Return().Once()

clearAllChildrenOf[gtki.Widget](boxMock)

boxMock.AssertExpectations(s.T())
}
9 changes: 4 additions & 5 deletions gui/key_list.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package gui

import (
"fmt"
"github.com/coyim/gotk3adapter/gtki"
"github.com/digitalautonomy/keymirror/api"
"github.com/digitalautonomy/keymirror/i18n"
)

func (u *ui) createKeyEntryBoxFrom(entry api.KeyEntry) gtki.Widget {
func (u *ui) createKeyEntryBoxFrom(entry api.KeyEntry, detailsBox gtki.Box) gtki.Widget {
b, builder := buildObjectFrom[gtki.Button](u, "KeyListEntry")
builder.get("keyListEntryLabel").(gtki.Label).SetLabel(entry.Locations()[0])
b.Connect("clicked", func() {
fmt.Println("======= OK HANDLER ========")
u.populateKeyDetails(entry, detailsBox)
})
return b
}

func (u *ui) populateListWithKeyEntries(access api.KeyAccess, box gtki.Box, onNoKeys func(box gtki.Box)) {
func (u *ui) populateListWithKeyEntries(access api.KeyAccess, box gtki.Box, detailsBox gtki.Box, onNoKeys func(box gtki.Box)) {
for _, e := range access.AllKeys() {
onNoKeys = func(box gtki.Box) {}
box.Add(u.createKeyEntryBoxFrom(e))
box.Add(u.createKeyEntryBoxFrom(e, detailsBox))
}
onNoKeys(box)
}
Expand Down
31 changes: 27 additions & 4 deletions gui/key_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,31 @@ func (s *guiSuite) Test_createKeyEntryBoxFrom_CreatesAGTKIBoxWithTheGivenASSHKey
keyEntry := &keyEntryMock{}
keyEntry.On("Locations").Return([]string{"/home/amnesia/id_rsa.pub"}).Once()

actualGtkBox := u.createKeyEntryBoxFrom(keyEntry)
var clickedHandler func() = nil
box.On("Connect", "clicked", mock.Anything).Return(nil).Once().Run(func(a mock.Arguments) {
clickedHandler = a.Get(1).(func())
})

detailsBoxMock := &gtk.MockBox{}
actualGtkBox := u.createKeyEntryBoxFrom(keyEntry, detailsBoxMock)

s.Equal(box, actualGtkBox)

keyEntry.AssertExpectations(s.T())

keyDetailsBoxMock := &gtk.MockBox{}
builder := s.setupBuildingOfObject(keyDetailsBoxMock, "KeyDetails")
detailsBoxMock.On("Add", keyDetailsBoxMock).Return().Once()
detailsBoxMock.On("GetChildren").Return(nil).Once()
publicKeyPathLabel := &gtk.MockLabel{}
builder.On("GetObject", "publicKeyPath").Return(publicKeyPathLabel, nil).Once()
keyEntry.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()
publicKeyPathLabel.On("SetLabel", "/a/path/to/a/public/key").Return().Once()

clickedHandler()

keyEntry.AssertExpectations(s.T())
detailsBoxMock.AssertExpectations(s.T())
}

type keyAccessMock struct {
Expand Down Expand Up @@ -70,7 +90,6 @@ func (s *guiSuite) setupBuildingOfKeyEntry(path string) *gtk.MockButton {
label := &gtk.MockLabel{}
label.On("SetLabel", path).Return().Once()
box := &gtk.MockButton{}
box.On("Connect", "clicked", mock.Anything).Return(nil).Once()
b := s.setupBuildingOfObject(box, "KeyListEntry")
b.On("GetObject", "keyListEntryLabel").Return(label, nil).Once()
s.addObjectToAssert(box)
Expand All @@ -88,6 +107,10 @@ func (s *guiSuite) Test_populateListWithKeyEntries_IfThereAreKeyEntriesAddsThemI
box2 := s.setupBuildingOfKeyEntry("/home/amnesia/.ssh/id_ed25519")
box3 := s.setupBuildingOfKeyEntry("/home/amnesia/.ssh/id_dsa")

box1.On("Connect", "clicked", mock.Anything).Return(nil).Once()
box2.On("Connect", "clicked", mock.Anything).Return(nil).Once()
box3.On("Connect", "clicked", mock.Anything).Return(nil).Once()

box := &gtk.MockBox{}
box.On("Add", box1).Return().Once()
box.On("Add", box2).Return().Once()
Expand All @@ -98,7 +121,7 @@ func (s *guiSuite) Test_populateListWithKeyEntries_IfThereAreKeyEntriesAddsThemI
called := false
onNoKeys := func(box gtki.Box) { called = true }

u.populateListWithKeyEntries(ka, box, onNoKeys)
u.populateListWithKeyEntries(ka, box, nil, onNoKeys)

box.AssertExpectations(s.T())
s.False(called)
Expand All @@ -114,7 +137,7 @@ func (s *guiSuite) Test_populateListWithKeyEntries_IfThereAreNoKeyEntriesExecute
called := false
onNoKeys := func(box gtki.Box) { called = true }

u.populateListWithKeyEntries(ka, box, onNoKeys)
u.populateListWithKeyEntries(ka, box, nil, onNoKeys)

box.AssertExpectations(s.T())
s.True(called)
Expand Down
5 changes: 2 additions & 3 deletions gui/main_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func (a *application) createMainWindow(app gtki.Application) gtki.Window {
return w
}

func (a *application) populateMainWindow(listBox, detailBox gtki.Box) {
a.ui.populateListWithKeyEntries(a.keys, listBox, a.ui.showNoAvailableKeysMessage)
a.ui.populateKeyDetails(detailBox)
func (a *application) populateMainWindow(listBox, detailsBox gtki.Box) {
a.ui.populateListWithKeyEntries(a.keys, listBox, detailsBox, a.ui.showNoAvailableKeysMessage)
}

func (a *application) activate(app gtki.Application) {
Expand Down

0 comments on commit 4556585

Please sign in to comment.