summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/ui/DefaultLayoutProviderTest.java
blob: 48335a602cdfa289e26aed3be064a798a8eb412b (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
134
135
136
137
138
139
140
141
142
/**
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.android.launcher3.ui;

import static org.junit.Assert.assertTrue;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor.AutoCloseOutputStream;

import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.testcomponent.TestCommandReceiver;
import com.android.launcher3.util.LauncherLayoutBuilder;
import com.android.launcher3.util.rule.ShellCommandRule;
import com.android.launcher3.widget.LauncherAppWidgetHostView;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.OutputStreamWriter;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.UiSelector;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class DefaultLayoutProviderTest extends AbstractLauncherUiTest {

    @Rule
    public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();

    private static final String SETTINGS_APP = "com.android.settings";

    private Context mContext;
    private String mAuthority;

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();

        mContext = InstrumentationRegistry.getContext();

        PackageManager pm = mTargetContext.getPackageManager();
        ProviderInfo pi = pm.getProviderInfo(new ComponentName(mContext,
                TestCommandReceiver.class), 0);
        mAuthority = pi.authority;
    }

    @Test
    // Convert test to TAPL; b/131116002
    public void testCustomProfileLoaded_with_icon_on_hotseat() throws Exception {
        writeLayout(new LauncherLayoutBuilder().atHotseat(0).putApp(SETTINGS_APP, SETTINGS_APP));

        // Launch the home activity
        mActivityMonitor.startLauncher();
        waitForModelLoaded();

        // Verify widget present
        UiSelector selector = new UiSelector().packageName(mTargetContext.getPackageName())
                .description(getSettingsApp().getLabel().toString());
        assertTrue(mDevice.findObject(selector).waitForExists(DEFAULT_UI_TIMEOUT));
    }

    @Test
    // Convert test to TAPL; b/131116002
    public void testCustomProfileLoaded_with_widget() throws Exception {
        // A non-restored widget with no config screen gets restored automatically.
        LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, false);

        writeLayout(new LauncherLayoutBuilder().atWorkspace(0, 1, 0)
                .putWidget(info.getComponent().getPackageName(),
                        info.getComponent().getClassName(), 2, 2));

        // Launch the home activity
        mActivityMonitor.startLauncher();
        waitForModelLoaded();

        // Verify widget present
        UiSelector selector = new UiSelector().packageName(mTargetContext.getPackageName())
                .className(LauncherAppWidgetHostView.class).description(info.label);
        assertTrue(mDevice.findObject(selector).waitForExists(DEFAULT_UI_TIMEOUT));
    }

    @Test
    // Convert test to TAPL; b/131116002
    public void testCustomProfileLoaded_with_folder() throws Exception {
        writeLayout(new LauncherLayoutBuilder().atHotseat(0).putFolder(android.R.string.copy)
                .addApp(SETTINGS_APP, SETTINGS_APP)
                .addApp(SETTINGS_APP, SETTINGS_APP)
                .addApp(SETTINGS_APP, SETTINGS_APP)
                .build());

        // Launch the home activity
        mActivityMonitor.startLauncher();
        waitForModelLoaded();

        // Verify widget present
        UiSelector selector = new UiSelector().packageName(mTargetContext.getPackageName())
                .descriptionContains(mTargetContext.getString(android.R.string.copy));
        assertTrue(mDevice.findObject(selector).waitForExists(DEFAULT_UI_TIMEOUT));
    }

    @After
    public void cleanup() throws Exception {
        mDevice.executeShellCommand("settings delete secure launcher3.layout.provider");
    }

    private void writeLayout(LauncherLayoutBuilder builder) throws Exception {
        mDevice.executeShellCommand("settings put secure launcher3.layout.provider " + mAuthority);
        ParcelFileDescriptor pfd = mTargetContext.getContentResolver().openFileDescriptor(
                Uri.parse("content://" + mAuthority + "/launcher_layout"), "w");

        try (OutputStreamWriter writer = new OutputStreamWriter(new AutoCloseOutputStream(pfd))) {
            builder.build(writer);
        }
        clearLauncherData();
    }
}