Skip to content

Commit

Permalink
Fix parsing of navigate CloudNotificationAction
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jul 18, 2024
1 parent d92e0ab commit b6326c1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ data class CloudNotificationAction internal constructor(
private val internalAction: String
) : Parcelable {
sealed class Action {
class UrlAction(val url: String) : Action()
class ItemCommandAction(val itemName: String, val command: String) : Action()
class UiCommandAction(val command: String) : Action()
object NoAction : Action()
data class UrlAction(val url: String) : Action()
data class ItemCommandAction(val itemName: String, val command: String) : Action()
data class UiCommandAction(val command: String) : Action()
data object NoAction : Action()
}

val action: Action get() {
Expand All @@ -164,7 +164,7 @@ data class CloudNotificationAction internal constructor(
internalAction.startsWith("http://") || internalAction.startsWith("https://") ->
Action.UrlAction(internalAction)
split[0] == "ui" && split.size == 3 -> {
Action.UiCommandAction("${split[1]}${split[2]}")
Action.UiCommandAction("${split[1]}:${split[2]}")
}
split[0] == "ui" && split.size == 2 -> {
Action.UiCommandAction("navigate:${split[1]}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.openhab.habdroid.model

import org.junit.Assert.assertEquals
import org.junit.Test

class CloudNotificationTest {
@Test
fun testStringToCloudNotificationAction() {
val commandAction = "Turn on=command:foo:ON".toCloudNotificationAction()!!
assertEquals("Turn on", commandAction.label)
assertEquals(commandAction.action, CloudNotificationAction.Action.ItemCommandAction("foo", "ON"))

val urlAction = "Open website=https://example.com".toCloudNotificationAction()!!
assertEquals("Open website", urlAction.label)
assertEquals(CloudNotificationAction.Action.UrlAction("https://example.com"), urlAction.action)

val uiCommandAction = "Open page=ui:/foo".toCloudNotificationAction()!!
assertEquals("Open page", uiCommandAction.label)
assertEquals(CloudNotificationAction.Action.UiCommandAction("navigate:/foo"), uiCommandAction.action)

val navigateUiCommandAction = "Open page2=ui:navigate:/foo".toCloudNotificationAction()!!
assertEquals("Open page2", navigateUiCommandAction.label)
assertEquals(CloudNotificationAction.Action.UiCommandAction("navigate:/foo"), navigateUiCommandAction.action)

val invalidAction = "Invalid=foo".toCloudNotificationAction()!!
assertEquals("Invalid", invalidAction.label)
assertEquals(CloudNotificationAction.Action.NoAction, invalidAction.action)
}
}

0 comments on commit b6326c1

Please sign in to comment.