From 8a6c0d006ec402f269ac6243e46a8c9827e2ef64 Mon Sep 17 00:00:00 2001 From: anthonyflynn Date: Sat, 3 Feb 2018 19:09:46 +0000 Subject: [PATCH 1/3] Adding PackageManager unit tests for getPackages method. --- .../jadb/test/unit/PackageManagerTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/se/vidstige/jadb/test/unit/PackageManagerTest.java diff --git a/test/se/vidstige/jadb/test/unit/PackageManagerTest.java b/test/se/vidstige/jadb/test/unit/PackageManagerTest.java new file mode 100644 index 0000000..4f72f47 --- /dev/null +++ b/test/se/vidstige/jadb/test/unit/PackageManagerTest.java @@ -0,0 +1,94 @@ +package se.vidstige.jadb.test.unit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import se.vidstige.jadb.JadbConnection; +import se.vidstige.jadb.JadbDevice; +import se.vidstige.jadb.managers.Package; +import se.vidstige.jadb.managers.PackageManager; +import se.vidstige.jadb.test.fakes.FakeAdbServer; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class PackageManagerTest { + private final String DEVICE_SERIAL = "serial-123"; + + private FakeAdbServer server; + private JadbConnection connection; + private JadbDevice device; + + @Before + public void setUp() throws Exception { + server = new FakeAdbServer(15037); + server.start(); + server.add(DEVICE_SERIAL); + connection = new JadbConnection("localhost", 15037); + + device = connection.getDevices().get(0); + } + + @After + public void tearDown() throws Exception { + server.stop(); + server.verifyExpectations(); + } + + @Test + public void testGetPackagesWithSeveralPackages() throws Exception { + //Arrange + List expected = new ArrayList<>(); + expected.add(new Package("/system/priv-app/Contacts.apk-com.android.contacts")); + expected.add(new Package("/system/priv-app/Teleservice.apk-com.android.phone")); + + String response = "package:/system/priv-app/Contacts.apk-com.android.contacts\n" + + "package:/system/priv-app/Teleservice.apk-com.android.phone"; + + server.expectShell(DEVICE_SERIAL, "pm list packages").returns(response); + + //Act + List actual = new PackageManager(device).getPackages(); + + //Assert + assertEquals(expected, actual); + } + + @Test + public void testGetPackagesMalformedIgnoredString() throws Exception { + //Arrange + List expected = new ArrayList<>(); + expected.add(new Package("/system/priv-app/Contacts.apk-com.android.contacts")); + expected.add(new Package("/system/priv-app/Teleservice.apk-com.android.phone")); + + String response = "package:/system/priv-app/Contacts.apk-com.android.contacts\n" + + "[malformed_line]\n" + + "package:/system/priv-app/Teleservice.apk-com.android.phone"; + + server.expectShell(DEVICE_SERIAL, "pm list packages").returns(response); + + //Act + List actual = new PackageManager(device).getPackages(); + + //Assert + assertEquals(expected, actual); + } + + @Test + public void testGetPackagesWithNoPackages() throws Exception { + //Arrange + List expected = new ArrayList<>(); + String response = ""; + + server.expectShell(DEVICE_SERIAL, "pm list packages").returns(response); + + //Act + List actual = new PackageManager(device).getPackages(); + + //Assert + assertEquals(expected, actual); + } + +} \ No newline at end of file From 1834bc4812fc4a8ed7383b379d00b28f1d7e4c03 Mon Sep 17 00:00:00 2001 From: anthonyflynn Date: Sat, 3 Feb 2018 19:11:42 +0000 Subject: [PATCH 2/3] Comparing attribute equality for equals method (reflecting RemoveFile class). --- src/se/vidstige/jadb/managers/Package.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/se/vidstige/jadb/managers/Package.java b/src/se/vidstige/jadb/managers/Package.java index bcfb751..c4194c1 100644 --- a/src/se/vidstige/jadb/managers/Package.java +++ b/src/se/vidstige/jadb/managers/Package.java @@ -14,7 +14,16 @@ public class Package { public String toString() { return name; } @Override - public boolean equals(Object o) { return name.equals(o); } + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Package that = (Package) o; + + if (!name.equals(that.name)) return false; + + return true; + } @Override public int hashCode() { return name.hashCode(); } From 23bfd4bf7dca570caa7283a9fd3bda0f0e801a88 Mon Sep 17 00:00:00 2001 From: anthonyflynn Date: Sun, 4 Feb 2018 19:55:02 +0000 Subject: [PATCH 3/3] Simplifying equals method and changing class check to use instanceof. --- src/se/vidstige/jadb/managers/Package.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/se/vidstige/jadb/managers/Package.java b/src/se/vidstige/jadb/managers/Package.java index c4194c1..93b8d78 100644 --- a/src/se/vidstige/jadb/managers/Package.java +++ b/src/se/vidstige/jadb/managers/Package.java @@ -16,14 +16,10 @@ public class Package { @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - + if (!(o instanceof Package)) return false; Package that = (Package) o; - - if (!name.equals(that.name)) return false; - - return true; - } + return name.equals(that.name); + } @Override public int hashCode() { return name.hashCode(); }