EMMA Coverage Report (generated Tue Aug 20 10:07:21 PDT 2013)
[all classes][org.chromium.content_shell_apk]

COVERAGE SUMMARY FOR SOURCE FILE [ContentShellTestBase.java]

nameclass, %method, %block, %line, %
ContentShellTestBase.java100% (5/5)89%  (17/19)81%  (201/247)72%  (33.7/47)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ContentShellTestBase100% (1/1)82%  (9/11)74%  (106/144)69%  (22.8/33)
startActivityWithTestUrl (String): void 0%   (0/1)0%   (0/18)0%   (0/5)
startActivityWithTestUrlAndCommandLineArgs (String, String []): void 0%   (0/1)0%   (0/13)0%   (0/4)
getContentViewCore (): ContentViewCore 100% (1/1)78%  (7/9)77%  (0.8/1)
launchContentShellWithUrlAndCommandLineArgs (String, String []): ContentShell... 100% (1/1)89%  (39/44)89%  (8/9)
ContentShellTestBase (): void 100% (1/1)100% (4/4)100% (2/2)
assertWaitForPageScaleFactorMatch (float): void 100% (1/1)100% (8/8)100% (2/2)
getContentView (): ContentView 100% (1/1)100% (6/6)100% (1/1)
handleBlockingCallbackAction (CallbackHelper, Runnable): void 100% (1/1)100% (13/13)100% (4/4)
launchContentShellWithUrl (String): ContentShellActivity 100% (1/1)100% (5/5)100% (1/1)
loadUrl (ContentView, TestCallbackHelperContainer, LoadUrlParams): void 100% (1/1)100% (11/11)100% (2/2)
waitForActiveShellToBeDoneLoading (): boolean 100% (1/1)100% (13/13)100% (2/2)
     
class ContentShellTestBase$1$1100% (1/1)100% (2/2)89%  (32/36)83%  (5/6)
run (): void 100% (1/1)85%  (23/27)80%  (4/5)
ContentShellTestBase$1$1 (ContentShellTestBase$1, AtomicBoolean): void 100% (1/1)100% (9/9)100% (1/1)
     
class ContentShellTestBase$1100% (1/1)100% (2/2)89%  (25/28)67%  (4/6)
isSatisfied (): boolean 100% (1/1)84%  (16/19)60%  (3/5)
ContentShellTestBase$1 (ContentShellTestBase, ContentShellActivity): void 100% (1/1)100% (9/9)100% (1/1)
     
class ContentShellTestBase$3100% (1/1)100% (2/2)95%  (20/21)96%  (1.9/2)
isSatisfied (): boolean 100% (1/1)92%  (11/12)91%  (0.9/1)
ContentShellTestBase$3 (ContentShellTestBase, float): void 100% (1/1)100% (9/9)100% (1/1)
     
class ContentShellTestBase$2100% (1/1)100% (2/2)100% (18/18)100% (3/3)
ContentShellTestBase$2 (ContentShellTestBase, ContentView, LoadUrlParams): void 100% (1/1)100% (12/12)100% (1/1)
run (): void 100% (1/1)100% (6/6)100% (2/2)

