|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.views.scroll |
| 9 | + |
| 10 | +import android.view.View |
| 11 | +import android.widget.FrameLayout |
| 12 | +import org.assertj.core.api.Assertions.assertThat |
| 13 | +import org.junit.Before |
| 14 | +import org.junit.Test |
| 15 | +import org.junit.runner.RunWith |
| 16 | +import org.robolectric.RobolectricTestRunner |
| 17 | +import org.robolectric.RuntimeEnvironment |
| 18 | + |
| 19 | +/** Verifies the containment check used by the scroll views' keyboard focus search. */ |
| 20 | +@RunWith(RobolectricTestRunner::class) |
| 21 | +class ReactScrollViewHelperFocusTest { |
| 22 | + |
| 23 | + private lateinit var host: FrameLayout |
| 24 | + private lateinit var content: FrameLayout |
| 25 | + |
| 26 | + @Before |
| 27 | + fun setup() { |
| 28 | + val context = RuntimeEnvironment.getApplication() |
| 29 | + host = FrameLayout(context) |
| 30 | + content = FrameLayout(context) |
| 31 | + host.addView(content) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun hostItselfIsIncluded() { |
| 36 | + assertThat(ReactScrollViewHelper.isSelfOrDescendant(host, host)).isTrue() |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun descendantWithIdIsIncluded() { |
| 41 | + val child = View(host.context).apply { id = 42 } |
| 42 | + content.addView(child) |
| 43 | + |
| 44 | + assertThat(ReactScrollViewHelper.isSelfOrDescendant(host, child)).isTrue() |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun descendantWithoutIdIsIncluded() { |
| 49 | + // For example the AndroidComposeView that a ComposeView adds as its child. |
| 50 | + val child = View(host.context) |
| 51 | + content.addView(child) |
| 52 | + |
| 53 | + assertThat(child.id).isEqualTo(View.NO_ID) |
| 54 | + assertThat(ReactScrollViewHelper.isSelfOrDescendant(host, child)).isTrue() |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun viewOutsideHostIsExcludedEvenIfIdMatchesADescendant() { |
| 59 | + content.addView(View(host.context).apply { id = 42 }) |
| 60 | + val parent = FrameLayout(host.context) |
| 61 | + parent.addView(host) |
| 62 | + val outside = View(host.context).apply { id = 42 } |
| 63 | + parent.addView(outside) |
| 64 | + |
| 65 | + assertThat(ReactScrollViewHelper.isSelfOrDescendant(host, outside)).isFalse() |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun detachedViewIsExcluded() { |
| 70 | + assertThat(ReactScrollViewHelper.isSelfOrDescendant(host, View(host.context))).isFalse() |
| 71 | + } |
| 72 | +} |
0 commit comments