~kyrofa/unity-scope-snappy/update_godbus_testability

« back to all changes in this revision

Viewing changes to scope/uninstalling_preview_test.go

  • Committer: Tarmac
  • Author(s): Kyle Fazzari
  • Date: 2015-06-17 15:09:55 UTC
  • mfrom: (8.4.5 unity-scope-snappy)
  • Revision ID: tarmac-20150617150955-merikzwopj9gtd7c
Add progress placeholder previews for manually refreshing.

Approved by PS Jenkins bot, Xavi Garcia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package scope
 
2
 
 
3
import (
 
4
        "launchpad.net/unity-scope-snappy/internal/launchpad.net/go-unityscopes/v2"
 
5
        "launchpad.net/unity-scope-snappy/webdm"
 
6
        "testing"
 
7
)
 
8
 
 
9
// Test typical NewUninstallingPreview usage.
 
10
func TestNewUninstallingPreview(t *testing.T) {
 
11
        preview, err := NewUninstallingPreview(webdm.Package{
 
12
                Id:     "package1",
 
13
                Status: webdm.StatusInstalled,
 
14
        })
 
15
        if err != nil {
 
16
                t.Error("Unexpected error creating new uninstalling preview: %s", err)
 
17
        }
 
18
 
 
19
        if preview.snap.Id != "package1" {
 
20
                t.Error(`Preview snap's ID is "%s", expected "package1"`, preview.snap.Id)
 
21
        }
 
22
}
 
23
 
 
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})
 
27
        if err == nil {
 
28
                t.Error("Expected an error if the package isn't installed")
 
29
        }
 
30
}
 
31
 
 
32
// Test typical Generate usage.
 
33
func TestUninstallingPreview_generate(t *testing.T) {
 
34
        preview, _ := NewUninstallingPreview(
 
35
                webdm.Package{
 
36
                        Id:           "package1",
 
37
                        Name:         "package1",
 
38
                        Origin:       "foo",
 
39
                        Version:      "0.1",
 
40
                        Vendor:       "bar",
 
41
                        Description:  "baz",
 
42
                        IconUrl:      "http://fake",
 
43
                        Status:       webdm.StatusInstalled,
 
44
                        DownloadSize: 123456,
 
45
                        Type:         "oem",
 
46
                })
 
47
 
 
48
        receiver := new(FakeWidgetReceiver)
 
49
 
 
50
        err := preview.Generate(receiver)
 
51
        if err != nil {
 
52
                t.Errorf("Unexpected error while generating preview: %s", err)
 
53
        }
 
54
 
 
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))
 
58
        }
 
59
 
 
60
        widget := receiver.widgets[0]
 
61
        if widget.WidgetType() == "text" {
 
62
                verifyUninstallingTextWidget(t, widget)
 
63
        } else {
 
64
                t.Error("Expected text to be first widget")
 
65
        }
 
66
 
 
67
        widget = receiver.widgets[1]
 
68
        if widget.WidgetType() == "actions" {
 
69
                verifyUninstallingActionsWidget(t, widget)
 
70
        } else {
 
71
                t.Error("Expected actions to be second widget")
 
72
        }
 
73
}
 
74
 
 
75
// Test that errors are displayed if the snap doesn't successfully uninstall
 
76
func TestUninstallingPreview_generate_uninstallFailed(t *testing.T) {
 
77
        preview, _ := NewUninstallingPreview(
 
78
                webdm.Package{
 
79
                        Id:      "package1",
 
80
                        Name:    "package1",
 
81
                        Status:  webdm.StatusInstalled,
 
82
                        Message: "Unable to uninstall", // This indicates failure
 
83
                })
 
84
 
 
85
        receiver := new(FakeWidgetReceiver)
 
86
 
 
87
        err := preview.Generate(receiver)
 
88
        if err != nil {
 
89
                t.Errorf("Unexpected error while generating preview: %s", err)
 
90
        }
 
91
 
 
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))
 
95
        }
 
96
 
 
97
        widget := receiver.widgets[0]
 
98
        if widget.WidgetType() == "text" {
 
99
                verifyUninstallingFailedTextWidget(t, widget)
 
100
        } else {
 
101
                t.Error("Expected text to be first widget")
 
102
        }
 
103
 
 
104
        widget = receiver.widgets[1]
 
