Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Material 3 DropdownMenu Skiko support #347

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions compose/material3/material3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
}

desktopMain.dependsOn(skikoMain)
jsMain.dependsOn(skikoMain)
nativeMain.dependsOn(skikoMain)

jsNativeMain.dependsOn(skikoMain)

jsMain.dependsOn(jsNativeMain)
nativeMain.dependsOn(jsNativeMain)

// TODO(b/214407011): These dependencies leak into instrumented tests as well. If you
// need to add Robolectric (which must be kept out of androidAndroidTest), use a top
Expand Down Expand Up @@ -149,6 +152,13 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
implementation(libs.mockitoKotlin)
implementation(libs.testUiautomator)
}

desktopTest.dependencies {
implementation(project(":compose:ui:ui-test-junit4"))
implementation(libs.truth)
implementation(libs.junit)
implementation(libs.skikoCurrentOs)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material3

import android.view.KeyEvent.KEYCODE_MOVE_END
import android.view.KeyEvent.KEYCODE_MOVE_HOME
import android.view.KeyEvent.KEYCODE_PAGE_UP
import android.view.KeyEvent.KEYCODE_PAGE_DOWN
import android.view.KeyEvent.KEYCODE_DPAD_UP
import android.view.KeyEvent.KEYCODE_DPAD_DOWN
import android.view.KeyEvent.KEYCODE_DPAD_LEFT
import android.view.KeyEvent.KEYCODE_DPAD_RIGHT
import android.view.KeyEvent.KEYCODE_ESCAPE
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.nativeKeyCode

internal actual val KeyEvent.isDirectionUp: Boolean
get() = key.nativeKeyCode == KEYCODE_DPAD_UP

internal actual val KeyEvent.isDirectionDown: Boolean
get() = key.nativeKeyCode == KEYCODE_DPAD_DOWN

internal actual val KeyEvent.isDirectionRight: Boolean
get() = key.nativeKeyCode == KEYCODE_DPAD_RIGHT

internal actual val KeyEvent.isDirectionLeft: Boolean
get() = key.nativeKeyCode == KEYCODE_DPAD_LEFT

internal actual val KeyEvent.isHome: Boolean
get() = key.nativeKeyCode == KEYCODE_MOVE_HOME

internal actual val KeyEvent.isMoveEnd: Boolean
get() = key.nativeKeyCode == KEYCODE_MOVE_END

internal actual val KeyEvent.isPgUp: Boolean
get() = key.nativeKeyCode == KEYCODE_PAGE_UP

internal actual val KeyEvent.isPgDn: Boolean
get() = key.nativeKeyCode == KEYCODE_PAGE_DOWN

internal actual val KeyEvent.isPgDn: Boolean
get() = key.nativeKeyCode == KEYCODE_PAGE_DOWN

internal actual val KeyEvent.isEsc: Boolean
get() = key.nativeKeyCode == KEYCODE_ESCAPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material3

import androidx.compose.ui.input.key.KeyEvent

internal expect val KeyEvent.isDirectionUp: Boolean
internal expect val KeyEvent.isDirectionDown: Boolean
internal expect val KeyEvent.isDirectionRight: Boolean
internal expect val KeyEvent.isDirectionLeft: Boolean
internal expect val KeyEvent.isHome: Boolean
internal expect val KeyEvent.isMoveEnd: Boolean
internal expect val KeyEvent.isPgUp: Boolean
internal expect val KeyEvent.isPgDn: Boolean
internal expect val KeyEvent.isEsc: Boolean
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material3

import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.nativeKeyCode

internal actual val KeyEvent.isDirectionUp: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_UP

internal actual val KeyEvent.isDirectionDown: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_DOWN

internal actual val KeyEvent.isDirectionRight: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_RIGHT

internal actual val KeyEvent.isDirectionLeft: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_LEFT

internal actual val KeyEvent.isHome: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_HOME

internal actual val KeyEvent.isMoveEnd: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_END

internal actual val KeyEvent.isPgUp: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_PAGE_UP

internal actual val KeyEvent.isPgDn: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_PAGE_DOWN

internal actual val KeyEvent.isEsc: Boolean
get() = key.nativeKeyCode == java.awt.event.KeyEvent.VK_ESCAPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material3

import androidx.compose.material3.internal.keyEvent
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performKeyPress
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import com.google.common.truth.Truth.assertThat
import org.junit.Assert
import org.junit.Rule
import org.junit.runners.JUnit4
import org.junit.runner.RunWith
import org.junit.Test

@RunWith(JUnit4::class)
class DesktopMenuTest {

@get:Rule
val rule = createComposeRule()

val windowSize = IntSize(100, 100)
val anchorPosition = IntOffset(10, 10)
val anchorSize = IntSize(80, 20)

@Test
fun menu_positioning_vertical_underAnchor() {
val popupSize = IntSize(80, 70)

val position = SkikoDropdownMenuPositionProvider(
DpOffset.Zero,
Density(1f)
).calculatePosition(
IntRect(anchorPosition, anchorSize),
windowSize,
LayoutDirection.Ltr,
popupSize
)

assertThat(position).isEqualTo(IntOffset(10, 30))
}

@Test
fun menu_positioning_vertical_windowTop() {
val popupSize = IntSize(80, 100)

val position = SkikoDropdownMenuPositionProvider(
DpOffset.Zero,
Density(1f)
).calculatePosition(
IntRect(anchorPosition, anchorSize),
windowSize,
LayoutDirection.Ltr,
popupSize
)

assertThat(position).isEqualTo(IntOffset(10, 0))
}

@OptIn(ExperimentalComposeUiApi::class)
@Test
fun `pressing ESC button invokes onDismissRequest`() {
var dismissCount = 0
rule.setContent {
CompositionLocalProvider(LocalDensity provides Density(1f, 1f)) {
DropdownMenu(true, onDismissRequest = {
dismissCount++
}, modifier = Modifier.testTag("dropDownMenu")) {
DropdownMenuItem(onClick = {}, text = { Text("item1") })
}
}
}

rule.onNodeWithTag("dropDownMenu")
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyDown))

rule.runOnIdle {
Assert.assertEquals(1, dismissCount)
}

rule.onNodeWithTag("dropDownMenu")
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyUp))

