Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions plugins/mobile/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,14 @@ $.each(plugin.statusFilterOptions, function(i, opt) {
plugin.applyFilters = function() {
// Drop filters whose label/tracker no longer exists in the torrent list
// (label/tracker ids are persistent, so check the current lists instead)
this.filters.label = $.grep(this.filters.label, function(name) {
return $.grep(plugin.labelList, function(o) {return o.name === name;}).length > 0;
});
this.filters.tracker = $.grep(this.filters.tracker, function(name) {
return $.grep(plugin.trackerList, function(o) {return o.name === name;}).length > 0;
});
if (this.torrents) {
this.filters.label = $.grep(this.filters.label, function(name) {
return $.grep(plugin.labelList, function(o) {return o.name === name;}).length > 0;
});
this.filters.tracker = $.grep(this.filters.tracker, function(name) {
return $.grep(plugin.trackerList, function(o) {return o.name === name;}).length > 0;
});
}

// Selections within a category are ORed, categories are ANDed
// (like the desktop panels), so the selector is the cartesian
Expand Down Expand Up @@ -544,6 +546,7 @@ plugin.filtersEqual = function(a, b) {
plugin.filterDone = function() {
if (this.filtersSnapshot && !this.filtersEqual(this.filters, this.filtersSnapshot)) {
this.scrollTop = 0;
this.storeSetting('filters', JSON.stringify(this.filters));
}
this.filtersSnapshot = null;
this.showList();
Expand Down Expand Up @@ -645,6 +648,42 @@ plugin.storedSetting = function(key, isValid, dflt) {
return ((typeof v == 'string') && isValid(v)) ? v : dflt;
};

plugin.storedFilters = function() {
var dflt = {status: [], label: [], tracker: []};
var v = theWebUI.settings['webui.mobile.filters'];
if (typeof v == 'string') {
try {
v = JSON.parse(v);
} catch (e) {
return dflt;
}
}
if (!v || (typeof v != 'object') || $.isArray(v)) {
return dflt;
}
var result = {
status: [],
label: [],
tracker: []
};
if ($.isArray(v.status)) {
result.status = $.grep(v.status, function(s, i) {
return (typeof s == 'string') && Object.prototype.hasOwnProperty.call(plugin.statusFilterClasses, s) && ($.inArray(s, v.status) === i);
});
}
if ($.isArray(v.label)) {
result.label = $.grep(v.label, function(l, i) {
return (typeof l == 'string') && ($.inArray(l, v.label) === i);
});
}
if ($.isArray(v.tracker)) {
result.tracker = $.grep(v.tracker, function(t, i) {
return (typeof t == 'string') && ($.inArray(t, v.tracker) === i);
});
}
return result;
};

// Re-fetch the stored settings and merge them in before saving, so this
// long-lived mobile session doesn't overwrite newer settings saved from
// the desktop UI (its own snapshot may be stale) -- same reason as the
Expand Down Expand Up @@ -1893,6 +1932,14 @@ plugin.processTorrents = function(torrents, singleUpdate) {
v.hash = n;
v.status = theWebUI.getStatusIcon(v)[1];
torrentArray.push(v);
if (v.label) {
if (plugin.labelIds[v.label] == undefined) {
plugin.labelIds[v.label] = plugin.nextLabelId++;
}
if ($.grep(plugin.labelList, function(o) {return o.name === v.label;}).length === 0) {
plugin.labelList.push({name: v.label, count: 0});
}
}
});
torrentArray.sort(plugin.dynamicSort(plugin.sort));

Expand Down Expand Up @@ -2400,6 +2447,7 @@ plugin.init = function() {
// plugin.sort. Restored before the first render so the list comes up
// in the order the user left it
this.sort = this.storedSetting('sort', this.isKnownSort, this.sort);
this.filters = this.storedFilters();

// The desktop engine keeps running underneath the mobile UI and can
// auto-save the whole webui.* settings snapshot without user interaction
Expand Down
186 changes: 186 additions & 0 deletions tests/plugins/mobile/settings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,190 @@ describe("mobile plugin settings", () => {
expect($("#sort_asc").prop("checked")).toBe(true);
});
});

describe("storedFilters", () => {
it("returns the stored filters when valid JSON is stored", () => {
window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({
status: ["downloading", "completed"],
label: ["music", ""],
tracker: ["tracker.example.com"],
});
expect(plugin.storedFilters()).toEqual({
status: ["downloading", "completed"],
label: ["music", ""],
tracker: ["tracker.example.com"],
});
});

it("filters out unknown status options and non-string values", () => {
window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({
status: ["downloading", "bogus_status", 123, "completed"],
label: ["music", 456, null, ""],
tracker: ["tracker.example.com", false],
});
expect(plugin.storedFilters()).toEqual({
status: ["downloading", "completed"],
label: ["music", ""],
tracker: ["tracker.example.com"],
});
});

it("deduplicates values in filter arrays", () => {
window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({
status: ["downloading", "downloading"],
label: ["music", "music"],
tracker: ["tracker.example.com", "tracker.example.com"],
});
expect(plugin.storedFilters()).toEqual({
status: ["downloading"],
label: ["music"],
tracker: ["tracker.example.com"],
});
});

it("falls back to default empty filters when nothing is stored", () => {
expect(plugin.storedFilters()).toEqual({
status: [],
label: [],
tracker: [],
});
});

it("falls back to default empty filters for invalid JSON", () => {
window.theWebUI.settings["webui.mobile.filters"] = "not-a-json-string";
expect(plugin.storedFilters()).toEqual({
status: [],
label: [],
tracker: [],
});
});

it("falls back to default empty filters for non-string / non-object values", () => {
window.theWebUI.settings["webui.mobile.filters"] = 42;
expect(plugin.storedFilters()).toEqual({
status: [],
label: [],
tracker: [],
});

window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify(["status", "downloading"]);
expect(plugin.storedFilters()).toEqual({
status: [],
label: [],
tracker: [],
});
});
});

describe("filterDone", () => {
beforeEach(() => {
plugin.showList = jest.fn();
plugin.filters = { status: [], label: [], tracker: [] };
plugin.filtersSnapshot = null;
});

it("persists the filters when changed from snapshot", () => {
plugin.filtersSnapshot = { status: [], label: [], tracker: [] };
plugin.filters = { status: ["downloading"], label: ["music"], tracker: [] };

plugin.filterDone();

expect(saveCount).toBe(1);
expect(JSON.parse(storedOnServer["webui.mobile.filters"])).toEqual({
status: ["downloading"],
label: ["music"],
tracker: [],
});
expect(plugin.filtersSnapshot).toBeNull();
expect(plugin.showList).toHaveBeenCalled();
});

it("does not re-save when filters are unchanged from snapshot", () => {
plugin.filtersSnapshot = { status: ["downloading"], label: ["music"], tracker: [] };
plugin.filters = { status: ["downloading"], label: ["music"], tracker: [] };

plugin.filterDone();

expect(saveCount).toBe(0);
expect(plugin.filtersSnapshot).toBeNull();
expect(plugin.showList).toHaveBeenCalled();
});
});

describe("applyFilters with saved filters", () => {
it("does not drop saved label and tracker filters before torrents are loaded", () => {
plugin.torrents = null;
plugin.labelList = [];
plugin.trackerList = [];
plugin.filters = { status: ["downloading"], label: ["music"], tracker: ["tr.example.com"] };

plugin.applyFilters();

expect(plugin.filters.label).toEqual(["music"]);
expect(plugin.filters.tracker).toEqual(["tr.example.com"]);
});

it("prunes absent label and tracker filters once torrents are loaded", () => {
plugin.torrents = { h1: { name: "test", size: 100 } };
plugin.labelList = [{ name: "other", count: 1 }];
plugin.trackerList = [{ name: "tr2.example.com", count: 1 }];
plugin.filters = { status: ["downloading"], label: ["music"], tracker: ["tr.example.com"] };

plugin.applyFilters();

expect(plugin.filters.label).toEqual([]);
expect(plugin.filters.tracker).toEqual([]);
});
});

describe("init", () => {
const label = 'tv "4k" \\ archive';
let ajaxSpy;

beforeEach(() => {
window.theUILang.All = "All";
window.theUILang.No_label = "No label";
window.theUILang.Total = "Total";
window.theUILang.Stopped = "Stopped";
window.thePlugins = { get: () => null };
window.cacheBust = (url) => url;
// The page markup arrives over ajax; this spec only needs what init
// restores before that request
ajaxSpy = jest.spyOn($, "ajax").mockImplementation(() => {});
$("body").append(
'<div id="initFilterUi"><span id="filterText"></span><span id="filterCount"></span>' +
'<span id="filterTotals"></span><div id="filterStatusList"></div>' +
'<div id="filterLabelsList"></div><div id="filterTrackersList"></div></div>'
);
plugin.filters = { status: [], label: [], tracker: [] };
});
afterEach(() => {
ajaxSpy.mockRestore();
$(window).off("hashchange");
$("#initFilterUi").remove();
});

it("restores the stored filters on load", () => {
window.theWebUI.settings["webui.mobile.filters"] = JSON.stringify({
status: ["stopped"],
label: [label],
tracker: [],
});

plugin.init();

expect(plugin.filters).toEqual({ status: ["stopped"], label: [label], tracker: [] });

plugin.torrents = null;
plugin.labelList = [{ name: "other", count: 1 }, { name: label, count: 1 }];
plugin.trackerList = [];
plugin.updateFilterButton($());
plugin.renderFilterPage();

expect($("#filterText").text()).toBe("Stopped · " + label);
expect($("#filterStatusList .active").text()).toBe("0Stopped");
expect($("#filterLabelsList .active").text()).toBe("1" + label);
expect($("#filterTrackersList .active").text()).toBe("0All");
});
});
});
Loading