Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit a59631a

Browse files
committed
Merge pull request #14713 from bsclifton/search-engine-by-region-config
Allow for a per-locale default search engine (value comes from config)
1 parent f8877ed commit a59631a

File tree

7 files changed

+194
-12
lines changed

7 files changed

+194
-12
lines changed

app/locale.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -386,24 +386,19 @@ availableLanguages.forEach(function (lang) {
386386
})
387387

388388
// Return the default locale in xx-XX format I.e. pt-BR
389-
const defaultLocale = function () {
390-
// If electron has the locale
389+
module.exports.getDefaultLocale = function (allowUnsupported = false) {
391390
if (app.getLocale()) {
392-
// Retrieve the language and convert _ to -
393391
var lang = app.getLocale().replace('_', '-')
394392
// If there is no country code designated use the language code
395393
if (!lang.match(/-/)) {
396394
lang = lang + '-' + lang.toUpperCase()
397395
}
398396
// If we have the language configured
399-
if (configuredLanguages[lang]) {
397+
if (allowUnsupported || configuredLanguages[lang]) {
400398
return lang
401-
} else {
402-
return DEFAULT_LANGUAGE
403399
}
404-
} else {
405-
return DEFAULT_LANGUAGE
406400
}
401+
return DEFAULT_LANGUAGE
407402
}
408403

409404
// Initialize translations for a language
@@ -427,7 +422,7 @@ exports.init = function (language) {
427422
}
428423

429424
// Currently selected language identifier I.e. 'en-US'
430-
lang = language || defaultLocale()
425+
lang = language || module.exports.getDefaultLocale()
431426

432427
// Languages to support
433428
const langs = availableLanguages.map(function (lang) {

app/sessionStore.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {execSync} = require('child_process')
2525
const UpdateStatus = require('../js/constants/updateStatus')
2626
const settings = require('../js/constants/settings')
2727
const siteTags = require('../js/constants/siteTags')
28+
const config = require('../js/constants/config')
2829
const downloadStates = require('../js/constants/downloadStates')
2930
const ledgerStatuses = require('./common/constants/ledgerStatuses')
3031
const promotionStatuses = require('./common/constants/promotionStatuses')
@@ -1015,6 +1016,22 @@ module.exports.runImportDefaultSettings = (data) => {
10151016
return data
10161017
}
10171018

1019+
module.exports.setDefaultSearchEngine = (immutableData) => {
1020+
const defaultLocale = locale.getDefaultLocale(true)
1021+
let defaultSearchEngine = config.defaultSearchEngineByLocale.default
1022+
1023+
for (let entry in config.defaultSearchEngineByLocale) {
1024+
if (entry === defaultLocale) {
1025+
defaultSearchEngine = config.defaultSearchEngineByLocale[entry]
1026+
break
1027+
}
1028+
}
1029+
1030+
return defaultSearchEngine
1031+
? immutableData.setIn(['settings', settings.DEFAULT_SEARCH_ENGINE], defaultSearchEngine)
1032+
: immutableData
1033+
}
1034+
10181035
/**
10191036
* Loads the browser state from storage.
10201037
*
@@ -1079,9 +1096,15 @@ module.exports.loadAppState = () => {
10791096
immutableData = module.exports.runPostMigrations(immutableData)
10801097
}
10811098

1082-
locale.init(immutableData.getIn(['settings', settings.LANGUAGE])).then((locale) => {
1099+
locale.init(immutableData.getIn(['settings', settings.LANGUAGE])).then((lang) => {
10831100
immutableData = setVersionInformation(immutableData)
1084-
app.setLocale(locale)
1101+
app.setLocale(lang)
1102+
1103+
// Set default search engine for locale (if not already set)
1104+
if (immutableData.getIn(['settings', settings.DEFAULT_SEARCH_ENGINE]) == null) {
1105+
immutableData = module.exports.setDefaultSearchEngine(immutableData)
1106+
}
1107+
10851108
resolve(immutableData)
10861109
})
10871110
})

js/constants/appConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = {
153153
'general.download-always-ask': true,
154154
'general.spellcheck-enabled': true,
155155
'general.spellcheck-languages': Immutable.fromJS(['en-US']),
156-
'search.default-search-engine': 'Google',
156+
'search.default-search-engine': null,
157157
'search.offer-search-suggestions': false, // false by default for privacy reasons
158158
'search.use-alternate-private-search-engine': false,
159159
'search.use-alternate-private-search-engine-tor': true,

js/constants/config.js

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ module.exports = {
4242
defaultSearchSuggestions: false,
4343
maxHistorySites: 10
4444
},
45+
// NOTE: names here correspond to `name` field in:
46+
// js/data/searchProviders.js
47+
defaultSearchEngineByLocale: {
48+
// Example entries look like this:
49+
// 'en-US': 'GitHub',
50+
// 'es-MX': 'YouTube',
51+
'default': 'Google'
52+
},
4553
defaultOpenSearchPath: 'content/search/google.xml',
4654
vault: {
4755
syncUrl: (userId) => `${vaultHost}/v1/users/${userId}/appState`,

js/settings.js

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const appConfig = require('./constants/appConfig')
66
const Immutable = require('immutable')
77
const settings = require('./constants/settings')
8+
const config = require('./constants/config')
89
const {passwordManagers, defaultPasswordManager, extensionIds, displayNames} = require('./constants/passwordManagers')
910
const {bookmarksToolbarMode} = require('../app/common/constants/settingsEnums')
1011

@@ -54,8 +55,14 @@ const getDefaultSetting = (settingKey, settingsCollection) => {
5455

5556
// 2) Get a default value when no value is set
5657
// allows for default to change until user locks it in
58+
//
59+
// These are overridden when:
60+
// >> user picks their own setting in about:preferences#payments
5761
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
5862
return contributionDefaultAmount(settingKey, settingsCollection)
63+
// >> locale is intialized (which determines default search engine)
64+
case settings.DEFAULT_SEARCH_ENGINE:
65+
return config.defaultSearchEngineByLocale.default
5966
}
6067
return undefined
6168
}

test/unit/app/localeTest.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* global describe, before, after, it */
2+
const mockery = require('mockery')
3+
const assert = require('assert')
4+
5+
require('../braveUnit')
6+
7+
describe('locale unit tests', function () {
8+
let locale
9+
let testLocale = 'en-US'
10+
const fakeElectron = require('../lib/fakeElectron')
11+
12+
before(function () {
13+
fakeElectron.app.getLocale = () => testLocale
14+
15+
mockery.enable({
16+
warnOnReplace: false,
17+
warnOnUnregistered: false,
18+
useCleanCache: true
19+
})
20+
mockery.registerMock('electron', fakeElectron)
21+
locale = require('../../../app/locale')
22+
})
23+
24+
after(function () {
25+
mockery.disable()
26+
})
27+
28+
describe('getDefaultLocale', function () {
29+
it('defaults to en-US if locale is falsey', function () {
30+
testLocale = undefined
31+
assert.equal(locale.getDefaultLocale(), 'en-US')
32+
})
33+
34+
it('defaults to en-US if locale is not in supported locales/languages list', function () {
35+
testLocale = 'not-a-real-locale'
36+
assert.equal(locale.getDefaultLocale(), 'en-US')
37+
})
38+
39+
it('ignores the supported locales/languages list if you pass `true`', function () {
40+
testLocale = 'not-a-real-locale'
41+
assert.equal(locale.getDefaultLocale(true), 'not-a-real-locale')
42+
})
43+
44+
it('replaces underscore in locale with hyphen', function () {
45+
testLocale = 'en_US'
46+
assert.equal(locale.getDefaultLocale(), 'en-US')
47+
})
48+
49+
it('puts a country code in place if one does not exist', function () {
50+
testLocale = 'fr'
51+
assert.equal(locale.getDefaultLocale(), 'fr-FR')
52+
})
53+
})
54+
})

test/unit/app/sessionStoreTest.js

+95
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ describe('sessionStore unit tests', function () {
6363
},
6464
translation: (token) => {
6565
return token
66+
},
67+
getDefaultLocale: (allowUnsupported = false) => {
68+
return 'en-US'
69+
}
70+
}
71+
const fakeConfig = {
72+
defaultSearchEngineByLocale: {
73+
default: 'MetaCrawler'
6674
}
6775
}
6876

@@ -77,6 +85,7 @@ describe('sessionStore unit tests', function () {
7785
mockery.registerMock('compare-versions', compareVersions)
7886
mockery.registerMock('electron', fakeElectron)
7987
mockery.registerMock('./locale', fakeLocale)
88+
mockery.registerMock('../js/constants/config', fakeConfig)
8089
mockery.registerMock('./autofill', fakeAutofill)
8190
mockery.registerMock('./common/state/tabState', fakeTabState)
8291
mockery.registerMock('./common/state/windowState', fakeWindowState)
@@ -912,6 +921,7 @@ describe('sessionStore unit tests', function () {
912921
let cleanAppDataStub
913922
let defaultAppStateSpy
914923
let runPostMigrationsSpy
924+
let setDefaultSearchEngineSpy
915925
let localeInitSpy
916926
let backupSessionStub
917927
let runImportDefaultSettings
@@ -922,6 +932,7 @@ describe('sessionStore unit tests', function () {
922932
cleanAppDataStub = sinon.stub(sessionStore, 'cleanAppData', (data) => data)
923933
defaultAppStateSpy = sinon.spy(sessionStore, 'defaultAppState')
924934
runPostMigrationsSpy = sinon.spy(sessionStore, 'runPostMigrations')
935+
setDefaultSearchEngineSpy = sinon.spy(sessionStore, 'setDefaultSearchEngine')
925936
localeInitSpy = sinon.spy(fakeLocale, 'init')
926937
backupSessionStub = sinon.stub(sessionStore, 'backupSession')
927938
runImportDefaultSettings = sinon.spy(sessionStore, 'runImportDefaultSettings')
@@ -933,6 +944,7 @@ describe('sessionStore unit tests', function () {
933944
runPreMigrationsSpy.restore()
934945
defaultAppStateSpy.restore()
935946
runPostMigrationsSpy.restore()
947+
setDefaultSearchEngineSpy.restore()
936948
localeInitSpy.restore()
937949
backupSessionStub.restore()
938950
clearHSTSDataSpy.restore()
@@ -1300,6 +1312,45 @@ describe('sessionStore unit tests', function () {
13001312
assert.ok(false, 'promise was rejected: ' + JSON.stringify(result))
13011313
})
13021314
})
1315+
1316+
describe('when checking DEFAULT_SEARCH_ENGINE', function () {
1317+
beforeEach(function () {
1318+
setDefaultSearchEngineSpy.reset()
1319+
})
1320+
1321+
let readFileSyncStub
1322+
1323+
afterEach(function () {
1324+
readFileSyncStub.restore()
1325+
})
1326+
1327+
it('calls setDefaultSearchEngine if DEFAULT_SEARCH_ENGINE is null', function () {
1328+
const session = {
1329+
settings: {}
1330+
}
1331+
readFileSyncStub = sinon.stub(fakeFileSystem, 'readFileSync').returns(JSON.stringify(session))
1332+
return sessionStore.loadAppState()
1333+
.then(function (result) {
1334+
assert.equal(setDefaultSearchEngineSpy.calledOnce, true)
1335+
}, function (result) {
1336+
assert.ok(false, 'promise was rejected: ' + JSON.stringify(result))
1337+
})
1338+
})
1339+
1340+
it('does not call setDefaultSearchEngine if DEFAULT_SEARCH_ENGINE has a value', function () {
1341+
const session = {
1342+
settings: {}
1343+
}
1344+
session.settings[settings.DEFAULT_SEARCH_ENGINE] = 'Excite'
1345+
readFileSyncStub = sinon.stub(fakeFileSystem, 'readFileSync').returns(JSON.stringify(session))
1346+
return sessionStore.loadAppState()
1347+
.then(function (result) {
1348+
assert.equal(setDefaultSearchEngineSpy.notCalled, true)
1349+
}, function (result) {
1350+
assert.ok(false, 'promise was rejected: ' + JSON.stringify(result))
1351+
})
1352+
})
1353+
})
13031354
})
13041355

13051356
describe('backupSession', function () {
@@ -1934,4 +1985,48 @@ describe('sessionStore unit tests', function () {
19341985
})
19351986
})
19361987
})
1988+
1989+
describe('setDefaultSearchEngine', function () {
1990+
let getDefaultLocaleSpy
1991+
1992+
beforeEach(function () {
1993+
getDefaultLocaleSpy = sinon.spy(fakeLocale, 'getDefaultLocale')
1994+
})
1995+
1996+
afterEach(function () {
1997+
getDefaultLocaleSpy.restore()
1998+
if (fakeConfig.defaultSearchEngineByLocale['en-US']) {
1999+
delete fakeConfig.defaultSearchEngineByLocale['en-US']
2000+
}
2001+
if (!fakeConfig.defaultSearchEngineByLocale.default) {
2002+
fakeConfig.defaultSearchEngineByLocale.default = 'MetaCrawler'
2003+
}
2004+
})
2005+
2006+
it('calls getDefaultLocale with true', function () {
2007+
const input = Immutable.fromJS({settings: {}})
2008+
sessionStore.setDefaultSearchEngine(input)
2009+
assert(getDefaultLocaleSpy.calledOnce)
2010+
})
2011+
2012+
it('defaults to `default` entry', function () {
2013+
const input = Immutable.fromJS({settings: {}})
2014+
const output = sessionStore.setDefaultSearchEngine(input)
2015+
assert.equal(output.getIn(['settings', settings.DEFAULT_SEARCH_ENGINE]), 'MetaCrawler')
2016+
})
2017+
2018+
it('matches a locale specific entry (if present)', function () {
2019+
fakeConfig.defaultSearchEngineByLocale['en-US'] = 'Yahoo'
2020+
const input = Immutable.fromJS({settings: {}})
2021+
const output = sessionStore.setDefaultSearchEngine(input)
2022+
assert.equal(output.getIn(['settings', settings.DEFAULT_SEARCH_ENGINE]), 'Yahoo')
2023+
})
2024+
2025+
it('does not change input if there is no default in config', function () {
2026+
delete fakeConfig.defaultSearchEngineByLocale.default
2027+
const input = Immutable.fromJS({settings: {}})
2028+
const output = sessionStore.setDefaultSearchEngine(input)
2029+
assert.deepEqual(input, output)
2030+
})
2031+
})
19372032
})

0 commit comments

Comments
 (0)