From 9129226b6b2e93c20390f1427b6a3c6f14600648 Mon Sep 17 00:00:00 2001 From: Martin Vysny Date: Tue, 6 Aug 2024 20:04:37 +0300 Subject: [PATCH] Add support for opening/closing ContextMenu/GridContextMenu --- .../kaributesting/v10/ContextMenuTest.kt | 72 +++++++++++++++++++ .../mvysny/kaributesting/v10/ContextMenu.kt | 15 ++++ 2 files changed, 87 insertions(+) diff --git a/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenuTest.kt b/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenuTest.kt index d6e1abc4..5056936c 100644 --- a/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenuTest.kt +++ b/karibu-testing-v10/kt10-tests/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenuTest.kt @@ -197,6 +197,41 @@ internal fun DynaNodeGroup.contextMenuTestbatch() { expect(1) { clicked } } + test("setOpened(true) fires ContextMenuBase.OpenedChangeEvent") { + var called = false + lateinit var cm: ContextMenu + UI.getCurrent().div { + cm = contextMenu { + item("click me") + } + } + cm.addOpenedChangeListener { e -> + called = true + expect(true) { e.isOpened } + expect(false) { e.isFromClient } + } + cm.setOpened(true) + expect(true) { called } + } + + test("setOpened(false) fires ContextMenuBase.OpenedChangeEvent") { + var called = false + lateinit var cm: ContextMenu + UI.getCurrent().div { + cm = contextMenu { + item("click me") + } + } + cm.setOpened(true) + cm.addOpenedChangeListener { e -> + called = true + expect(false) { e.isOpened } + expect(false) { e.isFromClient } + } + cm.setOpened(false) + expect(true) { called } + } + group("grid context menu") { test("simple click") { lateinit var clicked: String @@ -308,5 +343,42 @@ internal fun DynaNodeGroup.contextMenuTestbatch() { expect(true) { listenerCalled } expect("foo") { clicked } } + + test("setOpened(true) fires GridContextMenuOpenedEvent") { + var called = false + lateinit var cm: GridContextMenu + UI.getCurrent().grid { + cm = gridContextMenu { + item("click me") + } + } + cm.addGridContextMenuOpenedListener() { e -> + called = true + expect(true) { e.isOpened } + expect(false) { e.isFromClient } + expect("foo") { e.item.orElse(null) } + } + cm.setOpened(true, "foo") + expect(true) { called } + } + + test("setOpened(false) fires GridContextMenuOpenedEvent") { + var called = false + lateinit var cm: GridContextMenu + UI.getCurrent().grid { + cm = gridContextMenu { + item("click me") + } + } + cm.setOpened(true, "foo") + cm.addGridContextMenuOpenedListener { e -> + called = true + expect(false) { e.isOpened } + expect(false) { e.isFromClient } + expect("foo") { e.item.orElse(null) } + } + cm.setOpened(false, "foo") + expect(true) { called } + } } } diff --git a/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenu.kt b/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenu.kt index dc0233d5..e3d64b7b 100644 --- a/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenu.kt +++ b/karibu-testing-v10/src/main/kotlin/com/github/mvysny/kaributesting/v10/ContextMenu.kt @@ -250,3 +250,18 @@ private fun MenuItemBase<*, *, *>.checkMenuItemEnabled(originalItem: MenuItemBas } } } + +/** + * Opens or closes the menu. Fires the [ContextMenuBase.OpenedChangeEvent]. + */ +public fun ContextMenu.setOpened(opened: Boolean) { + element.setProperty("opened", opened) +} + +/** + * Opens or closes the menu. Fires the [ContextMenuBase.OpenedChangeEvent]. + */ +public fun GridContextMenu.setOpened(opened: Boolean, gridItem: T?) { + _setContextMenuTargetItemKey(gridItem) + element.setProperty("opened", opened) +}