4
"launchpad.net/unity-scope-snappy/internal/launchpad.net/go-unityscopes/v2"
4
5
"launchpad.net/unity-scope-snappy/webdm"
9
// Data for TestNewPreview
10
// Data for TestNewPreview.
10
11
var newPreviewTests = []struct {
13
scopeData *ProgressHack
14
{webdm.StatusUndefined, &PackagePreview{}},
15
{webdm.StatusInstalled, &PackagePreview{}},
16
{webdm.StatusNotInstalled, &PackagePreview{}},
17
{webdm.StatusInstalling, &PackagePreview{}},
18
{webdm.StatusUninstalling, &PackagePreview{}},
16
{webdm.StatusUndefined, nil, &PackagePreview{}},
17
{webdm.StatusInstalled, nil, &PackagePreview{}},
18
{webdm.StatusNotInstalled, nil, &PackagePreview{}},
19
{webdm.StatusInstalling, nil, &PackagePreview{}},
20
{webdm.StatusUninstalling, nil, &PackagePreview{}},
21
23
// Test typical NewPreview usage.
22
24
func TestNewPreview(t *testing.T) {
23
25
for i, test := range newPreviewTests {
24
26
snap := webdm.Package{Status: test.status}
26
preview, err := NewPreview(snap)
27
metadata := scopes.NewActionMetadata("us", "phone")
29
metadata.SetScopeData(test.scopeData)
31
preview, err := NewPreview(snap, metadata)
28
33
t.Errorf("Test case %d: Unexpected error: %s", i, err)
44
// Data for TestNewPreview_progressHack.
45
var progressHackTests = []struct {
47
scopeData *ProgressHack
51
// Valid ProgressHacks
52
{webdm.StatusUndefined, &ProgressHack{webdm.StatusUndefined}, &PackagePreview{}, false},
53
{webdm.StatusNotInstalled, &ProgressHack{webdm.StatusUndefined}, &PackagePreview{}, false},
54
{webdm.StatusInstalled, &ProgressHack{webdm.StatusUndefined}, &PackagePreview{}, false},
55
{webdm.StatusInstalling, &ProgressHack{webdm.StatusUndefined}, &PackagePreview{}, false},
56
{webdm.StatusUninstalling, &ProgressHack{webdm.StatusUndefined}, &PackagePreview{}, false},
58
{webdm.StatusUndefined, &ProgressHack{webdm.StatusInstalled}, &InstallingPreview{}, false},
59
{webdm.StatusNotInstalled, &ProgressHack{webdm.StatusInstalled}, &InstallingPreview{}, false},
60
{webdm.StatusInstalled, &ProgressHack{webdm.StatusInstalled}, &PackagePreview{}, false},
61
{webdm.StatusInstalling, &ProgressHack{webdm.StatusInstalled}, &InstallingPreview{}, false},
62
{webdm.StatusUninstalling, &ProgressHack{webdm.StatusInstalled}, &InstallingPreview{}, false},
64
{webdm.StatusUndefined, &ProgressHack{webdm.StatusNotInstalled}, &UninstallingPreview{}, false},
65
{webdm.StatusNotInstalled, &ProgressHack{webdm.StatusNotInstalled}, &PackagePreview{}, false},
66
{webdm.StatusInstalled, &ProgressHack{webdm.StatusNotInstalled}, &UninstallingPreview{}, false},
67
{webdm.StatusInstalling, &ProgressHack{webdm.StatusNotInstalled}, &UninstallingPreview{}, false},
68
{webdm.StatusUninstalling, &ProgressHack{webdm.StatusNotInstalled}, &UninstallingPreview{}, false},
70
// Invalid ProgressHacks
71
{webdm.StatusUndefined, &ProgressHack{webdm.StatusInstalling}, nil, true},
72
{webdm.StatusUndefined, &ProgressHack{webdm.StatusUninstalling}, nil, true},
74
{webdm.StatusNotInstalled, &ProgressHack{webdm.StatusInstalling}, nil, true},
75
{webdm.StatusNotInstalled, &ProgressHack{webdm.StatusUninstalling}, nil, true},
77
{webdm.StatusInstalled, &ProgressHack{webdm.StatusInstalling}, nil, true},
78
{webdm.StatusInstalled, &ProgressHack{webdm.StatusUninstalling}, nil, true},
80
{webdm.StatusInstalling, &ProgressHack{webdm.StatusInstalling}, nil, true},
81
{webdm.StatusInstalling, &ProgressHack{webdm.StatusUninstalling}, nil, true},
83
{webdm.StatusUninstalling, &ProgressHack{webdm.StatusInstalling}, nil, true},
84
{webdm.StatusUninstalling, &ProgressHack{webdm.StatusUninstalling}, nil, true},
87
// Test typical NewPreview usage with temporary progress-hack related stuff.
88
func TestNewPreview_progressHack(t *testing.T) {
89
for i, test := range progressHackTests {
90
snap := webdm.Package{Status: test.status}
91
metadata := scopes.NewActionMetadata("us", "phone")
93
metadata.SetScopeData(test.scopeData)
95
preview, err := NewPreview(snap, metadata)
98
t.Errorf("Test case %d: Expected an error", i)
102
t.Errorf("Test case %d: Unexpected error: %s", i, err)
105
previewType := reflect.TypeOf(preview)
106
expectedType := reflect.TypeOf(test.expected)
107
if previewType != expectedType {
108
t.Errorf(`Test case %d: Preview type was "%s", expected "%s"`, i, previewType, expectedType)
114
// Test that ProgressHack cannot be retrieved if it's nil
115
func TestRetrieveProgressHack(t *testing.T) {
116
metadata := scopes.NewActionMetadata("us", "phone")
117
if retrieveProgressHack(metadata, nil) {
118
t.Error("Expected retrieval to fail due to nil ProgressHack")