4
"launchpad.net/unity-scope-snappy/internal/launchpad.net/go-unityscopes/v2"
5
"launchpad.net/unity-scope-snappy/webdm"
9
// Test typical NewUninstallingPreview usage.
10
func TestNewUninstallingPreview(t *testing.T) {
11
preview, err := NewUninstallingPreview(webdm.Package{
13
Status: webdm.StatusInstalled,
16
t.Error("Unexpected error creating new uninstalling preview: %s", err)
19
if preview.snap.Id != "package1" {
20
t.Error(`Preview snap's ID is "%s", expected "package1"`, preview.snap.Id)
24
// Make sure an error occurs if the package isn't installed
25
func TestNewUninstallingPreview_notInstalled(t *testing.T) {
26
_, err := NewUninstallingPreview(webdm.Package{Status: webdm.StatusNotInstalled})
28
t.Error("Expected an error if the package isn't installed")
32
// Test typical Generate usage.
33
func TestUninstallingPreview_generate(t *testing.T) {
34
preview, _ := NewUninstallingPreview(
42
IconUrl: "http://fake",
43
Status: webdm.StatusInstalled,
48
receiver := new(FakeWidgetReceiver)
50
err := preview.Generate(receiver)
52
t.Errorf("Unexpected error while generating preview: %s", err)
55
if len(receiver.widgets) != 2 {
56
// Exit here so we don't index out of bounds later
57
t.Fatalf("Got %d widgets, expected 2", len(receiver.widgets))
60
widget := receiver.widgets[0]
61
if widget.WidgetType() == "text" {
62
verifyUninstallingTextWidget(t, widget)
64
t.Error("Expected text to be first widget")
67
widget = receiver.widgets[1]
68
if widget.WidgetType() == "actions" {
69
verifyUninstallingActionsWidget(t, widget)
71
t.Error("Expected actions to be second widget")
75
// Test that errors are displayed if the snap doesn't successfully uninstall
76
func TestUninstallingPreview_generate_uninstallFailed(t *testing.T) {
77
preview, _ := NewUninstallingPreview(
81
Status: webdm.StatusInstalled,
82
Message: "Unable to uninstall", // This indicates failure
85
receiver := new(FakeWidgetReceiver)
87
err := preview.Generate(receiver)
89
t.Errorf("Unexpected error while generating preview: %s", err)
92
if len(receiver.widgets) != 2 {
93
// Exit here so we don't index out of bounds later
94
t.Fatalf("Got %d widgets, expected 2", len(receiver.widgets))
97
widget := receiver.widgets[0]
98
if widget.WidgetType() == "text" {
99
verifyUninstallingFailedTextWidget(t, widget)
101
t.Error("Expected text to be first widget")
104
widget = receiver.widgets[1]
105
if widget.WidgetType() == "actions" {
106
verifyUninstallingFailedActionsWidget(t, widget)
108
t.Error("Expected actions to be second widget")
112
// Test that Generate fails if the package isn't installed
113
func TestUninstallingPreview_generate_notInstalled(t *testing.T) {
114
preview := UninstallingPreview{
116
Status: webdm.StatusNotInstalled,
120
receiver := new(FakeWidgetReceiver)
122
err := preview.Generate(receiver)
124
t.Error("Expected an error if the package isn't installed")
128
// verifyUninstallingTextWidget is used to verify that a text widget matches
132
// t: Testing handle for when errors occur.
133
// widget: Text widget to verify.
134
func verifyUninstallingTextWidget(t *testing.T, widget scopes.PreviewWidget) {
135
// Verify title presence
136
_, ok := widget["title"]
138
// Exit here so we don't dereference nil
139
t.Fatal("Expected text to include a title")
142
// Verify text presence
143
_, ok = widget["text"]
145
// Exit here so we don't dereference nil
146
t.Fatal("Expected text to include actual text")
150
// verifyUninstallingFailedTextWidget is used to verify that a text widget
151
// matches what we expect when the uninstall fails.
154
// t: Testing handle for when errors occur.
155
// widget: Text widget to verify.
156
func verifyUninstallingFailedTextWidget(t *testing.T, widget scopes.PreviewWidget) {
157
// Verify title presence
158
_, ok := widget["title"]
160
// Exit here so we don't dereference nil
161
t.Fatal("Expected text to include a title")
164
// Verify text presence
165
_, ok = widget["text"]
167
// Exit here so we don't dereference nil
168
t.Fatal("Expected text to include actual text")
172
// verifyUninstallingActionsWidget is used to verify that an actions widget
173
// matches what we expect.
176
// t: Testing handle for when errors occur.
177
// widget: Actions widget to verify.
178
func verifyUninstallingActionsWidget(t *testing.T, widget scopes.PreviewWidget) {
179
value, ok := widget["actions"]
181
t.Error("Expected actions widget to include actions")
184
actionsInterfaces := value.([]interface{})
186
if len(actionsInterfaces) != 1 {
187
// Exit here so we don't index out of bounds
188
t.Fatalf("Actions widget has %d actions, expected 1", len(actionsInterfaces))
191
// Verify the refresh action
192
action := actionsInterfaces[0].(map[string]interface{})
193
value, ok = action["id"]
195
t.Error("Expected refresh action to have an id")
197
if value != ActionRefreshUninstalling {
198
t.Errorf(`Expected refresh action's ID to be "%d"`, ActionRefreshUninstalling)
201
value, ok = action["label"]
203
t.Error("Expected refresh action to have a label")
205
if value != "Refresh" {
206
t.Error(`Expected refresh action's label to be "Refresh"`)
210
// verifyUninstallingFailedActionsWidget is used to verify that an actions
211
// widget matches what we expect when an uninstall failed.
214
// t: Testing handle for when errors occur.
215
// widget: Actions widget to verify.
216
func verifyUninstallingFailedActionsWidget(t *testing.T, widget scopes.PreviewWidget) {
217
value, ok := widget["actions"]
219
t.Error("Expected actions widget to include actions")
222
actionsInterfaces := value.([]interface{})
224
if len(actionsInterfaces) != 1 {
225
// Exit here so we don't index out of bounds
226
t.Fatalf("Actions widget has %d actions, expected 1", len(actionsInterfaces))
229
// Verify the okay action
230
action := actionsInterfaces[0].(map[string]interface{})
231
value, ok = action["id"]
233
t.Error("Expected okay action to have an id")
235
if value != ActionOk {
236
t.Errorf(`Expected okay action's ID to be "%d"`, ActionOk)
239
value, ok = action["label"]
241
t.Error("Expected okay action to have a label")
244
t.Error(`Expected okay action's label to be "Okay"`)