OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.webapps; |
| 6 |
| 7 import android.content.Intent; |
| 8 import android.net.Uri; |
| 9 |
| 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.chrome.browser.ShortcutHelper; |
| 12 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 13 import org.chromium.content.browser.test.util.CallbackHelper; |
| 14 import org.chromium.content.browser.test.util.Criteria; |
| 15 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 16 import org.chromium.content.browser.test.util.TestWebContentsObserver; |
| 17 import org.chromium.content_public.browser.LoadUrlParams; |
| 18 import org.chromium.ui.base.PageTransition; |
| 19 |
| 20 import java.util.concurrent.TimeoutException; |
| 21 |
| 22 /** |
| 23 * The base class of the WebappActivity tests. It provides the common methods to
access the activity |
| 24 * UI. This particular test base only instantiates WebappActivity0. |
| 25 */ |
| 26 public abstract class WebappActivityTestBase extends ChromeActivityTestCaseBase<
WebappActivity0> { |
| 27 static final String WEBAPP_ID = "webapp_id"; |
| 28 |
| 29 TestWebContentsObserver mTestObserver; |
| 30 |
| 31 public WebappActivityTestBase() { |
| 32 super(WebappActivity0.class); |
| 33 } |
| 34 |
| 35 @Override |
| 36 protected void setUp() throws Exception { |
| 37 super.setUp(); |
| 38 |
| 39 // Default to a webapp that just loads about:blank to avoid a network lo
ad. This results |
| 40 // in the URL bar showing since {@link UrlUtils} cannot parse this type
of URL. |
| 41 Intent intent = new Intent(getInstrumentation().getTargetContext(), Weba
ppActivity0.class); |
| 42 intent.setData(Uri.parse(WebappActivity.WEBAPP_SCHEME + "://" + WEBAPP_I
D)); |
| 43 intent.putExtra(ShortcutHelper.EXTRA_ID, WEBAPP_ID); |
| 44 intent.putExtra(ShortcutHelper.EXTRA_URL, "about:blank"); |
| 45 setActivityIntent(intent); |
| 46 |
| 47 waitUntilIdle(); |
| 48 |
| 49 // TODO(yfriedman): Change callers to be executed on the UI thread. Unfo
rtunately this is |
| 50 // super convenient as the caller is nearly always on the test thread wh
ich is fine to |
| 51 // block and it's cumbersome to keep bouncing to the UI thread. |
| 52 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 53 @Override |
| 54 public void run() { |
| 55 mTestObserver = new TestWebContentsObserver( |
| 56 getActivity().getActivityTab().getWebContents()); |
| 57 } |
| 58 }); |
| 59 } |
| 60 |
| 61 /** |
| 62 * Waits until any loads in progress have completed. |
| 63 */ |
| 64 protected void waitUntilIdle() { |
| 65 getInstrumentation().waitForIdleSync(); |
| 66 try { |
| 67 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 68 @Override |
| 69 public boolean isSatisfied() { |
| 70 return getActivity().getActivityTab() != null |
| 71 && !getActivity().getActivityTab().isLoading(); |
| 72 } |
| 73 })); |
| 74 } catch (InterruptedException exception) { |
| 75 fail(); |
| 76 } |
| 77 |
| 78 getInstrumentation().waitForIdleSync(); |
| 79 } |
| 80 |
| 81 /** |
| 82 * Loads the URL in the WebappActivity and waits until it has been fully loa
ded. |
| 83 * @param url URL to load. |
| 84 */ |
| 85 @Override |
| 86 public int loadUrl(final String url) throws IllegalArgumentException, Interr
uptedException { |
| 87 waitUntilIdle(); |
| 88 |
| 89 final CallbackHelper startLoadingHelper = mTestObserver.getOnPageStarted
Helper(); |
| 90 final CallbackHelper finishLoadingHelper = mTestObserver.getOnPageFinish
edHelper(); |
| 91 |
| 92 int startLoadingCount = startLoadingHelper.getCallCount(); |
| 93 int finishLoadingCount = finishLoadingHelper.getCallCount(); |
| 94 |
| 95 getInstrumentation().runOnMainSync(new Runnable() { |
| 96 @Override |
| 97 public void run() { |
| 98 int pageTransition = PageTransition.TYPED | PageTransition.FROM_
ADDRESS_BAR; |
| 99 getActivity().getActivityTab().loadUrl(new LoadUrlParams(url, pa
geTransition)); |
| 100 } |
| 101 }); |
| 102 |
| 103 try { |
| 104 startLoadingHelper.waitForCallback(startLoadingCount); |
| 105 } catch (TimeoutException e) { |
| 106 fail(); |
| 107 } |
| 108 |
| 109 try { |
| 110 finishLoadingHelper.waitForCallback(finishLoadingCount); |
| 111 } catch (TimeoutException e) { |
| 112 fail(); |
| 113 } |
| 114 return 0; |
| 115 } |
| 116 |
| 117 @Override |
| 118 public void startMainActivity() throws InterruptedException { |
| 119 // Do nothing |
| 120 } |
| 121 } |
OLD | NEW |