• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 static org.mockito.ArgumentMatchers.any;
8 import static org.mockito.Mockito.doThrow;
9 
10 import androidx.test.filters.SmallTest;
11 
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18 
19 import org.chromium.base.Log;
20 import org.chromium.base.test.BaseJUnit4ClassRunner;
21 import org.chromium.base.test.util.Batch;
22 
23 import java.util.Map;
24 
25 /**
26  * Tests for JUnitTestGURLs.
27  */
28 @RunWith(BaseJUnit4ClassRunner.class)
29 @Batch(Batch.UNIT_TESTS)
30 public class JUnitTestGURLsTest {
31     private static final String TAG = "JUnitTestGURLs";
32 
33     @Mock
34     GURL.Natives mGURLMocks;
35 
36     @Before
setUp()37     public void setUp() {
38         MockitoAnnotations.initMocks(this);
39     }
40 
getErrorForGURL(GURL gurl)41     private RuntimeException getErrorForGURL(GURL gurl) {
42         String serialized = gurl.serialize();
43         Assert.assertEquals(-1, serialized.indexOf(","));
44         serialized = serialized.replace(GURL.SERIALIZER_DELIMITER, ',');
45 
46         return new RuntimeException("Please update the serialization in JUnitTestGURLs.java for "
47                 + gurl.getPossiblyInvalidSpec() + " to: '" + serialized + "'");
48     }
49 
50     @SmallTest
51     @Test
testGURLEquivalence()52     public void testGURLEquivalence() throws Throwable {
53         doThrow(new RuntimeException("Deserialization required re-initialization."))
54                 .when(mGURLMocks)
55                 .init(any(), any());
56 
57         Throwable exception = null;
58         for (Map.Entry<String, String> entry : JUnitTestGURLs.sGURLMap.entrySet()) {
59             GURL gurl = new GURL(entry.getKey());
60             try {
61                 GURLJni.TEST_HOOKS.setInstanceForTesting(mGURLMocks);
62                 GURL deserialized = JUnitTestGURLs.getGURL(entry.getKey());
63                 GURLJni.TEST_HOOKS.setInstanceForTesting(null);
64                 GURLJavaTest.deepAssertEquals(deserialized, gurl);
65             } catch (Throwable e) {
66                 GURLJni.TEST_HOOKS.setInstanceForTesting(null);
67                 exception = getErrorForGURL(gurl);
68                 Log.e(TAG, "Error: ", exception);
69             }
70         }
71         if (exception != null) throw exception;
72     }
73 }
74