Refresh OTA screen in realtime
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package com.hyzq.boss;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
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 java.lang.reflect.Method;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(sdk = 34)
|
||||
public class AboutActivityTest {
|
||||
@Test
|
||||
public void otaUpdatedEventTriggersReload() throws Exception {
|
||||
TestAboutActivity activity = Robolectric
|
||||
.buildActivity(TestAboutActivity.class, new Intent())
|
||||
.setup()
|
||||
.resume()
|
||||
.get();
|
||||
activity.reloadEnabled = true;
|
||||
activity.reloadCount = 0;
|
||||
|
||||
Method handleRealtimeEvent = findHandleRealtimeEvent();
|
||||
assertNotNull(handleRealtimeEvent);
|
||||
|
||||
handleRealtimeEvent.invoke(
|
||||
activity,
|
||||
new BossRealtimeEvent("ota.updated", new JSONObject().put("deviceId", "mac-studio"))
|
||||
);
|
||||
Shadows.shadowOf(activity.getMainLooper()).idle();
|
||||
|
||||
assertEquals(1, activity.reloadCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unrelatedConversationEventDoesNotTriggerReload() throws Exception {
|
||||
TestAboutActivity activity = Robolectric
|
||||
.buildActivity(TestAboutActivity.class, new Intent())
|
||||
.setup()
|
||||
.resume()
|
||||
.get();
|
||||
activity.reloadEnabled = true;
|
||||
activity.reloadCount = 0;
|
||||
|
||||
Method handleRealtimeEvent = findHandleRealtimeEvent();
|
||||
assertNotNull(handleRealtimeEvent);
|
||||
|
||||
handleRealtimeEvent.invoke(
|
||||
activity,
|
||||
new BossRealtimeEvent("conversation.updated", new JSONObject().put("projectId", "master-agent"))
|
||||
);
|
||||
Shadows.shadowOf(activity.getMainLooper()).idle();
|
||||
|
||||
assertEquals(0, activity.reloadCount);
|
||||
}
|
||||
|
||||
private static Method findHandleRealtimeEvent() {
|
||||
for (Method method : AboutActivity.class.getDeclaredMethods()) {
|
||||
if ("handleRealtimeEvent".equals(method.getName())
|
||||
&& method.getParameterTypes().length == 1
|
||||
&& method.getParameterTypes()[0] == BossRealtimeEvent.class) {
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class TestAboutActivity extends AboutActivity {
|
||||
private boolean reloadEnabled;
|
||||
private int reloadCount;
|
||||
|
||||
@Override
|
||||
protected void reload() {
|
||||
if (!reloadEnabled) {
|
||||
return;
|
||||
}
|
||||
reloadCount += 1;
|
||||
setRefreshing(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user