github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/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 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/usersession/xdgopenproxy" 31 ) 32 33 func Test(t *testing.T) { TestingT(t) } 34 35 type xdgOpenSuite struct{} 36 37 var _ = Suite(&xdgOpenSuite{}) 38 39 func (s *xdgOpenSuite) TestOpenURL(c *C) { 40 launcher := &fakeLauncher{} 41 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "http://example.org"), IsNil) 42 c.Check(launcher.calls, DeepEquals, []string{ 43 "OpenURI http://example.org", 44 }) 45 } 46 47 func (s *xdgOpenSuite) TestOpenFile(c *C) { 48 launcher := &fakeLauncher{} 49 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "/path/test.txt"), IsNil) 50 c.Check(launcher.calls, DeepEquals, []string{ 51 "OpenFile /path/test.txt", 52 }) 53 } 54 55 func (s *xdgOpenSuite) TestOpenFileURL(c *C) { 56 launcher := &fakeLauncher{} 57 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "file:///path/test.txt"), IsNil) 58 c.Check(launcher.calls, DeepEquals, []string{ 59 "OpenFile /path/test.txt", 60 }) 61 } 62 63 func (s *xdgOpenSuite) TestStopOnFirstSuccess(c *C) { 64 l1 := &fakeLauncher{err: fmt.Errorf("failure one")} 65 l2 := &fakeLauncher{err: nil} 66 l3 := &fakeLauncher{err: nil} 67 launchers := []xdgopenproxy.DesktopLauncher{l1, l2, l3} 68 69 err := xdgopenproxy.Launch(nil, launchers, "http://example.org") 70 c.Check(err, IsNil) 71 c.Check(l1.calls, DeepEquals, []string{ 72 "OpenURI http://example.org", 73 }) 74 c.Check(l2.calls, DeepEquals, []string{ 75 "OpenURI http://example.org", 76 }) 77 c.Check(l3.calls, HasLen, 0) 78 } 79 80 func (s *xdgOpenSuite) TestStopOnResponseError(c *C) { 81 l1 := &fakeLauncher{err: fmt.Errorf("failure one")} 82 l2 := &fakeLauncher{err: xdgopenproxy.MakeResponseError("hello")} 83 l3 := &fakeLauncher{err: nil} 84 launchers := []xdgopenproxy.DesktopLauncher{l1, l2, l3} 85 86 err := xdgopenproxy.Launch(nil, launchers, "http://example.org") 87 c.Check(err, Equals, l2.err) 88 c.Check(l3.calls, HasLen, 0) 89 } 90 91 type fakeLauncher struct { 92 err error 93 calls []string 94 } 95 96 func (l *fakeLauncher) OpenFile(bus *dbus.Conn, path string) error { 97 l.calls = append(l.calls, "OpenFile "+path) 98 return l.err 99 } 100 101 func (l *fakeLauncher) OpenURI(bus *dbus.Conn, uri string) error { 102 l.calls = append(l.calls, "OpenURI "+uri) 103 return l.err 104 }