OkHttpProtocolNegotiator { @VisibleForTesting static OkHttpProtocolNegotiator createNegotiator(ClassLoader loader) { boolean android = true; try { loader.loadClass("com.android.org.conscrypt.OpenSSLSocketImpl"); } catch (ClassNotFoundException ignored) { try { loader.loadClass("org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl"); } catch (ClassNotFoundException ignored2) { android = false; } } return android ? new AndroidNegotiator(DEFAULT_PLATFORM, AndroidNegotiator.DEFAULT_TLS_EXTENSION_TYPE) : new OkHttpProtocolNegotiator(DEFAULT_PLATFORM); } @VisibleForTesting  OkHttpProtocolNegotiator(Platform platform); static OkHttpProtocolNegotiator get(); String negotiate(
      SSLSocket sslSocket, String hostname, @Nullable List<Protocol> protocols); String getSelectedProtocol(SSLSocket socket);  }
@Test public void createNegotiator_isAndroid() { ClassLoader cl = new ClassLoader(this.getClass().getClassLoader()) { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { if ("com.android.org.conscrypt.OpenSSLSocketImpl".equals(name)) { return null; } return super.findClass(name); } }; OkHttpProtocolNegotiator negotiator = OkHttpProtocolNegotiator.createNegotiator(cl); assertEquals(AndroidNegotiator.class, negotiator.getClass()); }
@Test public void createNegotiator_isAndroidLegacy() { ClassLoader cl = new ClassLoader(this.getClass().getClassLoader()) { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { if ("org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl".equals(name)) { return null; } return super.findClass(name); } }; OkHttpProtocolNegotiator negotiator = OkHttpProtocolNegotiator.createNegotiator(cl); assertEquals(AndroidNegotiator.class, negotiator.getClass()); }
@Test public void createNegotiator_notAndroid() { ClassLoader cl = new ClassLoader(this.getClass().getClassLoader()) { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { if ("com.android.org.conscrypt.OpenSSLSocketImpl".equals(name)) { throw new ClassNotFoundException(); } return super.findClass(name); } }; OkHttpProtocolNegotiator negotiator = OkHttpProtocolNegotiator.createNegotiator(cl); assertEquals(OkHttpProtocolNegotiator.class, negotiator.getClass()); }