Files
boss/android/app/src/test/java/com/hyzq/boss/GroupInfoActivityTest.java

172 lines
6.6 KiB
Java

package com.hyzq.boss;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 34)
public class GroupInfoActivityTest {
@Test
public void renderGroupUsesLightweightHeaderMenuAndMemberList() throws Exception {
Intent intent = new Intent()
.putExtra(GroupInfoActivity.EXTRA_PROJECT_ID, "group-1")
.putExtra(GroupInfoActivity.EXTRA_PROJECT_NAME, "巡检协作群");
TestGroupInfoActivity activity = Robolectric
.buildActivity(TestGroupInfoActivity.class, intent)
.setup()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"renderGroup",
ReflectionHelpers.ClassParameter.from(JSONObject.class, buildDetailPayload()),
ReflectionHelpers.ClassParameter.from(JSONObject.class, buildParticipantsPayload())
);
LinearLayout content = activity.findViewById(R.id.screen_content);
assertTrue(viewTreeContainsText(content.getChildAt(0), "巡检协作群"));
assertTrue(viewTreeContainsText(content.getChildAt(0), "协作群聊"));
assertTrue(viewTreeContainsText(content.getChildAt(1), "线程详情"));
assertTrue(viewTreeContainsText(content.getChildAt(1), "查看当前群聊对应项目"));
assertTrue(viewTreeContainsText(content, "群成员"));
assertTrue(viewTreeContainsText(content, "Boss 移动控制台"));
assertFalse(viewTreeContainsText(content, "群聊成员可点击查看对应项目详情。"));
}
@Test
public void threadDetailMenuRowStillOpensProjectDetail() throws Exception {
Intent intent = new Intent()
.putExtra(GroupInfoActivity.EXTRA_PROJECT_ID, "group-1")
.putExtra(GroupInfoActivity.EXTRA_PROJECT_NAME, "巡检协作群");
TestGroupInfoActivity activity = Robolectric
.buildActivity(TestGroupInfoActivity.class, intent)
.setup()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"renderGroup",
ReflectionHelpers.ClassParameter.from(JSONObject.class, buildDetailPayload()),
ReflectionHelpers.ClassParameter.from(JSONObject.class, buildParticipantsPayload())
);
View threadDetailRow = findClickableViewContainingText(
activity.findViewById(R.id.screen_content),
"线程详情"
);
assertNotNull(threadDetailRow);
threadDetailRow.performClick();
Intent nextIntent = Shadows.shadowOf(activity).getNextStartedActivity();
assertNotNull(nextIntent);
assertEquals(
ProjectDetailActivity.class.getName(),
nextIntent.getComponent().getClassName()
);
assertEquals(
"group-1",
nextIntent.getStringExtra(ProjectDetailActivity.EXTRA_PROJECT_ID)
);
assertEquals(
"巡检协作群",
nextIntent.getStringExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME)
);
}
private static JSONObject buildDetailPayload() throws Exception {
JSONObject threadMeta = new JSONObject()
.put("threadId", "group-thread-3")
.put("folderName", "Boss");
JSONObject project = new JSONObject()
.put("id", "group-1")
.put("name", "巡检协作群")
.put("isGroup", true)
.put("collaborationMode", "development")
.put("threadMeta", threadMeta);
return new JSONObject().put("project", project);
}
private static JSONObject buildParticipantsPayload() throws Exception {
JSONArray participants = new JSONArray()
.put(new JSONObject()
.put("projectId", "group-1")
.put("threadDisplayName", "巡检协作群")
.put("folderName", "Boss")
.put("deviceId", "Mac Studio")
.put("threadId", "group-thread-3")
.put("isSourceProject", true))
.put(new JSONObject()
.put("projectId", "project-2")
.put("threadDisplayName", "Boss 移动控制台")
.put("folderName", "Boss")
.put("deviceId", "MacBook Pro")
.put("threadId", "thread-8"));
return new JSONObject().put("participants", participants);
}
private static boolean viewTreeContainsText(View root, String expectedText) {
if (root instanceof TextView) {
CharSequence text = ((TextView) root).getText();
if (expectedText.contentEquals(text)) {
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;
}
private static View findClickableViewContainingText(View root, String expectedText) {
if (root == null) {
return null;
}
if (viewTreeContainsText(root, expectedText) && root.isClickable()) {
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;
}
public static class TestGroupInfoActivity extends GroupInfoActivity {
@Override
protected void reload() {
// Tests render the lightweight info state directly.
}
}
}