summaryrefslogtreecommitdiff
path: root/chromium/weblayer/shell/android/browsertests_apk/src/org/chromium/weblayer_browsertests_apk/WebLayerBrowserTestsActivity.java
blob: 56c2473bab1429a3614e0c092c872ace12b2462d (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2019 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.weblayer_browsertests_apk;

import android.net.Uri;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import org.chromium.base.CommandLine;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content_public.browser.BrowserStartupController;
import org.chromium.native_test.NativeBrowserTest;
import org.chromium.native_test.NativeBrowserTestActivity;
import org.chromium.weblayer.Browser;
import org.chromium.weblayer.NewTabCallback;
import org.chromium.weblayer.NewTabType;
import org.chromium.weblayer.Profile;
import org.chromium.weblayer.Tab;
import org.chromium.weblayer.TabCallback;
import org.chromium.weblayer.TestWebLayer;
import org.chromium.weblayer.WebLayer;

import java.io.File;

/** An Activity base class for running browser tests against WebLayerShell. */
public class WebLayerBrowserTestsActivity extends NativeBrowserTestActivity {
    private static final String TAG = "native_test";

    private WebLayer mWebLayer;
    private Profile mProfile;
    private Browser mBrowser;
    private Tab mTab;
    private EditText mUrlView;
    private View mMainView;

    @Override
    protected void initializeBrowserProcess() {
        BrowserStartupController.getInstance().setContentMainCallbackForTests(() -> {
            // This jumps into C++ to set up and run the test harness. The test harness runs
            // ContentMain()-equivalent code, and then waits for javaStartupTasksComplete()
            // to be called.
            runTests();
        });

        try {
            // Browser tests cannot be run in WebView compatibility mode since the class loader
            // WebLayer uses needs to match the class loader used for setup.
            TestWebLayer.disableWebViewCompatibilityMode();
            WebLayer.loadAsync(getApplication(), webLayer -> {
                mWebLayer = webLayer;
                createShell();

                NativeBrowserTest.javaStartupTasksComplete();
            });
        } catch (Exception e) {
            throw new RuntimeException("failed loading WebLayer", e);
        }

    }

    protected void createShell() {
        LinearLayout mainView = new LinearLayout(this);
        int viewId = View.generateViewId();
        mainView.setId(viewId);
        mMainView = mainView;
        setContentView(mainView);

        mUrlView = new EditText(this);
        mUrlView.setId(View.generateViewId());
        // The background of the top-view must be opaque, otherwise it bleeds through to the
        // cc::Layer that mirrors the contents of the top-view.
        mUrlView.setBackgroundColor(0xFFa9a9a9);

        RelativeLayout topContentsContainer = new RelativeLayout(this);
        topContentsContainer.addView(mUrlView,
                new RelativeLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        CommandLine commandLine = CommandLine.getInstance();
        String path = (commandLine.hasSwitch("start-in-incognito")) ? null : "BrowserTestProfile";

        Fragment fragment = WebLayer.createBrowserFragment(path);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(viewId, fragment);
        transaction.commitNow();

        mBrowser = Browser.fromFragment(fragment);
        mProfile = mBrowser.getProfile();
        mBrowser.setTopView(topContentsContainer);

        mTab = mBrowser.getActiveTab();
        mTab.registerTabCallback(new TabCallback() {
            @Override
            public void onVisibleUriChanged(Uri uri) {
                mUrlView.setText(uri.toString());
            }
        });
        // Set a new tab callback to make sure popups are added.
        mTab.setNewTabCallback(new NewTabCallback() {
            @Override
            public void onNewTab(Tab tab, @NewTabType int type) {}

            @Override
            public void onCloseTab() {}
        });
    }

    @Override
    protected File getPrivateDataDirectory() {
        return new File(UrlUtils.getIsolatedTestRoot(),
                WebLayerBrowserTestsApplication.PRIVATE_DATA_DIRECTORY_SUFFIX);
    }

    @Override
    /**
     * Ensure that the user data directory gets overridden to getPrivateDataDirectory() (which is
     * cleared at the start of every run); the directory that ANDROID_APP_DATA_DIR is set to in the
     * context of Java browsertests is not cleared as it also holds persistent state, which
     * causes test failures due to state bleedthrough. See crbug.com/617734 for details.
     */
    protected String getUserDataDirectoryCommandLineSwitch() {
        return "weblayer-user-data-dir";
    }
}