Test different behavior in Set#of

Bug: b/243679691
Change-Id: I7d67026c2c912a22f56ce5604022eeb6e5307595
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
index 9a4e70c..095f1d9 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/BackportedMethodRewriter.java
@@ -265,6 +265,11 @@
           || appView
               .options()
               .machineDesugaredLibrarySpecification
+              .getEmulatedInterfaces()
+              .containsKey(type)
+          || appView
+              .options()
+              .machineDesugaredLibrarySpecification
               .getMaintainType()
               .contains(type);
     }
diff --git a/src/test/examplesJava9/collectionof/CollectionOfMain.java b/src/test/examplesJava9/collectionof/CollectionOfMain.java
new file mode 100644
index 0000000..fc8384e
--- /dev/null
+++ b/src/test/examplesJava9/collectionof/CollectionOfMain.java
@@ -0,0 +1,24 @@
+// Copyright (c) 2022, 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 collectionof;
+
+import java.util.List;
+import java.util.Set;
+
+public class CollectionOfMain {
+
+  public static void main(String[] args) {
+    try {
+      System.out.println(Set.of("one").contains(null));
+    } catch (NullPointerException npe) {
+      System.out.println("npe");
+    }
+    try {
+      System.out.println(List.of("one").contains(null));
+    } catch (NullPointerException npe) {
+      System.out.println("npe");
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/CollectionOfTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/CollectionOfTest.java
new file mode 100644
index 0000000..c9f3f99
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/CollectionOfTest.java
@@ -0,0 +1,92 @@
+// Copyright (c) 2022, 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 com.android.tools.r8.desugar.desugaredlibrary;
+
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.D8_L8DEBUG;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.DEFAULT_SPECIFICATIONS;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK8;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.getJdk8Jdk11;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import org.junit.Assume;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class CollectionOfTest extends DesugaredLibraryTestBase {
+
+  private final TestParameters parameters;
+  private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+  private final CompilationSpecification compilationSpecification;
+
+  private static final Path INPUT_JAR =
+      Paths.get(ToolHelper.EXAMPLES_JAVA9_BUILD_DIR + "collectionof.jar");
+  private static final String EXPECTED_OUTPUT_BACKPORT = StringUtils.lines("false", "false");
+  private static final String EXPECTED_OUTPUT_CORRECT = StringUtils.lines("npe", "npe");
+  private static final String MAIN_CLASS = "collectionof.CollectionOfMain";
+
+  @Parameters(name = "{0}, spec: {1}, {2}")
+  public static List<Object[]> data() {
+    return buildParameters(
+        getTestParameters().withDexRuntimes().withAllApiLevels().build(),
+        getJdk8Jdk11(),
+        DEFAULT_SPECIFICATIONS);
+  }
+
+  public CollectionOfTest(
+      TestParameters parameters,
+      LibraryDesugaringSpecification libraryDesugaringSpecification,
+      CompilationSpecification compilationSpecification) {
+    this.parameters = parameters;
+    this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+    this.compilationSpecification = compilationSpecification;
+  }
+
+  private String getExpectedOutput(boolean desugaredLib) {
+    if (parameters.getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.R)) {
+      return EXPECTED_OUTPUT_CORRECT;
+    }
+    if (desugaredLib && libraryDesugaringSpecification != JDK8) {
+      if (parameters.getApiLevel().isLessThan(AndroidApiLevel.N)) {
+        return EXPECTED_OUTPUT_CORRECT;
+      }
+      // TODO(b/243679691): This should also be correct, but is not because we use backports in
+      //  partial desugaring.
+      return EXPECTED_OUTPUT_BACKPORT;
+    }
+    return EXPECTED_OUTPUT_BACKPORT;
+  }
+
+  @Test
+  public void testCollectionOf() throws Throwable {
+    testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+        .addProgramFiles(INPUT_JAR)
+        .addKeepMainRule(MAIN_CLASS)
+        .run(parameters.getRuntime(), MAIN_CLASS)
+        .assertSuccessWithOutput(getExpectedOutput(true));
+  }
+
+  @Test
+  public void testCollectionOfReference() throws Throwable {
+    Assume.assumeTrue(
+        "Run only once",
+        libraryDesugaringSpecification == JDK8 && compilationSpecification == D8_L8DEBUG);
+    testForD8()
+        .addProgramFiles(INPUT_JAR)
+        .setMinApi(parameters.getApiLevel())
+        .run(parameters.getRuntime(), MAIN_CLASS)
+        .assertSuccessWithOutput(getExpectedOutput(false));
+  }
+}