1 // Copyright 2019 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.base.library_loader; 6 7 import androidx.test.filters.SmallTest; 8 9 import org.jni_zero.JNINamespace; 10 import org.jni_zero.NativeLibraryLoadedStatus; 11 import org.jni_zero.NativeMethods; 12 import org.junit.After; 13 import org.junit.Assert; 14 import org.junit.Before; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 18 import org.chromium.base.test.BaseJUnit4ClassRunner; 19 import org.chromium.base.test.util.Batch; 20 import org.chromium.base.test.util.CallbackHelper; 21 import org.chromium.build.BuildConfig; 22 23 /** Tests for early JNI initialization. */ 24 @RunWith(BaseJUnit4ClassRunner.class) 25 @JNINamespace("base") 26 @Batch(Batch.UNIT_TESTS) 27 public class EarlyNativeTest { 28 private boolean mWasInitialized; 29 private CallbackHelper mLoadStarted; 30 private CallbackHelper mEnsureInitializedFinished; 31 32 @Before setUp()33 public void setUp() { 34 mWasInitialized = LibraryLoader.getInstance().isInitialized(); 35 LibraryLoader.getInstance().resetForTesting(); 36 mLoadStarted = new CallbackHelper(); 37 mEnsureInitializedFinished = new CallbackHelper(); 38 } 39 40 @After tearDown()41 public void tearDown() { 42 // Restore the simulated library state (due to the resetForTesting() call). 43 if (mWasInitialized) { 44 LibraryLoader.getInstance().ensureInitialized(); 45 } 46 } 47 48 @NativeMethods 49 interface Natives { isCommandLineInitialized()50 boolean isCommandLineInitialized(); 51 isProcessNameEmpty()52 boolean isProcessNameEmpty(); 53 } 54 55 @Test 56 @SmallTest testEnsureInitialized()57 public void testEnsureInitialized() { 58 // Make sure the Native library isn't considered ready for general use. 59 Assert.assertFalse(LibraryLoader.getInstance().isInitialized()); 60 61 LibraryLoader.getInstance().ensureInitialized(); 62 Assert.assertTrue(LibraryLoader.getInstance().isInitialized()); 63 64 // Test resetForTesting(). 65 LibraryLoader.getInstance().resetForTesting(); 66 Assert.assertFalse(LibraryLoader.getInstance().isInitialized()); 67 LibraryLoader.getInstance().ensureInitialized(); 68 Assert.assertTrue(LibraryLoader.getInstance().isInitialized()); 69 } 70 71 @Test 72 @SmallTest testNativeMethodsReadyAfterLibraryInitialized()73 public void testNativeMethodsReadyAfterLibraryInitialized() { 74 // Test is a no-op if DCHECK isn't on. 75 if (!BuildConfig.ENABLE_ASSERTS) return; 76 77 Assert.assertFalse( 78 NativeLibraryLoadedStatus.getProviderForTesting().areNativeMethodsReady()); 79 80 LibraryLoader.getInstance().ensureInitialized(); 81 Assert.assertTrue( 82 NativeLibraryLoadedStatus.getProviderForTesting().areNativeMethodsReady()); 83 } 84 85 @Test 86 @SmallTest testNativeMethodsNotReadyThrows()87 public void testNativeMethodsNotReadyThrows() { 88 // Test is a no-op if dcheck isn't on. 89 if (!BuildConfig.ENABLE_ASSERTS) return; 90 91 try { 92 EarlyNativeTestJni.get().isCommandLineInitialized(); 93 Assert.fail("Using JNI before the library is loaded should throw an exception."); 94 } catch (NativeLibraryLoadedStatus.NativeNotLoadedException e) { 95 } 96 } 97 } 98