Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2010 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.cts.util.PollingCheck;
     22 import android.test.ActivityInstrumentationTestCase2;
     23 import android.test.UiThreadTest;
     24 import android.util.Log;
     25 import android.webkit.CookieManager;
     26 import android.webkit.CookieSyncManager;
     27 import android.webkit.WebView;
     28 
     29 import java.util.regex.Matcher;
     30 import java.util.regex.Pattern;
     31 
     32 public class WebViewStartupTest
     33         extends ActivityInstrumentationTestCase2<WebViewStartupStubActivity> {
     34 
     35     private static final int TEST_TIMEOUT = 5000;
     36     private static final String TAG = "WebViewStartupTest";
     37 
     38     private WebViewStartupStubActivity mActivity;
     39 
     40     public WebViewStartupTest() {
     41         super("com.android.cts.stub", WebViewStartupStubActivity.class);
     42     }
     43 
     44     @Override
     45     public void setUp() throws Exception {
     46         mActivity = getActivity();
     47     }
     48 
     49     @UiThreadTest
     50     public void testCookieManagerBlockingUiThread() throws Throwable {
     51         CtsTestServer server = new CtsTestServer(mActivity, false);
     52         final String url = server.getCookieUrl("death.html");
     53 
     54         Thread background = new Thread(new Runnable() {
     55             @Override
     56             public void run() {
     57                 CookieSyncManager csm = CookieSyncManager.createInstance(mActivity);
     58                 CookieManager cookieManager = CookieManager.getInstance();
     59 
     60                 cookieManager.removeAllCookie();
     61                 cookieManager.setAcceptCookie(true);
     62                 cookieManager.setCookie(url, "count=41");
     63                 Log.i(TAG, "done setting cookie before creating webview");
     64             }
     65         });
     66         background.start();
     67         background.join();
     68 
     69         // Now create WebView and test that setting the cookie beforehand really worked.
     70         mActivity.createAndAttachWebView();
     71         WebViewOnUiThread onUiThread = new WebViewOnUiThread(this, mActivity.getWebView());
     72         onUiThread.loadUrlAndWaitForCompletion(url);
     73         assertEquals("1|count=41", onUiThread.getTitle()); // outgoing cookie
     74         CookieManager cookieManager = CookieManager.getInstance();
     75         String cookie = cookieManager.getCookie(url);
     76         assertNotNull(cookie);
     77         final Pattern pat = Pattern.compile("count=(\\d+)");
     78         Matcher m = pat.matcher(cookie);
     79         assertTrue(m.matches());
     80         assertEquals("42", m.group(1)); // value got incremented
     81     }
     82 
     83 }
     84