Skip to content

Commit 04a58d3

Browse files
committed
usage-time-limit: ignore case of the Action parameter
1 parent 762e4c3 commit 04a58d3

3 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎BrowserGuard/UsageTimeLimit/UsageTimeLimitConfig.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal class TimeRangeConfig
1818

1919
internal class UsageTimeExceededConfig
2020
{
21-
// "WarnOnly" or "Terminate". Anything else is read as "WarnOnly", so a
21+
// "WarnOnly" or "Terminate" (ignore case). Anything else is read as "WarnOnly", so a
2222
// misspelling cannot silently start closing the browser.
2323
public string Action { get; set; } = "WarnOnly";
2424
public int GraceSeconds { get; set; } = 60;

‎webextensions/edge/usage-time-limit.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const EMPTY_STATE = {
1919
terminateAt: 0,
2020
};
2121

22+
// What OnExceeded.Action may say, however it is spelled. A word that is not
23+
// one of these leaves the setting as it was, so that a misspelling cannot
24+
// quietly start closing the browser.
25+
const ACTIONS = {
26+
warnonly: 'WarnOnly',
27+
terminate: 'Terminate',
28+
};
29+
2230
const REASONS = {
2331
continuous: '連続して使用できる時間の上限に達しました。',
2432
schedule: '使用が許可された時間帯を過ぎています。',
@@ -81,8 +89,10 @@ export const UsageTimeLimit = {
8189
}
8290
const onExceeded = usageTimeLimit.OnExceeded;
8391
if (!onExceeded) return;
84-
if (onExceeded.Action === 'Terminate' || onExceeded.Action === 'WarnOnly') {
85-
this.action = onExceeded.Action;
92+
// Kept in the one spelling, so the rest of the module compares it plainly.
93+
const action = ACTIONS[String(onExceeded.Action ?? '').trim().toLowerCase()];
94+
if (action) {
95+
this.action = action;
8696
}
8797
this.graceSeconds = readNumber(onExceeded, 'GraceSeconds', this.graceSeconds);
8898
this.reWarnIntervalMinutes =

‎webextensions/test/usage-time-limit.test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,40 @@ describe('parseTimeOfDay', () => {
102102
});
103103
});
104104

105+
describe('applyConfig', () => {
106+
// The action is the one setting whose value is a word rather than a number
107+
// or a switch, so it is the one that can be spelled a different way.
108+
it('reads the action however it is spelled', () => {
109+
for (const spelling of ['Terminate', 'terminate', 'TERMINATE', ' Terminate ']) {
110+
configure({ OnExceeded: { Action: 'WarnOnly' } });
111+
configure({ OnExceeded: { Action: spelling } });
112+
113+
assert.equal(UsageTimeLimit.action, 'Terminate', `${spelling} should be read`);
114+
}
115+
});
116+
117+
it('reads warning only however it is spelled', () => {
118+
configure({ OnExceeded: { Action: 'Terminate' } });
119+
configure({ OnExceeded: { Action: 'warnonly' } });
120+
121+
assert.equal(UsageTimeLimit.action, 'WarnOnly');
122+
});
123+
124+
// A misspelling must not be able to start closing the browser, nor to stop
125+
// it closing when that was asked for.
126+
it('keeps what it had for a word it does not know', () => {
127+
configure({ OnExceeded: { Action: 'Terminate' } });
128+
configure({ OnExceeded: { Action: 'Close' } });
129+
130+
assert.equal(UsageTimeLimit.action, 'Terminate');
131+
132+
configure({ OnExceeded: { Action: 'WarnOnly' } });
133+
configure({ OnExceeded: { Action: 42 } });
134+
135+
assert.equal(UsageTimeLimit.action, 'WarnOnly');
136+
});
137+
});
138+
105139
describe('isWithinAllowedRanges', () => {
106140
it('allows any hour when no range is configured', () => {
107141
assert.equal(UsageTimeLimit.isWithinAllowedRanges(at(3)), true);

0 commit comments

Comments
 (0)