Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: FocusedInputHolder (Android) (#370)
## 馃摐 Description Added tests to `FocusedInputHolder`. Refactored code, reduced duplications, aligned to JS-based namings. ## 馃挕 Motivation and Context I decided to fully cover a new functionality with unit tests. The `ViewHierarchyTraversal` was already covered, so I decided to cover other classes. The next one `FocusedInputHolder`. This is a very simple class, so it contains two unit tests: - check that weak references implemented correctly; - check that `focus` actually calls expected methods. Along with that I did some refactoring: reduced code fragmentation, reduced code duplication, aligned API to look similar to RN JS API (and hide internals). ## 馃摙 Changelog ### iOS - use `focus` instead of `requestFocus`; - `FocusedInputHolder.shared.requestFocus()` -> `FocusedInputHolder.shared.focus()`; ### Android - added `FocusedInputHolderTests` - added `focus` extension to avoid code duplication; - use `focus` extension instead of `requestFocus`/`requestFocusFromJS`; - `FocusedInputHolder.requestFocus()` -> `FocusedInputHolder.focus()`; - `FocusedInputHolder` works with plain `EditText` (i. e. no react-specific code); - added `get` to `FocusedInputHolder`; ## 馃 How Has This Been Tested? Tested via CI. ## 馃摑 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
- Loading branch information
1 parent
e87de70
commit 667351b
Showing
9 changed files
with
87 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 10 additions & 5 deletions
15
android/src/main/java/com/reactnativekeyboardcontroller/traversal/FocusedInputHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
package com.reactnativekeyboardcontroller.traversal | ||
|
||
import com.facebook.react.views.textinput.ReactEditText | ||
import android.widget.EditText | ||
import com.reactnativekeyboardcontroller.extensions.focus | ||
import java.lang.ref.WeakReference | ||
|
||
object FocusedInputHolder { | ||
private var input: WeakReference<ReactEditText?>? = null | ||
private var input: WeakReference<EditText?>? = null | ||
|
||
fun set(textInput: ReactEditText) { | ||
fun set(textInput: EditText) { | ||
input = WeakReference(textInput) | ||
} | ||
|
||
fun requestFocus() { | ||
input?.get()?.requestFocusFromJS() | ||
fun get(): EditText? { | ||
return input?.get() | ||
} | ||
|
||
fun focus() { | ||
input?.get()?.focus() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
android/src/test/java/com/reactnativekeyboardcontroller/traversal/FocusedInputHolderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.reactnativekeyboardcontroller.traversal | ||
|
||
import android.content.Context | ||
import android.widget.EditText | ||
import androidx.test.core.app.ApplicationProvider | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class FocusedInputHolderTest { | ||
@Test | ||
fun `FocusedInputHolder should hold a weak reference`() { | ||
val context = ApplicationProvider.getApplicationContext<Context>() | ||
var input: EditText? = EditText(context) | ||
|
||
FocusedInputHolder.set(input as EditText) | ||
|
||
assertEquals(FocusedInputHolder.get(), input) | ||
|
||
input = null | ||
|
||
@Suppress("detekt:ExplicitGarbageCollectionCall") | ||
System.gc() | ||
|
||
assertNull(FocusedInputHolder.get()) | ||
} | ||
|
||
@Test | ||
fun `focus() should request focus on expected field`() { | ||
val context = ApplicationProvider.getApplicationContext<Context>() | ||
val input = EditText(context) | ||
|
||
assertFalse(input.hasFocus()) | ||
|
||
FocusedInputHolder.set(input) | ||
FocusedInputHolder.focus() | ||
|
||
assertTrue(input.hasFocus()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters