import { expect, getFixture, test } from "@odoo/hoot"; import { clear, click, edit, hover, keyDown, keyUp, middleClick, pointerDown, pointerUp, press, queryAll, queryAllProperties, queryAllTexts, queryFirst, queryOne, queryRect, queryText, unload, waitFor, } from "@odoo/hoot-dom"; import { animationFrame, Deferred, mockDate, mockTimeZone, runAllTimers, tick, } from "@odoo/hoot-mock"; import { Component, markup, onRendered, onWillStart, useRef, xml } from "@odoo/owl"; import { getPickerCell } from "@web/../tests/core/datetime/datetime_test_helpers"; import { clickFieldDropdown, clickModalButton, clickSave, contains, defineActions, defineModels, defineParams, editFavoriteName, editSelectMenu, fields, getFacetTexts, getMenuItemTexts, getMockEnv, getPagerLimit, getPagerValue, getService, installLanguages, makeKwArgs, makeServerError, MockServer, mockService, models, mountView, mountViewInDialog, mountWithCleanup, onRpc, pagerNext, pagerPrevious, patchWithCleanup, removeFacet, saveFavorite, selectFieldDropdownItem, selectGroup, serverState, stepAllNetworkCalls, switchView, toggleActionMenu, toggleMenuItem, toggleSaveFavorite, toggleSearchBarMenu, validateSearch, webModels, } from "@web/../tests/web_test_helpers"; import { localization } from "@web/core/l10n/localization"; import { buildSelector } from "@web/../tests/_framework/view_test_helpers"; import { currencies } from "@web/core/currency"; import { Domain } from "@web/core/domain"; import { registry } from "@web/core/registry"; import { user } from "@web/core/user"; import { useBus } from "@web/core/utils/hooks"; import { omit } from "@web/core/utils/objects"; import { RelationalModel } from "@web/model/relational_model/relational_model"; import { session } from "@web/session"; import { floatField } from "@web/views/fields/float/float_field"; import { Many2XAutocomplete } from "@web/views/fields/relational_utils"; import { standardFieldProps } from "@web/views/fields/standard_field_props"; import { ListController } from "@web/views/list/list_controller"; import { WebClient } from "@web/webclient/webclient"; const { ResCompany, ResPartner, ResUsers } = webModels; class Foo extends models.Model { foo = fields.Char(); bar = fields.Boolean(); date = fields.Date(); datetime = fields.Datetime(); int_field = fields.Integer(); qux = fields.Float(); m2o = fields.Many2one({ relation: "bar" }); o2m = fields.One2many({ relation: "bar" }); m2m = fields.Many2many({ relation: "bar" }); text = fields.Text(); amount = fields.Monetary({ currency_field: "currency_id" }); currency_id = fields.Many2one({ relation: "res.currency", default: 1 }); reference = fields.Reference({ selection: [ ["bar", "Bar"], ["res.currency", "Currency"], ], }); properties = fields.Properties({ definition_record: "m2o", definition_record_field: "definitions", }); _records = [ { id: 1, foo: "yop", bar: true, date: "2017-01-25", datetime: "2016-12-12 10:55:05", int_field: 10, qux: 0.4, m2o: 1, m2m: [1, 2], amount: 1200, currency_id: 2, reference: "bar,1", }, { id: 2, foo: "blip", bar: true, int_field: 9, qux: 13, m2o: 2, m2m: [1, 2, 3], amount: 500, reference: "res.currency,1", }, { id: 3, foo: "gnap", bar: true, int_field: 17, qux: -3, m2o: 1, m2m: [], amount: 300, reference: "res.currency,2", }, { id: 4, foo: "blip", bar: false, int_field: -4, qux: 9, m2o: 1, m2m: [1], amount: 0, }, ]; } class Bar extends models.Model { name = fields.Char(); definitions = fields.PropertiesDefinition(); _records = [ { id: 1, name: "Value 1", definitions: [] }, { id: 2, name: "Value 2", definitions: [] }, { id: 3, name: "Value 3", definitions: [] }, ]; } class Currency extends models.Model { _name = "res.currency"; name = fields.Char(); symbol = fields.Char(); position = fields.Selection({ selection: [ ["after", "A"], ["before", "B"], ], }); inverse_rate = fields.Float(); _records = [ { id: 1, name: "USD", symbol: "$", position: "before", inverse_rate: 1 }, { id: 2, name: "EUR", symbol: "€", position: "after", inverse_rate: 0.5 }, ]; } defineModels([Foo, Bar, Currency, ResCompany, ResPartner, ResUsers]); async function clickControlPanelAction(buttonName) { if (getMockEnv().isSmall) { await contains(".o_control_panel_breadcrumbs .o_cp_action_menus .fa-cog").click(); await contains(`.o-dropdown-item button[name="${buttonName}"]`).click(); } else { await contains(`.o_control_panel_actions button[name="${buttonName}"]`).click(); } } async function clickRecordSelector() { if (getMockEnv().isSmall) { await contains(".o_data_row").drag(); } else { await contains(`.o_data_row .o_list_record_selector input`).click(); } } async function selectAllRecords() { if (getMockEnv().isSmall) { const cells = queryAll("tbody tr.o_data_row[data-id]:not(.o_data_row_selected)"); for (const cell of cells) { await contains(cell).drag(); } } else { await contains(`thead .o_list_record_selector input`).click(); } } async function unselectAllRecords() { if (getMockEnv().isSmall) { const cells = queryAll("tbody tr.o_data_row.o_data_row_selected[data-id]"); for (const cell of cells) { await contains(cell).drag(); } } else { await contains(`thead .o_list_record_selector input`).click(); } } async function selectMany2xItem(fieldName, value) { await contains(`.o_field_cell:has(>.o_field_widget[name="${fieldName}"])`).click(); if (getMockEnv().isSmall) { await contains(`.o_selected_row .o_field_widget[name="${fieldName}"] input`).click(); await contains(`.o_dialog .o_kanban_record:contains(${value})`).click(); } else { await selectFieldDropdownItem(fieldName, value); } } async function toggleMultiCurrencyPopover(el) { if (getMockEnv().isSmall) { await contains(el).click(); } else { await contains(el).hover(); } } test(`simple readonly list`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(`th:not(.o_list_record_selector)`).toHaveCount(2, { message: "should have 1 th for checkbox (desktop only), 2 th for columns", }); expect(`td:contains(gnap)`).toHaveCount(1, { message: "should contain gnap" }); expect(`tbody tr`).toHaveCount(4, { message: "should have 4 rows" }); expect(`th.o_column_sortable`).toHaveCount(2, { message: "should have 2 sortable column" }); expect(`thead th:not(.o_list_record_selector):eq(1) .o_list_number_th`).toHaveCount(1, { message: "header cells of integer fields should have o_list_number_th class", }); expect(`tbody tr:eq(0) td:not(.o_list_record_selector):eq(1)`).toHaveStyle( { "text-align": "right" }, { message: "integer cells should be right aligned" } ); expect(`.o_list_button_add`).toBeVisible(); expect(`.o_list_button_save`).not.toHaveCount(); expect(`.o_list_button_discard`).not.toHaveCount(); }); test.tags("desktop"); test(`select record range with shift click`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); await contains(`.o_data_row .o_list_record_selector input`).click(); expect(`.o_control_panel_actions .o_selection_box`).toHaveCount(1); expect(`.o_selection_box .o_select_domain`).toHaveCount(0); expect(`.o_selection_box`).toHaveText("1\nselected"); expect(`.o_data_row .o_list_record_selector input:checked`).toHaveCount(1); // shift click the 4th record to have 0-1-2-3 toggled await contains(`.o_data_row .o_list_record_selector input:eq(3)`).click({ shiftKey: true }); expect(`.o_selection_box`).toHaveText("4\nselected"); expect(`.o_data_row .o_list_record_selector input:checked`).toHaveCount(4); // shift click the 3rd record to untoggle 2-3 await contains(`.o_data_row .o_list_record_selector input:eq(2)`).click({ shiftKey: true }); expect(`.o_selection_box`).toHaveText("2\nselected"); expect(`.o_data_row.o_data_row_selected`).toHaveCount(2); // shift click the 1st record to untoggle 0-1 await contains(`.o_data_row .o_list_record_selector input:eq(0)`).click({ shiftKey: true }); expect(`.o_data_row.o_data_row_selected`).toHaveCount(0); }); test.tags("desktop"); test(`select record range with shift+space`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); // Go to the first checkbox and check it await press("ArrowDown"); await press("ArrowDown"); await animationFrame(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeFocused(); await contains(`.o_data_row:eq(0) .o_list_record_selector input`).click(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeChecked(); // Go to the fourth checkbox and shift+space await press("ArrowDown"); await press("ArrowDown"); await press("ArrowDown"); await animationFrame(); expect(`.o_data_row:eq(3) .o_list_record_selector input`).toBeFocused(); expect(`.o_data_row:eq(3) .o_list_record_selector input`).not.toBeChecked(); await press(["shift", "space"]); await animationFrame(); // focus is on the input and not in the td cell expect(`.o_data_row:eq(3) .o_list_record_selector input`).toBeFocused(); // Check that all checkbox is checked expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(1) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(2) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(3) .o_list_record_selector input`).toBeChecked(); }); test.tags("desktop"); test(`expand range of checkbox with shift+arrow`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); // Go to the first checkbox and check it await press("ArrowDown"); await press("ArrowDown"); await animationFrame(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeFocused(); await contains(`.o_data_row:eq(0) .o_list_record_selector input`).click(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeChecked(); // expand the checkbox with arrowdown await press(["shift", "ArrowDown"]); await press(["shift", "ArrowDown"]); await press(["shift", "ArrowDown"]); await press(["shift", "ArrowUp"]); await animationFrame(); expect(`.o_data_row:eq(2) .o_list_record_selector input`).toBeFocused(); expect(`.o_data_row:eq(2) .o_list_record_selector input`).toBeChecked(); // Check that the three checkbox are checked expect(`.o_data_row:eq(1) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(2) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(3) .o_list_record_selector input`).toBeChecked(); }); test.tags("desktop"); test(`multiple interactions to change the range of checked boxes`, async () => { for (let i = 0; i < 5; i++) { Foo._records.push({ id: 5 + i, bar: true, foo: "foo" + i }); } await mountView({ resModel: "foo", type: "list", arch: ``, }); await press("down"); await animationFrame(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).not.toBeFocused(); await keyDown("shift"); await press("down"); await animationFrame(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeFocused(); await press("down"); await press("down"); await press("down"); await press("up"); await keyUp("shift"); await press("down"); await press("down"); await press(["shift", "down"]); await animationFrame(); await contains(`.o_data_row:eq(7) .o_list_record_selector .o-checkbox`).click(); await press(["shift", "down"]); await animationFrame(); expect(`.o_data_row:eq(0) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(1) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(2) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(3) .o_list_record_selector input`).not.toBeChecked(); expect(`.o_data_row:eq(4) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(5) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(6) .o_list_record_selector input`).not.toBeChecked(); expect(`.o_data_row:eq(7) .o_list_record_selector input`).toBeChecked(); expect(`.o_data_row:eq(8) .o_list_record_selector input`).toBeChecked(); }); test(`list with class and style attributes`, async () => { await mountView({ resModel: "foo", type: "list", arch: ` `, }); expect( `.o_view_controller[style*='border: 1px solid red;'], .o_view_controller [style*='border: 1px solid red;']` ).toHaveCount(0, { message: "style attribute should not be copied" }); expect(`.o_view_controller.o_list_view.myClass`).toHaveCount(1, { message: "class attribute should be passed to the view controller", }); expect(`.myClass`).toHaveCount(1, { message: "class attribute should ONLY be passed to the view controller", }); }); test(`list with integer field with human_readable option`, async () => { Foo._records[0].int_field = 5 * 1000 * 1000; await mountView({ type: "list", resModel: "foo", arch: ` `, }); expect(queryAllTexts(".o_data_cell")).toEqual(["5M", "9", "17", "-4"]); expect(".o_field_widget").toHaveCount(0); }); test(`list with create="0"`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(`.o_list_button_add`).toHaveCount(0, { message: "should not have the 'Create' button" }); }); test.tags("desktop"); test(`searchbar in listview doesn't take focus after unselected all items`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(`.o_searchview_input`).toBeFocused({ message: "The search input should be have the focus", }); await contains(`tbody .o_data_row:eq(0) input[type="checkbox"]`).click(); await contains(`tbody input[type="checkbox"]:checked`).click(); expect(`.o_searchview_input`).not.toBeFocused({ message: "The search input shouldn't have the focus", }); }); test.tags("desktop"); test(`basic list view and command palette`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); await press(["control", "k"]); await animationFrame(); expect(queryAllTexts(`.o_command_hotkey`)).toEqual([ "New\nALT + C", "Actions\nALT + U", "Search...\nALT + Q", "Toggle search panel\nALT + SHIFT + Q", ]); }); test(`list with delete="0"`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, actionMenus: {}, }); expect(`div.o_control_panel .o_cp_action_menus`).toHaveCount(1); expect(`tbody tr.o_data_row[data-id]`).toHaveCount(4, { message: "should have 4 records" }); await clickRecordSelector(); await toggleActionMenu(); expect(`.o-dropdown-item`).toHaveCount(2); expect(queryAllTexts(`.o-dropdown-item`)).toEqual(["Export", "Duplicate"]); }); test.tags("desktop"); test(`editable list with edit="0"`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, selectRecord(resId, options) { expect.step(`switch to form - resId: ${resId} activeIds: ${options.activeIds}`); }, }); expect(`tbody tr.o_data_row[data-id]`).toHaveCount(4); await contains(`.o_data_cell`).click(); expect(`tbody tr.o_selected_row`).toHaveCount(0, { message: "should not have editable row" }); expect.verifySteps(["switch to form - resId: 1 activeIds: 1,2,3,4"]); }); test(`non-editable list with open_form_view`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(".o_optional_columns_dropdown").toHaveCount(0); expect(`td.o_list_record_open_form_view`).toHaveCount(0, { message: "button to open form view should not be present on non-editable list", }); }); test.tags("desktop"); test(`editable list with open_form_view not set`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(`td.o_list_record_open_form_view`).toHaveCount(0, { message: "button to open form view should not be present", }); }); test.tags("desktop"); test(`editable list with open_form_view`, async () => { await mountView({ resModel: "foo", type: "list", arch: ``, selectRecord(resId, options) { expect.step(`switch to form - resId: ${resId} activeIds: ${options.activeIds}`); }, }); expect(".o_optional_columns_dropdown").toHaveCount(0); expect(`td.o_list_record_open_form_view`).toHaveCount(4, { message: "button to open form view should be present on each rows", }); await contains(`td.o_list_record_open_form_view`).click(); expect.verifySteps(["switch to form - resId: 1 activeIds: 1,2,3,4"]); }); test.tags("desktop"); test(`editable list with open_form_view in debug`, async () => { serverState.debug = "1"; await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(".o_optional_columns_dropdown").toHaveCount(0); expect(`td.o_list_record_open_form_view`).toHaveCount(4, { message: "button to open form view should be present on each rows", }); }); test.tags("desktop"); test(`editable list without open_form_view in debug`, async () => { patchWithCleanup(localStorage, { getItem(key) { const value = super.getItem(...arguments); if (key.startsWith("debug_open_view")) { expect.step(["getItem", key, value]); } return value; }, setItem(key, value) { if (key.startsWith("debug_open_view")) { expect.step(["setItem", key, value]); } super.setItem(...arguments); }, }); serverState.debug = "1"; await mountView({ resModel: "foo", type: "list", arch: ``, viewId: 1, selectRecord(resId, options) { expect.step(`switch to form - resId: ${resId} activeIds: ${options.activeIds}`); }, }); const localStorageKey = "debug_open_view,foo,list,1,foo"; expect.verifySteps([["getItem", localStorageKey, null]]); expect(`td.o_list_record_open_form_view`).toHaveCount(0); expect(".o_optional_columns_dropdown").toHaveCount(1); await contains(".o_optional_columns_dropdown button").click(); expect(".o-dropdown-item:contains('View Button')").toHaveCount(1); await contains(".o-dropdown-item:contains('View Button')").click(); expect.verifySteps([ ["setItem", localStorageKey, true], ["getItem", localStorageKey, "true"], ]); expect(`td.o_list_record_open_form_view`).toHaveCount(4, { message: "button to open form view should be present on each rows", }); await contains(`td.o_list_record_open_form_view`).click(); expect.verifySteps(["switch to form - resId: 1 activeIds: 1,2,3,4"]); await contains(".o_optional_columns_dropdown button").click(); await contains(".o-dropdown-item:contains('View Button')").click(); expect.verifySteps([ ["setItem", localStorageKey, false], ["getItem", localStorageKey, "false"], ]); expect(`td.o_list_record_open_form_view`).toHaveCount(0, { message: "button to open form view should no longer be present", }); }); test(`non-editable list in debug`, async () => { serverState.debug = "1"; await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(".o_optional_columns_dropdown").toHaveCount(0); }); test(`editable readonly list with open_form_view`, async () => { Foo._fields.foo_o2m = fields.One2many({ relation: "foo" }); Foo._records.push({ id: 5, bar: true, foo: "xxx" }, { id: 6, bar: true, foo: "yyy" }); Foo._records[0].foo_o2m = [5, 6]; await mountView({ resModel: "foo", type: "form", resId: 1, arch: `
`, }); expect(`td.o_list_record_open_form_view`).toHaveCount(2, { message: "button to open form view should be present on each rows", }); }); test(`export feature in list for users not in base.group_allow_export`, async () => { onRpc("has_group", ({ args }) => args[1] !== "base.group_allow_export"); await mountView({ resModel: "foo", type: "list", arch: ``, actionMenus: {}, }); expect(`div.o_control_panel .o_cp_action_menus`).toHaveCount(0); expect(`.o_data_row`).toHaveCount(4); expect(`div.o_control_panel .o_cp_buttons .o_list_export_xlsx`).toHaveCount(0); await clickRecordSelector(); expect(`div.o_control_panel .o_cp_action_menus`).toHaveCount(1); await toggleActionMenu(); expect(queryAllTexts(`.o-dropdown--menu .o_menu_item`)).toEqual(["Duplicate", "Delete"], { message: "action menu should not contain the Export button", }); }); test(`list with export button`, async () => { onRpc("has_group", ({ args }) => args[1] === "base.group_allow_export"); await mountView({ resModel: "foo", type: "list", arch: ``, actionMenus: {}, }); expect(`div.o_control_panel .o_cp_action_menus`).toHaveCount(1); expect(`.o_data_row`).toHaveCount(4); await clickRecordSelector(); expect(`div.o_control_panel .o_cp_action_menus`).toHaveCount(1); await toggleActionMenu(); expect(queryAllTexts(`.o-dropdown--menu .o_menu_item`)).toEqual( ["Export", "Duplicate", "Delete"], { message: "action menu should have Export button" } ); }); test(`Direct export button invisible`, async () => { onRpc("has_group", ({ args }) => args[1] === "base.group_allow_export"); await mountView({ resModel: "foo", type: "list", arch: ``, }); expect(`.o_list_export_xlsx`).toHaveCount(0); }); test(`list view with adjacent buttons`, async () => { await mountView({ resModel: "foo", type: "list", arch: `