diff options
Diffstat (limited to 'core/tests/coretests/src/android')
6 files changed, 211 insertions, 3 deletions
diff --git a/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java b/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java new file mode 100644 index 000000000000..90b33058d4e8 --- /dev/null +++ b/core/tests/coretests/src/android/app/time/ExternalTimeSuggestionTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2022 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.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; + +import static org.junit.Assert.assertEquals; + +import android.os.ShellCommand; + +import org.junit.Test; + +/** + * Tests for non-SDK methods on {@link ExternalTimeSuggestion}. + * Also see {@link android.app.time.cts.ExternalTimeSuggestionTest} + */ +public class ExternalTimeSuggestionTest { + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noReferenceTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--unix_epoch_time 12345"); + ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noUnixEpochTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321"); + ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test + public void testParseCommandLineArg_validSuggestion() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345"); + ExternalTimeSuggestion expectedSuggestion = new ExternalTimeSuggestion(54321L, 12345L); + ExternalTimeSuggestion actualSuggestion = + ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); + assertEquals(expectedSuggestion, actualSuggestion); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_unknownArgument() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + ExternalTimeSuggestion.parseCommandLineArg(testShellCommand); + } +} diff --git a/core/tests/coretests/src/android/app/timedetector/GnssTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/GnssTimeSuggestionTest.java index e248010319e1..af403a20ae7b 100644 --- a/core/tests/coretests/src/android/app/timedetector/GnssTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/GnssTimeSuggestionTest.java @@ -18,10 +18,12 @@ package android.app.timedetector; import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ParcelableTestSupport.roundTripParcelable; +import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.os.ShellCommand; import android.os.TimestampedValue; import org.junit.Test; @@ -63,4 +65,36 @@ public class GnssTimeSuggestionTest { GnssTimeSuggestion rtSuggestion = roundTripParcelable(suggestion); assertEquals(suggestion.getDebugInfo(), rtSuggestion.getDebugInfo()); } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noReferenceTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--unix_epoch_time 12345"); + GnssTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noUnixEpochTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321"); + GnssTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test + public void testParseCommandLineArg_validSuggestion() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345"); + TimestampedValue<Long> timeSignal = new TimestampedValue<>(54321L, 12345L); + GnssTimeSuggestion expectedSuggestion = new GnssTimeSuggestion(timeSignal); + GnssTimeSuggestion actualSuggestion = + GnssTimeSuggestion.parseCommandLineArg(testShellCommand); + assertEquals(expectedSuggestion, actualSuggestion); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_unknownArgument() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + GnssTimeSuggestion.parseCommandLineArg(testShellCommand); + } } diff --git a/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java index 750ffa1c9a54..94218cdea0ed 100644 --- a/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/ManualTimeSuggestionTest.java @@ -18,10 +18,12 @@ package android.app.timedetector; import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ParcelableTestSupport.roundTripParcelable; +import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.os.ShellCommand; import android.os.TimestampedValue; import org.junit.Test; @@ -63,4 +65,36 @@ public class ManualTimeSuggestionTest { ManualTimeSuggestion rtSuggestion = roundTripParcelable(suggestion); assertEquals(suggestion.getDebugInfo(), rtSuggestion.getDebugInfo()); } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noReferenceTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--unix_epoch_time 12345"); + ManualTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noUnixEpochTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321"); + ManualTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test + public void testParseCommandLineArg_validSuggestion() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345"); + TimestampedValue<Long> timeSignal = new TimestampedValue<>(54321L, 12345L); + ManualTimeSuggestion expectedSuggestion = new ManualTimeSuggestion(timeSignal); + ManualTimeSuggestion actualSuggestion = + ManualTimeSuggestion.parseCommandLineArg(testShellCommand); + assertEquals(expectedSuggestion, actualSuggestion); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_unknownArgument() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + ManualTimeSuggestion.parseCommandLineArg(testShellCommand); + } } diff --git a/core/tests/coretests/src/android/app/timedetector/NetworkTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/NetworkTimeSuggestionTest.java index b88c36f20bc6..0e09dd3e9aab 100644 --- a/core/tests/coretests/src/android/app/timedetector/NetworkTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/NetworkTimeSuggestionTest.java @@ -18,10 +18,12 @@ package android.app.timedetector; import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ParcelableTestSupport.roundTripParcelable; +import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.os.ShellCommand; import android.os.TimestampedValue; import org.junit.Test; @@ -63,4 +65,36 @@ public class NetworkTimeSuggestionTest { NetworkTimeSuggestion rtSuggestion = roundTripParcelable(suggestion); assertEquals(suggestion.getDebugInfo(), rtSuggestion.getDebugInfo()); } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noReferenceTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--unix_epoch_time 12345"); + NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noUnixEpochTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321"); + NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test + public void testParseCommandLineArg_validSuggestion() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345"); + TimestampedValue<Long> timeSignal = new TimestampedValue<>(54321L, 12345L); + NetworkTimeSuggestion expectedSuggestion = new NetworkTimeSuggestion(timeSignal); + NetworkTimeSuggestion actualSuggestion = + NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); + assertEquals(expectedSuggestion, actualSuggestion); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_unknownArgument() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + NetworkTimeSuggestion.parseCommandLineArg(testShellCommand); + } } diff --git a/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java b/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java index cc7557977e80..bb995a852637 100644 --- a/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java +++ b/core/tests/coretests/src/android/app/timedetector/TelephonyTimeSuggestionTest.java @@ -18,10 +18,12 @@ package android.app.timedetector; import static android.app.timezonedetector.ParcelableTestSupport.assertRoundTripParcelable; import static android.app.timezonedetector.ParcelableTestSupport.roundTripParcelable; +import static android.app.timezonedetector.ShellCommandTestSupport.createShellCommandWithArgsAndOptions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import android.os.ShellCommand; import android.os.TimestampedValue; import org.junit.Test; @@ -95,4 +97,45 @@ public class TelephonyTimeSuggestionTest { assertEquals(suggestion1.getDebugInfo(), rtSuggestion1.getDebugInfo()); } } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noSlotIndex() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--reference_time 54321 --unix_epoch_time 12345"); + TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noReferenceTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--slot_index 0 --unix_epoch_time 12345"); + TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_noUnixEpochTime() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--slot_index 0 --reference_time 54321"); + TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); + } + + @Test + public void testParseCommandLineArg_validSuggestion() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--slot_index 0 --reference_time 54321 --unix_epoch_time 12345"); + TelephonyTimeSuggestion expectedSuggestion = + new TelephonyTimeSuggestion.Builder(0) + .setUnixEpochTime(new TimestampedValue<>(54321L, 12345L)) + .build(); + TelephonyTimeSuggestion actualSuggestion = + TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); + assertEquals(expectedSuggestion, actualSuggestion); + } + + @Test(expected = IllegalArgumentException.class) + public void testParseCommandLineArg_unknownArgument() { + ShellCommand testShellCommand = createShellCommandWithArgsAndOptions( + "--slot_index 0 --reference_time 54321 --unix_epoch_time 12345 --bad_arg 0"); + TelephonyTimeSuggestion.parseCommandLineArg(testShellCommand); + } } diff --git a/core/tests/coretests/src/android/app/timezonedetector/ShellCommandTestSupport.java b/core/tests/coretests/src/android/app/timezonedetector/ShellCommandTestSupport.java index 8d8290c7bdc9..4efaed11168e 100644 --- a/core/tests/coretests/src/android/app/timezonedetector/ShellCommandTestSupport.java +++ b/core/tests/coretests/src/android/app/timezonedetector/ShellCommandTestSupport.java @@ -26,14 +26,14 @@ import java.util.Arrays; import java.util.List; /** Utility methods related to {@link ShellCommand} objects used in several tests. */ -final class ShellCommandTestSupport { +public final class ShellCommandTestSupport { private ShellCommandTestSupport() {} - static ShellCommand createShellCommandWithArgsAndOptions(String argsWithSpaces) { + public static ShellCommand createShellCommandWithArgsAndOptions(String argsWithSpaces) { return createShellCommandWithArgsAndOptions(Arrays.asList(argsWithSpaces.split(" "))); } - static ShellCommand createShellCommandWithArgsAndOptions(List<String> args) { + public static ShellCommand createShellCommandWithArgsAndOptions(List<String> args) { ShellCommand command = mock(ShellCommand.class); class ArgProvider { private int mCount; |