1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#! /usr/bin/python3
# -*- coding: utf-8 -*-
from gi.repository import Unity
from unittest import TestCase
import imp
import os
class ResultSet(Unity.ResultSet):
def __init__(self):
Unity.ResultSet.__init__(self)
self.results = []
def do_add_result(self, result):
self.results.append({'uri':result.uri,
'title':result.title,
'comment':result.comment,
'icon':result.icon_hint})
class ScopeTestCase(TestCase):
def init_scope(self, scope_path):
self.scope_module = imp.load_source('scope', scope_path)
self.scope = self.scope_module.load_scope()
def perform_query(self, query, filter_set = Unity.FilterSet.new()):
result_set = ResultSet()
ctx = Unity.SearchContext.create(query, 0, filter_set,
None, result_set, None)
s = self.scope.create_search_for_query(ctx)
s.run()
return result_set
class TestYelpScope(ScopeTestCase):
def setUp(self):
self.init_scope('src/unity_yelp_daemon.py')
self.scope_module.YELP_EXECUTABLE = os.path.join(
os.path.dirname(__file__), "fake-yelp.py")
def tearDown(self):
self.scope = None
self.scope_module = None
def test_questions_search(self):
self.scope_module.HELP_DIR = 'tests/data/'
expected_results = ["tests/data/C/sample_help/mock_yelp_file",
"A visual introduction to the Unity desktop."]
results = []
for s in ['unity']:
result_set = self.perform_query(s)
results.append(result_set.results[0]['uri'])
results.append(result_set.results[0]['title'])
self.assertEqual(results, expected_results)
def test_questions_failing_search(self):
self.scope_module.HELP_DIR = 'tests/data/'
for s in ['upnriitnyt']:
result_set = self.perform_query(s)
self.assertEqual(len(result_set.results), 0)
def test_activation(self):
result = Unity.ScopeResult()
result.uri = "tests/data/C/sample_help/mock_yelp_file"
activation = self.scope.activate(result, Unity.SearchMetadata(), None)
self.assertEqual(activation.props.goto_uri, None)
self.assertEqual(activation.props.handled, Unity.HandledType.HIDE_DASH)
if __name__ == '__main__':
unittest.main()
|