Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
@testable import Client
6
import Foundation
7
import XCTest
8
import Shared
9
10
private let DefaultSearchEngineName = "Google"
11
private let ExpectedEngineNames = ["Amazon.com", "Bing", "DuckDuckGo", "Google", "Twitter", "Wikipedia"]
12
13
class SearchEnginesTests: XCTestCase {
14
15
func testIncludesExpectedEngines() {
16
// Verify that the set of shipped engines includes the expected subset.
17
let profile = MockProfile()
18
let engines = SearchEngines(prefs: profile.prefs, files: profile.files).orderedEngines
19
XCTAssertTrue((engines?.count)! >= ExpectedEngineNames.count)
20
21
for engineName in ExpectedEngineNames {
22
XCTAssertTrue(((engines?.filter { engine in engine.shortName == engineName })?.count)! > 0)
23
}
24
}
25
26
func testDefaultEngineOnStartup() {
27
// If this is our first run, Google should be first for the en locale.
28
let profile = MockProfile()
29
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
30
XCTAssertEqual(engines.defaultEngine.shortName, DefaultSearchEngineName)
31
XCTAssertEqual(engines.orderedEngines[0].shortName, DefaultSearchEngineName)
32
}
33
34
func testAddingAndDeletingCustomEngines() {
35
let testEngine = OpenSearchEngine(engineID: "ATester", shortName: "ATester", image: UIImage(), searchTemplate: "http://firefox.com/find?q={searchTerm}", suggestTemplate: nil, isCustomEngine: true)
36
let profile = MockProfile()
37
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
38
engines.addSearchEngine(testEngine)
39
XCTAssertEqual(engines.orderedEngines[1].engineID, testEngine.engineID)
40
41
engines.deleteCustomEngine(testEngine)
42
let deleted = engines.orderedEngines.filter {$0 == testEngine}
43
XCTAssertEqual(deleted, [])
44
}
45
46
func testDefaultEngine() {
47
let profile = MockProfile()
48
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
49
let engineSet = engines.orderedEngines
50
51
engines.defaultEngine = (engineSet?[0])!
52
XCTAssertTrue(engines.isEngineDefault((engineSet?[0])!))
53
XCTAssertFalse(engines.isEngineDefault((engineSet?[1])!))
54
// The first ordered engine is the default.
55
XCTAssertEqual(engines.orderedEngines[0].shortName, engineSet?[0].shortName)
56
57
engines.defaultEngine = (engineSet?[1])!
58
XCTAssertFalse(engines.isEngineDefault((engineSet?[0])!))
59
XCTAssertTrue(engines.isEngineDefault((engineSet?[1])!))
60
// The first ordered engine is the default.
61
XCTAssertEqual(engines.orderedEngines[0].shortName, engineSet?[1].shortName)
62
63
let engines2 = SearchEngines(prefs: profile.prefs, files: profile.files)
64
// The default engine should have been persisted.
65
XCTAssertTrue(engines2.isEngineDefault((engineSet?[1])!))
66
// The first ordered engine is the default.
67
XCTAssertEqual(engines.orderedEngines[0].shortName, engineSet?[1].shortName)
68
}
69
70
func testOrderedEngines() {
71
let profile = MockProfile()
72
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
73
74
engines.orderedEngines = [ExpectedEngineNames[4], ExpectedEngineNames[2], ExpectedEngineNames[0]].map { name in
75
for engine in engines.orderedEngines {
76
if engine.shortName == name {
77
return engine
78
}
79
}
80
XCTFail("Could not find engine: \(name)")
81
return engines.orderedEngines.first!
82
}
83
XCTAssertEqual(engines.orderedEngines[0].shortName, ExpectedEngineNames[4])
84
XCTAssertEqual(engines.orderedEngines[1].shortName, ExpectedEngineNames[2])
85
XCTAssertEqual(engines.orderedEngines[2].shortName, ExpectedEngineNames[0])
86
87
let engines2 = SearchEngines(prefs: profile.prefs, files: profile.files)
88
// The ordering should have been persisted.
89
XCTAssertEqual(engines2.orderedEngines[0].shortName, ExpectedEngineNames[4])
90
XCTAssertEqual(engines2.orderedEngines[1].shortName, ExpectedEngineNames[2])
91
XCTAssertEqual(engines2.orderedEngines[2].shortName, ExpectedEngineNames[0])
92
93
// Remaining engines should be appended in alphabetical order.
94
XCTAssertEqual(engines2.orderedEngines[3].shortName, ExpectedEngineNames[1])
95
XCTAssertEqual(engines2.orderedEngines[4].shortName, ExpectedEngineNames[3])
96
XCTAssertEqual(engines2.orderedEngines[5].shortName, ExpectedEngineNames[5])
97
}
98
99
func testQuickSearchEngines() {
100
let profile = MockProfile()
101
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
102
let engineSet = engines.orderedEngines
103
104
// You can't disable the default engine.
105
engines.defaultEngine = (engineSet?[1])!
106
engines.disableEngine((engineSet?[1])!)
107
XCTAssertTrue(engines.isEngineEnabled((engineSet?[1])!))
108
109
// The default engine is not included in the quick search engines.
110
XCTAssertEqual(0, engines.quickSearchEngines.filter { engine in engine.shortName == engineSet?[1].shortName }.count)
111
112
// Enable and disable work.
113
engines.enableEngine((engineSet?[0])!)
114
XCTAssertTrue(engines.isEngineEnabled((engineSet?[0])!))
115
XCTAssertEqual(1, engines.quickSearchEngines.filter { engine in engine.shortName == engineSet?[0].shortName }.count)
116
117
engines.disableEngine((engineSet?[0])!)
118
XCTAssertFalse(engines.isEngineEnabled((engineSet?[0])!))
119
XCTAssertEqual(0, engines.quickSearchEngines.filter { engine in engine.shortName == engineSet?[0].shortName }.count)
120
121
// Setting the default engine enables it.
122
engines.defaultEngine = (engineSet?[0])!
123
XCTAssertTrue(engines.isEngineEnabled((engineSet?[1])!))
124
125
// Setting the order may change the default engine, which enables it.
126
engines.orderedEngines = [(engineSet?[2])!, (engineSet?[1])!, (engineSet?[0])!]
127
XCTAssertTrue(engines.isEngineDefault((engineSet?[2])!))
128
XCTAssertTrue(engines.isEngineEnabled((engineSet?[2])!))
129
130
// The enabling should be persisted.
131
engines.enableEngine((engineSet?[2])!)
132
engines.disableEngine((engineSet?[1])!)
133
engines.enableEngine((engineSet?[0])!)
134
135
let engines2 = SearchEngines(prefs: profile.prefs, files: profile.files)
136
XCTAssertTrue(engines2.isEngineEnabled((engineSet?[2])!))
137
XCTAssertFalse(engines2.isEngineEnabled((engineSet?[1])!))
138
XCTAssertTrue(engines2.isEngineEnabled((engineSet?[0])!))
139
}
140
141
func testSearchSuggestionSettings() {
142
let profile = MockProfile()
143
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
144
145
// By default, you should see search suggestions
146
XCTAssertTrue(engines.shouldShowSearchSuggestions)
147
148
// Setting should be persisted.
149
engines.shouldShowSearchSuggestions = false
150
151
let engines2 = SearchEngines(prefs: profile.prefs, files: profile.files)
152
XCTAssertFalse(engines2.shouldShowSearchSuggestions)
153
}
154
155
func testUnorderedSearchEngines() {
156
XCTAssertEqual(SearchEngines.getUnorderedBundledEnginesFor(locale: Locale(identifier: "zh-TW")).compactMap({$0.shortName}), ["Google", "Bing", "DuckDuckGo", "Wikipedia (zh)"])
157
XCTAssertEqual(SearchEngines.getUnorderedBundledEnginesFor(locale: Locale(identifier: "en-CA")).compactMap({$0.shortName}), ["Google", "Bing", "Amazon.com", "DuckDuckGo", "Twitter", "Wikipedia"])
158
XCTAssertEqual(SearchEngines.getUnorderedBundledEnginesFor(locale: Locale(identifier: "de-DE")).compactMap({$0.shortName}), ["Google", "Bing", "Amazon.de", "DuckDuckGo", "Qwant", "Twitter", "Wikipedia (de)"])
159
XCTAssertEqual(SearchEngines.getUnorderedBundledEnginesFor(locale: Locale(identifier: "en-US")).compactMap({$0.shortName}), ["Google", "Bing", "Amazon.com", "DuckDuckGo", "Twitter", "Wikipedia"])
160
}
161
162
func testGetOrderedEngines() {
163
// setup an existing search engine in the profile
164
let profile = MockProfile()
165
profile.prefs.setObject(["Google"], forKey: "search.orderedEngineNames")
166
let engines = SearchEngines(prefs: profile.prefs, files: profile.files)
167
XCTAssert(engines.orderedEngines.count > 1, "There should be more than one search engine")
168
XCTAssertEqual(engines.orderedEngines.first!.shortName, "Google", "Google should be the first search engine")
169
}
170
171
}