|
| 1 | +package com.wpe.wpeview; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import androidx.annotation.NonNull; |
| 6 | +import androidx.lifecycle.Lifecycle; |
| 7 | +import androidx.test.core.app.ActivityScenario; |
| 8 | +import androidx.test.ext.junit.rules.ActivityScenarioRule; |
| 9 | +import androidx.test.runner.AndroidJUnit4; |
| 10 | + |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | + |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 19 | + |
| 20 | +@RunWith(AndroidJUnit4.class) |
| 21 | +public class WPEViewDialogOverrideTest { |
| 22 | + private static final String EMPTY_PAGE = "<!doctype html>" |
| 23 | + + "<title>Dialog Test</title><p>Testcase.</p>"; |
| 24 | + |
| 25 | + @Rule |
| 26 | + public ActivityScenarioRule<WPEViewTestActivity> wpeViewActivityTestRule = |
| 27 | + new ActivityScenarioRule<>(WPEViewTestActivity.class); |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testOverrideAlertHandling() throws Throwable { |
| 31 | + final String alertText = "Hello World!"; |
| 32 | + |
| 33 | + final AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 34 | + |
| 35 | + TestWPEViewClient wpeViewClient = new TestWPEViewClient(); |
| 36 | + ActivityScenario<WPEViewTestActivity> scenario = wpeViewActivityTestRule.getScenario(); |
| 37 | + scenario |
| 38 | + .onActivity(activity -> { |
| 39 | + activity.getWPEView().setWPEChromeClient(new WPEChromeClient() { |
| 40 | + @Override |
| 41 | + public boolean onJsAlert(@NonNull WPEView view, @NonNull String url, @NonNull String message, |
| 42 | + @NonNull WPEJsResult result) { |
| 43 | + callbackCalled.set(true); |
| 44 | + result.confirm(); |
| 45 | + return true; |
| 46 | + } |
| 47 | + }); |
| 48 | + }) |
| 49 | + .moveToState(Lifecycle.State.RESUMED); |
| 50 | + |
| 51 | + WPEViewActivityScenarioHelper.loadHtmlSync(scenario, wpeViewClient, EMPTY_PAGE); |
| 52 | + WPEViewActivityScenarioHelper.evaluateJavaScriptAndWaitForResult(scenario, wpeViewClient, |
| 53 | + "alert('" + alertText + "')"); |
| 54 | + |
| 55 | + Assert.assertTrue(callbackCalled.get()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testOverrideConfirmHandlingConfirmed() throws Throwable { |
| 60 | + final String confirmText = "Would you like to confirm?"; |
| 61 | + |
| 62 | + final AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 63 | + |
| 64 | + TestWPEViewClient wpeViewClient = new TestWPEViewClient(); |
| 65 | + ActivityScenario<WPEViewTestActivity> scenario = wpeViewActivityTestRule.getScenario(); |
| 66 | + scenario |
| 67 | + .onActivity(activity -> { |
| 68 | + activity.getWPEView().setWPEChromeClient(new WPEChromeClient() { |
| 69 | + @Override |
| 70 | + public boolean onJsConfirm(@NonNull WPEView view, @NonNull String url, @NonNull String message, |
| 71 | + @NonNull WPEJsResult result) { |
| 72 | + callbackCalled.set(true); |
| 73 | + result.confirm(); |
| 74 | + return true; |
| 75 | + } |
| 76 | + }); |
| 77 | + activity.getWPEView().loadHtml(EMPTY_PAGE, null); |
| 78 | + }) |
| 79 | + .moveToState(Lifecycle.State.RESUMED); |
| 80 | + |
| 81 | + WPEViewActivityScenarioHelper.loadHtmlSync(scenario, wpeViewClient, EMPTY_PAGE); |
| 82 | + String result = WPEViewActivityScenarioHelper.evaluateJavaScriptAndWaitForResult( |
| 83 | + scenario, wpeViewClient, "confirm('" + confirmText + "')"); |
| 84 | + |
| 85 | + Assert.assertTrue(callbackCalled.get()); |
| 86 | + Assert.assertEquals("true", result); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testOverrideConfirmHandlingCancelled() throws Throwable { |
| 91 | + final String confirmText = "Would you like to confirm?"; |
| 92 | + |
| 93 | + final AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 94 | + |
| 95 | + TestWPEViewClient wpeViewClient = new TestWPEViewClient(); |
| 96 | + ActivityScenario<WPEViewTestActivity> scenario = wpeViewActivityTestRule.getScenario(); |
| 97 | + scenario |
| 98 | + .onActivity(activity -> { |
| 99 | + activity.getWPEView().setWPEChromeClient(new WPEChromeClient() { |
| 100 | + @Override |
| 101 | + public boolean onJsConfirm(@NonNull WPEView view, @NonNull String url, @NonNull String message, |
| 102 | + @NonNull WPEJsResult result) { |
| 103 | + callbackCalled.set(true); |
| 104 | + result.cancel(); |
| 105 | + return true; |
| 106 | + } |
| 107 | + }); |
| 108 | + activity.getWPEView().loadHtml(EMPTY_PAGE, null); |
| 109 | + }) |
| 110 | + .moveToState(Lifecycle.State.RESUMED); |
| 111 | + |
| 112 | + WPEViewActivityScenarioHelper.loadHtmlSync(scenario, wpeViewClient, EMPTY_PAGE); |
| 113 | + String result = WPEViewActivityScenarioHelper.evaluateJavaScriptAndWaitForResult( |
| 114 | + scenario, wpeViewClient, "confirm('" + confirmText + "')"); |
| 115 | + |
| 116 | + Assert.assertTrue(callbackCalled.get()); |
| 117 | + Assert.assertEquals("false", result); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testOverridePromptHandling() throws Throwable { |
| 122 | + final String promptText = "How is the weather today?"; |
| 123 | + final String promptDefault = "It's sunny and warm today."; |
| 124 | + final String promptResult = "It’s so hot."; |
| 125 | + |
| 126 | + final AtomicBoolean callbackCalled = new AtomicBoolean(false); |
| 127 | + |
| 128 | + TestWPEViewClient wpeViewClient = new TestWPEViewClient(); |
| 129 | + ActivityScenario<WPEViewTestActivity> scenario = wpeViewActivityTestRule.getScenario(); |
| 130 | + scenario |
| 131 | + .onActivity(activity -> { |
| 132 | + activity.getWPEView().setWPEChromeClient(new WPEChromeClient() { |
| 133 | + @Override |
| 134 | + public boolean onJsPrompt(@NonNull WPEView view, @NonNull String url, @NonNull String message, |
| 135 | + @NonNull String defaultValue, @NonNull WPEJsPromptResult result) { |
| 136 | + Assert.assertEquals(promptText, message); |
| 137 | + Assert.assertEquals(promptDefault, defaultValue); |
| 138 | + |
| 139 | + result.confirm(promptResult); |
| 140 | + callbackCalled.set(true); |
| 141 | + return true; |
| 142 | + } |
| 143 | + }); |
| 144 | + }) |
| 145 | + .moveToState(Lifecycle.State.RESUMED); |
| 146 | + WPEViewActivityScenarioHelper.loadHtmlSync(scenario, wpeViewClient, EMPTY_PAGE); |
| 147 | + String result = WPEViewActivityScenarioHelper.evaluateJavaScriptAndWaitForResult( |
| 148 | + scenario, wpeViewClient, "prompt(\"" + promptText + "\", \"" + promptDefault + "\")"); |
| 149 | + Assert.assertTrue(callbackCalled.get()); |
| 150 | + Assert.assertEquals("\"" + promptResult + "\"", result); |
| 151 | + } |
| 152 | +} |
0 commit comments