Create a core-util module with some common utilities.

This commit is contained in:
Greyson Parrelli
2020-12-04 18:31:58 -05:00
parent 831cd2f297
commit 8e93bf9075
958 changed files with 1879 additions and 2035 deletions

View File

@@ -6,7 +6,6 @@ import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import org.thoughtcrime.securesms.logging.Log;
import org.junit.Before;
import org.junit.runner.RunWith;
@@ -15,6 +14,7 @@ import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.signal.core.util.logging.Log;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;

View File

@@ -2,15 +2,17 @@ package org.thoughtcrime.securesms.database;
import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

View File

@@ -8,8 +8,8 @@ import com.google.protobuf.InvalidProtocolBufferException;
import org.junit.Test;
import org.signal.storageservice.protos.groups.GroupInviteLink;
import org.signal.zkgroup.InvalidInputException;
import org.whispersystems.util.Base64UrlSafe;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.util.Base64UrlSafe;
import java.io.IOException;

View File

@@ -1,9 +1,9 @@
package org.thoughtcrime.securesms.groups.v2;
import org.junit.Test;
import org.signal.core.util.logging.Log;
import org.signal.zkgroup.profiles.ProfileKey;
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.testutil.LogRecorder;
import java.util.Collections;

View File

@@ -2,11 +2,11 @@ package org.thoughtcrime.securesms.groups.v2.processing;
import org.junit.Before;
import org.junit.Test;
import org.signal.core.util.logging.Log;
import org.signal.storageservice.protos.groups.local.DecryptedGroup;
import org.signal.storageservice.protos.groups.local.DecryptedGroupChange;
import org.signal.storageservice.protos.groups.local.DecryptedMember;
import org.signal.storageservice.protos.groups.local.DecryptedString;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.testutil.LogRecorder;
import org.whispersystems.signalservice.api.util.UuidUtil;

View File

@@ -4,9 +4,9 @@ import androidx.annotation.NonNull;
import org.junit.BeforeClass;
import org.junit.Test;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
import org.thoughtcrime.securesms.jobmanager.persistence.JobStorage;
import org.thoughtcrime.securesms.logging.Log;
import java.util.ArrayList;
import java.util.Arrays;

View File

