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

COVERAGE SUMMARY FOR SOURCE FILE [ChromiumTestShellTestBase.java]

nameclass, %method, %block, %line, %
ChromiumTestShellTestBase.java80%  (4/5)83%  (15/18)84%  (249/298)76%  (44/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ChromiumTestShellTestBase$30%   (0/1)0%   (0/2)0%   (0/18)0%   (0/3)
ChromiumTestShellTestBase$3 (ChromiumTestShellTestBase, String): void 0%   (0/1)0%   (0/9)0%   (0/1)
run (): void 0%   (0/1)0%   (0/9)0%   (0/2)
     
class ChromiumTestShellTestBase100% (1/1)90%  (9/10)89%  (155/175)81%  (30/37)
loadUrlWithSanitization (String): void 0%   (0/1)0%   (0/12)0%   (0/3)
removeFile (File): boolean 100% (1/1)88%  (30/34)75%  (6/8)
removeAppData (String): boolean 100% (1/1)89%  (34/38)71%  (5/7)
ChromiumTestShellTestBase (): void 100% (1/1)100% (4/4)100% (2/2)
access$000 (ChromiumTestShellTestBase, String): boolean 100% (1/1)100% (4/4)100% (1/1)
clearAppData (): boolean 100% (1/1)100% (14/14)100% (3/3)
getAppDir (): String 100% (1/1)100% (15/15)100% (3/3)
launchChromiumTestShellWithBlankPage (): ChromiumTestShellActivity 100% (1/1)100% (4/4)100% (1/1)
launchChromiumTestShellWithUrl (String): ChromiumTestShellActivity 100% (1/1)100% (37/37)100% (7/7)
waitForActiveShellToBeDoneLoading (): boolean 100% (1/1)100% (13/13)100% (2/2)
     
class ChromiumTestShellTestBase$1$1100% (1/1)100% (2/2)89%  (32/36)83%  (5/6)
run (): void 100% (1/1)85%  (23/27)80%  (4/5)
ChromiumTestShellTestBase$1$1 (ChromiumTestShellTestBase$1, AtomicBoolean): void 100% (1/1)100% (9/9)100% (1/1)
     
class ChromiumTestShellTestBase$1100% (1/1)100% (2/2)89%  (25/28)67%  (4/6)
isSatisfied (): boolean 100% (1/1)84%  (16/19)60%  (3/5)
ChromiumTestShellTestBase$1 (ChromiumTestShellTestBase, ChromiumTestShellActi... 100% (1/1)100% (9/9)100% (1/1)
     
class ChromiumTestShellTestBase$2100% (1/1)100% (2/2)90%  (37/41)78%  (7/9)
isSatisfied (): boolean 100% (1/1)86%  (25/29)71%  (5/7)
ChromiumTestShellTestBase$2 (ChromiumTestShellTestBase, String): void 100% (1/1)100% (12/12)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.chrome.testshell;
6 
7import android.content.ComponentName;
8import android.content.Context;
9import android.content.Intent;
10import android.net.Uri;
11import android.test.ActivityInstrumentationTestCase2;
12import android.text.TextUtils;
13 
14import org.chromium.content.browser.test.util.Criteria;
15import org.chromium.content.browser.test.util.CriteriaHelper;
16 
17import java.io.File;
18import java.util.concurrent.atomic.AtomicBoolean;
19 
20/**
21 * Base test class for all ChromiumTestShell based tests.
22 */
23public class ChromiumTestShellTestBase extends
24        ActivityInstrumentationTestCase2<ChromiumTestShellActivity> {
25    /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */
26    private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = 10000;
27 
28    public ChromiumTestShellTestBase() {
29        super(ChromiumTestShellActivity.class);
30    }
31 
32    /**
33     * Starts the ChromiumTestShell activity and loads the given URL.
34     */
35    protected ChromiumTestShellActivity launchChromiumTestShellWithUrl(String url) {
36        Intent intent = new Intent(Intent.ACTION_MAIN);
37        intent.addCategory(Intent.CATEGORY_LAUNCHER);
38        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
39        if (url != null) intent.setData(Uri.parse(url));
40        intent.setComponent(new ComponentName(getInstrumentation().getTargetContext(),
41                ChromiumTestShellActivity.class));
42        setActivityIntent(intent);
43        return getActivity();
44    }
45 
46    /**
47     * Starts the ChromiumTestShell activity and loads a blank page.
48     */
49    protected ChromiumTestShellActivity launchChromiumTestShellWithBlankPage() {
50        return launchChromiumTestShellWithUrl("about:blank");
51    }
52 
53    /**
54     * Waits for the Active shell to finish loading.  This times out after
55     * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be used for long
56     * loading pages. Instead it should be used more for test initialization. The proper way
57     * to wait is to use a TestCallbackHelperContainer after the initial load is completed.
58     * @return Whether or not the Shell was actually finished loading.
59     * @throws Exception
60     */
61    protected boolean waitForActiveShellToBeDoneLoading() throws InterruptedException {
62        final ChromiumTestShellActivity activity = getActivity();
63 
64        // Wait for the Content Shell to be initialized.
65        return CriteriaHelper.pollForCriteria(new Criteria() {
66            @Override
67            public boolean isSatisfied() {
68                try {
69                    final AtomicBoolean isLoaded = new AtomicBoolean(false);
70                    runTestOnUiThread(new Runnable() {
71                        @Override
72                        public void run() {
73                            TestShellTab tab = activity.getActiveTab();
74                            if (tab != null) {
75                                isLoaded.set(!tab.isLoading()
76                                        && !TextUtils.isEmpty(tab.getContentView().getUrl()));
77                            } else {
78                                isLoaded.set(false);
79                            }
80                        }
81                    });
82 
83                    return isLoaded.get();
84                } catch (Throwable e) {
85                    return false;
86                }
87            }
88        }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
89    }
90 
91    /**
92     * Clear all files in the testshell's directory except 'lib'.
93     *
94     * @return Whether clearing the application data was successful.
95     */
96    protected boolean clearAppData() throws Exception {
97        final int MAX_TIMEOUT_MS = 3000;
98        final String appDir = getAppDir();
99        return CriteriaHelper.pollForCriteria(
100                new Criteria() {
101                    private boolean mDataRemoved = false;
102                    @Override
103                    public boolean isSatisfied() {
104                        if (!mDataRemoved && !removeAppData(appDir)) {
105                            return false;
106                        }
107                        mDataRemoved = true;
108                        // We have to make sure the cache directory still exists, as the framework
109                        // will try to create it otherwise and will fail for sandbox processes with
110                        // an NPE.
111                        File cacheDir = new File(appDir, "cache");
112                        if (cacheDir.exists()) {
113                            return true;
114                        }
115                        return cacheDir.mkdir();
116                    }
117                },
118                MAX_TIMEOUT_MS, MAX_TIMEOUT_MS / 10);
119    }
120 
121    /**
122     * Remove all files and directories under the given appDir except 'lib'
123     *
124     * @param appDir the app directory to remove.
125     *
126     * @return true if succeeded.
127     */
128    private boolean removeAppData(String appDir) {
129        File[] files = new File(appDir).listFiles();
130        if (files == null) {
131            return true;
132        }
133        for (File file : files) {
134            if (!file.getAbsolutePath().endsWith("/lib") && !removeFile(file)) {
135                return false;
136            }
137        }
138        return true;
139    }
140 
141    private String getAppDir() {
142        Context target_ctx = getInstrumentation().getTargetContext();
143        String cacheDir = target_ctx.getCacheDir().getAbsolutePath();
144        return cacheDir.substring(0, cacheDir.lastIndexOf('/'));
145    }
146 
147    /**
148     * Remove the given file or directory.
149     *
150     * @param file the file or directory to remove.
151     *
152     * @return true if succeeded.
153     */
154    private static boolean removeFile(File file) {
155        if (file.isDirectory()) {
156            File[] files = file.listFiles();
157            if (files == null) {
158                return true;
159            }
160            for (File sub_file : files) {
161                if (!removeFile(sub_file))
162                    return false;
163            }
164        }
165        return file.delete();
166    }
167 
168 
169    /**
170     * Navigates the currently active tab to a sanitized version of {@code url}.
171     * @param url The potentially unsanitized URL to navigate to.
172     */
173    public void loadUrlWithSanitization(final String url) throws InterruptedException {
174        getInstrumentation().runOnMainSync(new Runnable() {
175            @Override
176            public void run() {
177                getActivity().getActiveTab().loadUrlWithSanitization(url);
178            }
179        });
180        waitForActiveShellToBeDoneLoading();
181    }
182}

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