diff --git a/src/main/kotlin/io/snyk/plugin/ui/UIUtils.kt b/src/main/kotlin/io/snyk/plugin/ui/UIUtils.kt index 655716b01..330d35ea9 100644 --- a/src/main/kotlin/io/snyk/plugin/ui/UIUtils.kt +++ b/src/main/kotlin/io/snyk/plugin/ui/UIUtils.kt @@ -6,6 +6,9 @@ import com.intellij.ui.BrowserHyperlinkListener import com.intellij.ui.ColorUtil import com.intellij.ui.ScrollPaneFactory import com.intellij.ui.components.ActionLink +import com.intellij.ui.jcef.JBCefApp +import com.intellij.ui.jcef.JBCefBrowser +import com.intellij.ui.jcef.JBCefBrowserBuilder import com.intellij.uiDesigner.core.GridConstraints import com.intellij.uiDesigner.core.GridLayoutManager import com.intellij.uiDesigner.core.Spacer @@ -373,3 +376,14 @@ fun expandTreeNodeRecursively(tree: JTree, node: DefaultMutableTreeNode) { expandTreeNodeRecursively(tree, it as DefaultMutableTreeNode) } } + +//JComponent, String +fun getJBCefBrowserIfSupported () : Pair { + if (!JBCefApp.isSupported()) { + return null to "" + } + val cefClient = JBCefApp.getInstance().createClient() + val jbCefBrowser = JBCefBrowserBuilder().setClient(cefClient).setEnableOpenDevToolsMenuItem(true).build() + + return jbCefBrowser to jbCefBrowser.cefBrowser.url +} diff --git a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt index 79f679ad3..65061eede 100644 --- a/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt +++ b/src/main/kotlin/io/snyk/plugin/ui/toolwindow/panels/SuggestionDescriptionPanelFromLS.kt @@ -4,8 +4,6 @@ import com.intellij.icons.AllIcons import com.intellij.ui.HyperlinkLabel import com.intellij.ui.ScrollPaneFactory import com.intellij.ui.components.JBTabbedPane -import com.intellij.ui.jcef.JBCefApp -import com.intellij.ui.jcef.JBCefBrowserBuilder import com.intellij.uiDesigner.core.GridConstraints import com.intellij.uiDesigner.core.GridLayoutManager import com.intellij.util.ui.JBInsets @@ -19,6 +17,7 @@ import io.snyk.plugin.ui.DescriptionHeaderPanel import io.snyk.plugin.ui.SnykBalloonNotificationHelper import io.snyk.plugin.ui.baseGridConstraintsAnchorWest import io.snyk.plugin.ui.descriptionHeaderPanel +import io.snyk.plugin.ui.getJBCefBrowserIfSupported import io.snyk.plugin.ui.panelGridConstraints import io.snyk.plugin.ui.toolwindow.SnykToolWindowPanel import io.snyk.plugin.ui.wrapWithScrollPane @@ -49,14 +48,12 @@ class SuggestionDescriptionPanelFromLS( init { if (pluginSettings().isGlobalIgnoresFeatureEnabled && issue.additionalData.details != null) { - if (!JBCefApp.isSupported()) { + val (jbCefBrowser, jbCefBrowserUrl) = getJBCefBrowserIfSupported() + if (jbCefBrowser == null) { val statePanel = StatePanel(SnykToolWindowPanel.SELECT_ISSUE_TEXT) this.add(wrapWithScrollPane(statePanel), BorderLayout.CENTER) SnykBalloonNotificationHelper.showError(unexpectedErrorMessage, null) } else { - val cefClient = JBCefApp.getInstance().createClient() - val jbCefBrowser = JBCefBrowserBuilder().setClient(cefClient).setEnableOpenDevToolsMenuItem(true).build() - val panel = JPanel() panel.add(jbCefBrowser.component, BorderLayout()) this.add( @@ -64,10 +61,9 @@ class SuggestionDescriptionPanelFromLS( BorderLayout.CENTER ) - jbCefBrowser.loadHTML(issue.additionalData.details, jbCefBrowser.cefBrowser.url) + jbCefBrowser.loadHTML(issue.additionalData.details, jbCefBrowserUrl) } } else { - createUI() } } diff --git a/src/test/kotlin/io/snyk/plugin/ui/toolwindow/SuggestionDescriptionPanelFromLSTest.kt b/src/test/kotlin/io/snyk/plugin/ui/toolwindow/SuggestionDescriptionPanelFromLSTest.kt index 2f8b03b9b..13bd831de 100644 --- a/src/test/kotlin/io/snyk/plugin/ui/toolwindow/SuggestionDescriptionPanelFromLSTest.kt +++ b/src/test/kotlin/io/snyk/plugin/ui/toolwindow/SuggestionDescriptionPanelFromLSTest.kt @@ -2,18 +2,22 @@ package io.snyk.plugin.ui.toolwindow import com.intellij.openapi.application.WriteAction -import com.intellij.openapi.util.registry.Registry import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiFile import com.intellij.testFramework.fixtures.BasePlatformTestCase +import com.intellij.ui.jcef.JBCefBrowser import io.mockk.every import io.mockk.mockk +import io.mockk.mockkStatic import io.mockk.unmockkAll import io.snyk.plugin.Severity import io.snyk.plugin.pluginSettings import io.snyk.plugin.resetSettings import io.snyk.plugin.snykcode.core.SnykCodeFile +import io.snyk.plugin.ui.getJBCefBrowserIfSupported import io.snyk.plugin.ui.toolwindow.panels.SuggestionDescriptionPanelFromLS +import org.cef.browser.CefBrowserFactory +import org.cef.browser.CefRequestContext import org.eclipse.lsp4j.Position import org.eclipse.lsp4j.Range import org.junit.Before @@ -26,6 +30,7 @@ import snyk.common.lsp.DataFlow import snyk.common.lsp.ExampleCommitFix import snyk.common.lsp.ScanIssue import java.nio.file.Paths +import javax.swing.JPanel class SuggestionDescriptionPanelFromLSTest : BasePlatformTestCase() { private lateinit var cut: SuggestionDescriptionPanelFromLS @@ -96,11 +101,12 @@ class SuggestionDescriptionPanelFromLSTest : BasePlatformTestCase() { fun `test createUI should show nothing if feature flag is enabled but JCEF is not`() { pluginSettings().isGlobalIgnoresFeatureEnabled = true + mockkStatic("io.snyk.plugin.ui.UIUtilsKt") + every { getJBCefBrowserIfSupported() } returns (null to "") + every { issue.additionalData.details } returns "HTML message" cut = SuggestionDescriptionPanelFromLS(snykCodeFile, issue) - Registry.get("ide.browser.jcef.enabled").setValue("false") - val actual = getJLabelByText(cut, "Test message") assertNull(actual) @@ -112,15 +118,16 @@ class SuggestionDescriptionPanelFromLSTest : BasePlatformTestCase() { fun `test createUI should build panel with HTML from details if feature flag is enabled`() { pluginSettings().isGlobalIgnoresFeatureEnabled = true + val mockJBCefBrowser = mockk() + every { mockJBCefBrowser.component } returns JPanel() + every { mockJBCefBrowser.loadHTML(eq("HTML message"), eq("http://foo/bar")) } returns + mockkStatic("io.snyk.plugin.ui.UIUtilsKt") + every { getJBCefBrowserIfSupported() } returns (mockJBCefBrowser to "http://foo/bar") + every { issue.additionalData.details } returns "HTML message" cut = SuggestionDescriptionPanelFromLS(snykCodeFile, issue) - Registry.get("ide.browser.jcef.enabled").setValue("true") - val actual = getJLabelByText(cut, "Test message") assertNull(actual) - - val actualBrowser = getJBCEFBrowser(cut) - assertNotNull(actualBrowser) } }