Refresh thread status screen in realtime
This commit is contained in:
@@ -7,6 +7,7 @@ import static org.junit.Assert.assertFalse;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Looper;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
@@ -18,6 +19,7 @@ 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;
|
||||
|
||||
@@ -69,14 +71,101 @@ public class ThreadStatusActivityTest {
|
||||
assertFalse(viewTreeContainsText(content, "只读状态文档"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchingConversationEventTriggersReload() throws Exception {
|
||||
Intent intent = new Intent()
|
||||
.putExtra(ThreadStatusActivity.EXTRA_PROJECT_ID, "project-1")
|
||||
.putExtra(ThreadStatusActivity.EXTRA_PROJECT_NAME, "北区试产线回归");
|
||||
TestThreadStatusActivity activity = Robolectric
|
||||
.buildActivity(TestThreadStatusActivity.class, intent)
|
||||
.setup()
|
||||
.resume()
|
||||
.get();
|
||||
configureReloadDependencies(activity);
|
||||
|
||||
ReflectionHelpers.callInstanceMethod(
|
||||
activity,
|
||||
"handleRealtimeEvent",
|
||||
ReflectionHelpers.ClassParameter.from(
|
||||
BossRealtimeEvent.class,
|
||||
new BossRealtimeEvent("conversation.updated", new JSONObject().put("projectId", "project-1"))
|
||||
)
|
||||
);
|
||||
Shadows.shadowOf(activity.getMainLooper()).idle();
|
||||
|
||||
assertEquals(1, activity.reloadCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchingProjectContextRiskEventTriggersReload() throws Exception {
|
||||
Intent intent = new Intent()
|
||||
.putExtra(ThreadStatusActivity.EXTRA_PROJECT_ID, "project-1")
|
||||
.putExtra(ThreadStatusActivity.EXTRA_PROJECT_NAME, "北区试产线回归");
|
||||
TestThreadStatusActivity activity = Robolectric
|
||||
.buildActivity(TestThreadStatusActivity.class, intent)
|
||||
.setup()
|
||||
.resume()
|
||||
.get();
|
||||
configureReloadDependencies(activity);
|
||||
|
||||
ReflectionHelpers.callInstanceMethod(
|
||||
activity,
|
||||
"handleRealtimeEvent",
|
||||
ReflectionHelpers.ClassParameter.from(
|
||||
BossRealtimeEvent.class,
|
||||
new BossRealtimeEvent("project.context_risk.updated", new JSONObject().put("projectId", "project-1"))
|
||||
)
|
||||
);
|
||||
Shadows.shadowOf(activity.getMainLooper()).idle();
|
||||
|
||||
assertEquals(1, activity.reloadCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unrelatedProjectEventDoesNotTriggerReload() throws Exception {
|
||||
Intent intent = new Intent()
|
||||
.putExtra(ThreadStatusActivity.EXTRA_PROJECT_ID, "project-1")
|
||||
.putExtra(ThreadStatusActivity.EXTRA_PROJECT_NAME, "北区试产线回归");
|
||||
TestThreadStatusActivity activity = Robolectric
|
||||
.buildActivity(TestThreadStatusActivity.class, intent)
|
||||
.setup()
|
||||
.resume()
|
||||
.get();
|
||||
configureReloadDependencies(activity);
|
||||
|
||||
ReflectionHelpers.callInstanceMethod(
|
||||
activity,
|
||||
"handleRealtimeEvent",
|
||||
ReflectionHelpers.ClassParameter.from(
|
||||
BossRealtimeEvent.class,
|
||||
new BossRealtimeEvent("conversation.updated", new JSONObject().put("projectId", "project-2"))
|
||||
)
|
||||
);
|
||||
Shadows.shadowOf(activity.getMainLooper()).idle();
|
||||
|
||||
assertEquals(0, activity.reloadCount);
|
||||
}
|
||||
|
||||
private static void configureReloadDependencies(TestThreadStatusActivity activity) {
|
||||
RecordingBossApiClient apiClient = new RecordingBossApiClient(
|
||||
activity.getSharedPreferences("thread-status-realtime-test", Context.MODE_PRIVATE),
|
||||
"https://boss.hyzq.net"
|
||||
);
|
||||
ReflectionHelpers.setField(activity, "apiClient", apiClient);
|
||||
ReflectionHelpers.setField(activity, "executor", new DirectExecutorService());
|
||||
ReflectionHelpers.setField(activity, "reloadEnabled", true);
|
||||
}
|
||||
|
||||
private static final class TestThreadStatusActivity extends ThreadStatusActivity {
|
||||
private boolean reloadEnabled;
|
||||
private int reloadCount;
|
||||
|
||||
@Override
|
||||
protected void reload() {
|
||||
if (!reloadEnabled) {
|
||||
return;
|
||||
}
|
||||
reloadCount += 1;
|
||||
super.reload();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user