Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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.webkit.cts;
     18 
     19 
     20 import android.content.Context;
     21 import android.test.ActivityInstrumentationTestCase2;
     22 import android.webkit.CookieManager;
     23 import android.webkit.WebView;
     24 
     25 import com.android.compatibility.common.util.NullWebViewUtils;
     26 
     27 public class WebViewDataDirTest extends ActivityInstrumentationTestCase2<WebViewCtsActivity> {
     28 
     29     private static final long REMOTE_TIMEOUT_MS = 5000;
     30     private static final String ALTERNATE_DIR_NAME = "test";
     31     private static final String COOKIE_URL = "https://www.example.com/";
     32     private static final String COOKIE_VALUE = "foo=main";
     33     private static final String SET_COOKIE_PARAMS = "; Max-Age=86400";
     34 
     35     public WebViewDataDirTest() throws Exception {
     36         super("android.webkit.cts", WebViewCtsActivity.class);
     37     }
     38 
     39     static class TestDisableThenUseImpl extends TestProcessClient.TestRunnable {
     40         @Override
     41         public void run(Context ctx) {
     42             WebView.disableWebView();
     43             try {
     44                 new WebView(ctx);
     45                 fail("didn't throw IllegalStateException");
     46             } catch (IllegalStateException e) {}
     47         }
     48     }
     49 
     50     public void testDisableThenUse() throws Throwable {
     51         if (!NullWebViewUtils.isWebViewAvailable()) {
     52             return;
     53         }
     54 
     55         try (TestProcessClient process = TestProcessClient.createProcessA(getActivity())) {
     56             process.run(TestDisableThenUseImpl.class, REMOTE_TIMEOUT_MS);
     57         }
     58     }
     59 
     60     public void testUseThenDisable() throws Throwable {
     61         if (!NullWebViewUtils.isWebViewAvailable()) {
     62             return;
     63         }
     64 
     65         assertNotNull(getActivity().getWebView());
     66         try {
     67             WebView.disableWebView();
     68             fail("didn't throw IllegalStateException");
     69         } catch (IllegalStateException e) {}
     70     }
     71 
     72     public void testUseThenChangeDir() throws Throwable {
     73         if (!NullWebViewUtils.isWebViewAvailable()) {
     74             return;
     75         }
     76 
     77         assertNotNull(getActivity().getWebView());
     78         try {
     79             WebView.setDataDirectorySuffix(ALTERNATE_DIR_NAME);
     80             fail("didn't throw IllegalStateException");
     81         } catch (IllegalStateException e) {}
     82     }
     83 
     84     static class TestInvalidDirImpl extends TestProcessClient.TestRunnable {
     85         @Override
     86         public void run(Context ctx) {
     87             try {
     88                 WebView.setDataDirectorySuffix("no/path/separators");
     89                 fail("didn't throw IllegalArgumentException");
     90             } catch (IllegalArgumentException e) {}
     91         }
     92     }
     93 
     94     public void testInvalidDir() throws Throwable {
     95         if (!NullWebViewUtils.isWebViewAvailable()) {
     96             return;
     97         }
     98 
     99         try (TestProcessClient process = TestProcessClient.createProcessA(getActivity())) {
    100             process.run(TestInvalidDirImpl.class, REMOTE_TIMEOUT_MS);
    101         }
    102     }
    103 
    104     static class TestDefaultDirDisallowed extends TestProcessClient.TestRunnable {
    105         @Override
    106         public void run(Context ctx) {
    107             try {
    108                 new WebView(ctx);
    109                 fail("didn't throw RuntimeException");
    110             } catch (RuntimeException e) {}
    111         }
    112     }
    113 
    114     public void testSameDirTwoProcesses() throws Throwable {
    115         if (!NullWebViewUtils.isWebViewAvailable()) {
    116             return;
    117         }
    118 
    119         assertNotNull(getActivity().getWebView());
    120 
    121         try (TestProcessClient processA = TestProcessClient.createProcessA(getActivity())) {
    122             processA.run(TestDefaultDirDisallowed.class, REMOTE_TIMEOUT_MS);
    123         }
    124     }
    125 
    126     static class TestCookieInAlternateDir extends TestProcessClient.TestRunnable {
    127         @Override
    128         public void run(Context ctx) {
    129             WebView.setDataDirectorySuffix(ALTERNATE_DIR_NAME);
    130             CookieManager cm = CookieManager.getInstance();
    131             String cookie = cm.getCookie(COOKIE_URL);
    132             assertNull("cookie leaked to alternate cookie jar", cookie);
    133         }
    134     }
    135 
    136     public void testCookieJarsSeparate() throws Throwable {
    137         if (!NullWebViewUtils.isWebViewAvailable()) {
    138             return;
    139         }
    140 
    141         CookieManager cm = CookieManager.getInstance();
    142         cm.setCookie(COOKIE_URL, COOKIE_VALUE + SET_COOKIE_PARAMS);
    143         cm.flush();
    144         String cookie = cm.getCookie(COOKIE_URL);
    145         assertEquals("wrong cookie in default cookie jar", COOKIE_VALUE, cookie);
    146 
    147         try (TestProcessClient processA = TestProcessClient.createProcessA(getActivity())) {
    148             processA.run(TestCookieInAlternateDir.class, REMOTE_TIMEOUT_MS);
    149         }
    150     }
    151 }
    152