1 /* 2 * Copyright (C) 2009 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 import android.graphics.Bitmap; 20 import android.test.ActivityInstrumentationTestCase2; 21 import android.webkit.WebBackForwardList; 22 import android.webkit.cts.WebViewOnUiThread.WaitForProgressClient; 23 import android.webkit.WebHistoryItem; 24 import android.webkit.WebIconDatabase; 25 import android.webkit.WebView; 26 27 import com.android.compatibility.common.util.NullWebViewUtils; 28 import com.android.compatibility.common.util.PollingCheck; 29 30 public class WebHistoryItemTest extends ActivityInstrumentationTestCase2<WebViewCtsActivity> { 31 private final static long TEST_TIMEOUT = 10000; 32 private CtsTestServer mWebServer; 33 private WebViewOnUiThread mOnUiThread; 34 private WebIconDatabase mIconDb; 35 36 class WaitForIconClient extends WaitForProgressClient { 37 private boolean mReceivedIcon; 38 39 public WaitForIconClient(WebViewOnUiThread onUiThread) { 40 super(onUiThread); 41 } 42 43 @Override 44 public synchronized void onReceivedIcon(WebView webview, Bitmap icon) { 45 mReceivedIcon = true; 46 } 47 48 public synchronized boolean receivedIcon() { return mReceivedIcon; } 49 }; 50 51 public WebHistoryItemTest() { 52 super("android.webkit.cts", WebViewCtsActivity.class); 53 } 54 55 @Override 56 protected void setUp() throws Exception { 57 super.setUp(); 58 mWebServer = new CtsTestServer(getActivity()); 59 WebView webview = getActivity().getWebView(); 60 if (webview != null) { 61 mOnUiThread = new WebViewOnUiThread(this, webview); 62 } 63 } 64 65 @Override 66 protected void tearDown() throws Exception { 67 if (mOnUiThread != null) { 68 mOnUiThread.cleanUp(); 69 } 70 mWebServer.shutdown(); 71 super.tearDown(); 72 if (mIconDb != null) { 73 mIconDb.removeAllIcons(); 74 mIconDb.close(); 75 } 76 } 77 78 public void testWebHistoryItem() throws Throwable { 79 if (!NullWebViewUtils.isWebViewAvailable()) { 80 return; 81 } 82 final WaitForIconClient waitForIconClient = new WaitForIconClient(mOnUiThread); 83 mOnUiThread.setWebChromeClient(waitForIconClient); 84 runTestOnUiThread(new Runnable() { 85 @Override 86 public void run() { 87 // getInstance must run on the UI thread 88 mIconDb = WebIconDatabase.getInstance(); 89 String dbPath = getActivity().getFilesDir().toString() + "/icons"; 90 mIconDb.open(dbPath); 91 } 92 }); 93 94 WebBackForwardList list = mOnUiThread.copyBackForwardList(); 95 assertEquals(0, list.getSize()); 96 97 String url = mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL); 98 mOnUiThread.loadUrlAndWaitForCompletion(url); 99 new PollingCheck() { 100 @Override 101 protected boolean check() { 102 return waitForIconClient.receivedIcon(); 103 } 104 }.run(); 105 106 list = mOnUiThread.copyBackForwardList(); 107 assertEquals(1, list.getSize()); 108 WebHistoryItem item = list.getCurrentItem(); 109 assertNotNull(item); 110 assertEquals(url, item.getUrl()); 111 assertEquals(url, item.getOriginalUrl()); 112 assertEquals(TestHtmlConstants.HELLO_WORLD_TITLE, item.getTitle()); 113 Bitmap icon = mOnUiThread.getFavicon(); 114 assertTrue(icon.sameAs(item.getFavicon())); 115 116 url = mWebServer.getAssetUrl(TestHtmlConstants.BR_TAG_URL); 117 mOnUiThread.loadUrlAndWaitForCompletion(url); 118 list = mOnUiThread.copyBackForwardList(); 119 assertEquals(2, list.getSize()); 120 item = list.getCurrentItem(); 121 assertNotNull(item); 122 assertEquals(TestHtmlConstants.BR_TAG_TITLE, item.getTitle()); 123 } 124 } 125