From 4556585533e084482d77cb862c873c27e2767e12 Mon Sep 17 00:00:00 2001 From: "Fausto M. Lagos S" Date: Tue, 28 Jun 2022 15:03:37 -0500 Subject: [PATCH] #38 - Add test for click handler --- gui/key_details.go | 24 +++++++++++++++++++---- gui/key_details_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ gui/key_list.go | 9 ++++----- gui/key_list_test.go | 31 +++++++++++++++++++++++++---- gui/main_window.go | 5 ++--- 5 files changed, 96 insertions(+), 16 deletions(-) diff --git a/gui/key_details.go b/gui/key_details.go index 6390be7..865537d 100644 --- a/gui/key_details.go +++ b/gui/key_details.go @@ -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) } diff --git a/gui/key_details_test.go b/gui/key_details_test.go index 194757a..f5ededc 100644 --- a/gui/key_details_test.go +++ b/gui/key_details_test.go @@ -1 +1,44 @@ package gui + +import ( + "github.com/coyim/gotk3adapter/gtki" + "github.com/coyim/gotk3mocks/gtk" +) + +func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThePublicKeyPath() { + keyDetailsBoxMock := >k.MockBox{} + builder := s.setupBuildingOfObject(keyDetailsBoxMock, "KeyDetails") + + keyDetailsHolder := >k.MockBox{} + keyDetailsHolder.On("Add", keyDetailsBoxMock).Return().Once() + keyDetailsHolder.On("GetChildren").Return(nil).Once() + + publicKeyPathLabel := >k.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 := >k.MockBox{} + child1 := >k.MockWidget{} + child2 := >k.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()) +} diff --git a/gui/key_list.go b/gui/key_list.go index 4eb6d12..dc75a04 100644 --- a/gui/key_list.go +++ b/gui/key_list.go @@ -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) } diff --git a/gui/key_list_test.go b/gui/key_list_test.go index ba2258c..7f274ea 100644 --- a/gui/key_list_test.go +++ b/gui/key_list_test.go @@ -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 := >k.MockBox{} + actualGtkBox := u.createKeyEntryBoxFrom(keyEntry, detailsBoxMock) s.Equal(box, actualGtkBox) keyEntry.AssertExpectations(s.T()) + + keyDetailsBoxMock := >k.MockBox{} + builder := s.setupBuildingOfObject(keyDetailsBoxMock, "KeyDetails") + detailsBoxMock.On("Add", keyDetailsBoxMock).Return().Once() + detailsBoxMock.On("GetChildren").Return(nil).Once() + publicKeyPathLabel := >k.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 { @@ -70,7 +90,6 @@ func (s *guiSuite) setupBuildingOfKeyEntry(path string) *gtk.MockButton { label := >k.MockLabel{} label.On("SetLabel", path).Return().Once() box := >k.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) @@ -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 := >k.MockBox{} box.On("Add", box1).Return().Once() box.On("Add", box2).Return().Once() @@ -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) @@ -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) diff --git a/gui/main_window.go b/gui/main_window.go index eb2eba8..d5f2224 100644 --- a/gui/main_window.go +++ b/gui/main_window.go @@ -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) {