rule.runOnIdle {
Assert.assertEquals(1, dismissCount)
}
}

@OptIn(ExperimentalComposeUiApi::class)
@Test
fun `navigate DropDownMenu using arrows`() {
var item1Clicked = 0
var item2Clicked = 0
var item3Clicked = 0

rule.setContent {
CompositionLocalProvider(LocalDensity provides Density(1f, 1f)) {
DropdownMenu(true, onDismissRequest = {},
modifier = Modifier.testTag("dropDownMenu")) {
DropdownMenuItem(
text = { Text("item1") },
onClick = { item1Clicked++ }
)
DropdownMenuItem(
text = { Text("item2") },
onClick = { item2Clicked++ }
)
DropdownMenuItem(
text = { Text("item3") },
onClick = { item3Clicked++ }
)
}
}
}

fun performKeyDownAndUp(key: Key) {
rule.onNodeWithTag("dropDownMenu").apply {
performKeyPress(keyEvent(key, KeyEventType.KeyDown))
performKeyPress(keyEvent(key, KeyEventType.KeyUp))
}
}

fun assertClicksCount(i1: Int, i2: Int, i3: Int) {
rule.runOnIdle {
assertThat(item1Clicked).isEqualTo(i1)
assertThat(item2Clicked).isEqualTo(i2)
assertThat(item3Clicked).isEqualTo(i3)
}
}

performKeyDownAndUp(Key.DirectionDown)
performKeyDownAndUp(Key.Enter)
assertClicksCount(1, 0, 0)

performKeyDownAndUp(Key.DirectionUp)
performKeyDownAndUp(Key.Enter)
assertClicksCount(1, 0, 1)

performKeyDownAndUp(Key.DirectionUp)
performKeyDownAndUp(Key.Enter)
assertClicksCount(1, 1, 1)

performKeyDownAndUp(Key.DirectionDown)
performKeyDownAndUp(Key.Enter)
assertClicksCount(1, 1, 2)

performKeyDownAndUp(Key.DirectionDown)
performKeyDownAndUp(Key.Enter)
assertClicksCount(2, 1, 2)

performKeyDownAndUp(Key.DirectionDown)
performKeyDownAndUp(Key.Enter)
assertClicksCount(2, 2, 2)
}
}