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

Commit 7d85c9d4 authored by Neil Fuller's avatar Neil Fuller Committed by Automerger Merge Worker
Browse files

Merge "Move some tests into CTS to avoid duplication" into sc-dev am: 7f9caf4c

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14140779

Change-Id: I6ba744b3ce4f65252603dd50bf8321cffbdafb14
parents 82a99bd9 7f9caf4c
Loading
Loading
Loading
Loading
+0 −71
Original line number Diff line number Diff line
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.time;

import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable;
import static android.app.timezonedetector.ParcelableTestSupport.roundTripParcelable;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import android.os.TimestampedValue;

import org.junit.Test;

public class ExternalTimeSuggestionTest {

    private static final TimestampedValue<Long> ARBITRARY_TIME =
            new TimestampedValue<>(1111L, 2222L);

    @Test
    public void testEquals() {
        ExternalTimeSuggestion one = new ExternalTimeSuggestion(
                ARBITRARY_TIME.getReferenceTimeMillis(),
                ARBITRARY_TIME.getValue());
        assertEquals(one, one);

        ExternalTimeSuggestion two = new ExternalTimeSuggestion(
                ARBITRARY_TIME.getReferenceTimeMillis(),
                ARBITRARY_TIME.getValue());
        assertEquals(one, two);
        assertEquals(two, one);

        ExternalTimeSuggestion three = new ExternalTimeSuggestion(
                ARBITRARY_TIME.getReferenceTimeMillis() + 1,
                ARBITRARY_TIME.getValue());
        assertNotEquals(one, three);
        assertNotEquals(three, one);

        // DebugInfo must not be considered in equals().
        one.addDebugInfo("Debug info 1");
        two.addDebugInfo("Debug info 2");
        assertEquals(one, two);
    }

    @Test
    public void testParcelable() {
        ExternalTimeSuggestion suggestion = new ExternalTimeSuggestion(
                ARBITRARY_TIME.getReferenceTimeMillis(),
                ARBITRARY_TIME.getValue());
        assertRoundTripParcelable(suggestion);

        // DebugInfo should also be stored (but is not checked by equals())
        suggestion.addDebugInfo("This is debug info");
        ExternalTimeSuggestion rtSuggestion = roundTripParcelable(suggestion);
        assertEquals(suggestion.getDebugInfo(), rtSuggestion.getDebugInfo());
    }
}
+0 −53
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.service.timezone;

import static org.junit.Assert.assertEquals;

import android.os.Parcel;
import android.os.Parcelable;

import java.lang.reflect.Field;

/** Utility methods related to {@link Parcelable} objects used in several tests. */
final class ParcelableTestSupport {

    private ParcelableTestSupport() {}

    /** Returns the result of parceling and unparceling the argument. */
    @SuppressWarnings("unchecked")
    public static <T extends Parcelable> T roundTripParcelable(T parcelable) {
        Parcel parcel = Parcel.obtain();
        parcel.writeTypedObject(parcelable, 0);
        parcel.setDataPosition(0);

        Parcelable.Creator<T> creator;
        try {
            Field creatorField = parcelable.getClass().getField("CREATOR");
            creator = (Parcelable.Creator<T>) creatorField.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new AssertionError(e);
        }
        T toReturn = parcel.readTypedObject(creator);
        parcel.recycle();
        return toReturn;
    }

    public static <T extends Parcelable> void assertRoundTripParcelable(T instance) {
        assertEquals(instance, roundTripParcelable(instance));
    }
}
+0 −107
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.service.timezone;

import static android.service.timezone.ParcelableTestSupport.assertRoundTripParcelable;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import static java.util.Collections.singletonList;

import org.junit.Test;

import java.util.List;

public class TimeZoneProviderSuggestionTest {

    private static final long ARBITRARY_ELAPSED_REALTIME_MILLIS = 9999;

    private static final List<String> ARBITRARY_TIME_ZONE_IDS = singletonList("Europe/London");

    @Test(expected = RuntimeException.class)
    public void testInvalidTimeZoneIds() {
        new TimeZoneProviderSuggestion.Builder()
                .setTimeZoneIds(null);
    }

    @Test
    public void testEquals() {
        TimeZoneProviderSuggestion.Builder builder1 = new TimeZoneProviderSuggestion.Builder()
                .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS);
        {
            TimeZoneProviderSuggestion one = builder1.build();
            assertEquals(one, one);
        }

        TimeZoneProviderSuggestion.Builder builder2 = new TimeZoneProviderSuggestion.Builder()
                .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS);
        {
            TimeZoneProviderSuggestion one = builder1.build();
            TimeZoneProviderSuggestion two = builder2.build();
            assertEquals(one, two);
            assertEquals(two, one);
        }

        builder1.setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS + 1);
        {
            TimeZoneProviderSuggestion one = builder1.build();
            TimeZoneProviderSuggestion two = builder2.build();
            assertNotEquals(one, two);
            assertNotEquals(two, one);
        }

        builder2.setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS + 1);
        {
            TimeZoneProviderSuggestion one = builder1.build();
            TimeZoneProviderSuggestion two = builder2.build();
            assertEquals(one, two);
            assertEquals(two, one);
        }

        builder2.setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS);
        {
            TimeZoneProviderSuggestion one = builder1.build();
            TimeZoneProviderSuggestion two = builder2.build();
            assertNotEquals(one, two);
            assertNotEquals(two, one);
        }

        builder1.setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS);
        {
            TimeZoneProviderSuggestion one = builder1.build();
            TimeZoneProviderSuggestion two = builder2.build();
            assertEquals(one, two);
            assertEquals(two, one);
        }
    }

    @Test
    public void testParcelable_noTimeZoneIds() {
        TimeZoneProviderSuggestion.Builder builder = new TimeZoneProviderSuggestion.Builder()
                .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS);
        assertRoundTripParcelable(builder.build());
    }

    @Test
    public void testParcelable_withTimeZoneIds() {
        TimeZoneProviderSuggestion.Builder builder = new TimeZoneProviderSuggestion.Builder()
                .setElapsedRealtimeMillis(ARBITRARY_ELAPSED_REALTIME_MILLIS)
                .setTimeZoneIds(ARBITRARY_TIME_ZONE_IDS);
        assertRoundTripParcelable(builder.build());
    }
}