/** @odoo-module **/
import { registry } from "@web/core/registry";
import testUtils from "web.test_utils";
import ListController from "web.ListController";
import FormView from "web.FormView";
import ListView from "web.ListView";
import {
click,
destroy,
getFixture,
makeDeferred,
legacyExtraNextTick,
patchWithCleanup,
triggerEvents,
} from "../../helpers/utils";
import KanbanView from "web.KanbanView";
import { registerCleanup } from "../../helpers/cleanup";
import { makeTestEnv } from "../../helpers/mock_env";
import { createWebClient, doAction, getActionManagerServerData } from "./../helpers";
import makeTestEnvironment from "web.test_env";
import { ClientActionAdapter, ViewAdapter } from "@web/legacy/action_adapters";
import { makeLegacyCrashManagerService } from "@web/legacy/utils";
import { useDebugCategory } from "@web/core/debug/debug_context";
import { ErrorDialog } from "@web/core/errors/error_dialogs";
import * as cpHelpers from "@web/../tests/search/helpers";
import AbstractView from "web.AbstractView";
import ControlPanel from "web.ControlPanel";
import core from "web.core";
import AbstractAction from "web.AbstractAction";
import Widget from "web.Widget";
import SystrayMenu from "web.SystrayMenu";
import legacyViewRegistry from "web.view_registry";
let serverData;
let target;
QUnit.module("ActionManager", (hooks) => {
hooks.beforeEach(() => {
registry.category("views").remove("form"); // remove new form from registry
registry.category("views").remove("kanban"); // remove new kanban from registry
registry.category("views").remove("list"); // remove new list from registry
legacyViewRegistry.add("form", FormView); // add legacy form -> will be wrapped and added to new registry
legacyViewRegistry.add("kanban", KanbanView); // add legacy kanban -> will be wrapped and added to new registry
legacyViewRegistry.add("list", ListView); // add legacy list -> will be wrapped and added to new registry
serverData = getActionManagerServerData();
target = getFixture();
});
QUnit.module("Legacy tests (to eventually drop)");
QUnit.test("display warning as notification", async function (assert) {
// this test can be removed as soon as the legacy layer is dropped
assert.expect(5);
let list;
patchWithCleanup(ListController.prototype, {
init() {
this._super(...arguments);
list = this;
},
});
const webClient = await createWebClient({ serverData });
await doAction(webClient, 3);
assert.containsOnce(target, ".o_legacy_list_view");
list.trigger_up("warning", {
title: "Warning!!!",
message: "This is a warning...",
});
await testUtils.nextTick();
await legacyExtraNextTick();
assert.containsOnce(target, ".o_legacy_list_view");
assert.containsOnce(document.body, ".o_notification.border-warning");
assert.strictEqual($(".o_notification_title").text(), "Warning!!!");
assert.strictEqual($(".o_notification_content").text(), "This is a warning...");
});
QUnit.test("display warning as modal", async function (assert) {
// this test can be removed as soon as the legacy layer is dropped
assert.expect(5);
let list;
patchWithCleanup(ListController.prototype, {
init() {
this._super(...arguments);
list = this;
},
});
const webClient = await createWebClient({ serverData });
await doAction(webClient, 3);
assert.containsOnce(target, ".o_legacy_list_view");
list.trigger_up("warning", {
title: "Warning!!!",
message: "This is a warning...",
type: "dialog",
});
await testUtils.nextTick();
await legacyExtraNextTick();
assert.containsOnce(target, ".o_legacy_list_view");
assert.containsOnce(document.body, ".modal");
assert.strictEqual($(".modal-title").text(), "Warning!!!");
assert.strictEqual($(".modal-body").text(), "This is a warning...");
});
QUnit.test("display multiline warning as modal", async function (assert) {
assert.expect(5);
let list;
patchWithCleanup(ListController.prototype, {
init() {
this._super(...arguments);
list = this;
},
});
const webClient = await createWebClient({ serverData });
await doAction(webClient, 3);
assert.containsOnce(target, ".o_legacy_list_view");
list.trigger_up("warning", {
title: "Warning!!!",
message: "This is a warning...\nabc",
type: "dialog",
});
await testUtils.nextTick();
await legacyExtraNextTick();
assert.containsOnce(target, ".o_legacy_list_view");
assert.containsOnce(document.body, ".modal");
assert.strictEqual($(".modal-title").text(), "Warning!!!");
assert.strictEqual($(".modal-body")[0].innerText, "This is a warning...\nabc");
});
QUnit.test(
"legacy crash manager is still properly remapped to error service",
async function (assert) {
// this test can be removed as soon as the legacy layer is dropped
assert.expect(2);
const legacyEnv = makeTestEnvironment();
registry
.category("services")
.add("legacy_crash_manager", makeLegacyCrashManagerService(legacyEnv))
.add("dialog", {
start() {
return {
add(dialogClass, props) {
assert.strictEqual(dialogClass, ErrorDialog);
assert.strictEqual(props.traceback, "BOOM");
},
};
},
});
await makeTestEnv();
legacyEnv.services.crash_manager.show_message("BOOM");
}
);
QUnit.test("redraw a controller and open debugManager does not crash", async (assert) => {
assert.expect(11);
const LegacyAction = AbstractAction.extend({
start() {
const ret = this._super(...arguments);
const el = document.createElement("div");
el.classList.add("custom-action");
this.el.append(el);
return ret;
},
});
core.action_registry.add("customLegacy", LegacyAction);
patchWithCleanup(ClientActionAdapter.prototype, {
setup() {
useDebugCategory("custom", { widget: this });
this._super();
},
});
registry
.category("debug")
.category("custom")
.add("item1", ({ widget }) => {
assert.step("debugItems executed");
assert.ok(widget);
return {};
});
patchWithCleanup(odoo, { debug: true });
const mockRPC = (route) => {
if (route.includes("check_access_rights")) {
return true;
}
};
const webClient = await createWebClient({ serverData, mockRPC });
await doAction(webClient, "customLegacy");
assert.containsOnce(target, ".custom-action");
assert.verifySteps([]);
await click(target, ".o_debug_manager button");
assert.verifySteps(["debugItems executed"]);
await doAction(webClient, 5); // action in Dialog
await click(target, ".modal .o_form_button_cancel");
assert.containsNone(target, ".modal");
assert.containsOnce(target, ".custom-action");
assert.verifySteps([]);
// close debug menu
await click(target, ".o_debug_manager button");
// open debug menu
await click(target, ".o_debug_manager button");
assert.verifySteps(["debugItems executed"]);
delete core.action_registry.map.customLegacy;
});
QUnit.test("willUnmount is called down the legacy layers", async (assert) => {
assert.expect(7);
let mountCount = 0;
patchWithCleanup(ControlPanel.prototype, {
setup() {
this._super();
owl.onMounted(() => {
mountCount = mountCount + 1;
this.__uniqueId = mountCount;
assert.step(`mounted ${this.__uniqueId}`);
});
owl.onWillUnmount(() => {
assert.step(`willUnmount ${this.__uniqueId}`);
});
},
});
const LegacyAction = AbstractAction.extend({
hasControlPanel: true,
start() {
const ret = this._super(...arguments);
const el = document.createElement("div");
el.classList.add("custom-action");
this.el.append(el);
return ret;
},
});
core.action_registry.add("customLegacy", LegacyAction);
const webClient = await createWebClient({ serverData });
await doAction(webClient, 1);
await doAction(webClient, "customLegacy");
await click(target.querySelectorAll(".breadcrumb-item")[0]);
await legacyExtraNextTick();
destroy(webClient);
assert.verifySteps([
"mounted 1",
"willUnmount 1",
"mounted 2",
"willUnmount 2",
"mounted 3",
"willUnmount 3",
]);
delete core.action_registry.map.customLegacy;
});
QUnit.test("Checks the availability of all views in the action", async (assert) => {
assert.expect(2);
patchWithCleanup(ListView.prototype, {
init(viewInfo, params) {
const action = params.action;
const views = action.views.map((view) => [view.viewID, view.type]);
assert.deepEqual(views, [
[1, "list"],
[2, "kanban"],
[3, "form"],
]);
assert.deepEqual(action._views, [
[1, "list"],
[2, "kanban"],
[3, "form"],
[false, "search"],
]);
this._super(...arguments);
},
});
const models = {
partner: {
fields: {
display_name: { string: "Displayed name", type: "char", searchable: true },
foo: {
string: "Foo",
type: "char",
default: "My little Foo Value",
searchable: true,
},
bar: { string: "Bar", type: "boolean" },
int_field: { string: "Integer field", type: "integer", group_operator: "sum" },
},
records: [
{
id: 1,
display_name: "first record",
foo: "yop",
int_field: 3,
},
{
id: 2,
display_name: "second record",
foo: "lalala",
int_field: 5,
},
{
id: 4,
display_name: "aaa",
foo: "abc",
int_field: 2,
},
],
},
};
const views = {
"partner,1,list": '
',
"partner,2,kanban": "