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

Commit 6f0f602b authored by Neil Fuller's avatar Neil Fuller
Browse files

Make NitzStateMachine testable

This commit does not modify behavior of NitzStateMachine.
It is a refactoring that separates NitzStateMachine from
various device concerns that would otherwise make unit
testing difficult.

There are three main categories of interaction from
NitzStateManager with other components:

1) External actions / querying of the NITZ state triggered
by ServiceStateTracker.

2) NitzStateManager interacting with the device's
time / time zone state and associated user settings. This
is now handled by TimeServiceHelper. In future this will
mostly be replaced by calls to a proposed TimeDetectionService
as part of the work to improve support for devices that have
multiple ways of detecting time / time zone. Time / time zone
detection being enabled will dependent on which component is
asking.

3) NitzStateManager interacting with global / shared device
state. i.e. reading and writing SystemProperties and reading
unmodifiable clocks. Abstracting these has clear
advantages for unit testing since it allows simulation of
various device states and avoids modification of the system
properties (which is not permitted, anyway).

This commit contains a single new test in NitzStateMachineTest
to demonstrate the purpose of this change and prove that
there is nothing preventing more extensive time / time zone
detection tests being written in future commits.

The only test in ServiceStateTrackerTest that touched on NITZ
logic has been modified to reflect the fact it delegates to a
(test stubbed) NitzStateMachine and no longer handles the NITZ
logic itself.

Testing:

To run individual tests:

atest FrameworksTelephonyTests:com.android.internal.telephony.NitzStateMachineTest \
    FrameworksTelephonyTests:com.android.internal.telephony.ServiceStateTrackerTest

To run all telephony tests:

atest FrameworksTelephonyTests

Bug: 63743683
Test: See above

(cherry picked from commit f9e4314c)
Merged-In: Ie0be1bc4a2d6baf4860ed93902c697f3edfa4d44
Change-Id: Ie0be1bc4a2d6baf4860ed93902c697f3edfa4d44
parent c516d5f0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -359,7 +359,7 @@ public final class MccTable {
     * @param mcc Mobile Country Code of the SIM or SIM-like entity (build prop on CDMA)
     */
    private static void setTimezoneFromMccIfNeeded(Context context, int mcc) {
        String timezone = SystemProperties.get(NitzStateMachine.TIMEZONE_PROPERTY);
        String timezone = SystemProperties.get(TimeServiceHelper.TIMEZONE_PROPERTY);
        // timezone.equals("GMT") will be true and only true if the timezone was
        // set to a default value by the system server (when starting, system server.
        // sets the persist.sys.timezone to "GMT" if it's not set)."GMT" is not used by
+49 −0
Original line number Diff line number Diff line
@@ -54,6 +54,9 @@ public final class NitzData {

    private NitzData(String originalString, int zoneOffsetMillis, Integer dstOffsetMillis,
            long utcTimeMillis, TimeZone timeZone) {
        if (originalString == null) {
            throw new NullPointerException("originalString==null");
        }
        this.mOriginalString = originalString;
        this.mZoneOffset = zoneOffsetMillis;
        this.mDstOffset = dstOffsetMillis;
@@ -134,6 +137,13 @@ public final class NitzData {
        }
    }

    /** A method for use in tests to create NitzData instances. */
    public static NitzData createForTests(int zoneOffsetMillis, Integer dstOffsetMillis,
            long utcTimeMillis, TimeZone timeZone) {
        return new NitzData("Test data", zoneOffsetMillis, dstOffsetMillis, utcTimeMillis,
                timeZone);
    }

    /**
     * Returns the current time as the number of milliseconds since the beginning of the Unix epoch
     * (1/1/1970 00:00:00 UTC).
@@ -215,6 +225,45 @@ public final class NitzData {
        return guess;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        NitzData nitzData = (NitzData) o;

        if (mZoneOffset != nitzData.mZoneOffset) {
            return false;
        }
        if (mCurrentTimeMillis != nitzData.mCurrentTimeMillis) {
            return false;
        }
        if (!mOriginalString.equals(nitzData.mOriginalString)) {
            return false;
        }
        if (mDstOffset != null ? !mDstOffset.equals(nitzData.mDstOffset)
                : nitzData.mDstOffset != null) {
            return false;
        }
        return mEmulatorHostTimeZone != null ? mEmulatorHostTimeZone
                .equals(nitzData.mEmulatorHostTimeZone) : nitzData.mEmulatorHostTimeZone == null;
    }

    @Override
    public int hashCode() {
        int result = mOriginalString.hashCode();
        result = 31 * result + mZoneOffset;
        result = 31 * result + (mDstOffset != null ? mDstOffset.hashCode() : 0);
        result = 31 * result + (int) (mCurrentTimeMillis ^ (mCurrentTimeMillis >>> 32));
        result = 31 * result + (mEmulatorHostTimeZone != null ? mEmulatorHostTimeZone.hashCode()
                : 0);
        return result;
    }

    @Override
    public String toString() {
        return "NitzData{"
+145 −122

File changed.

Preview size limit exceeded, changes collapsed.

+1 −1
Original line number Diff line number Diff line
@@ -462,7 +462,7 @@ public class ServiceStateTracker extends Handler {
    private static final int INVALID_LTE_EARFCN = -1;

    public ServiceStateTracker(GsmCdmaPhone phone, CommandsInterface ci) {
        mNitzState = new NitzStateMachine(phone);
        mNitzState = TelephonyComponentFactory.getInstance().makeNitzStateMachine(phone);
        mPhone = phone;
        mCi = ci;

+14 −0
Original line number Diff line number Diff line
@@ -61,6 +61,20 @@ public class TelephonyComponentFactory {
        return new ServiceStateTracker(phone, ci);
    }

    /**
     * Returns a new {@link NitzStateMachine} instance.
     */
    public NitzStateMachine makeNitzStateMachine(GsmCdmaPhone phone) {
        return new NitzStateMachine(phone);
    }

    /**
     * Returns a new {@link TimeServiceHelper} instance.
     */
    public TimeServiceHelper makeTimeServiceHelper(Context context) {
        return new TimeServiceHelper(context);
    }

    public SimActivationTracker makeSimActivationTracker(Phone phone) {
        return new SimActivationTracker(phone);
    }
Loading