Skip to content

Commit

Permalink
improve test stability and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lastpeony committed Oct 2, 2024
1 parent d376767 commit aef95cf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import android.content.Context;
Expand All @@ -25,6 +26,9 @@
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;

import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.GrantPermissionRule;
Expand All @@ -34,6 +38,7 @@
import androidx.test.uiautomator.Until;


import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -231,7 +236,7 @@ public void testConferenceSwitchStreamSource() throws InterruptedException {

onView(withId(R.id. stats_popup_container)).perform(swipeUp());

//Thread.sleep(3000);
onView(withId(R.id.stats_popup_container)).perform(waitFor(2000));

onView(withId(R.id.multitrack_stats_popup_close_button)).perform(click());

Expand Down Expand Up @@ -281,4 +286,23 @@ public void testConferenceSwitchStreamSource() throws InterruptedException {
.check(matches(withText(R.string.disconnected)));
}

public static ViewAction waitFor(long millis) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isDisplayed(); // No constraints, can be used on any view
}

@Override
public String getDescription() {
return "Wait for " + millis + " milliseconds.";
}

@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadForAtLeast(millis);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import androidx.test.rule.GrantPermissionRule;
import androidx.test.uiautomator.UiDevice;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -54,6 +55,8 @@
public class PeerActivityTest {
private IdlingResource mIdlingResource;
private String runningTest;
private float videoBytesSent = 0;
private float videoBytesReceived = 0;

@Rule
public GrantPermissionRule permissionRule
Expand Down Expand Up @@ -111,16 +114,16 @@ public void testPeerToPeer() throws InterruptedException {

onView(withId(R.id.start_streaming_button)).check(matches(withText("Join")));
Espresso.closeSoftKeyboard();
onView(withId(R.id.stream_id_edittext)).perform(replaceText(PeerActivity.PEER_ROOM_ID_FOR_TEST));
String randomPeerRoomId = "p2p"+ RandomStringUtils.randomAlphanumeric(6);
onView(withId(R.id.stream_id_edittext)).perform(replaceText(randomPeerRoomId));

onView(withId(R.id.start_streaming_button)).perform(click());

onView(withId(R.id.start_streaming_button)).check(matches(withText("Leave")));

onView(withId(R.id.broadcasting_text_view))
.check(matches(anyOf(withText(R.string.connecting), withText(R.string.live))));

RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(PeerActivity.PEER_ROOM_ID_FOR_TEST, runningTest);
RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(randomPeerRoomId, runningTest);

Thread.sleep(10000);

Expand Down Expand Up @@ -172,10 +175,11 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio
IdlingRegistry.getInstance().register(mIdlingResource);
activity.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
});
String randomPeerRoomId = "p2p"+ RandomStringUtils.randomAlphanumeric(6);

onView(withId(R.id.start_streaming_button)).check(matches(withText("Join")));
Espresso.closeSoftKeyboard();
onView(withId(R.id.stream_id_edittext)).perform(replaceText(PeerActivity.PEER_ROOM_ID_FOR_TEST));
onView(withId(R.id.stream_id_edittext)).perform(replaceText(randomPeerRoomId));

onView(withId(R.id.start_streaming_button)).perform(click());

Expand All @@ -184,7 +188,7 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio
onView(withId(R.id.broadcasting_text_view))
.check(matches(anyOf(withText(R.string.connecting), withText(R.string.live))));

RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(PeerActivity.PEER_ROOM_ID_FOR_TEST, runningTest);
RemoteP2PParticipant remoteP2PParticipant = RemoteP2PParticipant.addP2PParticipant(randomPeerRoomId, runningTest);

Thread.sleep(10000);

Expand All @@ -198,6 +202,15 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio

onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview)).inRoot(isDialog()).check(matches(isDisplayed()));


onView(withId(R.id.multitrack_stats_popup_bytes_sent_video_textview)).check((view, noViewFoundException) -> {
String text = ((TextView) view).getText().toString();
float value = Float.parseFloat(text);
assertTrue(value > 0f);
videoBytesSent = value;
});


onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview))
.check((view, noViewFoundException) -> {
if (noViewFoundException != null) {
Expand All @@ -206,8 +219,11 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0);
TextView bytesReceivedText = viewHolder.itemView.findViewById(R.id.track_stats_item_bytes_received_textview);

int bytesReceived = Integer.parseInt(( bytesReceivedText).getText().toString());

assertTrue(bytesReceived > 0);
videoBytesReceived = bytesReceived;
});

onView(withId(R.id. stats_popup_container)).perform(swipeUp());
Expand Down Expand Up @@ -240,6 +256,16 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio

onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview)).inRoot(isDialog()).check(matches(isDisplayed()));


onView(withId(R.id.multitrack_stats_popup_bytes_sent_video_textview)).check((view, noViewFoundException) -> {
String text = ((TextView) view).getText().toString();
float value = Float.parseFloat(text);
assertTrue(value > 0f);
assertTrue(value > videoBytesSent);
videoBytesSent = value;
});


onView(withId(R.id.multitrack_stats_popup_play_stats_video_track_recyclerview))
.check((view, noViewFoundException) -> {
if (noViewFoundException != null) {
Expand All @@ -248,8 +274,11 @@ public void testPeerToPeerReconnection() throws InterruptedException, IOExceptio
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0);
TextView bytesReceivedText = viewHolder.itemView.findViewById(R.id.track_stats_item_bytes_received_textview);

int bytesReceived = Integer.parseInt(( bytesReceivedText).getText().toString());

assertTrue(bytesReceived > 0);
assertTrue(bytesReceived > videoBytesReceived);
});

onView(withId(R.id. stats_popup_container)).perform(swipeUp());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.antmedia.webrtc_android_sample_app;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Window;
Expand All @@ -12,11 +11,12 @@
import androidx.test.espresso.IdlingResource;
import androidx.test.espresso.idling.CountingIdlingResource;

import org.apache.commons.lang3.RandomStringUtils;

public abstract class TestableActivity extends AppCompatActivity {
public CountingIdlingResource idlingResource = new CountingIdlingResource("Load", true);
protected SharedPreferences sharedPreferences;
public static String CONFERENCE_ROOM_ID_FOR_TEST = "room_12877";
public static String PEER_ROOM_ID_FOR_TEST = "p2pRoom_12877";
public static String PEER_ROOM_ID_FOR_TEST = "p2p"+ RandomStringUtils.randomAlphanumeric(3);

public void incrementIdle() {
idlingResource.increment();
Expand Down

0 comments on commit aef95cf

Please sign in to comment.