diff options
| author | Pawel Stolowski <stolowski@gmail.com> | 2018-02-23 12:14:43 +0100 |
|---|---|---|
| committer | Pawel Stolowski <stolowski@gmail.com> | 2018-02-23 12:14:43 +0100 |
| commit | 24834b6382cb45d9301848f006aea7f9593a013b (patch) | |
| tree | 382a28f61e04cf40e1f260fdd5f66ad70f7d9672 | |
| parent | a07f493cf34d4a19c49e31a5a48f1498d388ac9b (diff) | |
Test both zenity and kdialog calls and mock kdialog too to make sure we don't invoke it on KDE desktop.mock-kdialog
| -rw-r--r-- | userd/settings_test.go | 65 | ||||
| -rw-r--r-- | userd/ui/ui.go | 28 |
2 files changed, 84 insertions, 9 deletions
diff --git a/userd/settings_test.go b/userd/settings_test.go index 51eca51721..e0d8b525e8 100644 --- a/userd/settings_test.go +++ b/userd/settings_test.go @@ -30,6 +30,7 @@ import ( "github.com/snapcore/snapd/dirs" "github.com/snapcore/snapd/testutil" "github.com/snapcore/snapd/userd" + "github.com/snapcore/snapd/userd/ui" ) type settingsSuite struct { @@ -69,6 +70,15 @@ func (s *settingsSuite) TearDownTest(c *C) { s.restoreSnapFromSender() } +func mockUIcommands(c *C, script string) func() { + mockZenity := testutil.MockCommand(c, "zenity", script) + mockKDialog := testutil.MockCommand(c, "kdialog", script) + return func() { + mockZenity.Restore() + mockKDialog.Restore() + } +} + func (s *settingsSuite) TestGetUnhappy(c *C) { for _, t := range []struct { setting string @@ -123,10 +133,7 @@ func (s *settingsSuite) TestSetInvalidSetting(c *C) { c.Assert(s.mockXdgSettings.Calls(), IsNil) } -func (s *settingsSuite) TestSetUserDeclined(c *C) { - mockZenity := testutil.MockCommand(c, "zenity", "false") - defer mockZenity.Restore() - +func (s *settingsSuite) testSetUserDeclined(c *C) { df := filepath.Join(dirs.SnapDesktopFilesDir, "some-snap_bar.desktop") err := os.MkdirAll(filepath.Dir(df), 0755) c.Assert(err, IsNil) @@ -144,10 +151,31 @@ func (s *settingsSuite) TestSetUserDeclined(c *C) { */ } -func (s *settingsSuite) TestSetUserAccepts(c *C) { - mockZenity := testutil.MockCommand(c, "zenity", "true") - defer mockZenity.Restore() +func (s *settingsSuite) TestSetUserDeclinedKDialog(c *C) { + // force zenity exec missing + restoreZenity := ui.MockHasZenityExecutable(func() bool { return false }) + restoreCmds := mockUIcommands(c, "false") + defer func() { + restoreZenity() + restoreCmds() + }() + + s.testSetUserDeclined(c) +} + +func (s *settingsSuite) TestSetUserDeclinedZenity(c *C) { + // force kdialog exec missing + restoreKDialog := ui.MockHasKDialogExecutable(func() bool { return false }) + restoreCmds := mockUIcommands(c, "false") + defer func() { + restoreKDialog() + restoreCmds() + }() + s.testSetUserDeclined(c) +} + +func (s *settingsSuite) testSetUserAccepts(c *C) { df := filepath.Join(dirs.SnapDesktopFilesDir, "some-snap_foo.desktop") err := os.MkdirAll(filepath.Dir(df), 0755) c.Assert(err, IsNil) @@ -165,5 +193,28 @@ func (s *settingsSuite) TestSetUserAccepts(c *C) { {"zenity", "--question", "--text=Allow changing setting \"default-web-browser\" to \"foo.desktop\" ?"}, }) */ +} + +func (s *settingsSuite) TestSetUserAcceptsZenity(c *C) { + // force kdialog exec missing + restoreKDialog := ui.MockHasKDialogExecutable(func() bool { return false }) + restoreCmds := mockUIcommands(c, "true") + defer func() { + restoreKDialog() + restoreCmds() + }() + + s.testSetUserAccepts(c) +} + +func (s *settingsSuite) TestSetUserAcceptsKDialog(c *C) { + // force zenity exec missing + restoreZenity := ui.MockHasZenityExecutable(func() bool { return false }) + restoreCmds := mockUIcommands(c, "true") + defer func() { + restoreZenity() + restoreCmds() + }() + s.testSetUserAccepts(c) } diff --git a/userd/ui/ui.go b/userd/ui/ui.go index c683814faa..5e9cf95fb3 100644 --- a/userd/ui/ui.go +++ b/userd/ui/ui.go @@ -46,11 +46,35 @@ type DialogOptions struct { Timeout time.Duration } +var hasZenityExecutable = func() bool { + return osutil.ExecutableExists("zenity") +} + +var hasKDialogExecutable = func() bool { + return osutil.ExecutableExists("kdialog") +} + +func MockHasZenityExecutable(f func() bool) func() { + oldHasZenityExecutable := hasZenityExecutable + hasZenityExecutable := f + return func() { + hasZenityExecutable = oldHasZenityExecutable + } +} + +func MockHasKDialogExecutable(f func() bool) func() { + oldHasKDialogExecutable := hasKDialogExecutable + hasKDialogExecutable := f + return func() { + hasKDialogExecutable = oldHasKDialogExecutable + } +} + // New returns the best matching UI interface for the given system // or an error if no ui can be created. func New() (UI, error) { - hasZenity := osutil.ExecutableExists("zenity") - hasKDialog := osutil.ExecutableExists("kdialog") + hasZenity := hasZenityExecutable() + hasKDialog := hasKDialogExecutable() switch { case hasZenity && hasKDialog: |
