diff options
author | 2025-02-19 16:00:51 -0800 | |
---|---|---|
committer | 2025-02-25 18:52:16 -0800 | |
commit | 0b7dd7fca71875e84a4630c22a1e762841861660 (patch) | |
tree | c64f12d7ff8293e7fe9356805b4c65ebc86886b6 | |
parent | 3e23c7864467431d270bd657674517d75400f16b (diff) |
Errorprone fix & enforce SelfAssertion
Also fix suppress of TruthIncompatibleType that were related
Bug: 311772251
Test: m BluetoothInstrumentationTests
Flag: TEST_ONLY
Change-Id: Ic63c26bcf9411e1670684b17f05b4fe1785b9a10
10 files changed, 69 insertions, 175 deletions
diff --git a/Android.bp b/Android.bp index e1e9a5041d..b04b2333ad 100644 --- a/Android.bp +++ b/Android.bp @@ -172,6 +172,7 @@ java_defaults { "-Xep:ReferenceEquality:ERROR", "-Xep:ReturnAtTheEndOfVoidFunction:ERROR", "-Xep:ReturnFromVoid:ERROR", + "-Xep:SelfAssertion:ERROR", "-Xep:StaticAssignmentInConstructor:ERROR", "-Xep:StaticGuardedByInstance:ERROR", "-Xep:StringCaseLocaleUsage:ERROR", diff --git a/android/app/tests/unit/Android.bp b/android/app/tests/unit/Android.bp index e0ed60ac22..01d195cc43 100644 --- a/android/app/tests/unit/Android.bp +++ b/android/app/tests/unit/Android.bp @@ -43,6 +43,7 @@ android_test { "flag-junit", "framework-bluetooth-pre-jarjar", "gson", + "guava-android-testlib", "mmslib", "mockito-target-extended", "modules-utils-handlerexecutor", diff --git a/android/app/tests/unit/AndroidTest.xml b/android/app/tests/unit/AndroidTest.xml index f640db51d4..a81108ed95 100644 --- a/android/app/tests/unit/AndroidTest.xml +++ b/android/app/tests/unit/AndroidTest.xml @@ -62,6 +62,8 @@ So instead we use the app cache folder for filter --> <option name="test-filter-dir" value="/data/data/com.android.bluetooth/cache" /> <option name="hidden-api-checks" value="false"/> + <!-- Ignores tests introduced by guava-android-testlib --> + <option name="exclude-annotation" value="org.junit.Ignore"/> </test> <!-- Only run if the Bluetooth Mainline module is installed. --> diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpItemTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpItemTest.java index a059bb12c4..dbadfc526a 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpItemTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/AvrcpItemTest.java @@ -20,7 +20,6 @@ import static com.android.bluetooth.TestUtils.getTestDevice; import static com.google.common.truth.Truth.assertThat; -import android.annotation.SuppressLint; import android.bluetooth.BluetoothDevice; import android.net.Uri; import android.support.v4.media.MediaBrowserCompat.MediaItem; @@ -29,6 +28,8 @@ import android.support.v4.media.MediaMetadataCompat; import androidx.test.runner.AndroidJUnit4; +import com.google.common.testing.EqualsTester; + import org.junit.Test; import org.junit.runner.RunWith; @@ -601,33 +602,15 @@ public final class AvrcpItemTest { } @Test - public void equals_withItself() { - AvrcpItem.Builder builder = new AvrcpItem.Builder(); + public void equals() { + AvrcpItem item = new AvrcpItem.Builder().build(); + AvrcpItem itemEqual = new AvrcpItem.Builder().build(); - AvrcpItem item = builder.build(); - - assertThat(item).isEqualTo(item); - } - - @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void equals_withDifferentInstance() { - AvrcpItem.Builder builder = new AvrcpItem.Builder(); String notAvrcpItem = "notAvrcpItem"; - AvrcpItem item = builder.build(); - - assertThat(item).isNotEqualTo(notAvrcpItem); - } - - @Test - public void equals_withItemContainingSameInfo() { - AvrcpItem.Builder builder = new AvrcpItem.Builder(); - AvrcpItem.Builder builderEqual = new AvrcpItem.Builder(); - - AvrcpItem item = builder.build(); - AvrcpItem itemEqual = builderEqual.build(); - - assertThat(item).isEqualTo(itemEqual); + new EqualsTester() + .addEqualityGroup(item, item, itemEqual) + .addEqualityGroup(notAvrcpItem) + .testEquals(); } } diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/BrowseNodeTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/BrowseNodeTest.java index edf38bad24..dab7613a9a 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/BrowseNodeTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/BrowseNodeTest.java @@ -20,7 +20,6 @@ import static com.android.bluetooth.TestUtils.getTestDevice; import static com.google.common.truth.Truth.assertThat; -import android.annotation.SuppressLint; import android.bluetooth.BluetoothDevice; import android.platform.test.annotations.EnableFlags; import android.platform.test.flag.junit.SetFlagsRule; @@ -31,6 +30,8 @@ import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.avrcpcontroller.BrowseTree.BrowseNode; import com.android.bluetooth.flags.Flags; +import com.google.common.testing.EqualsTester; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -223,21 +224,18 @@ public class BrowseNodeTest { } @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void equals_withDifferentClass() { - AvrcpItem avrcpItem = new AvrcpItem.Builder().setUuid(TEST_UUID).build(); - - assertThat(mRootNode).isNotEqualTo(avrcpItem); - } - - @Test - public void equals_withSameId() { + public void equals() { BrowseNode browseNodeOne = mBrowseTree.new BrowseNode(new AvrcpItem.Builder().setUuid(TEST_UUID).build()); BrowseNode browseNodeTwo = mBrowseTree.new BrowseNode(new AvrcpItem.Builder().setUuid(TEST_UUID).build()); - assertThat(browseNodeOne).isEqualTo(browseNodeTwo); + AvrcpItem avrcpItem = new AvrcpItem.Builder().setUuid(TEST_UUID).build(); + + new EqualsTester() + .addEqualityGroup(browseNodeOne, browseNodeTwo) + .addEqualityGroup(avrcpItem) + .testEquals(); } @Test diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipAttachmentFormatTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipAttachmentFormatTest.java index 02a63b298b..03afb2826a 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipAttachmentFormatTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipAttachmentFormatTest.java @@ -22,6 +22,8 @@ import android.annotation.SuppressLint; import androidx.test.runner.AndroidJUnit4; +import com.google.common.testing.EqualsTester; + import org.junit.Test; import org.junit.runner.RunWith; @@ -422,30 +424,17 @@ public class BipAttachmentFormatTest { } @Test - public void testEquals_withSameInstance() { - BipAttachmentFormat attachment = - new BipAttachmentFormat("text/plain", null, "thisisatextfile.txt", -1, null, null); - - assertThat(attachment).isEqualTo(attachment); - } - - @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void testEquals_withDifferentClass() { - BipAttachmentFormat attachment = - new BipAttachmentFormat("text/plain", null, "thisisatextfile.txt", -1, null, null); - String notAttachment = "notAttachment"; - - assertThat(attachment).isNotEqualTo(notAttachment); - } - - @Test - public void testEquals_withSameInfo() { + public void testEquals() { BipAttachmentFormat attachment = new BipAttachmentFormat("text/plain", null, "thisisatextfile.txt", -1, null, null); BipAttachmentFormat attachmentEqual = new BipAttachmentFormat("text/plain", null, "thisisatextfile.txt", -1, null, null); - assertThat(attachment).isEqualTo(attachmentEqual); + String notAttachment = "notAttachment"; + + new EqualsTester() + .addEqualityGroup(attachment, attachment, attachmentEqual) + .addEqualityGroup(notAttachment) + .testEquals(); } } diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipDatetimeTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipDatetimeTest.java index bf3b29a00d..5d93884efb 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipDatetimeTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipDatetimeTest.java @@ -22,6 +22,8 @@ import android.annotation.SuppressLint; import androidx.test.runner.AndroidJUnit4; +import com.google.common.testing.EqualsTester; + import org.junit.Test; import org.junit.runner.RunWith; @@ -213,29 +215,7 @@ public class BipDatetimeTest { } @Test - public void testEquals_withSameInstance() { - TimeZone utc = TimeZone.getTimeZone("UTC"); - utc.setRawOffset(0); - - BipDateTime bipDate = new BipDateTime(makeDate(1, 1, 2000, 6, 1, 15, utc)); - - assertThat(bipDate).isEqualTo(bipDate); - } - - @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void testEquals_withDifferentClass() { - TimeZone utc = TimeZone.getTimeZone("UTC"); - utc.setRawOffset(0); - - BipDateTime bipDate = new BipDateTime(makeDate(1, 1, 2000, 6, 1, 15, utc)); - String notBipDate = "notBipDate"; - - assertThat(bipDate).isNotEqualTo(notBipDate); - } - - @Test - public void testEquals_withSameInfo() { + public void testEquals() { TimeZone utc = TimeZone.getTimeZone("UTC"); utc.setRawOffset(0); Date date = makeDate(1, 1, 2000, 6, 1, 15, utc); @@ -243,6 +223,11 @@ public class BipDatetimeTest { BipDateTime bipDate = new BipDateTime(date); BipDateTime bipDateEqual = new BipDateTime(date); - assertThat(bipDate).isEqualTo(bipDateEqual); + String notBipDate = "notBipDate"; + + new EqualsTester() + .addEqualityGroup(bipDate, bipDate, bipDateEqual) + .addEqualityGroup(notBipDate) + .testEquals(); } } diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageDescriptorTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageDescriptorTest.java index 58f89753cd..8d0415dcaa 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageDescriptorTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageDescriptorTest.java @@ -18,10 +18,10 @@ package com.android.bluetooth.avrcpcontroller; import static com.google.common.truth.Truth.assertThat; -import android.annotation.SuppressLint; - import androidx.test.runner.AndroidJUnit4; +import com.google.common.testing.EqualsTester; + import org.junit.Test; import org.junit.runner.RunWith; @@ -240,33 +240,15 @@ public class BipImageDescriptorTest { } @Test - public void testEquals_sameInstance() { - BipImageDescriptor.Builder builder = new BipImageDescriptor.Builder(); - - BipImageDescriptor descriptor = builder.build(); - - assertThat(descriptor).isEqualTo(descriptor); - } - - @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void testEquals_differentClass() { - BipImageDescriptor.Builder builder = new BipImageDescriptor.Builder(); + public void testEquals() { + BipImageDescriptor descriptor = new BipImageDescriptor.Builder().build(); + BipImageDescriptor descriptorEqual = new BipImageDescriptor.Builder().build(); - BipImageDescriptor descriptor = builder.build(); String notDescriptor = "notDescriptor"; - assertThat(descriptor).isNotEqualTo(notDescriptor); - } - - @Test - public void testEquals_sameInfo() { - BipImageDescriptor.Builder builder = new BipImageDescriptor.Builder(); - BipImageDescriptor.Builder builderEqual = new BipImageDescriptor.Builder(); - - BipImageDescriptor descriptor = builder.build(); - BipImageDescriptor descriptorEqual = builderEqual.build(); - - assertThat(descriptor).isEqualTo(descriptorEqual); + new EqualsTester() + .addEqualityGroup(descriptor, descriptor, descriptorEqual) + .addEqualityGroup(notDescriptor) + .testEquals(); } } diff --git a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageFormatTest.java b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageFormatTest.java index 2f1e60cf34..75d04f6c8b 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageFormatTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/avrcpcontroller/bip/BipImageFormatTest.java @@ -18,10 +18,10 @@ package com.android.bluetooth.avrcpcontroller; import static com.google.common.truth.Truth.assertThat; -import android.annotation.SuppressLint; - import androidx.test.runner.AndroidJUnit4; +import com.google.common.testing.EqualsTester; + import org.junit.Test; import org.junit.runner.RunWith; @@ -308,37 +308,18 @@ public class BipImageFormatTest { } @Test - public void testEquals_withSameInstance() { - BipImageFormat format = - BipImageFormat.createNative( - new BipEncoding(BipEncoding.JPEG, null), - BipPixel.createFixed(1280, 1024), - -1); - - assertThat(format).isEqualTo(format); - } - - @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void testEquals_withDifferentClass() { - BipImageFormat format = - BipImageFormat.createNative( - new BipEncoding(BipEncoding.JPEG, null), - BipPixel.createFixed(1280, 1024), - -1); - String notFormat = "notFormat"; - - assertThat(format).isNotEqualTo(notFormat); - } - - @Test - public void testEquals_withSameInfo() { + public void testEquals() { BipEncoding encoding = new BipEncoding(BipEncoding.JPEG, null); BipPixel pixel = BipPixel.createFixed(1280, 1024); BipImageFormat format = BipImageFormat.createNative(encoding, pixel, -1); BipImageFormat formatEqual = BipImageFormat.createNative(encoding, pixel, -1); - assertThat(format).isEqualTo(formatEqual); + String notFormat = "notFormat"; + + new EqualsTester() + .addEqualityGroup(format, format, formatEqual) + .addEqualityGroup(notFormat) + .testEquals(); } } diff --git a/android/app/tests/unit/src/com/android/bluetooth/map/MsgTest.java b/android/app/tests/unit/src/com/android/bluetooth/map/MsgTest.java index 041a7ff86d..f9f0b50fa1 100644 --- a/android/app/tests/unit/src/com/android/bluetooth/map/MsgTest.java +++ b/android/app/tests/unit/src/com/android/bluetooth/map/MsgTest.java @@ -18,11 +18,11 @@ package com.android.bluetooth.map; import static com.google.common.truth.Truth.assertThat; -import android.annotation.SuppressLint; - import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; +import com.google.common.testing.EqualsTester; + import org.junit.Test; import org.junit.runner.RunWith; @@ -53,50 +53,22 @@ public class MsgTest { } @Test - public void equals_withSameInstance() { - BluetoothMapContentObserver.Msg msg = - new BluetoothMapContentObserver.Msg(TEST_ID, TEST_FOLDER_ID, TEST_READ_FLAG); - - assertThat(msg.equals(msg)).isTrue(); - } - - @Test - public void equals_withNull() { - BluetoothMapContentObserver.Msg msg = - new BluetoothMapContentObserver.Msg(TEST_ID, TEST_FOLDER_ID, TEST_READ_FLAG); - - assertThat(msg).isNotNull(); - } - - @Test - @SuppressLint("TruthIncompatibleType") // That the point of this test - public void equals_withDifferentClass() { - BluetoothMapContentObserver.Msg msg = - new BluetoothMapContentObserver.Msg(TEST_ID, TEST_FOLDER_ID, TEST_READ_FLAG); - String msgOfDifferentClass = "msg_of_different_class"; - - assertThat(msg).isNotEqualTo(msgOfDifferentClass); - } - - @Test - public void equals_withDifferentId() { + public void equals() { long idOne = 1; long idTwo = 2; BluetoothMapContentObserver.Msg msg = new BluetoothMapContentObserver.Msg(idOne, TEST_FOLDER_ID, TEST_READ_FLAG); + BluetoothMapContentObserver.Msg msgWithSameId = + new BluetoothMapContentObserver.Msg(idOne, TEST_FOLDER_ID, TEST_READ_FLAG); BluetoothMapContentObserver.Msg msgWithDifferentId = new BluetoothMapContentObserver.Msg(idTwo, TEST_FOLDER_ID, TEST_READ_FLAG); - assertThat(msg).isNotEqualTo(msgWithDifferentId); - } - - @Test - public void equals_withEqualInstance() { - BluetoothMapContentObserver.Msg msg = - new BluetoothMapContentObserver.Msg(TEST_ID, TEST_FOLDER_ID, TEST_READ_FLAG); - BluetoothMapContentObserver.Msg msgWithSameId = - new BluetoothMapContentObserver.Msg(TEST_ID, TEST_FOLDER_ID, TEST_READ_FLAG); + String msgOfDifferentClass = "msg_of_different_class"; - assertThat(msg).isEqualTo(msgWithSameId); + new EqualsTester() + .addEqualityGroup(msg, msg, msgWithSameId) + .addEqualityGroup(msgWithDifferentId, msgWithDifferentId) + .addEqualityGroup(msgOfDifferentClass) + .testEquals(); } } |