Reproduce Mockito's mock of empty interface after devirtualization.
Bug: 120675359
Change-Id: I77fe93d4af84dbe97870278cf62cbd030d680b7a
diff --git a/src/test/examples/mockito_interface/Implementer.java b/src/test/examples/mockito_interface/Implementer.java
new file mode 100644
index 0000000..40c8fff
--- /dev/null
+++ b/src/test/examples/mockito_interface/Implementer.java
@@ -0,0 +1,21 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package mockito_interface;
+
+import java.util.logging.Logger;
+
+public class Implementer implements Interface {
+ private static final String TAG = Implementer.class.getSimpleName();
+ private Logger logger;
+
+ public Implementer() {
+ this.logger = Logger.getLogger(TAG);
+ }
+
+ @Override
+ public void onEnterForeground() {
+ logger.info("onEnterForeground called");
+ }
+
+}
diff --git a/src/test/examples/mockito_interface/Interface.java b/src/test/examples/mockito_interface/Interface.java
new file mode 100644
index 0000000..235de62
--- /dev/null
+++ b/src/test/examples/mockito_interface/Interface.java
@@ -0,0 +1,8 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package mockito_interface;
+
+public interface Interface {
+ void onEnterForeground();
+}
diff --git a/src/test/examples/mockito_interface/InterfaceTest.java b/src/test/examples/mockito_interface/InterfaceTest.java
new file mode 100644
index 0000000..2502b13
--- /dev/null
+++ b/src/test/examples/mockito_interface/InterfaceTest.java
@@ -0,0 +1,47 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package mockito_interface;
+
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+@RunWith(Parameterized.class)
+public class InterfaceTest {
+ @Mock
+ private Interface fld;
+
+ private InterfaceUser user;
+
+ private boolean flag;
+
+ @Parameterized.Parameters(name = "flag: {0}")
+ public static Boolean[] data() {
+ return new Boolean[] {true, false};
+ }
+
+ public InterfaceTest(boolean flag) {
+ this.flag = flag;
+ }
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ user = new InterfaceUser(fld);
+ }
+
+ @Test
+ public void test() {
+ if (flag) {
+ user.consume();
+ }
+ verify(fld, times(flag ? 1 : 0)).onEnterForeground();
+ }
+}
diff --git a/src/test/examples/mockito_interface/InterfaceUser.java b/src/test/examples/mockito_interface/InterfaceUser.java
new file mode 100644
index 0000000..67729d6
--- /dev/null
+++ b/src/test/examples/mockito_interface/InterfaceUser.java
@@ -0,0 +1,24 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package mockito_interface;
+
+public class InterfaceUser {
+
+ private Interface itf;
+
+ public InterfaceUser(Interface itf) {
+ this.itf = itf;
+ }
+
+ void consume() {
+ itf.onEnterForeground();
+ }
+
+ public static void main() {
+ Implementer impl = new Implementer();
+ InterfaceUser user = new InterfaceUser(impl);
+ user.consume();
+ }
+
+}
diff --git a/src/test/examples/mockito_interface/keep-rules-conditional-on-mock.txt b/src/test/examples/mockito_interface/keep-rules-conditional-on-mock.txt
new file mode 100644
index 0000000..67f437f
--- /dev/null
+++ b/src/test/examples/mockito_interface/keep-rules-conditional-on-mock.txt
@@ -0,0 +1,23 @@
+# Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+-keepattributes *Annotation*
+
+-keep class **.InterfaceUser {
+ public static void main(...);
+}
+
+-keep class org.junit.** { *; }
+-keep class org.mockito.** { *; }
+-keep @**.RunWith class * { *; }
+
+# Mockito generates mocks of interface types at runtime. If interface methods are optimized, i.e.,
+# stripped out, mock-based tests will fail. So, keep all methods of interfaces if they are used as
+# field type and annotated with @Mock.
+-if class * {
+ @org.mockito.Mock * *;
+}
+-keep interface <2> {
+ <methods>;
+}
\ No newline at end of file
diff --git a/src/test/examples/mockito_interface/keep-rules.txt b/src/test/examples/mockito_interface/keep-rules.txt
new file mode 100644
index 0000000..40dd170
--- /dev/null
+++ b/src/test/examples/mockito_interface/keep-rules.txt
@@ -0,0 +1,13 @@
+# Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+-keepattributes *Annotation*
+
+-keep class **.InterfaceUser {
+ public static void main(...);
+}
+
+-keep class org.junit.** { *; }
+-keep class org.mockito.** { *; }
+-keep @**.RunWith class * { *; }
\ No newline at end of file