Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1fd75b98 authored by Jack Yu's avatar Jack Yu Committed by Gerrit Code Review
Browse files

Merge changes If146f3dd,I53d9c8d2 into main

* changes:
  Added unit tests for SubscriptionManagerService
  Enhanced subscription database manager logs
parents 7e9ac17a 4cc00ee3
Loading
Loading
Loading
Loading
+25 −9
Original line number Diff line number Diff line
@@ -506,8 +506,8 @@ public class SubscriptionDatabaseManager extends Handler {
            new HashMap<>(16);

    /** Whether database has been initialized after boot up. */
    @GuardedBy("mDatabaseInitialized")
    private Boolean mDatabaseInitialized = false;
    @GuardedBy("this")
    private boolean mDatabaseInitialized = false;

    /**
     * This is the callback used for listening events from {@link SubscriptionDatabaseManager}.
@@ -759,7 +759,7 @@ public class SubscriptionDatabaseManager extends Handler {
                    + "insert. subInfo=" + subInfo);
        }

        synchronized (mDatabaseInitialized) {
        synchronized (this) {
            if (!mDatabaseInitialized) {
                throw new IllegalStateException(
                        "Database has not been initialized. Can't insert new "
@@ -831,7 +831,7 @@ public class SubscriptionDatabaseManager extends Handler {
    private int updateDatabase(int subId, @NonNull ContentValues contentValues) {
        logv("updateDatabase: prepare to update sub " + subId);

        synchronized (mDatabaseInitialized) {
        synchronized (this) {
            if (!mDatabaseInitialized) {
                logel("updateDatabase: Database has not been initialized. Can't update database at "
                        + "this point. contentValues=" + contentValues);
@@ -1852,7 +1852,7 @@ public class SubscriptionDatabaseManager extends Handler {
        if (mAsyncMode) {
            // Load the database asynchronously.
            post(() -> {
                synchronized (mDatabaseInitialized) {
                synchronized (this) {
                    loadDatabaseInternal();
                    mDatabaseInitialized = true;
                    mCallback.invokeFromExecutor(mCallback::onInitialized);
@@ -1860,7 +1860,7 @@ public class SubscriptionDatabaseManager extends Handler {
            });
        } else {
            // Load the database synchronously.
            synchronized (mDatabaseInitialized) {
            synchronized (this) {
                loadDatabaseInternal();
                mDatabaseInitialized = true;
                mCallback.invokeFromExecutor(mCallback::onInitialized);
@@ -2120,7 +2120,7 @@ public class SubscriptionDatabaseManager extends Handler {
    }

    /**
     * Dump the state of {@link SubscriptionManagerService}.
     * Dump the state of {@link SubscriptionDatabaseManager}.
     *
     * @param fd File descriptor
     * @param printWriter Print writer
@@ -2129,11 +2129,27 @@ public class SubscriptionDatabaseManager extends Handler {
    public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter printWriter,
            @NonNull String[] args) {
        IndentingPrintWriter pw = new IndentingPrintWriter(printWriter, "  ");
        pw.println(SubscriptionManagerService.class.getSimpleName() + ":");
        pw.println(SubscriptionDatabaseManager.class.getSimpleName() + ":");
        pw.increaseIndent();
        pw.println("All subscriptions:");
        pw.increaseIndent();
        mReadWriteLock.readLock().lock();
        try {
            mAllSubscriptionInfoInternalCache.forEach((subId, subInfo) -> pw.println(subInfo));
        } finally {
            mReadWriteLock.readLock().unlock();
        }
        pw.decreaseIndent();
        pw.println();
        pw.println("mAsyncMode=" + mAsyncMode);
        synchronized (this) {
            pw.println("mDatabaseInitialized=" + mDatabaseInitialized);
        }
        pw.println("mReadWriteLock=" + mReadWriteLock);
        pw.println();
        pw.println("Local log:");
        pw.increaseIndent();
        mLocalLog.dump(fd, printWriter, args);
        pw.decreaseIndent();
        pw.decreaseIndent();
    }
+2 −0
Original line number Diff line number Diff line
@@ -3920,5 +3920,7 @@ public class SubscriptionManagerService extends ISub.Stub {
        mLocalLog.dump(fd, pw, args);
        pw.decreaseIndent();
        pw.decreaseIndent();
        pw.println();
        mSubscriptionDatabaseManager.dump(fd, pw, args);
    }
}
+61 −0
Original line number Diff line number Diff line
@@ -113,11 +113,15 @@ import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@@ -558,6 +562,7 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);

        mSubscriptionManagerServiceUT.setDefaultVoiceSubId(1);
        assertThat(mSubscriptionManagerServiceUT.getDefaultVoiceSubId()).isEqualTo(1);

        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION)).isEqualTo(1);
@@ -599,6 +604,7 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);

        mSubscriptionManagerServiceUT.setDefaultDataSubId(1);
        assertThat(mSubscriptionManagerServiceUT.getDefaultDataSubId()).isEqualTo(1);

        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                        Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION)).isEqualTo(1);
@@ -638,6 +644,7 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);

        mSubscriptionManagerServiceUT.setDefaultSmsSubId(1);
        assertThat(mSubscriptionManagerServiceUT.getDefaultSmsSubId()).isEqualTo(1);

        assertThat(Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION)).isEqualTo(1);
@@ -1774,6 +1781,11 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        mSubscriptionManagerServiceUT.updateSimState(
                0, TelephonyManager.SIM_STATE_LOADED, null, null);
        processAllMessages();

        assertThat(mSubscriptionManagerServiceUT.getSubId(0)).isEqualTo(1);
        assertThat(mSubscriptionManagerServiceUT.getSlotIndex(1)).isEqualTo(0);
        assertThat(mSubscriptionManagerServiceUT.getPhoneId(1)).isEqualTo(0);

        mSubscriptionManagerServiceUT.setCarrierId(1, FAKE_CARRIER_ID1);
        mSubscriptionManagerServiceUT.setDisplayNameUsingSrc(FAKE_CARRIER_NAME1, 1,
                SubscriptionManager.NAME_SOURCE_SIM_SPN);
@@ -1911,4 +1923,53 @@ public class SubscriptionManagerServiceTest extends TelephonyTest {
        assertThat(subInfoList.get(0).isActive()).isTrue();
        assertThat(subInfoList.get(0).getIccId()).isEqualTo(FAKE_ICCID2);
    }

    @Test
    public void testDump() {
        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
        insertSubscription(FAKE_SUBSCRIPTION_INFO2);

        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
        final StringWriter stringWriter = new StringWriter();
        mSubscriptionManagerServiceUT.dump(new FileDescriptor(), new PrintWriter(stringWriter),
                null);
        assertThat(stringWriter.toString().length()).isGreaterThan(0);
    }

    @Test
    public void testOnSubscriptionChanged() {
        CountDownLatch latch = new CountDownLatch(1);
        SubscriptionManagerServiceCallback callback =
                new SubscriptionManagerServiceCallback(Runnable::run) {
                    @Override
                    public void onSubscriptionChanged(int subId) {
                        latch.countDown();
                        logd("testOnSubscriptionChanged: onSubscriptionChanged");
                    }
                };
        mSubscriptionManagerServiceUT.registerCallback(callback);
        insertSubscription(FAKE_SUBSCRIPTION_INFO1);
        processAllMessages();
        assertThat(latch.getCount()).isEqualTo(0);
    }

    @Test
    public void testOnUiccApplicationsEnabled() {
        CountDownLatch latch = new CountDownLatch(1);
        SubscriptionManagerServiceCallback callback =
                new SubscriptionManagerServiceCallback(Runnable::run) {
                    @Override
                    public void onUiccApplicationsEnabled(int subId) {
                        latch.countDown();
                        logd("testOnSubscriptionChanged: onUiccApplicationsEnabled");
                    }
                };
        mSubscriptionManagerServiceUT.registerCallback(callback);
        int subId = insertSubscription(FAKE_SUBSCRIPTION_INFO1);

        mContextFixture.addCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE);
        mSubscriptionManagerServiceUT.setUiccApplicationsEnabled(false, subId);
        processAllMessages();
        assertThat(latch.getCount()).isEqualTo(0);
    }
}