/*
 * Copyright 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 androidx.appcompat.app;

import static androidx.appcompat.testutils.TestUtils.executeShellCommandAndFind;

import static org.junit.Assert.assertFalse;

import android.app.Instrumentation;
import android.view.KeyEvent;

import androidx.appcompat.testutils.TestUtils.Predicate;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.MediumTest;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;

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

@RunWith(AndroidJUnit4.class)
public class EmptyOptionsPanelTest {
    @Rule
    public final ActivityTestRule<EmptyOptionsPanelActivity> mActivityTestRule;

    private Instrumentation mInstrumentation;
    private EmptyOptionsPanelActivity mActivity;

    public EmptyOptionsPanelTest() {
        mActivityTestRule = new ActivityTestRule<>(EmptyOptionsPanelActivity.class);
    }

    @Before
    public void setup() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mActivity = mActivityTestRule.getActivity();
    }

    // executeShellCommandAndFind() is only available on API 21+ and for some reason the shell
    // command doesn't execute correctly below API 26, so suppress everything below that.
    @SdkSuppress(minSdkVersion = 26)
    @Test
    @MediumTest
    public void testEmptyOptionsPanelNotShown() throws Exception {
        mActivity.setShouldPopulateOptionsMenu(false);

        mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
        mInstrumentation.waitForIdleSync();

        assertFalse("Sub-panel was added after first KEYCODE_MENU",
                executeShellCommandAndFind("wm dump", new Predicate<String>() {
                    public boolean test(String t) {
                        return t.contains(
                                "SubPanel:" + mActivity.getComponentName().flattenToString());
                    }
                }));

        mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
        mInstrumentation.waitForIdleSync();

        assertFalse("Sub-panel was added after second KEYCODE_MENU",
                executeShellCommandAndFind("wm dump", new Predicate<String>() {
                    public boolean test(String t) {
                        return t.contains(
                                "SubPanel:" + mActivity.getComponentName().flattenToString());
                    }
                }));
    }
}

@@ -647,90 +647,8 @@
 p;%0A%0A
-import static androidx.appcompat.testutils.TestUtils.executeShellCommandAndFind;%0A%0A
 impo
@@ -680,21 +680,22 @@
 t.assert
-F
+Equ
 als
-e
 ;%0A%0Aimpor
@@ -755,17 +755,16 @@
 yEvent;%0A
-%0A
 import a
@@ -773,50 +773,41 @@
 roid
-x.appcompat.testutils.TestUtils.Predicate;
+.view.inspector.WindowInspector;%0A
 %0Aimp
@@ -1707,193 +1707,8 @@
  %7D%0A%0A
-    // executeShellCommandAndFind() is only available on API 21+ and for some reason the shell%0A    // command doesn't execute correctly below API 26, so suppress everything below that.%0A
     
@@ -1741,9 +1741,9 @@
  = 2
-6
+9
 )%0A  
@@ -2013,377 +2013,374 @@
     
-assertFalse(%22Sub-panel was added after first KEYCODE_MENU%22,%0A                executeShellCommandAndFind(%22wm dump%22, new Predicate%3CString%3E() %7B%0A                    public boolean test(String t) %7B%0A                        return t.contains(%0A                                %22SubPanel:%22 + mActivity.getComponentName().flattenToString());%0A                    %7D%0A                %7D
+// UiAutomator is flaky, so instead we'll just check how many windows are showing. This%0A        // is... not a great way to test this behavior, but we don't have any other way to hook%0A        // into an empty options panel.%0A        assertEquals(%22Sub-panel should not be added after first KEYCODE_MENU%22,%0A                1, WindowInspector.getGlobalWindowViews().size(
 ));%0A
@@ -2510,13 +2510,14 @@
 sert
-F
+Equ
 als
-e
 (%22Su
@@ -2528,11 +2528,21 @@
 nel 
-was
+should not be
  add
@@ -2592,301 +2592,55 @@
     
-executeShellCommandAndFind(%22wm dump%22, new Predicate%3CString%3E() %7B%0A                    public boolean test(String t) %7B%0A                        return t.contains(%0A                                %22SubPanel:%22 + mActivity.getComponentName().flattenToString());%0A                    %7D%0A                %7D
+1, WindowInspector.getGlobalWindowViews().size(
 ));%0A

Migrate away from UiAutomator