aboutsummaryrefslogtreecommitdiff
path: root/src/org/chromium/webview_shell/TelemetryActivity.java
blob: a29e525e702affa04cf5c55069aecd8cb0af906d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.webview_shell;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Trace;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * This activity is designed for Telemetry testing of WebView.
 */
public class TelemetryActivity extends Activity {
    static final String DEFAULT_START_UP_TRACE_TAG = "WebViewStartupInterval";
    static final String DEFAULT_LOAD_URL_TRACE_TAG = "WebViewBlankUrlLoadInterval";
    static final String DEFAULT_START_UP_AND_LOAD_URL_TRACE_TAG =
            "WebViewStartupAndLoadBlankUrlInterval";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setTitle(
                getResources().getString(R.string.title_activity_telemetry));

        Intent intent = getIntent();
        final String startUpTraceTag = intent.getStringExtra("WebViewStartUpTraceTag");
        final String loadUrlTraceTag = intent.getStringExtra("WebViewLoadUrlTraceTag");
        final String startUpAndLoadUrlTraceTag =
                intent.getStringExtra("WebViewStartUpAndLoadUrlTraceTag");

        Trace.beginSection(startUpTraceTag == null ? DEFAULT_START_UP_AND_LOAD_URL_TRACE_TAG
                                                   : startUpAndLoadUrlTraceTag);
        Trace.beginSection(startUpTraceTag == null ? DEFAULT_START_UP_TRACE_TAG : startUpTraceTag);
        WebView webView = new WebView(this);
        setContentView(webView);
        Trace.endSection();

        CookieManager.setAcceptFileSchemeCookies(true);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDomStorageEnabled(true);
        settings.setMediaPlaybackRequiresUserGesture(false);
        String userAgentString = intent.getStringExtra("userAgent");
        if (userAgentString != null) {
            settings.setUserAgentString(userAgentString);
        }

        webView.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation") // because we support api level 19 and up.
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Trace.endSection();
                Trace.endSection();
            }
        });

        Trace.beginSection(loadUrlTraceTag == null ? DEFAULT_LOAD_URL_TRACE_TAG : loadUrlTraceTag);
        webView.loadUrl("about:blank");
    }
}