1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.adservices.topics; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.android.adservices.common.AdServicesUnitTestCase; 22 import com.android.adservices.shared.testing.EqualsTester; 23 import com.android.adservices.shared.testing.annotations.RequiresSdkLevelAtLeastS; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 28 /** Unit tests for {@link android.adservices.topics.Topic} */ 29 @RequiresSdkLevelAtLeastS 30 public final class TopicTest extends AdServicesUnitTestCase { 31 32 private static final long TAXONOMY_VERSION_2 = 2L; 33 private static final long MODEL_VERSION_5 = 5L; 34 private static final int TOPIC_ID_1 = 1; 35 36 private Topic mTopic1; 37 private Topic mTopic2; 38 39 @Before setup()40 public void setup() throws Exception { 41 generateTopics(); 42 } 43 44 @Test testGetters()45 public void testGetters() { 46 expect.that(mTopic1.getTopicId()).isEqualTo(1); 47 expect.that(mTopic1.getModelVersion()).isEqualTo(5L); 48 expect.that(mTopic1.getTaxonomyVersion()).isEqualTo(2L); 49 } 50 51 @Test testToString()52 public void testToString() { 53 String expectedTopicString = "Topic{mTaxonomyVersion=2, mModelVersion=5, mTopicCode=1}"; 54 expect.that(mTopic1.toString()).isEqualTo(expectedTopicString); 55 expect.that(mTopic2.toString()).isEqualTo(expectedTopicString); 56 } 57 58 @Test testEquals()59 public void testEquals() { 60 EqualsTester et = new EqualsTester(expect); 61 et.expectObjectsAreEqual(mTopic1, mTopic2); 62 } 63 64 @Test testEquals_nullObject()65 public void testEquals_nullObject() { 66 // To test code won't throw if comparing to a null object. 67 assertThat(mTopic1).isNotEqualTo(null); 68 } 69 70 @Test testNotEquals()71 public void testNotEquals() { 72 Topic different = 73 new Topic( 74 /* mTaxonomyVersion */ 100L, /* mModelVersion */ 101L, /* mTopicId */ 102); 75 EqualsTester et = new EqualsTester(expect); 76 et.expectObjectsAreNotEqual(mTopic1, different); 77 } 78 generateTopics()79 private void generateTopics() { 80 mTopic1 = new Topic(TAXONOMY_VERSION_2, MODEL_VERSION_5, TOPIC_ID_1); 81 mTopic2 = new Topic(TAXONOMY_VERSION_2, MODEL_VERSION_5, TOPIC_ID_1); 82 } 83 } 84