-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #395 from ForgeRock/SDKS-545
SDKS 545 - TextInputCallback Support
- Loading branch information
Showing
10 changed files
with
377 additions
and
6 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
59 changes: 59 additions & 0 deletions
59
...th-ui/src/main/java/org/forgerock/android/auth/ui/callback/TextInputCallbackFragment.java
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,59 @@ | ||
/* | ||
* Copyright (c) 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
package org.forgerock.android.auth.ui.callback; | ||
|
||
import android.os.Bundle; | ||
import android.text.Editable; | ||
import android.text.TextWatcher; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.EditText; | ||
|
||
import com.google.android.material.textfield.TextInputLayout; | ||
|
||
import org.forgerock.android.auth.callback.TextInputCallback; | ||
import org.forgerock.android.auth.ui.R; | ||
|
||
/** | ||
* UI representation for {@link TextInputCallback} | ||
*/ | ||
public class TextInputCallbackFragment extends CallbackFragment<TextInputCallback> { | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
// Inflate the layout for this fragment | ||
final View view = inflater.inflate(R.layout.fragment_text_input_callback, container, false); | ||
|
||
EditText text = view.findViewById(R.id.text); | ||
if (callback.getDefaultText() != null) { | ||
text.setText(callback.getDefaultText().toString()); | ||
} | ||
TextInputLayout textInputLayout = view.findViewById(R.id.textInputLayout); | ||
textInputLayout.setHint(callback.getPrompt()); | ||
text.addTextChangedListener(new TextWatcher() { | ||
@Override | ||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||
|
||
} | ||
|
||
@Override | ||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
|
||
} | ||
|
||
@Override | ||
public void afterTextChanged(Editable s) { | ||
callback.setValue(s.toString()); | ||
onDataCollected(); | ||
} | ||
}); | ||
return view; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
forgerock-auth-ui/src/main/res/layout/fragment_text_input_callback.xml
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,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright (c) 2024 ForgeRock. All rights reserved. | ||
~ | ||
~ This software may be modified and distributed under the terms | ||
~ of the MIT license. See the LICENSE file for details. | ||
--> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".callback.TextInputCallbackFragment" android:id="@+id/frameLayout"> | ||
|
||
<com.google.android.material.textfield.TextInputLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/textInputLayout" | ||
app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:passwordToggleEnabled="true"> | ||
|
||
<com.google.android.material.textfield.TextInputEditText | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/text" | ||
android:inputType="text"/> | ||
</com.google.android.material.textfield.TextInputLayout> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
72 changes: 72 additions & 0 deletions
72
...-auth/src/androidTest/java/org/forgerock/android/auth/callback/TextInputCallbackTest.java
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,72 @@ | ||
/* | ||
* Copyright (c) 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
package org.forgerock.android.auth.callback; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.forgerock.android.auth.AndroidBaseTest; | ||
import org.forgerock.android.auth.FRSession; | ||
import org.forgerock.android.auth.Node; | ||
import org.forgerock.android.auth.NodeListener; | ||
import org.forgerock.android.auth.NodeListenerFuture; | ||
import org.forgerock.android.auth.UsernamePasswordNodeListener; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
public class TextInputCallbackTest extends AndroidBaseTest { | ||
protected final static String TREE = "TextInputCallbackTest"; | ||
|
||
@Test | ||
public void testTextInputCallback() throws ExecutionException, InterruptedException { | ||
final int[] textInputCallbackReceived = {0}; | ||
final int[] success = {0}; | ||
NodeListenerFuture<FRSession> nodeListenerFuture = new UsernamePasswordNodeListener(context) { | ||
final NodeListener<FRSession> nodeListener = this; | ||
|
||
@Override | ||
public void onCallbackReceived(Node node) { | ||
if (node.getCallback(NameCallback.class) != null) { | ||
NameCallback nameCallback = node.getCallback(NameCallback.class); | ||
nameCallback.setValue(USERNAME); | ||
node.next(context, this ); | ||
return; | ||
} | ||
if (node.getCallback(TextInputCallback.class) != null) { | ||
TextInputCallback callback = node.getCallback(TextInputCallback.class); | ||
assertThat(callback.getPrompt()).isEqualTo("What is your username?"); | ||
assertThat(callback.getDefaultText()).isEqualTo("ForgerRocker"); | ||
textInputCallbackReceived[0]++; | ||
callback.setValue(USERNAME); | ||
node.next(context, nodeListener); | ||
return; | ||
} | ||
// This step here is to ensure that the SDK correctly sets the value in the TextInputCallback... | ||
// The values entered in the NameCallback and TextInputCallback above should match for "success" | ||
if (node.getCallback(TextOutputCallback.class) != null) { | ||
TextOutputCallback callback = node.getCallback(TextOutputCallback.class); | ||
assertThat(callback.getMessage()).isEqualTo("Success"); | ||
success[0]++; | ||
node.next(context, nodeListener); | ||
return; | ||
} | ||
super.onCallbackReceived(node); | ||
} | ||
}; | ||
|
||
FRSession.authenticate(context, TREE, nodeListenerFuture); | ||
Assert.assertNotNull(nodeListenerFuture.get()); | ||
assertThat(textInputCallbackReceived[0]).isEqualTo(1); | ||
assertThat(success[0]).isEqualTo(1); | ||
|
||
// Ensure that the journey finishes with success | ||
Assert.assertNotNull(FRSession.getCurrentSession()); | ||
Assert.assertNotNull(FRSession.getCurrentSession().getSessionToken()); | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
forgerock-auth/src/main/java/org/forgerock/android/auth/callback/TextInputCallback.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,78 @@ | ||
/* | ||
* Copyright (c) 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
package org.forgerock.android.auth.callback | ||
|
||
import androidx.annotation.Keep | ||
import org.json.JSONObject | ||
|
||
/** | ||
* Callback for collection of a single text input attribute from a user. | ||
* | ||
* | ||
*/ | ||
class TextInputCallback : AbstractPromptCallback { | ||
|
||
/** | ||
* TextInputCallback sample. | ||
* | ||
* { | ||
* "type": "TextInputCallback", | ||
* "output": [ | ||
* { | ||
* "name": "prompt", | ||
* "value": "Text input" | ||
* }, | ||
* { | ||
* "name": "defaultText", | ||
* "value": "" | ||
* } | ||
* ], | ||
* "input": [ | ||
* { | ||
* "name": "IDToken1", | ||
* "value": "" | ||
* } | ||
* ] | ||
* } | ||
* | ||
*/ | ||
|
||
/** | ||
* The text to be used as the default text displayed with the prompt. | ||
*/ | ||
var defaultText: String? = null | ||
private set | ||
|
||
@Keep | ||
@JvmOverloads | ||
constructor() : super() | ||
|
||
@Keep | ||
@JvmOverloads | ||
constructor(raw: JSONObject?, index: Int) : super(raw, index) | ||
|
||
override fun setAttribute(name: String, value: Any) { | ||
super.setAttribute(name, value) | ||
when (name) { | ||
"defaultText" -> defaultText = value as String | ||
else -> {} | ||
} | ||
} | ||
|
||
/** | ||
* Set the text. | ||
* @param value the text, which may be null. | ||
*/ | ||
fun setValue(value: String?) { | ||
super.setValue(value) | ||
} | ||
|
||
override fun getType(): String { | ||
return "TextInputCallback" | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
forgerock-auth/src/test/java/org/forgerock/android/auth/callback/TextInputCallbackTest.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,51 @@ | ||
/* | ||
* Copyright (c) 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
package org.forgerock.android.auth.callback | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class TextInputCallbackTest { | ||
@Test | ||
@Throws(JSONException::class) | ||
fun basicTest() { | ||
val raw = JSONObject("""{ | ||
"type": "TextInputCallback", | ||
"output": [ | ||
{ | ||
"name": "prompt", | ||
"value": "One Time Pin" | ||
}, | ||
{ | ||
"name": "defaultText", | ||
"value": "" | ||
} | ||
], | ||
"input": [ | ||
{ | ||
"name": "IDToken1", | ||
"value": "" | ||
} | ||
], | ||
"_id": 0 | ||
}""") | ||
val textInputCallback = TextInputCallback(raw, 0) | ||
Assert.assertEquals("One Time Pin", textInputCallback.getPrompt()) | ||
Assert.assertEquals("", textInputCallback.defaultText) | ||
textInputCallback.setValue("010101") | ||
Assert.assertEquals((textInputCallback.contentAsJson.getJSONArray("input")[0] as JSONObject).getString( | ||
"value"), | ||
"010101") | ||
Assert.assertEquals(0, textInputCallback.get_id().toLong()) | ||
} | ||
} |
Oops, something went wrong.