2021-06-29 17:18:03 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import * as sinon from 'sinon';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MainSQL } from '../../sql/main';
|
2021-06-29 17:18:03 +00:00
|
|
|
import { SystemTraySetting } from '../../types/SystemTraySetting';
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
import type { ConfigType } from '../../../app/base_config';
|
2021-06-29 17:18:03 +00:00
|
|
|
import { SystemTraySettingCache } from '../../../app/SystemTraySettingCache';
|
|
|
|
|
|
|
|
describe('SystemTraySettingCache', () => {
|
|
|
|
let sandbox: sinon.SinonSandbox;
|
|
|
|
|
|
|
|
let sqlCallStub: sinon.SinonStub;
|
2021-10-20 19:56:49 +00:00
|
|
|
let configGetStub: sinon.SinonStub;
|
|
|
|
let configSetStub: sinon.SinonStub;
|
2021-06-29 17:18:03 +00:00
|
|
|
let sql: Pick<MainSQL, 'sqlCall'>;
|
2021-10-20 19:56:49 +00:00
|
|
|
let config: Pick<ConfigType, 'get' | 'set'>;
|
2021-06-29 17:18:03 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sandbox = sinon.createSandbox();
|
|
|
|
|
|
|
|
sqlCallStub = sandbox.stub().resolves();
|
|
|
|
sql = { sqlCall: sqlCallStub };
|
2021-10-20 19:56:49 +00:00
|
|
|
|
|
|
|
configGetStub = sandbox.stub().returns(undefined);
|
|
|
|
configSetStub = sandbox.stub().returns(undefined);
|
|
|
|
config = { get: configGetStub, set: configSetStub };
|
2021-06-29 17:18:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns MinimizeToAndStartInSystemTray if passed the --start-in-tray argument', async () => {
|
2021-06-29 19:29:40 +00:00
|
|
|
const justOneArg = new SystemTraySettingCache(
|
|
|
|
sql,
|
2021-10-20 19:56:49 +00:00
|
|
|
config,
|
2021-06-29 19:29:40 +00:00
|
|
|
['--start-in-tray'],
|
|
|
|
'1.2.3'
|
|
|
|
);
|
2021-06-29 17:18:03 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
await justOneArg.get(),
|
|
|
|
SystemTraySetting.MinimizeToAndStartInSystemTray
|
|
|
|
);
|
|
|
|
|
2021-06-29 19:29:40 +00:00
|
|
|
const bothArgs = new SystemTraySettingCache(
|
|
|
|
sql,
|
2021-10-20 19:56:49 +00:00
|
|
|
config,
|
2021-06-29 19:29:40 +00:00
|
|
|
['--start-in-tray', '--use-tray-icon'],
|
|
|
|
'1.2.3'
|
|
|
|
);
|
2021-06-29 17:18:03 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
await bothArgs.get(),
|
|
|
|
SystemTraySetting.MinimizeToAndStartInSystemTray
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.notCalled(sqlCallStub);
|
2021-10-20 19:56:49 +00:00
|
|
|
sinon.assert.notCalled(configGetStub);
|
|
|
|
sinon.assert.notCalled(configSetStub);
|
2021-06-29 17:18:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns MinimizeToSystemTray if passed the --use-tray-icon argument', async () => {
|
2021-10-20 19:56:49 +00:00
|
|
|
const cache = new SystemTraySettingCache(
|
|
|
|
sql,
|
|
|
|
config,
|
|
|
|
['--use-tray-icon'],
|
|
|
|
'1.2.3'
|
|
|
|
);
|
2021-06-29 17:18:03 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
await cache.get(),
|
|
|
|
SystemTraySetting.MinimizeToSystemTray
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.notCalled(sqlCallStub);
|
2021-10-20 19:56:49 +00:00
|
|
|
sinon.assert.notCalled(configGetStub);
|
|
|
|
sinon.assert.notCalled(configSetStub);
|
2021-06-29 17:18:03 +00:00
|
|
|
});
|
|
|
|
|
2022-09-06 22:09:52 +00:00
|
|
|
it('returns Uninitialized if system tray is supported but no preference is stored', async () => {
|
2021-06-29 17:18:03 +00:00
|
|
|
sandbox.stub(process, 'platform').value('win32');
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
const cache = new SystemTraySettingCache(sql, config, [], '1.2.3');
|
2022-09-06 22:09:52 +00:00
|
|
|
assert.strictEqual(await cache.get(), SystemTraySetting.Uninitialized);
|
2021-10-20 19:56:49 +00:00
|
|
|
assert(configGetStub.calledOnceWith('system-tray-setting'));
|
|
|
|
assert(
|
|
|
|
configSetStub.calledOnceWith(
|
|
|
|
'system-tray-setting',
|
2022-09-06 22:09:52 +00:00
|
|
|
SystemTraySetting.Uninitialized
|
2021-10-20 19:56:49 +00:00
|
|
|
)
|
|
|
|
);
|
2021-06-29 17:18:03 +00:00
|
|
|
});
|
|
|
|
|
2022-09-06 22:09:52 +00:00
|
|
|
it('returns Uninitialized if system tray is supported but the stored preference is invalid', async () => {
|
2021-06-29 17:18:03 +00:00
|
|
|
sandbox.stub(process, 'platform').value('win32');
|
|
|
|
|
|
|
|
sqlCallStub.resolves({ value: 'garbage' });
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
const cache = new SystemTraySettingCache(sql, config, [], '1.2.3');
|
2022-09-06 22:09:52 +00:00
|
|
|
assert.strictEqual(await cache.get(), SystemTraySetting.Uninitialized);
|
2021-10-20 19:56:49 +00:00
|
|
|
assert(configGetStub.calledOnceWith('system-tray-setting'));
|
|
|
|
assert(
|
|
|
|
configSetStub.calledOnceWith(
|
|
|
|
'system-tray-setting',
|
2022-09-06 22:09:52 +00:00
|
|
|
SystemTraySetting.Uninitialized
|
2021-10-20 19:56:49 +00:00
|
|
|
)
|
|
|
|
);
|
2021-06-29 17:18:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the stored preference if system tray is supported and something is stored', async () => {
|
|
|
|
sandbox.stub(process, 'platform').value('win32');
|
|
|
|
|
|
|
|
sqlCallStub.resolves({ value: 'MinimizeToSystemTray' });
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
const cache = new SystemTraySettingCache(sql, config, [], '1.2.3');
|
2021-06-29 17:18:03 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
await cache.get(),
|
|
|
|
SystemTraySetting.MinimizeToSystemTray
|
|
|
|
);
|
2021-10-20 19:56:49 +00:00
|
|
|
assert(configGetStub.calledOnceWith('system-tray-setting'));
|
|
|
|
assert(
|
|
|
|
configSetStub.calledOnceWith(
|
|
|
|
'system-tray-setting',
|
|
|
|
SystemTraySetting.MinimizeToSystemTray
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the cached preference if system tray is supported and something is stored', async () => {
|
|
|
|
sandbox.stub(process, 'platform').value('win32');
|
|
|
|
|
|
|
|
configGetStub.returns('MinimizeToSystemTray');
|
|
|
|
|
|
|
|
const cache = new SystemTraySettingCache(sql, config, [], '1.2.3');
|
|
|
|
assert.strictEqual(
|
|
|
|
await cache.get(),
|
|
|
|
SystemTraySetting.MinimizeToSystemTray
|
|
|
|
);
|
|
|
|
assert(configGetStub.calledOnceWith('system-tray-setting'));
|
|
|
|
sinon.assert.notCalled(sqlCallStub);
|
2021-06-29 17:18:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('only kicks off one request to the database if multiple sources ask at once', async () => {
|
|
|
|
sandbox.stub(process, 'platform').value('win32');
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
const cache = new SystemTraySettingCache(sql, config, [], '1.2.3');
|
2021-06-29 17:18:03 +00:00
|
|
|
|
|
|
|
await Promise.all([cache.get(), cache.get(), cache.get()]);
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
assert(configGetStub.calledOnceWith('system-tray-setting'));
|
2021-06-29 17:18:03 +00:00
|
|
|
sinon.assert.calledOnce(sqlCallStub);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns DoNotUseSystemTray if system tray is unsupported and there are no CLI flags', async () => {
|
|
|
|
sandbox.stub(process, 'platform').value('darwin');
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
const cache = new SystemTraySettingCache(sql, config, [], '1.2.3');
|
2021-06-29 17:18:03 +00:00
|
|
|
assert.strictEqual(await cache.get(), SystemTraySetting.DoNotUseSystemTray);
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
sinon.assert.notCalled(configGetStub);
|
|
|
|
sinon.assert.notCalled(configSetStub);
|
2021-06-29 17:18:03 +00:00
|
|
|
sinon.assert.notCalled(sqlCallStub);
|
|
|
|
});
|
|
|
|
});
|