diff --git a/tests/componenttests/AndroidManifest.xml b/tests/componenttests/AndroidManifest.xml index fb6c26f09a8..90101627d17 100644 --- a/tests/componenttests/AndroidManifest.xml +++ b/tests/componenttests/AndroidManifest.xml @@ -16,7 +16,8 @@ + package="com.android.settings.tests.component" + android:sharedUserId="android.uid.systemui"> diff --git a/tests/componenttests/src/com/android/settings/notification/AppNotificationComponentTest.java b/tests/componenttests/src/com/android/settings/notification/AppNotificationComponentTest.java new file mode 100644 index 00000000000..e08a8acfc6b --- /dev/null +++ b/tests/componenttests/src/com/android/settings/notification/AppNotificationComponentTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2021 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.settings.notification; + +import static com.google.common.truth.Truth.assertThat; + +import android.app.Instrumentation; +import android.content.Intent; +import android.provider.Settings; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.Switch; + +import androidx.test.core.app.ActivityScenario; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.runner.lifecycle.Stage; + +import com.android.settings.R; +import com.android.settings.testutils.UiUtils; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class AppNotificationComponentTest { + private static final String TAG = + AppNotificationComponentTest.class.getSimpleName(); + private final Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation(); + private final String mNoSlientAppName = "com.google.android.dialer"; + + + @Rule + public ActivityScenarioRule + rule = new ActivityScenarioRule<>( + new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) + .putExtra(Settings.EXTRA_APP_PACKAGE, mNoSlientAppName) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); + + @Test + public void test_special_app_could_not_disable_notification() { + ActivityScenario ac = rule.getScenario(); + ac.onActivity( + activity -> { + View rv = activity.findViewById(R.id.recycler_view); + + UiUtils.waitUntilCondition(5000, + () -> rv.findViewById(R.id.main_switch_bar) != null); + + View mainSwitchBar = rv.findViewById(R.id.main_switch_bar); + + assertThat(mainSwitchBar.isEnabled()).isEqualTo(false); + + UiUtils.waitForActivitiesInStage(1000, Stage.RESUMED); + + for (int i = 0; i < ((ViewGroup) rv).getChildCount(); i++) { + if (((ViewGroup) rv).getChildAt(i) instanceof LinearLayout) { + Switch sWidget = rv.findViewById(R.id.switchWidget); + if (sWidget != null) { + assertThat(sWidget.isEnabled()).isEqualTo(false); + } + } + } + } + ); + } +} diff --git a/tests/componenttests/src/com/android/settings/testutils/UiUtils.java b/tests/componenttests/src/com/android/settings/testutils/UiUtils.java index a482add9dd7..481a7b2a71e 100644 --- a/tests/componenttests/src/com/android/settings/testutils/UiUtils.java +++ b/tests/componenttests/src/com/android/settings/testutils/UiUtils.java @@ -17,8 +17,11 @@ package com.android.settings.testutils; import android.app.Activity; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; -import androidx.test.platform.app.InstrumentationRegistry; +import androidx.fragment.app.FragmentActivity; import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; import androidx.test.runner.lifecycle.Stage; @@ -27,6 +30,7 @@ import java.util.Collection; import java.util.function.Supplier; public class UiUtils { + private static final String TAG = "UI_UTILS"; public static void waitUntilCondition(long timeoutInMillis, Supplier condition) { long start = System.nanoTime(); @@ -42,17 +46,48 @@ public class UiUtils { e.printStackTrace(); } } + if (System.nanoTime() - start >= (timeoutInMillis * 1000000)) { + Log.w(TAG, "Condition not match and timeout for waiting " + timeoutInMillis + "(ms)."); + } else { + Log.d(TAG, "Condition matched."); + } } public static boolean waitForActivitiesInStage(long timeoutInMillis, Stage stage) { final Collection activities = new ArrayList<>(); waitUntilCondition(Constants.ACTIVITY_LAUNCH_WAIT_TIMEOUT, () -> { - InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> activities.addAll( + activities.addAll( ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage( - Stage.RESUMED))); + Stage.RESUMED)); return activities.size() > 0; }); return activities.size() > 0; } + + public static void dumpView(View view) { + dumpViewRecursive(view, 0, 0, 0); + } + + public static View getFirstViewFromActivity(Activity activity) { + return ((FragmentActivity) activity).getSupportFragmentManager().getFragments().get( + 0).getView(); + } + + private static void dumpViewRecursive(View view, int layer, int index, int total) { + if (view instanceof ViewGroup) { + Log.i(TAG, "L[" + layer + "] PARENT -> " + (index + 1) + "/" + total + " >> " + + view.toString()); + System.out.println( + TAG + " L[" + layer + "] PARENT -> " + (index + 1) + "/" + total + " >> " + + view.toString()); + for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { + dumpViewRecursive(((ViewGroup) view).getChildAt(i), layer + 1, i + 1, + ((ViewGroup) view).getChildCount()); + } + } else { + Log.i(TAG, "L[" + layer + "] =END= -> " + (index + 1) + "/" + total + " >> " + + view.toString()); + } + } }