summaryrefslogtreecommitdiff
path: root/adservices/apk/tests/notification/src/com/android/adservices/ui/notifications/NotificationActivityUiAutomatorTest.java
blob: ce4cc00773254319efdfbc2cd699f4521c889dce (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2022 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.adservices.ui.notifications;

import static com.android.adservices.service.ui.ux.collection.PrivacySandboxUxCollection.BETA_UX;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import android.content.Context;
import android.content.Intent;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.FlakyTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.Until;

import com.android.adservices.api.R;
import com.android.adservices.common.AdServicesExtendedMockitoTestCase;
import com.android.adservices.common.AdservicesTestHelper;
import com.android.adservices.service.Flags;
import com.android.adservices.service.FlagsFactory;
import com.android.adservices.service.PhFlags;
import com.android.adservices.service.common.BackgroundJobsManager;
import com.android.adservices.service.consent.ConsentManager;
import com.android.adservices.service.ui.data.UxStatesManager;
import com.android.adservices.ui.util.ApkTestUtil;
import com.android.dx.mockito.inline.extended.ExtendedMockito;
import com.android.modules.utils.testing.ExtendedMockitoRule.SpyStatic;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

import java.io.IOException;

@SpyStatic(PhFlags.class)
@SpyStatic(BackgroundJobsManager.class)
@SpyStatic(FlagsFactory.class)
@SpyStatic(ConsentManager.class)
@SpyStatic(UxStatesManager.class)
@RunWith(AndroidJUnit4.class)
public final class NotificationActivityUiAutomatorTest extends AdServicesExtendedMockitoTestCase {

    private static final String NOTIFICATION_TEST_PACKAGE =
            "android.test.adservices.ui.NOTIFICATIONS";
    private static final int LAUNCH_TIMEOUT = 5000;
    private static final UiDevice sDevice =
            UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    @Mock private ConsentManager mConsentManager;
    private Context mContext;

    // TODO(b/261216850): Migrate this NotificationActivity to non-mock test
    @Mock private Flags mMockFlags;
    @Mock private UxStatesManager mUxStatesManager;

    @Before
    public void setup() throws UiObjectNotFoundException, IOException {
        mContext = spy(appContext.get());

        doReturn(true).when(mMockFlags).getUIDialogsFeatureEnabled();
        doReturn(true).when(mMockFlags).isUiFeatureTypeLoggingEnabled();
        doReturn(true).when(mMockFlags).getRecordManualInteractionEnabled();
        doReturn(true).when(mMockFlags).getConsentNotificationActivityDebugMode();
        doReturn(BETA_UX).when(mUxStatesManager).getUx();
        doReturn("BETA_UX").when(mMockFlags).getDebugUx();

        extendedMockito.mockGetFlags(mMockFlags);
        ExtendedMockito.doReturn(mUxStatesManager)
                .when(() -> UxStatesManager.getInstance(any(Context.class)));
        ExtendedMockito.doReturn(mConsentManager)
                .when(() -> ConsentManager.getInstance(any(Context.class)));
        ExtendedMockito.doNothing()
                .when(() -> BackgroundJobsManager.scheduleAllBackgroundJobs(any(Context.class)));

        sDevice.pressHome();

        final String launcherPackage = sDevice.getLauncherPackageName();
        assertThat(launcherPackage).isNotNull();
        sDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
    }

    @After
    public void teardown() throws Exception {
        ApkTestUtil.takeScreenshot(sDevice, getClass().getSimpleName() + "_" + getTestName() + "_");

        AdservicesTestHelper.killAdservicesProcess(mContext);
    }
    @Test
    @FlakyTest(bugId = 302607350)
    public void notificationRowGaTest() throws Exception {
        doReturn(true).when(mMockFlags).getGaUxFeatureEnabled();
        doReturn("GA_UX").when(mMockFlags).getDebugUx();

        startActivity(false);

        UiObject notificationGaTitle = getElement(R.string.notificationUI_header_ga_title);
        assertThat(notificationGaTitle.exists()).isTrue();

        UiObject leftControlButton = getElement(R.string.notificationUI_left_control_button_text);
        UiObject rightControlButton = getElement(R.string.notificationUI_right_control_button_text);
        UiObject moreButton = getElement(R.string.notificationUI_more_button_text);
    }

    @Test
    @FlakyTest(bugId = 302607350)
    public void notificationEuGaTest() throws Exception {
        doReturn(true).when(mMockFlags).getGaUxFeatureEnabled();
        doReturn("GA_UX").when(mMockFlags).getDebugUx();

        startActivity(true);

        UiObject leftControlButton =
                getElement(R.string.notificationUI_confirmation_left_control_button_text);
        UiObject rightControlButton =
                getElement(R.string.notificationUI_confirmation_right_control_button_text);
        UiObject moreButton = getElement(R.string.notificationUI_more_button_text);

        verifyControlsAndMoreButtonAreDisplayed(leftControlButton, rightControlButton, moreButton);

        rightControlButton.click();

        UiObject acceptedTitle = getElement(R.string.notificationUI_header_ga_title_eu_v2);
        assertThat(acceptedTitle.exists()).isTrue();
        UiObject leftControlButtonOnSecondPage =
                getElement(R.string.notificationUI_left_control_button_text_eu_v2);
        UiObject rightControlButtonOnSecondPage =
                getElement(R.string.notificationUI_right_control_button_ga_text_eu_v2);
        UiObject moreButtonOnSecondPage = getElement(R.string.notificationUI_more_button_text);
        verifyControlsAndMoreButtonAreDisplayed(
                leftControlButtonOnSecondPage,
                rightControlButtonOnSecondPage,
                moreButtonOnSecondPage);
    }

    private void verifyControlsAndMoreButtonAreDisplayed(
            UiObject leftControlButton, UiObject rightControlButton, UiObject moreButton)
            throws UiObjectNotFoundException, InterruptedException {
        UiObject scrollView =
                sDevice.findObject(new UiSelector().className("android.widget.ScrollView"));

        // TODO: clean up the following code with NotificationPages.goThroughNotificationPage()
        if (scrollView.isScrollable()) {
            // there should be a more button
            assertThat(leftControlButton.exists()).isFalse();
            assertThat(rightControlButton.exists()).isFalse();
            assertThat(moreButton.exists()).isTrue();

            while (moreButton.exists()) {
                moreButton.click();
                Thread.sleep(2000);
            }
            assertThat(leftControlButton.exists()).isTrue();
            assertThat(rightControlButton.exists()).isTrue();
            assertThat(moreButton.exists()).isFalse();
        } else {
            // fix the flaky test where test fails due to only moreButton exists
            int clickCount = 10;
            while (moreButton.exists() && clickCount-- > 0) {
                moreButton.click();
                Thread.sleep(2000);
            }
            assertThat(leftControlButton.exists()).isTrue();
            assertThat(rightControlButton.exists()).isTrue();
            assertThat(moreButton.exists()).isFalse();
        }
    }

    private void startActivity(boolean isEUActivity) {
        Intent intent = new Intent(NOTIFICATION_TEST_PACKAGE);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("isEUDevice", isEUActivity);
        mContext.startActivity(intent);

        sDevice.wait(Until.hasObject(By.pkg(NOTIFICATION_TEST_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
    }

    private String getString(int resourceId) {
        return ApplicationProvider.getApplicationContext().getResources().getString(resourceId);
    }

    private UiObject getElement(int resId) {
        return sDevice.findObject(new UiSelector().text(getString(resId)));
    }

    private boolean isDefaultBrowserOpenedAfterClicksOnTheBottomOfSentence(
            String packageNameOfDefaultBrowser, UiObject sentence, int countOfClicks)
            throws Exception {
        int right = sentence.getBounds().right,
                bottom = sentence.getBounds().bottom,
                left = sentence.getBounds().left;
        for (int x = left; x < right; x += (right - left) / countOfClicks) {
            sDevice.click(x, bottom - 2);
            Thread.sleep(200);
        }

        if (!sentence.exists()) {
            sDevice.pressBack();
            ApkTestUtil.killDefaultBrowserPkgName(sDevice, mContext);
            return true;
        }

        return false;
    }
}