~dobey/go-unityscopes/fix-typo

« back to all changes in this revision

Viewing changes to tests/goscope/goscope.go

  • Committer: Xavi Garcia
  • Date: 2015-03-19 13:33:42 UTC
  • mfrom: (54.1.19 v1)
  • Revision ID: xavi.garcia.mena@canonical.com-20150319133342-kxg0jgf773se14fr
merged branch lp:~xavi-garcia-mena/go-unityscopes/unit-tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "launchpad.net/go-unityscopes/v1"
 
5
        "log"
 
6
)
 
7
 
 
8
const searchCategoryTemplate = `{
 
9
  "schema-version": 1,
 
10
  "template": {
 
11
    "category-layout": "grid",
 
12
    "card-size": "small"
 
13
  },
 
14
  "components": {
 
15
    "title": "title",
 
16
    "art":  "art",
 
17
    "subtitle": "username"
 
18
  }
 
19
}`
 
20
 
 
21
// SCOPE ***********************************************************************
 
22
 
 
23
var scope_interface scopes.Scope
 
24
 
 
25
type MyScope struct {
 
26
        base *scopes.ScopeBase
 
27
}
 
28
 
 
29
func (s *MyScope) Preview(result *scopes.Result, metadata *scopes.ActionMetadata, reply *scopes.PreviewReply, cancelled <-chan bool) error {
 
30
        layout1col := scopes.NewColumnLayout(1)
 
31
        layout2col := scopes.NewColumnLayout(2)
 
32
        layout3col := scopes.NewColumnLayout(3)
 
33
 
 
34
        // Single column layout
 
35
        layout1col.AddColumn([]string{"image", "header", "summary", "actions"})
 
36
 
 
37
        // Two column layout
 
38
        layout2col.AddColumn([]string{"image"})
 
39
        layout2col.AddColumn([]string{"header", "summary", "actions"})
 
40
 
 
41
        // Three cokumn layout
 
42
        layout3col.AddColumn([]string{"image"})
 
43
        layout3col.AddColumn([]string{"header", "summary", "actions"})
 
44
        layout3col.AddColumn([]string{})
 
45
 
 
46
        // Register the layouts we just created
 
47
        reply.RegisterLayout([]*scopes.ColumnLayout{layout1col, layout2col, layout3col})
 
48
 
 
49
        header := scopes.NewPreviewWidget("header", "header")
 
50
 
 
51
        // It has title and a subtitle properties
 
52
        header.AddAttributeMapping("title", "title")
 
53
        header.AddAttributeMapping("subtitle", "subtitle")
 
54
 
 
55
        // Define the image section
 
56
        image := scopes.NewPreviewWidget("image", "image")
 
57
        // It has a single source property, mapped to the result's art property
 
58
        image.AddAttributeMapping("source", "art")
 
59
 
 
60
        // Define the summary section
 
61
        description := scopes.NewPreviewWidget("summary", "text")
 
62
        // It has a text property, mapped to the result's description property
 
63
        description.AddAttributeMapping("text", "description")
 
64
 
 
65
        // build variant map.
 
66
        tuple1 := make(map[string]interface{})
 
67
        tuple1["id"] = "open"
 
68
        tuple1["label"] = "Open"
 
69
        tuple1["uri"] = "application:///tmp/non-existent.desktop"
 
70
 
 
71
        tuple2 := make(map[string]interface{})
 
72
        tuple1["id"] = "download"
 
73
        tuple1["label"] = "Download"
 
74
 
 
75
        tuple3 := make(map[string]interface{})
 
76
        tuple1["id"] = "hide"
 
77
        tuple1["label"] = "Hide"
 
78
 
 
79
        actions := scopes.NewPreviewWidget("actions", "actions")
 
80
        actions.AddAttributeValue("actions", []interface{}{tuple1, tuple2, tuple3})
 
81
 
 
82
        var scope_data string
 
83
        metadata.ScopeData(scope_data)
 
84
        if len(scope_data) > 0 {
 
85
                extra := scopes.NewPreviewWidget("extra", "text")
 
86
                extra.AddAttributeValue("text", "test Text")
 
87
                reply.PushWidgets(header, image, description, actions, extra)
 
88
        } else {
 
89
                reply.PushWidgets(header, image, description, actions)
 
90
        }
 
91
 
 
92
        return nil
 
93
}
 
94
 
 
95
func (s *MyScope) Search(query *scopes.CannedQuery, metadata *scopes.SearchMetadata, reply *scopes.SearchReply, cancelled <-chan bool) error {
 
96
        root_department := s.CreateDepartments(query, metadata, reply)
 
97
        reply.RegisterDepartments(root_department)
 
98
 
 
99
        return s.AddQueryResults(reply, query.QueryString())
 
100
}
 
101
 
 
102
func (s *MyScope) SetScopeBase(base *scopes.ScopeBase) {
 
103
        s.base = base
 
104
}
 
105
 
 
106
// RESULTS *********************************************************************
 
