InstantRunVerifier { @VisibleForTesting @NonNull static <T> Diff diffList( @Nullable List<T> one, @Nullable List<T> two, @NonNull Comparator<T> comparator) { if (one == null && two == null) { return Diff.NONE; } if (one == null) { return Diff.ADDITION; } if (two == null) { return Diff.REMOVAL; } List<T> copyOfOne = new ArrayList<T>(one); List<T> copyOfTwo = new ArrayList<T>(two); for (T elementOfTwo : two) { T commonElement = getElementOf(copyOfOne, elementOfTwo, comparator); if (commonElement != null) { copyOfOne.remove(commonElement); } } for (T elementOfOne : one) { T commonElement = getElementOf(copyOfTwo, elementOfOne, comparator); if (commonElement != null) { copyOfTwo.remove(commonElement); } } if ((!copyOfOne.isEmpty()) && (copyOfOne.size() == copyOfTwo.size())) { return Diff.CHANGE; } if (!copyOfOne.isEmpty()) { return Diff.REMOVAL; } return copyOfTwo.isEmpty() ? Diff.NONE : Diff.ADDITION; } private  InstantRunVerifier(); static InstantRunVerifierStatus run(File original, File updated); @SuppressWarnings("unchecked") @NonNull static InstantRunVerifierStatus run(ClassBytesProvider original, ClassBytesProvider updated); @Nullable static T getElementOf(List<T> list, T element, Comparator<T> comparator);  }
@Test public void testDiffListOnAnnotationNodes() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); original.values = Lists.newArrayList("modules", Type.getObjectType("test/DaggerModule")); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); updated.values = Lists.newArrayList("modules", Type.getObjectType("test/DaggerModule")); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesEntriesDataSwap() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); original.visit("entry1", "data1"); original.visit("entry2", "data2"); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); updated.visit("entry1", "data2"); updated.visit("entry2", "data1"); assertEquals( InstantRunVerifier.Diff.CHANGE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesDiffEntriesOrder() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); original.visit("entry1", "data1"); original.visit("entry2", "data2"); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); updated.visit("entry2", "data2"); updated.visit("entry1", "data1"); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesWithEnumEntryValue() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); original.visitEnum("entry", "Test", "LMyEnum"); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); updated.visitEnum("entry", "Test", "LMyEnum"); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesWithAnnotationNodeValue() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); AnnotationVisitor newAnnotation = original.visitAnnotation("InnerAnnotation", "LTest;"); newAnnotation.visit("innerEntry", "innerValue"); newAnnotation.visitEnd(); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); newAnnotation = updated.visitAnnotation("InnerAnnotation", "LTest;"); newAnnotation.visit("innerEntry", "innerValue"); newAnnotation.visitEnd(); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesWithArrayValues() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); original.visit("byteArray", new byte[] {1, 2, 3}); original.visit("booleanArray", new boolean[] {false, true, false}); original.visit("shortArray", new short[] {1, 2, 3}); original.visit("charArray", new char[] {'a', 'b', 'c'}); original.visit("intArray", new int[] {1, 2, 3}); original.visit("longArray", new long[] {1, 2, 3}); original.visit("floatArray", new float[] {1, 2, 3}); original.visit("doubleArray", new double[] {1, 2, 3}); original.visit("stringArray", new String[] {"1", "2", "3"}); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); updated.visit("byteArray", new byte[] {1, 2, 3}); updated.visit("booleanArray", new boolean[] {false, true, false}); updated.visit("shortArray", new short[] {1, 2, 3}); updated.visit("charArray", new char[] {'a', 'b', 'c'}); updated.visit("intArray", new int[] {1, 2, 3}); updated.visit("longArray", new long[] {1, 2, 3}); updated.visit("floatArray", new float[] {1, 2, 3}); updated.visit("doubleArray", new double[] {1, 2, 3}); updated.visit("stringArray", new String[] {"1", "2", "3"}); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesWithAnnotationArrayValue() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); AnnotationVisitor arrayVisitor = original.visitArray("test"); AnnotationVisitor newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;"); newAnnotation.visit("innerEntry", "innerValue"); newAnnotation.visitEnd(); newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;"); newAnnotation.visit("innerEntry", "innerValue"); newAnnotation.visitEnd(); arrayVisitor.visitEnd(); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); arrayVisitor = updated.visitArray("test"); newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;"); newAnnotation.visit("innerEntry", "innerValue"); newAnnotation.visitEnd(); newAnnotation = arrayVisitor.visitAnnotation("InnerAnnotation", "LTest;"); newAnnotation.visit("innerEntry", "innerValue"); newAnnotation.visitEnd(); arrayVisitor.visitEnd(); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }
@Test public void testDiffListOnAnnotationNodesWithIntArrayValue() throws Exception { AnnotationNode original = new AnnotationNode("Ltest/SomeAnnotation;"); AnnotationVisitor test = original.visitArray("test"); test.visit("stub", 1); test.visit("stub", 2); test.visit("stub", 3); test.visitEnd(); AnnotationNode updated = new AnnotationNode("Ltest/SomeAnnotation;"); test = updated.visitArray("test"); test.visit("stub", 1); test.visit("stub", 2); test.visit("stub", 3); test.visitEnd(); assertEquals( InstantRunVerifier.Diff.NONE, InstantRunVerifier.diffList( Lists.newArrayList(original), Lists.newArrayList(updated), InstantRunVerifier.ANNOTATION_COMPARATOR)); }