• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.url;
6 
7 import org.robolectric.annotation.Implementation;
8 import org.robolectric.annotation.Implements;
9 
10 import org.chromium.url.GURL.Natives;
11 
12 /**
13  * Shadow of {@link GURL}. Lets Robolectric tests use {@code GURL} without the native libraries
14  * loaded.
15  *
16  * <p>This shadow can create only GURLs listed in {@link JUnitTestGURLs}.
17  */
18 @Implements(GURL.class)
19 public class ShadowGURL {
20     /**
21      * The {@link GURL.Natives} implementation used by a shadowed {@link GURL}.
22      */
23     private static class NativesImpl implements GURL.Natives {
24         @Override
init(String url, GURL target)25         public void init(String url, GURL target) {
26             target.initForTesting(JUnitTestGURLs.getGURL(url));
27         }
28 
29         @Override
getOrigin(String spec, boolean isValid, long nativeParsed, GURL target)30         public void getOrigin(String spec, boolean isValid, long nativeParsed, GURL target) {
31             throw new UnsupportedOperationException(
32                     "ShadowGURL.NativesImpl#getOrigin is not implemented");
33         }
34 
35         @Override
domainIs(String spec, boolean isValid, long nativeParsed, String domain)36         public boolean domainIs(String spec, boolean isValid, long nativeParsed, String domain) {
37             throw new UnsupportedOperationException(
38                     "ShadowGURL.NativesImpl#domainIs is not implemented");
39         }
40 
41         @Override
createNative(String spec, boolean isValid, long nativeParsed)42         public long createNative(String spec, boolean isValid, long nativeParsed) {
43             throw new UnsupportedOperationException(
44                     "ShadowGURL.NativesImpl#createNative is not implemented");
45         }
46     }
47     private static final NativesImpl sNativesInstance = new NativesImpl();
48 
49     /**
50      * We could instead shadow {@code GURLJni#get}, but that would require tests using this to load
51      * both shadows.
52      */
53     @Implementation
getNatives()54     protected static Natives getNatives() {
55         return sNativesInstance;
56     }
57 
58     @Implementation
ensureNativeInitializedForGURL()59     protected static void ensureNativeInitializedForGURL() {
60         // Skip native initialization.
61     }
62 }
63