Refresh device import draft in realtime

This commit is contained in:
kris
2026-04-07 16:16:37 +08:00
parent ef3bf35463
commit 9268f64506
5 changed files with 151 additions and 4 deletions

View File

@@ -14,6 +14,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;
@@ -103,6 +104,62 @@ public class DeviceImportDraftActivityTest {
assertFalse(viewTreeContainsText(content, "树莓派二代接入与联调"));
}
@Test
public void matchingDeviceUpdatedEventTriggersReload() throws Exception {
TestDeviceImportDraftActivity activity = Robolectric
.buildActivity(
TestDeviceImportDraftActivity.class,
new Intent()
.putExtra(DeviceImportDraftActivity.EXTRA_DEVICE_ID, "device-1")
.putExtra(DeviceImportDraftActivity.EXTRA_DEVICE_NAME, "Mac Studio")
)
.setup()
.resume()
.get();
activity.reloadEnabled = true;
activity.reloadCount = 0;
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("devices.updated", new JSONObject().put("deviceId", "device-1"))
)
);
Shadows.shadowOf(activity.getMainLooper()).idle();
assertTrue(activity.reloadCount == 1);
}
@Test
public void unrelatedDeviceEventDoesNotTriggerReload() throws Exception {
TestDeviceImportDraftActivity activity = Robolectric
.buildActivity(
TestDeviceImportDraftActivity.class,
new Intent()
.putExtra(DeviceImportDraftActivity.EXTRA_DEVICE_ID, "device-1")
.putExtra(DeviceImportDraftActivity.EXTRA_DEVICE_NAME, "Mac Studio")
)
.setup()
.resume()
.get();
activity.reloadEnabled = true;
activity.reloadCount = 0;
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("devices.updated", new JSONObject().put("deviceId", "device-2"))
)
);
Shadows.shadowOf(activity.getMainLooper()).idle();
assertTrue(activity.reloadCount == 0);
}
private static JSONObject buildPendingDraft() throws Exception {
return new JSONObject()
.put("draftId", "draft-1")
@@ -222,9 +279,16 @@ public class DeviceImportDraftActivityTest {
}
public static class TestDeviceImportDraftActivity extends DeviceImportDraftActivity {
private boolean reloadEnabled;
private int reloadCount;
@Override
protected void reload() {
// Tests render synthetic payloads directly.
if (!reloadEnabled) {
return;
}
reloadCount += 1;
setRefreshing(false);
}
}
}