Skip to content

Commit 8b01abe

Browse files
committed
TabRecency: fix a counter bug when loading recency from storage
1 parent a697efe commit 8b01abe

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

background_scripts/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,11 @@ const BackgroundCommands = {
317317
},
318318

319319
visitPreviousTab({ count, tab }) {
320-
const tabIds = BgUtils.tabRecency.getTabsByRecency().filter((tabId) => tabId !== tab.id);
320+
let tabIds = BgUtils.tabRecency.getTabsByRecency();
321+
tabIds = tabIds.filter((tabId) => tabId !== tab.id);
321322
if (tabIds.length > 0) {
322-
selectSpecificTab({ id: tabIds[(count - 1) % tabIds.length] });
323+
const id = tabIds[(count - 1) % tabIds.length];
324+
selectSpecificTab({ id });
323325
}
324326
},
325327

background_scripts/tab_recency.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class TabRecency {
4545
for (const counter of Object.values(storage.tabRecency)) {
4646
if (maxCounter < counter) maxCounter = counter;
4747
}
48+
if (this.counter < maxCounter + 1) {
49+
this.counter = maxCounter + 1;
50+
}
4851
// Tabs loaded from storage should be considered accessed less recently than any tab tracked in
4952
// memory, so increase all of the in-memory tabs's counters by maxCounter.
5053
for (const [id, counter] of Object.entries(this.tabIdToCounter)) {

tests/unit_tests/tab_recency_test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ context("TabRecency", () => {
5858
assert.equal([4, 3, 2, 1], tabRecency.getTabsByRecency());
5959
});
6060

61+
should("loadFromStorage works if there are no in-memory tab tabs", async () => {
62+
const tabs = [{ id: 1 }, { id: 2 }];
63+
stub(chrome.tabs, "query", () => Promise.resolve(tabs));
64+
65+
const storage = { tabRecency: { 1: 4, 2: 1 } };
66+
stub(chrome.storage.session, "get", () => Promise.resolve(storage));
67+
68+
await tabRecency.loadFromStorage();
69+
70+
// This tab's counter should be the max of the tab counters loaded from storage.
71+
tabRecency.register(2);
72+
73+
assert.equal([2, 1], tabRecency.getTabsByRecency());
74+
});
75+
6176
should("loadFromStorage prunes out tabs which are no longer active", async () => {
6277
const tabs = [{ id: 1 }];
6378
stub(chrome.tabs, "query", () => Promise.resolve(tabs));

0 commit comments

Comments
 (0)