105
        if widget.WidgetType() == "actions" {
 
106
                verifyUninstallingFailedActionsWidget(t, widget)
 
107
        } else {
 
108
                t.Error("Expected actions to be second widget")
 
109
        }
 
110
}
 
111
 
 
112
// Test that Generate fails if the package isn't installed
 
113
func TestUninstallingPreview_generate_notInstalled(t *testing.T) {
 
114
        preview := UninstallingPreview{
 
115
                snap: webdm.Package{
 
116
                        Status: webdm.StatusNotInstalled,
 
117
                },
 
118
        }
 
119
 
 
120
        receiver := new(FakeWidgetReceiver)
 
121
 
 
122
        err := preview.Generate(receiver)
 
123
        if err == nil {
 
124
                t.Error("Expected an error if the package isn't installed")
 
125
        }
 
126
}
 
127
 
 
128
// verifyUninstallingTextWidget is used to verify that a text widget matches
 
129
// what we expect.
 
130
//
 
131
// Parameters:
 
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"]
 
137
        if !ok {
 
138
                // Exit here so we don't dereference nil
 
139
                t.Fatal("Expected text to include a title")
 
140
        }
 
141
 
 
142
        // Verify text presence
 
143
        _, ok = widget["text"]
 
144
        if !ok {
 
145
                // Exit here so we don't dereference nil
 
146
                t.Fatal("Expected text to include actual text")
 
147
        }
 
148
}
 
149
 
 
150
// verifyUninstallingFailedTextWidget is used to verify that a text widget
 
151
// matches what we expect when the uninstall fails.
 
152
//
 
153
// Parameters:
 
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"]
 
159
        if !ok {
 
160
                // Exit here so we don't dereference nil
 
161
                t.Fatal("Expected text to include a title")
 
162
        }
 
163
 
 
164
        // Verify text presence
 
165
        _, ok = widget["text"]
 
166
        if !ok {
 
167
                // Exit here so we don't dereference nil
 
168
                t.Fatal("Expected text to include actual text")
 
169
        }
 
170
}
 
171
 
 
172
// verifyUninstallingActionsWidget is used to verify that an actions widget
 
173
// matches what we expect.
 
174
//
 
175
// Parameters:
 
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"]
 
180
        if !ok {
 
181
                t.Error("Expected actions widget to include actions")
 
182
        }
 
183
 
 
184
        actionsInterfaces := value.([]interface{})
 
185
 
 
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))
 
189
        }
 
190
 
 
191
        // Verify the refresh action
 
192
        action := actionsInterfaces[0].(map[string]interface{})
 
193
        value, ok = action["id"]
 
194
        if !ok {
 
195
                t.Error("Expected refresh action to have an id")
 
196
        }
 
197
        if value != ActionRefreshUninstalling {
 
198
                t.Errorf(`Expected refresh action's ID to be "%d"`, ActionRefreshUninstalling)
 
199
        }
 
200
 
 
201
        value, ok = action["label"]
 
202
        if !ok {
 
203
                t.Error("Expected refresh action to have a label")
 
204
        }
 
205
        if value != "Refresh" {
 
206
                t.Error(`Expected refresh action's label to be "Refresh"`)
 
207
        }
 
208
}
 
209
 
 
210
// verifyUninstallingFailedActionsWidget is used to verify that an actions
 
211
// widget matches what we expect when an uninstall failed.
 
212
//
 
213
// Parameters:
 
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"]
 
218
        if !ok {
 
219
                t.Error("Expected actions widget to include actions")
 
220
        }
 
221
 
 
222
        actionsInterfaces := value.([]interface{})
 
223
 
 
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))
 
227
        }
 
228
 
 
229
        // Verify the okay action
 
230
        action := actionsInterfaces[0].(map[string]interface{})
 
231
        value, ok = action["id"]
 
232
        if !ok {
 
233
                t.Error("Expected okay action to have an id")
 
234
        }
 
235
        if value != ActionOk {
 
236
                t.Errorf(`Expected okay action's ID to be "%d"`, ActionOk)
 
237
        }
 
238
 
 
239
        value, ok = action["label"]
 
240
        if !ok {
 
241
                t.Error("Expected okay action to have a label")
 
242
        }
 
243
        if value != "Okay" {
 
244
                t.Error(`Expected okay action's label to be "Okay"`)
 
245
        }
 
246
}