1 package com.android.server.location; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.platform.test.annotations.Presubmit; 6 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 import org.robolectric.RobolectricTestRunner; 10 11 import java.util.HashSet; 12 13 /** 14 * Unit tests for {@link GnssPositionMode}. 15 */ 16 @RunWith(RobolectricTestRunner.class) 17 @Presubmit 18 public class GnssPositionModeTest { 19 20 private GnssPositionMode positionMode1 = createGnssPositionMode(0, 1000); 21 private GnssPositionMode positionMode2 = createGnssPositionMode(0, 1000); 22 private GnssPositionMode positionMode3 = createGnssPositionMode(1, 1000); 23 24 @Test testHashCode()25 public void testHashCode() { 26 assertThat(positionMode1.hashCode()).isEqualTo(positionMode2.hashCode()); 27 assertThat(positionMode1.hashCode()).isNotEqualTo(positionMode3.hashCode()); 28 assertThat(positionMode1.hashCode()).isNotEqualTo(positionMode3.hashCode()); 29 30 HashSet<Integer> hashSet = new HashSet<>(); 31 hashSet.add(positionMode1.hashCode()); 32 hashSet.add(positionMode2.hashCode()); 33 assertThat(hashSet.size()).isEqualTo(1); 34 hashSet.add(positionMode3.hashCode()); 35 assertThat(hashSet.size()).isEqualTo(2); 36 } 37 38 @Test checkIfEqualsImpliesSameHashCode()39 public void checkIfEqualsImpliesSameHashCode() { 40 assertTEqualsImpliesSameHashCode(positionMode1, positionMode2); 41 assertTEqualsImpliesSameHashCode(positionMode2, positionMode3); 42 } 43 assertTEqualsImpliesSameHashCode(GnssPositionMode mode1, GnssPositionMode mode2)44 private void assertTEqualsImpliesSameHashCode(GnssPositionMode mode1, GnssPositionMode mode2) { 45 if (mode1.equals(mode2)) { 46 assertThat(mode1.hashCode()).isEqualTo(mode2.hashCode()); 47 } 48 } 49 createGnssPositionMode(int mode, int minInterval)50 private GnssPositionMode createGnssPositionMode(int mode, int minInterval) { 51 return new GnssPositionMode(mode, 0, minInterval, 0, 0, true); 52 } 53 } 54