github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/usersession/xdgopenproxy/xdgopenproxy_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package xdgopenproxy_test 21 22 import ( 23 "fmt" 24 "testing" 25 26 "github.com/godbus/dbus" 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/usersession/xdgopenproxy" 30 ) 31 32 func Test(t *testing.T) { TestingT(t) } 33 34 type xdgOpenSuite struct{} 35 36 var _ = Suite(&xdgOpenSuite{}) 37 38 func (s *xdgOpenSuite) TestOpenURL(c *C) { 39 launcher := &fakeLauncher{} 40 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "http://example.org"), IsNil) 41 c.Check(launcher.calls, DeepEquals, []string{ 42 "OpenURI http://example.org", 43 }) 44 } 45 46 func (s *xdgOpenSuite) TestOpenFile(c *C) { 47 launcher := &fakeLauncher{} 48 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "/path/test.txt"), IsNil) 49 c.Check(launcher.calls, DeepEquals, []string{ 50 "OpenFile /path/test.txt", 51 }) 52 } 53 54 func (s *xdgOpenSuite) TestOpenFileURL(c *C) { 55 launcher := &fakeLauncher{} 56 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "file:///path/test.txt"), IsNil) 57 c.Check(launcher.calls, DeepEquals, []string{ 58 "OpenFile /path/test.txt", 59 }) 60 } 61 62 func (s *xdgOpenSuite) TestStopOnFirstSuccess(c *C) { 63 l1 := &fakeLauncher{err: fmt.Errorf("failure one")} 64 l2 := &fakeLauncher{err: nil} 65 l3 := &fakeLauncher{err: nil} 66 launchers := []xdgopenproxy.DesktopLauncher{l1, l2, l3} 67 68 err := xdgopenproxy.Launch(nil, launchers, "http://example.org") 69 c.Check(err, IsNil) 70 c.Check(l1.calls, DeepEquals, []string{ 71 "OpenURI http://example.org", 72 }) 73 c.Check(l2.calls, DeepEquals, []string{ 74 "OpenURI http://example.org", 75 }) 76 c.Check(l3.calls, HasLen, 0) 77 } 78 79 func (s *xdgOpenSuite) TestStopOnResponseError(c *C) { 80 l1 := &fakeLauncher{err: fmt.Errorf("failure one")} 81 l2 := &fakeLauncher{err: xdgopenproxy.MakeResponseError("hello")} 82 l3 := &fakeLauncher{err: nil} 83 launchers := []xdgopenproxy.DesktopLauncher{l1, l2, l3} 84 85 err := xdgopenproxy.Launch(nil, launchers, "http://example.org") 86 c.Check(err, Equals, l2.err) 87 c.Check(l3.calls, HasLen, 0) 88 } 89 90 type fakeLauncher struct { 91 err error 92 calls []string 93 } 94 95 func (l *fakeLauncher) OpenFile(bus *dbus.Conn, path string) error { 96 l.calls = append(l.calls, "OpenFile "+path) 97 return l.err 98 } 99 100 func (l *fakeLauncher) OpenURI(bus *dbus.Conn, uri string) error { 101 l.calls = append(l.calls, "OpenURI "+uri) 102 return l.err 103 }