| 1 |
package desktop |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
// specExample is the example desktop file given in the spec. |
| 9 |
const specExample = ` |
| 10 |
[Desktop Entry] |
| 11 |
Version=1.0 |
| 12 |
Type=Application |
| 13 |
Name=Foo Viewer |
| 14 |
Comment=The best viewer for Foo objects available! |
| 15 |
TryExec=fooview |
| 16 |
Exec=fooview %F |
| 17 |
Icon=fooview |
| 18 |
MimeType=image/x-foo; |
| 19 |
Actions=Gallery;Create; |
| 20 |
|
| 21 |
[Desktop Action Gallery] |
| 22 |
Exec=fooview --gallery |
| 23 |
Name=Browse Gallery |
| 24 |
|
| 25 |
[Desktop Action Create] |
| 26 |
Exec=fooview --create-new |
| 27 |
Name=Create a new Foo! |
| 28 |
Icon=fooview-new |
| 29 |
` |
| 30 |
|
| 31 |
func TestSpecExample(t *testing.T) { |
| 32 |
d, err := New(strings.NewReader(specExample)) |
| 33 |
if err != nil { |
| 34 |
t.Error(err) |
| 35 |
} |
| 36 |
|
| 37 |
if d.Type != Application { |
| 38 |
t.Error("Type") |
| 39 |
} |
| 40 |
|
| 41 |
arr := map[string]string{ |
| 42 |
d.Version: "1.0", |
| 43 |
d.Name: "Foo Viewer", |
| 44 |
d.Comment: "The best viewer for Foo objects available!", |
| 45 |
d.TryExec: "fooview", |
| 46 |
d.Exec: "fooview %F", |
| 47 |
d.Icon: "fooview", |
| 48 |
} |
| 49 |
for act, exp := range arr { |
| 50 |
if act != exp { |
| 51 |
t.Log("expected: " + exp) |
| 52 |
t.Log("actual: " + act) |
| 53 |
t.Fail() |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestActions(t *testing.T) { |
| 59 |
d, err := New(strings.NewReader(specExample)) |
| 60 |
if err != nil { |
| 61 |
t.Error(err) |
| 62 |
} |
| 63 |
|
| 64 |
a := d.Actions |
| 65 |
if a[0].Name != "Browse Gallery" { |
| 66 |
t.Fail() |
| 67 |
} |
| 68 |
if a[0].Exec != "fooview --gallery" { |
| 69 |
t.Fail() |
| 70 |
} |
| 71 |
if a[1].Name != "Create a new Foo!" { |
| 72 |
t.Fail() |
| 73 |
} |
| 74 |
if a[1].Exec != "fooview --create-new" { |
| 75 |
t.Fail() |
| 76 |
} |
| 77 |
if a[1].Icon != "fooview-new" { |
| 78 |
t.Fail() |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
const fullExample = ` |
| 83 |
# This example will use all the keys. |
| 84 |
[Desktop Entry] |
| 85 |
Type=Application |
| 86 |
Version=1.0 |
| 87 |
|
| 88 |
Name=fullExample |
| 89 |
GenericName=Desktop Entry Test |
| 90 |
Comment=This is a test comment. |
| 91 |
Icon=test-icon |
| 92 |
|
| 93 |
NoDisplay=true |
| 94 |
Hidden=true |
| 95 |
OnlyShowIn=Unity;Gnome; |
| 96 |
NotShowIn=KDE;xfce; |
| 97 |
|
| 98 |
DBusActivatable=true |
| 99 |
TryExec=echo |
| 100 |
Exec=echo %F |
| 101 |
Path=/ |
| 102 |
Terminal=true |
| 103 |
|
| 104 |
Actions=NewFile;TacoSalad; |
| 105 |
MimeType=text/plain;text/markdown; |
| 106 |
Categories=Tests;Golang; |
| 107 |
Implements=Interface; |
| 108 |
Keywords=full;test;golang;xdg;desktop; |
| 109 |
|
| 110 |
StartupNotify=true |
| 111 |
StartupWMClass=test |
| 112 |
|
| 113 |
X-Unity-IconBackgroundColor=#000000 |
| 114 |
X-Gnome-Something=foo |
| 115 |
X-KDE-plasma=workspaces |
| 116 |
|
| 117 |
[Desktop Action NewFile] |
| 118 |
Name=New File |
| 119 |
Exec=echo |
| 120 |
Icon=file |
| 121 |
|
| 122 |
[Desktop Action TacoSalad] |
| 123 |
Name=Taco Salad |
| 124 |
Exec=echo Taco Salad |
| 125 |
Icon=taco |
| 126 |
` |
| 127 |
|
| 128 |
func TestPrintIt(t *testing.T) { |
| 129 |
d, err := New(strings.NewReader(fullExample)) |
| 130 |
if err != nil { |
| 131 |
t.Error(err) |
| 132 |
} |
| 133 |
|
| 134 |
t.Logf("%#v", d) |
| 135 |
} |
| 136 |
|
| 137 |
const linkExample = ` |
| 138 |
[Desktop Entry] |
| 139 |
Version=1.0 |
| 140 |
Type=Link |
| 141 |
Name=Go |
| 142 |
Name[en_US]=Go |
| 143 |
URL=http://www.golang.org/ |
| 144 |
` |
| 145 |
|
| 146 |
func TestLinkExample(t *testing.T) { |
| 147 |
d, err := New(strings.NewReader(linkExample)) |
| 148 |
if err != nil { |
| 149 |
t.Error(err) |
| 150 |
} |
| 151 |
|
| 152 |
if d.Type != Link { |
| 153 |
t.Error("Type") |
| 154 |
} |
| 155 |
|
| 156 |
arr := map[string]string{ |
| 157 |
d.Version: "1.0", |
| 158 |
d.Name: "Go", |
| 159 |
d.URL: "http://www.golang.org/", |
| 160 |
} |
| 161 |
for act, exp := range arr { |
| 162 |
if act != exp { |
| 163 |
t.Log("expected: " + exp) |
| 164 |
t.Log("actual: " + act) |
| 165 |
t.Fail() |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func TestEntryValidation(t *testing.T) { |
| 171 |
_, err := New(strings.NewReader("[Desktop Entry]\n")) |
| 172 |
if err != ErrMissingType { |
| 173 |
t.Errorf("expected ErrMissingType, got %v", err) |
| 174 |
} |
| 175 |
|
| 176 |
_, err = New(strings.NewReader("[Desktop Entry]\nType=Application\n")) |
| 177 |
if err != ErrMissingName { |
| 178 |
t.Errorf("expected ErrMissingName, got %v", err) |
| 179 |
} |
| 180 |
|
| 181 |
_, err = New(strings.NewReader("[Desktop Entry]\nType=Link\n")) |
| 182 |
if err != ErrMissingName { |
| 183 |
t.Errorf("expected ErrMissingName, got %v", err) |
| 184 |
} |
| 185 |
|
| 186 |
_, err = New(strings.NewReader("[Desktop Entry]\nType=Directory\n")) |
| 187 |
if err != ErrMissingName { |
| 188 |
t.Errorf("expected ErrMissingName, got %v", err) |
| 189 |
} |
| 190 |
|
| 191 |
_, err = New(strings.NewReader("[Desktop Entry]\nType=Link\nName=No URL\n")) |
| 192 |
if err != ErrMissingURL { |
| 193 |
t.Errorf("expected ErrMissingURL, got %v", err) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func TestInvalidFileFormat(t *testing.T) { |
| 198 |
_, err := New(strings.NewReader("")) |
| 199 |
if err == nil { |
| 200 |
t.Error("err == nil on empty file") |
| 201 |
} |
| 202 |
|
| 203 |
_, err = New(strings.NewReader("hello, world!")) |
| 204 |
if err == nil { |
| 205 |
t.Error("err == nil on invalid file") |
| 206 |
} |
| 207 |
} |