Skip to content

Commit

Permalink
test: try to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu committed Mar 28, 2024
1 parent 28f220b commit 436ae22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/main/kotlin/io/snyk/plugin/ui/UIUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -373,3 +376,14 @@ fun expandTreeNodeRecursively(tree: JTree, node: DefaultMutableTreeNode) {
expandTreeNodeRecursively(tree, it as DefaultMutableTreeNode)
}
}

//JComponent, String
fun getJBCefBrowserIfSupported () : Pair<JBCefBrowser?, String> {
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -49,25 +48,22 @@ 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(
wrapWithScrollPane(panel),
BorderLayout.CENTER
)

jbCefBrowser.loadHTML(issue.additionalData.details, jbCefBrowser.cefBrowser.url)
jbCefBrowser.loadHTML(issue.additionalData.details, jbCefBrowserUrl)
}
} else {

createUI()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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>HTML message</html>"
cut = SuggestionDescriptionPanelFromLS(snykCodeFile, issue)

Registry.get("ide.browser.jcef.enabled").setValue("false")

val actual = getJLabelByText(cut, "<html>Test message</html>")
assertNull(actual)

Expand All @@ -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<JBCefBrowser>()
every { mockJBCefBrowser.component } returns JPanel()
every { mockJBCefBrowser.loadHTML(eq("<html>HTML message</html>"), 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>HTML message</html>"
cut = SuggestionDescriptionPanelFromLS(snykCodeFile, issue)

Registry.get("ide.browser.jcef.enabled").setValue("true")

val actual = getJLabelByText(cut, "<html>Test message</html>")
assertNull(actual)

val actualBrowser = getJBCEFBrowser(cut)
assertNotNull(actualBrowser)
}
}

0 comments on commit 436ae22

Please sign in to comment.