From f1d2b4dd429745a3e1fb0fa6299bb62392981679 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 26 Nov 2019 18:49:14 +0300 Subject: [PATCH] ui: add test for AnAction.setShortcutSet warning follow-up: 18b7feeecd2847e97402ac0664ac488a0bfc99fe GitOrigin-RevId: 762ec465c48ab8e57f67113015dd0f033175cb5e --- .../actions/BadActionShortcutCheckTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 platform/platform-tests/testSrc/com/intellij/ide/actions/BadActionShortcutCheckTest.java diff --git a/platform/platform-tests/testSrc/com/intellij/ide/actions/BadActionShortcutCheckTest.java b/platform/platform-tests/testSrc/com/intellij/ide/actions/BadActionShortcutCheckTest.java new file mode 100644 index 000000000000..fef2797f94cc --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/ide/actions/BadActionShortcutCheckTest.java @@ -0,0 +1,80 @@ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.ide.actions; + +import com.intellij.openapi.actionSystem.ActionManager; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CustomShortcutSet; +import com.intellij.testFramework.LightPlatformTestCase; +import com.intellij.testFramework.LoggedErrorProcessor; +import com.intellij.testFramework.RunAll; +import com.intellij.util.containers.ContainerUtil; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.util.List; + +public class BadActionShortcutCheckTest extends LightPlatformTestCase { + private static final String MARKER = "ShortcutSet of global AnActions should not be changed outside of KeymapManager"; + + private final List myLoggedWarnings = ContainerUtil.createConcurrentList(); + + @Override + public void setUp() throws Exception { + super.setUp(); + LoggedErrorProcessor.setNewInstance(new LoggedErrorProcessor() { + @Override + public void processWarn(String message, Throwable t, @NotNull Logger logger) { + super.processWarn(message, t, logger); + myLoggedWarnings.add(message); + } + }); + } + + @Override + public void tearDown() throws Exception { + new RunAll() + .append(() -> myLoggedWarnings.clear()) + .append(() -> LoggedErrorProcessor.restoreDefaultProcessor()) + .append(() -> super.tearDown()) + .run(); + } + + public void testActionCanChangeShortcut() { + AnAction action1 = new AnAction() { + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + } + }; + AnAction action2 = ActionManager.getInstance().getAction("$Delete"); + JPanel component = new JPanel(); + + action1.registerCustomShortcutSet(action2.getShortcutSet(), component); + assertWarningShown(false); + } + + public void testGlobalActionCantChangeShortcut1() { + AnAction action1 = ActionManager.getInstance().getAction("$Copy"); + AnAction action2 = ActionManager.getInstance().getAction("$Delete"); + JPanel component = new JPanel(); + + action1.registerCustomShortcutSet(action2.getShortcutSet(), component); + assertWarningShown(true); + } + + public void testGlobalActionCantChangeShortcut2() { + AnAction action = ActionManager.getInstance().getAction("$Copy"); + JPanel component = new JPanel(); + + action.registerCustomShortcutSet(CustomShortcutSet.EMPTY, component); + assertWarningShown(true); + } + + private void assertWarningShown(boolean isExpected) { + boolean hasWarning = ContainerUtil.exists(myLoggedWarnings, message -> message.contains(MARKER)); + if (hasWarning != isExpected) { + fail(myLoggedWarnings.toString()); + } + } +}