107
 
 
108
func (s *MyScope) AddQueryResults(reply *scopes.SearchReply, query string) error {
 
109
        cat := reply.RegisterCategory("category", "Category", "", searchCategoryTemplate)
 
110
 
 
111
        result := scopes.NewCategorisedResult(cat)
 
112
        result.SetURI("http://localhost/" + query)
 
113
        result.SetDndURI("http://localhost_dnduri" + query)
 
114
        result.SetTitle("TEST" + query)
 
115
        result.SetArt("https://pbs.twimg.com/profile_images/1117820653/5ttls5.jpg.png")
 
116
        result.Set("test_value_bool", true)
 
117
        result.Set("test_value_string", "test_value"+query)
 
118
        result.Set("test_value_int", 1999)
 
119
        result.Set("test_value_float", 1.999)
 
120
        if err := reply.Push(result); err != nil {
 
121
                return err
 
122
        }
 
123
 
 
124
        result.SetURI("http://localhost2/" + query)
 
125
        result.SetDndURI("http://localhost_dnduri2" + query)
 
126
        result.SetTitle("TEST2")
 
127
        result.SetArt("https://pbs.twimg.com/profile_images/1117820653/5ttls5.jpg.png")
 
128
        result.Set("test_value_bool", false)
 
129
        result.Set("test_value_string", "test_value2"+query)
 
130
        result.Set("test_value_int", 2000)
 
131
        result.Set("test_value_float", 2.100)
 
132
 
 
133
        // add a variant map value
 
134
        m := make(map[string]interface{})
 
135
        m["value1"] = 1
 
136
        m["value2"] = "string_value"
 
137
        result.Set("test_value_map", m)
 
138
 
 
139
        // add a variant array value
 
140
        l := make([]interface{}, 0)
 
141
        l = append(l, 1999)
 
142
        l = append(l, "string_value")
 
143
        result.Set("test_value_array", l)
 
144
        if err := reply.Push(result); err != nil {
 
145
                return err
 
146
        }
 
147
 
 
148
        return nil
 
149
}
 
150
 
 
151
// DEPARTMENTS *****************************************************************
 
152
 
 
153
func SearchDepartment(root *scopes.Department, id string) *scopes.Department {
 
154
        sub_depts := root.Subdepartments()
 
155
        for _, element := range sub_depts {
 
156
                if element.Id() == id {
 
157
                        return element
 
158
                }
 
159
        }
 
160
        return nil
 
161
}
 
162
 
 
163
func (s *MyScope) GetRockSubdepartments(query *scopes.CannedQuery,
 
164
        metadata *scopes.SearchMetadata,
 
165
        reply *scopes.SearchReply) *scopes.Department {
 
166
        active_dep, err := scopes.NewDepartment("Rock", query, "Rock Music")
 
167
        if err == nil {
 
168
                active_dep.SetAlternateLabel("Rock Music Alt")
 
169
                department, _ := scopes.NewDepartment("60s", query, "Rock from the 60s")
 
170
                active_dep.AddSubdepartment(department)
 
171
 
 
172
                department2, _ := scopes.NewDepartment("70s", query, "Rock from the 70s")
 
173
                active_dep.AddSubdepartment(department2)
 
174
        }
 
175
 
 
176
        return active_dep
 
177
}
 
178
 
 
179
func (s *MyScope) GetSoulSubdepartments(query *scopes.CannedQuery,
 
180
        metadata *scopes.SearchMetadata,
 
181
        reply *scopes.SearchReply) *scopes.Department {
 
182
        active_dep, err := scopes.NewDepartment("Soul", query, "Soul Music")
 
183
        if err == nil {
 
184
                active_dep.SetAlternateLabel("Soul Music Alt")
 
185
                department, _ := scopes.NewDepartment("Motown", query, "Motown Soul")
 
186
                active_dep.AddSubdepartment(department)
 
187
 
 
188
                department2, _ := scopes.NewDepartment("New Soul", query, "New Soul")
 
189
                active_dep.AddSubdepartment(department2)
 
190
        }
 
191
 
 
192
        return active_dep
 
193
}
 
194
 
 
195
func (s *MyScope) CreateDepartments(query *scopes.CannedQuery,
 
196
        metadata *scopes.SearchMetadata,
 
197
        reply *scopes.SearchReply) *scopes.Department {
 
198
        department, _ := scopes.NewDepartment("", query, "Browse Music")
 
199
        department.SetAlternateLabel("Browse Music Alt")
 
200
 
 
201
        rock_dept := s.GetRockSubdepartments(query, metadata, reply)
 
202
        if rock_dept != nil {
 
203
                department.AddSubdepartment(rock_dept)
 
204
        }
 
205
 
 
206
        soul_dept := s.GetSoulSubdepartments(query, metadata, reply)
 
207
        if soul_dept != nil {
 
208
                department.AddSubdepartment(soul_dept)
 
209
        }
 
210
 
 
211
        return department
 
212
}
 
213
        
 
214
func (s *MyScope) PerformAction(result *scopes.Result, metadata *scopes.ActionMetadata, widgetId, actionId string) (*scopes.ActivationResponse, error) {
 
215
        
 
216
        if (widgetId == "actions" && actionId == "hide")
 
217
    {
 
218
        return ActivationQueryBase::UPtr(new MyActivation(result, meta));
 
219
    }
 
220
    else if (widget_id == "actions" && action_id == "download")
 
221
    {
 
222
        MyActivation* response = new MyActivation(result, meta, ActivationResponse::ShowPreview);
 
223
        response->setExtraData(meta.scope_data());
 
224
        return ActivationQueryBase::UPtr(response);
 
225
    }
 
226
    return ActivationQueryBase::UPtr(new MyActivation(result, meta, ActivationResponse::NotHandled));
 
227
}
 
228
 
 
229
// MAIN ************************************************************************
 
230
 
 
231
func main() {
 
232
        var sc MyScope
 
233
        scope_interface = &sc
 
234
 
 
235
        if err := scopes.Run(&MyScope{}); err != nil {
 
236
                log.Fatalln(err)
 
237
        }
 
238
}