diff options
author | John Reck <jreck@google.com> | 2011-06-29 11:31:24 -0700 |
---|---|---|
committer | John Reck <jreck@google.com> | 2011-06-29 13:28:03 -0700 |
commit | db22ec4ee014900988062d910bc810172a07df56 (patch) | |
tree | 317dd31937a995619e36882d3b2571adf55a7916 /tests | |
parent | 749b93662b6c3152899d91dbd80139224dc46961 (diff) | |
download | packages_apps_browser-db22ec4ee014900988062d910bc810172a07df56.zip packages_apps_browser-db22ec4ee014900988062d910bc810172a07df56.tar.gz packages_apps_browser-db22ec4ee014900988062d910bc810172a07df56.tar.bz2 |
Fix IntentHandler behavior
Bug: 4473779
Plus, TESTS! Huzzah!
Change-Id: I043e100d99d4653b7fb7885217d7fb305930a137
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/com/android/browser/IntentHandlerTests.java | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/tests/src/com/android/browser/IntentHandlerTests.java b/tests/src/com/android/browser/IntentHandlerTests.java new file mode 100644 index 0000000..2bdadae --- /dev/null +++ b/tests/src/com/android/browser/IntentHandlerTests.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.browser; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.provider.Browser; +import android.test.ActivityInstrumentationTestCase2; +import android.text.TextUtils; +import android.webkit.WebView; + +public class IntentHandlerTests extends ActivityInstrumentationTestCase2<BrowserActivity> { + + // How long to wait to receive onPageStarted + static final int START_LOAD_TIMEOUT = 20000; // ms + static final int POLL_INTERVAL = 50; // ms + boolean mHasStarted = false; + + public IntentHandlerTests() { + super(BrowserActivity.class); + } + + public void testSwitchToTabWithUrl() throws Throwable { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse("http://google.com/")); + sendIntent(intent); + Controller controller = getActivity().getController(); + Tab tabGoogle = controller.getCurrentTab(); + assertNotNull("Current tab (google.com", tabGoogle); + assertEquals("http://google.com/", tabGoogle.getOriginalUrl()); + assertEquals(1, controller.getTabs().size()); + intent.setData(Uri.parse("http://maps.google.com/")); + sendIntent(intent); + Tab tabMaps = controller.getCurrentTab(); + assertNotSame(tabGoogle, tabMaps); + assertNotNull("Current tab (maps.google.com)", tabMaps); + assertEquals(2, controller.getTabs().size()); + intent.setData(Uri.parse("http://google.com/")); + sendIntent(intent); + assertEquals(tabGoogle, controller.getCurrentTab()); + assertEquals(2, controller.getTabs().size()); + } + + public void testShortcut() throws Throwable { + Intent intent = BookmarkUtils.createShortcutIntent("http://google.com/"); + sendIntent(intent); + Controller controller = getActivity().getController(); + Tab tabGoogle = controller.getCurrentTab(); + assertEquals("http://google.com/", tabGoogle.getOriginalUrl()); + assertEquals(1, controller.getTabs().size()); + sendIntent(intent); + assertEquals(1, controller.getTabs().size()); + assertEquals(tabGoogle, controller.getCurrentTab()); + directlyLoadUrl(tabGoogle, "http://maps.google.com/"); + sendIntent(intent); + if (BrowserActivity.isTablet(getActivity())) { + assertEquals(2, controller.getTabs().size()); + assertNotSame(tabGoogle, controller.getCurrentTab()); + assertEquals("http://maps.google.com/", tabGoogle.getOriginalUrl()); + Tab currentTab = controller.getCurrentTab(); + assertEquals("http://google.com/", currentTab.getOriginalUrl()); + } else { + assertEquals(1, controller.getTabs().size()); + assertEquals(tabGoogle, controller.getCurrentTab()); + assertEquals("http://google.com/", tabGoogle.getOriginalUrl()); + } + } + + public void testApplication() throws Throwable { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse("http://google.com/")); + intent.putExtra(Browser.EXTRA_APPLICATION_ID, getClass().getName()); + sendIntent(intent); + Controller controller = getActivity().getController(); + Tab tabGoogle = controller.getCurrentTab(); + assertNotNull("Current tab (google.com", tabGoogle); + assertEquals("http://google.com/", tabGoogle.getOriginalUrl()); + assertEquals(1, controller.getTabs().size()); + intent.setData(Uri.parse("http://maps.google.com/")); + sendIntent(intent); + Tab tabMaps = controller.getCurrentTab(); + assertEquals("http://maps.google.com/", tabMaps.getOriginalUrl()); + if (BrowserActivity.isTablet(getActivity())) { + assertEquals(2, controller.getTabs().size()); + assertNotSame(tabGoogle, tabMaps); + assertEquals("http://google.com/", tabGoogle.getOriginalUrl()); + } else { + assertEquals(1, controller.getTabs().size()); + assertEquals(tabGoogle, tabMaps); + } + } + + /** + * Simulate clicking a link by loading a URL directly on the WebView, + * bypassing Tab, Controller, etc.. + * @throws Throwable + */ + private void directlyLoadUrl(final Tab tab, final String url) throws Throwable { + runTestOnUiThread(new Runnable() { + @Override + public void run() { + WebView web = tab.getWebView(); + web.loadUrl(url); + } + }); + waitForLoadStart(tab, url); + } + + void waitForLoadStart(final Tab tab, final String url) throws InterruptedException { + long start = System.currentTimeMillis(); + while (!TextUtils.equals(tab.getOriginalUrl(), url)) { + if (start + START_LOAD_TIMEOUT < System.currentTimeMillis()) { + throw new RuntimeException("Didn't receive onPageStarted!"); + } + Thread.sleep(POLL_INTERVAL); + } + } + + private void sendIntent(final Intent intent) throws Throwable { + sendIntent(intent, true); + } + + private void sendIntent(final Intent intent, boolean waitForLoadStart) throws Throwable { + if (!mHasStarted) { + // Prevent crash recovery from happening + intent.putExtra(Controller.NO_CRASH_RECOVERY, true); + setActivityIntent(intent); + getActivity(); + } else { + final Activity activity = getActivity(); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + getInstrumentation().callActivityOnNewIntent(activity, intent); + } + }); + } + if (waitForLoadStart) { + String url = intent.getDataString(); + Tab tab = getActivity().getController().getCurrentTab(); + waitForLoadStart(tab, url); + } + } + + @Override + public BrowserActivity getActivity() { + mHasStarted = true; + return super.getActivity(); + } +} |