Loading tests/telephonytests/src/com/android/internal/telephony/ims/ImsTestBase.java +11 −58 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.MessageQueue; import android.os.SystemClock; import android.testing.TestableLooper; import androidx.test.InstrumentationRegistry; Loading @@ -31,7 +32,6 @@ import com.android.internal.telephony.TelephonyTest; import org.mockito.MockitoAnnotations; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; Loading @@ -41,22 +41,6 @@ import java.util.concurrent.TimeUnit; * Helper class to load Mockito Resources into a test. */ public class ImsTestBase { private static final Field MESSAGE_QUEUE_FIELD; private static final Field MESSAGE_WHEN_FIELD; private static final Field MESSAGE_NEXT_FIELD; static { try { MESSAGE_QUEUE_FIELD = MessageQueue.class.getDeclaredField("mMessages"); MESSAGE_QUEUE_FIELD.setAccessible(true); MESSAGE_WHEN_FIELD = Message.class.getDeclaredField("when"); MESSAGE_WHEN_FIELD.setAccessible(true); MESSAGE_NEXT_FIELD = Message.class.getDeclaredField("next"); MESSAGE_NEXT_FIELD.setAccessible(true); } catch (NoSuchFieldException e) { throw new RuntimeException("Failed to initialize TelephonyTest", e); } } protected Context mContext; protected List<TestableLooper> mTestableLoopers = new ArrayList<>(); Loading Loading @@ -130,37 +114,13 @@ public class ImsTestBase { } } /** * @return The longest delay from all the message queues. */ private long getLongestDelay() { long delay = 0; for (TestableLooper looper : mTestableLoopers) { MessageQueue queue = looper.getLooper().getQueue(); try { Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue); while (msg != null) { delay = Math.max(msg.getWhen(), delay); msg = (Message) MESSAGE_NEXT_FIELD.get(msg); } } catch (IllegalAccessException e) { throw new RuntimeException("Access failed in TelephonyTest", e); } } return delay; } /** * @return {@code true} if there are any messages in the queue. */ private boolean messagesExist() { for (TestableLooper looper : mTestableLoopers) { MessageQueue queue = looper.getLooper().getQueue(); try { Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue); if (msg != null) return true; } catch (IllegalAccessException e) { throw new RuntimeException("Access failed in TelephonyTest", e); if (looper.peekWhen() > 0) { return true; } } return false; Loading @@ -170,8 +130,14 @@ public class ImsTestBase { * Handle all messages including the delayed messages. */ public void processAllFutureMessages() { final long now = SystemClock.uptimeMillis(); while (messagesExist()) { moveTimeForward(getLongestDelay()); for (TestableLooper looper : mTestableLoopers) { long nextDelay = looper.peekWhen() - now; if (nextDelay > 0) { looper.moveTimeForward(nextDelay); } } processAllMessages(); } } Loading @@ -196,20 +162,7 @@ public class ImsTestBase { */ public void moveTimeForward(long milliSeconds) { for (TestableLooper looper : mTestableLoopers) { MessageQueue queue = looper.getLooper().getQueue(); try { Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue); while (msg != null) { long updatedWhen = msg.getWhen() - milliSeconds; if (updatedWhen < 0) { updatedWhen = 0; } MESSAGE_WHEN_FIELD.set(msg, updatedWhen); msg = (Message) MESSAGE_NEXT_FIELD.get(msg); } } catch (IllegalAccessException e) { throw new RuntimeException("Access failed in TelephonyTest", e); } looper.moveTimeForward(milliSeconds); } } } Loading
tests/telephonytests/src/com/android/internal/telephony/ims/ImsTestBase.java +11 −58 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.MessageQueue; import android.os.SystemClock; import android.testing.TestableLooper; import androidx.test.InstrumentationRegistry; Loading @@ -31,7 +32,6 @@ import com.android.internal.telephony.TelephonyTest; import org.mockito.MockitoAnnotations; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; Loading @@ -41,22 +41,6 @@ import java.util.concurrent.TimeUnit; * Helper class to load Mockito Resources into a test. */ public class ImsTestBase { private static final Field MESSAGE_QUEUE_FIELD; private static final Field MESSAGE_WHEN_FIELD; private static final Field MESSAGE_NEXT_FIELD; static { try { MESSAGE_QUEUE_FIELD = MessageQueue.class.getDeclaredField("mMessages"); MESSAGE_QUEUE_FIELD.setAccessible(true); MESSAGE_WHEN_FIELD = Message.class.getDeclaredField("when"); MESSAGE_WHEN_FIELD.setAccessible(true); MESSAGE_NEXT_FIELD = Message.class.getDeclaredField("next"); MESSAGE_NEXT_FIELD.setAccessible(true); } catch (NoSuchFieldException e) { throw new RuntimeException("Failed to initialize TelephonyTest", e); } } protected Context mContext; protected List<TestableLooper> mTestableLoopers = new ArrayList<>(); Loading Loading @@ -130,37 +114,13 @@ public class ImsTestBase { } } /** * @return The longest delay from all the message queues. */ private long getLongestDelay() { long delay = 0; for (TestableLooper looper : mTestableLoopers) { MessageQueue queue = looper.getLooper().getQueue(); try { Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue); while (msg != null) { delay = Math.max(msg.getWhen(), delay); msg = (Message) MESSAGE_NEXT_FIELD.get(msg); } } catch (IllegalAccessException e) { throw new RuntimeException("Access failed in TelephonyTest", e); } } return delay; } /** * @return {@code true} if there are any messages in the queue. */ private boolean messagesExist() { for (TestableLooper looper : mTestableLoopers) { MessageQueue queue = looper.getLooper().getQueue(); try { Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue); if (msg != null) return true; } catch (IllegalAccessException e) { throw new RuntimeException("Access failed in TelephonyTest", e); if (looper.peekWhen() > 0) { return true; } } return false; Loading @@ -170,8 +130,14 @@ public class ImsTestBase { * Handle all messages including the delayed messages. */ public void processAllFutureMessages() { final long now = SystemClock.uptimeMillis(); while (messagesExist()) { moveTimeForward(getLongestDelay()); for (TestableLooper looper : mTestableLoopers) { long nextDelay = looper.peekWhen() - now; if (nextDelay > 0) { looper.moveTimeForward(nextDelay); } } processAllMessages(); } } Loading @@ -196,20 +162,7 @@ public class ImsTestBase { */ public void moveTimeForward(long milliSeconds) { for (TestableLooper looper : mTestableLoopers) { MessageQueue queue = looper.getLooper().getQueue(); try { Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue); while (msg != null) { long updatedWhen = msg.getWhen() - milliSeconds; if (updatedWhen < 0) { updatedWhen = 0; } MESSAGE_WHEN_FIELD.set(msg, updatedWhen); msg = (Message) MESSAGE_NEXT_FIELD.get(msg); } } catch (IllegalAccessException e) { throw new RuntimeException("Access failed in TelephonyTest", e); } looper.moveTimeForward(milliSeconds); } } }