-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.html
136 lines (121 loc) · 4.91 KB
/
test.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<html>
<head>
<meta charset="utf-8" />
<title>Searsia Client Unit Tests</title>
</head>
<body>
<h1>Searsia Client Unit Tests</h1>
<p>Also run with: <a href="http://kb.mozillazine.org/Dom.storage.enabled">dom.storage.enabled</a> = false in Firefox' about:config</p>
<h2>Test results</h2>
<div id="testresults"></div>
<script src="js/jquery.min.js"></script>
<script src="js/searsia.js"></script>
<script> <!--
function printText(text) {
var node = document.createElement("p")
var textnode = document.createTextNode(text)
node.appendChild(textnode)
document.getElementById("testresults").appendChild(node)
}
function printObject(object) {
printText(JSON.stringify(object))
}
function printTestEquals(name, expected, result) {
var text = name
if (expected === result) {
text += ': passed'
} else {
text += ': failed. Expected: ' + expected +' Got: ' + result
}
printText(text)
}
function printTestAtleast(name, expected, result) {
var text = name
if (result >= expected) {
text += ': passed'
} else {
text += ': failed. Expected at least: ' + expected +' Got: ' + result
}
printText(text)
}
function printTestIsNull(name, result) {
var text = name
if (result == null) {
text += ': passed'
} else {
text += ': failed. Expected null. Got: ' + result
}
printText(text)
}
function printUnitTestConnect(object) {
var len
if (object != null && object.hits != null) {
len = object.hits.length
} else {
len = object.error
}
printTestEquals('connect 1', 10, len)
}
function printUnitTestSearch(object) {
var len, result
if (object != null && object.hits != null) {
len = object.hits.length
} else {
len = object.error
}
result = ' '
if (object.sessionid) {
result += object.sessionid
}
if (object.rank) {
result += ' ' + object.rank
}
if (object.resource && object.resource.id) {
result += ' (' + object.resource.id + ')'
}
if (object.status) {
if (object.status === 'start') {
printTestEquals('start' + result, 'start', object.status)
} else if (object.status === 'done') {
printTestEquals('final' + result, 'done', object.status)
} else if (object.status === 'hits') {
printTestAtleast('hits' + result, 4, len)
}
}
}
try { window.localStorage.clear() } catch (ignore) { }
// correctUrl and fillUrlTemplate
var template = 'https://drsheetmusic.com/searsia/index.json?q={searchTerms}&page={startPage?}&type={resultType?}&optional={optional?}'
printTestEquals('url 1', 'https://drsheetmusic.com/searsia/boo.html', searsia.correctUrl(template, 'boo.html'))
printTestEquals('url 2', 'https://drsheetmusic.com/boo.html', searsia.correctUrl(template, '/boo.html'))
printTestEquals('url 3', 'http://searsia.org', searsia.correctUrl(template, 'http://searsia.org'))
printTestEquals('url 4', '//searsia.org', searsia.correctUrl(template, '//searsia.org'))
printTestEquals('template 1', 'https://drsheetmusic.com/searsia/index.json?q=boo+baa&page=2&type=&optional=',
searsia.fillUrlTemplate(template, 'boo+baa', 2, null, null))
printTestEquals('template 2', 'https://drsheetmusic.com/searsia/index.json?q=test&page=1&type=&optional=',
searsia.fillUrlTemplate(template, 'test', null, null, null))
printTestEquals('template 3', 'https://drsheetmusic.com/searsia/test.json?q=other&page=1&type=&optional=',
searsia.fillUrlTemplate(template, 'other', '1', 'test', null))
printTestEquals('template 4', 'https://drsheetmusic.com/searsia/view.json?q=help&page=1&type=video&optional=',
searsia.fillUrlTemplate(template, 'help', 1, 'view', 'video'))
// init
var teststring = 'do not remove this, please.'
var field = 'foo'
window.localStorage[field] = teststring
window.localStorage['searsia-' + field] = 'please remove'
printTestIsNull('init 1', searsia.initClient(template))
printTestEquals('local storage', teststring, window.localStorage[field])
printTestIsNull('Searsia store', window.localStorage['searsia-' + field])
// connect
searsia.connectToServer(printUnitTestConnect)
// search
var params1 = { 'q': 'mozart', 'p': '1' }
var id1 = searsia.searchFederated(params1, printUnitTestSearch)
printText('Call 1 = ' + id1);
var params2 = { 'q': 'bach', 'p': '1' }
var id2 = searsia.searchFederated(params2, printUnitTestSearch)
printText('Call 2 = ' + id2);
-->
</script>
</body>
</html>