1// Copyright (c) 2012 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 
5package org.chromium.content_shell_apk;
6 
7import android.content.ComponentName;
8import android.content.Intent;
9import android.net.Uri;
10import android.test.ActivityInstrumentationTestCase2;
11import android.text.TextUtils;
12 
13import org.chromium.base.test.util.UrlUtils;
14import org.chromium.content.browser.ContentView;
15import org.chromium.content.browser.ContentViewCore;
16import org.chromium.content.browser.LoadUrlParams;
17import org.chromium.content.browser.test.util.CallbackHelper;
18import org.chromium.content.browser.test.util.Criteria;
19import org.chromium.content.browser.test.util.CriteriaHelper;
20import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
21import org.chromium.content_shell.Shell;
22 
23import java.util.concurrent.TimeUnit;
24import java.util.concurrent.atomic.AtomicBoolean;
25 
26/**
27 * Base test class for all ContentShell based tests.
28 */
29public class ContentShellTestBase extends ActivityInstrumentationTestCase2<ContentShellActivity> {
30 
31    /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */
32    private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = 10000;
33 
34    protected static final int WAIT_PAGE_LOADING_TIMEOUT_SECONDS = 15;
35 
36    public ContentShellTestBase() {
37        super(ContentShellActivity.class);
38    }
39 
40    /**
41     * Starts the ContentShell activity and loads the given URL.
42     * The URL can be null, in which case will default to ContentShellActivity.DEFAULT_SHELL_URL.
43     */
44    protected ContentShellActivity launchContentShellWithUrl(String url) {
45        return launchContentShellWithUrlAndCommandLineArgs(url, null);
46    }
47 
48    /**
49     * Starts the ContentShell activity appending the provided command line arguments
50     * and loads the given URL. The URL can be null, in which case will default to
51     * ContentShellActivity.DEFAULT_SHELL_URL.
52     */
53    protected ContentShellActivity launchContentShellWithUrlAndCommandLineArgs(String url,
54            String[] commandLineArgs) {
55        Intent intent = new Intent(Intent.ACTION_MAIN);
56        intent.addCategory(Intent.CATEGORY_LAUNCHER);
57        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
58        if (url != null) intent.setData(Uri.parse(url));
59        intent.setComponent(new ComponentName(getInstrumentation().getTargetContext(),
60              ContentShellActivity.class));
61        if (commandLineArgs != null) {
62            intent.putExtra(ContentShellActivity.COMMAND_LINE_ARGS_KEY, commandLineArgs);
63        }
64        setActivityIntent(intent);
65        return getActivity();
66    }
67 
68    // TODO(cjhopman): These functions are inconsistent with launchContentShell***. Should be
69    // startContentShell*** and should use the url exactly without the getTestFileUrl call. Possibly
70    // these two ways of starting the activity (launch* and start*) should be merged into one.
71    /**
72     * Starts the content shell activity with the provided test url.
73     * The url is synchronously loaded.
74     * @param url Test url to load.
75     */
76    protected void startActivityWithTestUrl(String url) throws Throwable {
77        launchContentShellWithUrl(UrlUtils.getTestFileUrl(url));
78        assertNotNull(getActivity());
79        assertTrue(waitForActiveShellToBeDoneLoading());
80        assertEquals(UrlUtils.getTestFileUrl(url), getContentView().getUrl());
81    }
82 
83    /**
84     * Starts the content shell activity with the provided test url and optional command line
85     * arguments to append.
86     * The url is synchronously loaded.
87     * @param url Test url to load.
88     * @param commandLineArgs Optional command line args to append when launching the activity.
89     */
90    protected void startActivityWithTestUrlAndCommandLineArgs(
91            String url, String[] commandLineArgs) throws Throwable {
92        launchContentShellWithUrlAndCommandLineArgs(
93                UrlUtils.getTestFileUrl(url), commandLineArgs);
94        assertNotNull(getActivity());
95        assertTrue(waitForActiveShellToBeDoneLoading());
96    }
97 
98    /**
99     * Returns the current ContentView.
100     */
101    protected ContentView getContentView() {
102        return getActivity().getActiveShell().getContentView();
103    }
104 
105    /**
106     * Returns the current ContentViewCore or null if there is no ContentView.
107     */
108    protected ContentViewCore getContentViewCore() {
109        return getContentView() == null ? null : getContentView().getContentViewCore();
110    }
111 
112    /**
113     * Waits for the Active shell to finish loading.  This times out after
114     * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be used for long
115     * loading pages. Instead it should be used more for test initialization. The proper way
116     * to wait is to use a TestCallbackHelperContainer after the initial load is completed.
117     * @return Whether or not the Shell was actually finished loading.
118     * @throws InterruptedException
119     */
120    protected boolean waitForActiveShellToBeDoneLoading() throws InterruptedException {
121        final ContentShellActivity activity = getActivity();
122 
123        // Wait for the Content Shell to be initialized.
124        return CriteriaHelper.pollForCriteria(new Criteria() {
125            @Override
126            public boolean isSatisfied() {
127                try {
128                    final AtomicBoolean isLoaded = new AtomicBoolean(false);
129                    runTestOnUiThread(new Runnable() {
130                        @Override
131                        public void run() {
132                            Shell shell = activity.getActiveShell();
133                            if (shell != null) {
134                                // There are two cases here that need to be accounted for.
135                                // The first is that we've just created a Shell and it isn't
136                                // loading because it has no URL set yet.  The second is that
137                                // we've set a URL and it actually is loading.
138                                isLoaded.set(!shell.isLoading()
139                                        && !TextUtils.isEmpty(shell.getContentView().getUrl()));
140                            } else {
141                                isLoaded.set(false);
142                            }
143                        }
144                    });
145 
146                    return isLoaded.get();
147                } catch (Throwable e) {
148                    return false;
149                }
150            }
151        }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
152    }
153 
154    /**
155     * Loads a URL in the specified content view.
156     *
157     * @param contentView The content view to load the URL in.
158     * @param callbackHelperContainer The callback helper container used to monitor progress.
159     * @param params The URL params to use.
160     */
161    protected void loadUrl(
162            final ContentView contentView, TestCallbackHelperContainer callbackHelperContainer,
163            final LoadUrlParams params) throws Throwable {
164        handleBlockingCallbackAction(
165                callbackHelperContainer.getOnPageFinishedHelper(),
166                new Runnable() {
167                    @Override
168                    public void run() {
169                        contentView.loadUrl(params);
170                    }
171                });
172    }
173 
174    /**
175     * Handles performing an action on the UI thread that will return when the specified callback
176     * is incremented.
177     *
178     * @param callbackHelper The callback helper that will be blocked on.
179     * @param action The action to be performed on the UI thread.
180     */
181    protected void handleBlockingCallbackAction(
182            CallbackHelper callbackHelper, Runnable action) throws Throwable {
183        int currentCallCount = callbackHelper.getCallCount();
184        runTestOnUiThread(action);
185        callbackHelper.waitForCallback(
186                currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS);
187    }
188 
189    // TODO(aelias): This method needs to be removed once http://crbug.com/179511 is fixed.
190    // Meanwhile, we have to wait if the page has the <meta viewport> tag.
191    /**
192     * Waits till the ContentViewCore receives the expected page scale factor
193     * from the compositor and asserts that this happens.
194     */
195    protected void assertWaitForPageScaleFactorMatch(final float expectedScale)
196            throws InterruptedException {
197        assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
198            @Override
199            public boolean isSatisfied() {
200                return getContentViewCore().getScale() == expectedScale;
201            }
202        }));
203    }
204}

[all classes][org.chromium.content_shell_apk]
EMMA 2.0.5312 (C) Vladimir Roubtsov