• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.resources.registerresourcepaths2;
18 
19 import static org.junit.Assume.assumeTrue;
20 
21 import android.content.Context;
22 import android.os.Handler;
23 import android.os.HandlerThread;
24 import android.util.Log;
25 import android.webkit.WebSettings;
26 
27 import androidx.test.InstrumentationRegistry;
28 import androidx.test.annotation.UiThreadTest;
29 
30 import com.android.compatibility.common.util.NullWebViewUtils;
31 
32 import org.junit.After;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 
37 public class RegisterResourcePathsTest {
38     private HandlerThread mBackgroundThread;
39     private Handler mBackgroundHandler;
40     private static final String TAG = "RegisterSharedLibTest";
41 
42     @Before
setUp()43     public void setUp() {
44         startBackgroundThread();
45     }
46 
47     @After
tearDown()48     public void tearDown() {
49         stopBackgroundThread();
50     }
51 
52     @Test
53     @UiThreadTest
testWebViewInitializeOnBackGroundThread()54     public void testWebViewInitializeOnBackGroundThread() {
55         if (!NullWebViewUtils.isWebViewAvailable()) {
56             assumeTrue("WebView is not available on the device.", false);
57         }
58 
59         final Context context = InstrumentationRegistry.getContext();
60 
61         mBackgroundHandler.post(new Runnable() {
62             @Override
63             public void run() {
64                 try {
65                     // This would trigger WebView init behind scene and post tasks on main thread.
66                     // It could help to verify that the resources are accessible from the main
67                     // thread if it returns without process crashing.
68                     final String userAgent = WebSettings.getDefaultUserAgent(context);
69                     Assert.assertNotEquals("", userAgent);
70                 } catch (Exception e) {
71                     Assert.fail("Exception occurred during WebView initialization: "
72                             + e.getMessage());
73                 }
74             }
75         });
76     }
77 
startBackgroundThread()78     private void startBackgroundThread() {
79         mBackgroundThread = new HandlerThread("WebViewBackground");
80         mBackgroundThread.start();
81         mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
82     }
83 
stopBackgroundThread()84     private void stopBackgroundThread() {
85         mBackgroundThread.quitSafely();
86         try {
87             mBackgroundThread.join();
88             mBackgroundThread = null;
89             mBackgroundHandler = null;
90         } catch (InterruptedException e) {
91             Log.e(TAG, "Interrupted exception thrown while stopping WebViewBackground thread.");
92         }
93     }
94 }
95