~kyrofa/unity-scope-snappy/integration-tests_previews

« back to all changes in this revision

Viewing changes to scope/preview_generator_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
1
package scope
2
2
 
3
3
import (
 
4
        "launchpad.net/unity-scope-snappy/internal/launchpad.net/go-unityscopes/v2"
4
5
        "launchpad.net/unity-scope-snappy/webdm"
5
6
        "reflect"
6
7
        "testing"
7
8
)
8
9
 
9
 
// Data for TestNewPreview
 
10
// Data for TestNewPreview.
10
11
var newPreviewTests = []struct {
11
 
        status   webdm.Status
12
 
        expected interface{}
 
12
        status    webdm.Status
 
13
        scopeData *ProgressHack
 
14
        expected  interface{}
13
15
}{
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{}},
19
21
}
20
22
 
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}
25
 
 
26
 
                preview, err := NewPreview(snap)
 
27
                metadata := scopes.NewActionMetadata("us", "phone")
 
28
 
 
29
                metadata.SetScopeData(test.scopeData)
 
30
 
 
31
                preview, err := NewPreview(snap, metadata)
27
32
                if err != nil {
28
33
                        t.Errorf("Test case %d: Unexpected error: %s", i, err)
29
34
                }
35
40
                }
36
41
        }
37
42
}
 
43
 
 
44
// Data for TestNewPreview_progressHack.
 
45
var progressHackTests = []struct {
 
46
        status      webdm.Status
 
47
        scopeData   *ProgressHack
 
48
        expected    interface{}
 
49
        expectError bool
 
50
}{
 
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},
 
57
 
 
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},
 
63
 
 
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},
 
69
 
 
70
        // Invalid ProgressHacks
 
71
        {webdm.StatusUndefined, &ProgressHack{webdm.StatusInstalling}, nil, true},
 
72
        {webdm.StatusUndefined, &ProgressHack{webdm.StatusUninstalling}, nil, true},
 
73
 
 
74
        {webdm.StatusNotInstalled, &ProgressHack{webdm.StatusInstalling}, nil, true},
 
75
        {webdm.StatusNotInstalled, &ProgressHack{webdm.StatusUninstalling}, nil, true},
 
76
 
 
77
        {webdm.StatusInstalled, &ProgressHack{webdm.StatusInstalling}, nil, true},
 
78
        {webdm.StatusInstalled, &ProgressHack{webdm.StatusUninstalling}, nil, true},
 
79
 
 
80
        {webdm.StatusInstalling, &ProgressHack{webdm.StatusInstalling}, nil, true},
 
81
        {webdm.StatusInstalling, &ProgressHack{webdm.StatusUninstalling}, nil, true},
 
82
 
 
83
        {webdm.StatusUninstalling, &ProgressHack{webdm.StatusInstalling}, nil, true},
 
84
        {webdm.StatusUninstalling, &ProgressHack{webdm.StatusUninstalling}, nil, true},
 
85
}
 
86
 
 
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")
 
92
 
 
93
                metadata.SetScopeData(test.scopeData)
 
94
 
 
95
                preview, err := NewPreview(snap, metadata)
 
96
                if test.expectError {
 
97
                        if err == nil {
 
98
                                t.Errorf("Test case %d: Expected an error", i)
 
99
                        }
 
100
                } else {
 
101
                        if err != nil {
 
102
                                t.Errorf("Test case %d: Unexpected error: %s", i, err)
 
103
                        }
 
104
 
 
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)
 
109
                        }
 
110
                }
 
111
        }
 
112
}
 
113
 
 
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")
 
119
        }
 
120
}