feat: add browser-assisted openai onboarding flow
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package com.hyzq.boss;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = 34)
|
||||
public class OpenAiOnboardingActivityTest {
|
||||
@Test
|
||||
public void autoOpenLoginLaunchesOpenAiPlatformLoginPage() {
|
||||
Intent intent = new Intent()
|
||||
.putExtra(OpenAiOnboardingActivity.EXTRA_AUTO_OPEN_LOGIN, true);
|
||||
OpenAiOnboardingActivity activity = Robolectric
|
||||
.buildActivity(OpenAiOnboardingActivity.class, intent)
|
||||
.setup()
|
||||
.get();
|
||||
|
||||
ShadowActivity shadowActivity = org.robolectric.Shadows.shadowOf(activity);
|
||||
Intent nextIntent = shadowActivity.getNextStartedActivity();
|
||||
assertNotNull(nextIntent);
|
||||
assertEquals(Intent.ACTION_VIEW, nextIntent.getAction());
|
||||
assertEquals(Uri.parse("https://platform.openai.com/login"), nextIntent.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tappingApiKeysRowLaunchesApiKeysPage() {
|
||||
OpenAiOnboardingActivity activity = Robolectric
|
||||
.buildActivity(OpenAiOnboardingActivity.class)
|
||||
.setup()
|
||||
.get();
|
||||
|
||||
View row = findClickableViewContainingText(
|
||||
activity.findViewById(R.id.screen_content),
|
||||
"第二步:打开 API Keys 页面"
|
||||
);
|
||||
assertNotNull(row);
|
||||
row.performClick();
|
||||
|
||||
ShadowActivity shadowActivity = org.robolectric.Shadows.shadowOf(activity);
|
||||
Intent nextIntent = shadowActivity.getNextStartedActivity();
|
||||
assertNotNull(nextIntent);
|
||||
assertEquals(Intent.ACTION_VIEW, nextIntent.getAction());
|
||||
assertEquals(Uri.parse("https://platform.openai.com/api-keys"), nextIntent.getData());
|
||||
}
|
||||
|
||||
private static View findClickableViewContainingText(View root, String expectedText) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
if (root.isClickable() && viewTreeContainsText(root, expectedText)) {
|
||||
return root;
|
||||
}
|
||||
if (!(root instanceof ViewGroup)) {
|
||||
return null;
|
||||
}
|
||||
ViewGroup group = (ViewGroup) root;
|
||||
for (int index = 0; index < group.getChildCount(); index += 1) {
|
||||
View match = findClickableViewContainingText(group.getChildAt(index), expectedText);
|
||||
if (match != null) {
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean viewTreeContainsText(View root, String expectedText) {
|
||||
if (root instanceof TextView) {
|
||||
CharSequence text = ((TextView) root).getText();
|
||||
if (text != null && text.toString().contains(expectedText)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!(root instanceof ViewGroup)) {
|
||||
return false;
|
||||
}
|
||||
ViewGroup group = (ViewGroup) root;
|
||||
for (int index = 0; index < group.getChildCount(); index += 1) {
|
||||
if (viewTreeContainsText(group.getChildAt(index), expectedText)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user