@@ -1,12 +1,15 @@
package org.thoughtcrime.securesms.jobmanager.impl;
import org.junit.Test;
import org.signal.core.util.StreamUtil;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.util.Util;
import java.io.IOException;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public final class JsonDataSerializerTest {
@@ -14,7 +17,7 @@ public final class JsonDataSerializerTest {
@Test
public void deserialize_dataMatchesExpected() throws IOException {
Data data = new JsonDataSerializer().deserialize(Util.readFullyAsString(ClassLoader.getSystemClassLoader().getResourceAsStream("data/data_serialized.json")));
Data data = new JsonDataSerializer().deserialize(StreamUtil.readFullyAsString(ClassLoader.getSystemClassLoader().getResourceAsStream("data/data_serialized.json")));
assertEquals("s1 value", data.getString("s1"));
assertEquals("s2 value", data.getString("s2"));

View File

@@ -13,10 +13,8 @@ import org.thoughtcrime.securesms.jobmanager.persistence.ConstraintSpec;
import org.thoughtcrime.securesms.jobmanager.persistence.DependencySpec;
import org.thoughtcrime.securesms.jobmanager.persistence.FullSpec;
import org.thoughtcrime.securesms.jobmanager.persistence.JobSpec;
import org.thoughtcrime.securesms.testutil.DirectExecutor;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

View File

@@ -3,6 +3,8 @@ package org.thoughtcrime.securesms.l10n;
import android.app.Application;
import android.content.res.Resources;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@@ -16,8 +18,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import androidx.test.core.app.ApplicationProvider;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

View File

@@ -1,37 +0,0 @@
package org.thoughtcrime.securesms.logging;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public final class LogTest {
@Test
public void tag_short_class_name() {
assertEquals("MyClass", Log.tag(MyClass.class));
}
@Test
public void tag_23_character_class_name() {
String tag = Log.tag(TwentyThreeCharacters23.class);
assertEquals("TwentyThreeCharacters23", tag);
assertEquals(23, tag.length());
}
@Test
public void tag_24_character_class_name() {
assertEquals(24, TwentyFour24Characters24.class.getSimpleName().length());
String tag = Log.tag(TwentyFour24Characters24.class);
assertEquals("TwentyFour24Characters2", tag);
assertEquals(23, Log.tag(TwentyThreeCharacters23.class).length());
}
private class MyClass {
}
private class TwentyThreeCharacters23 {
}
private class TwentyFour24Characters24 {
}
}

View File

@@ -11,8 +11,8 @@ import org.robolectric.annotation.Config;
import org.thoughtcrime.securesms.profiles.ProfileName;
import org.whispersystems.libsignal.util.guava.Optional;
import static android.provider.ContactsContract.Intents.Insert.NAME;
import static android.provider.ContactsContract.Intents.Insert.EMAIL;
import static android.provider.ContactsContract.Intents.Insert.NAME;
import static android.provider.ContactsContract.Intents.Insert.PHONE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

View File

@@ -5,7 +5,7 @@ import androidx.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.testutil.LogRecorder;
import org.whispersystems.libsignal.util.guava.Optional;

View File

@@ -12,7 +12,6 @@ import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.util.FeatureFlags;
import static org.junit.Assert.assertFalse;

View File

@@ -1,8 +1,8 @@
package org.thoughtcrime.securesms.registration.v2;
import org.junit.Test;
import org.signal.core.util.StreamUtil;
import org.thoughtcrime.securesms.registration.v2.testdata.KbsTestVector;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.api.crypto.InvalidCiphertextException;
import org.whispersystems.signalservice.api.kbs.HashedPin;
import org.whispersystems.signalservice.api.kbs.KbsData;
@@ -50,7 +50,7 @@ public final class HashedPinKbsDataTest {
private static KbsTestVector[] getKbsTestVectorList() throws IOException {
try (InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("data/kbs_vectors.json")) {
KbsTestVector[] data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), KbsTestVector[].class);
KbsTestVector[] data = JsonUtil.fromJson(StreamUtil.readFullyAsString(resourceAsStream), KbsTestVector[].class);
assertTrue(data.length > 0);

View File

@@ -1,8 +1,8 @@
package org.thoughtcrime.securesms.registration.v2;
import org.junit.Test;
import org.signal.core.util.StreamUtil;
import org.thoughtcrime.securesms.registration.v2.testdata.PinSanitationVector;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.internal.registrationpin.PinHasher;
import org.whispersystems.signalservice.internal.util.Hex;
import org.whispersystems.signalservice.internal.util.JsonUtil;
@@ -32,7 +32,7 @@ public final class PinHasher_normalize_Test {
private static PinSanitationVector[] getKbsPinSanitationTestVectorList() throws IOException {
try (InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("data/kbs_pin_normalization_vectors.json")) {
PinSanitationVector[] data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), PinSanitationVector[].class);
PinSanitationVector[] data = JsonUtil.fromJson(StreamUtil.readFullyAsString(resourceAsStream), PinSanitationVector[].class);
assertTrue(data.length > 0);

View File

@@ -1,8 +1,8 @@
package org.thoughtcrime.securesms.registration.v2;
import org.junit.Test;
import org.signal.core.util.StreamUtil;
import org.thoughtcrime.securesms.registration.v2.testdata.PinValidityVector;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.signalservice.internal.registrationpin.PinValidityChecker;
import org.whispersystems.signalservice.internal.util.JsonUtil;
@@ -28,7 +28,7 @@ public final class PinValidityChecker_validity_Test {
private static PinValidityVector[] getKbsPinValidityTestVectorList() throws IOException {
try (InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("data/kbs_pin_validity_vectors.json")) {
PinValidityVector[] data = JsonUtil.fromJson(Util.readFullyAsString(resourceAsStream), PinValidityVector[].class);
PinValidityVector[] data = JsonUtil.fromJson(StreamUtil.readFullyAsString(resourceAsStream), PinValidityVector[].class);
assertTrue(data.length > 0);

View File

@@ -1,7 +1,7 @@
package org.thoughtcrime.securesms.storage;
import org.junit.Test;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.storage.StorageSyncHelper.KeyGenerator;
import org.whispersystems.libsignal.util.guava.Optional;

View File

@@ -6,11 +6,11 @@ import com.annimon.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.storage.StorageSyncHelper.KeyDifferenceResult;
import org.thoughtcrime.securesms.storage.StorageSyncHelper.MergeResult;

View File

@@ -1,6 +1,6 @@
package org.thoughtcrime.securesms.testutil;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
public class EmptyLogger extends Log.Logger {
@Override

View File

@@ -3,7 +3,7 @@ package org.thoughtcrime.securesms.testutil;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
import java.util.ArrayList;
import java.util.List;

View File

@@ -3,7 +3,7 @@ package org.thoughtcrime.securesms.testutil;
import com.annimon.stream.Stream;
import com.google.common.collect.Sets;
import org.thoughtcrime.securesms.util.Conversions;
import org.signal.core.util.Conversions;
import org.whispersystems.libsignal.util.ByteUtil;
import java.nio.ByteBuffer;

View File

@@ -1,12 +1,11 @@
package org.thoughtcrime.securesms.util;
import org.junit.Test;
import org.signal.zkgroup.util.UUIDUtil;
import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.UUID;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
public class BucketingUtilTest {

View File

@@ -2,33 +2,18 @@ package org.thoughtcrime.securesms.util;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.util.Pair;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.thoughtcrime.securesms.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, application = Application.class)

View File

@@ -6,7 +6,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.testutil.EmptyLogger;
import java.util.Arrays;
@@ -14,7 +14,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class PopulationFeatureFlagsTest_determineCountEnabled {

View File

@@ -4,7 +4,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.thoughtcrime.securesms.logging.Log;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.testutil.EmptyLogger;
import java.util.Arrays;

View File

@@ -3,7 +3,6 @@ package org.thoughtcrime.securesms.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
import java.util.Arrays;
import java.util.Collection;

View File

@@ -7,7 +7,8 @@ import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, application = Application.class)