<commit_before>/*
 * Copyright (C) 2016 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.test.uiautomator;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;

import android.app.Instrumentation;
import android.app.UiAutomation;
import android.os.Build;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.mockito.Mockito;

public class UiDeviceTests {

    @Test
    public void testGetUiAutomation_withDefaultFlags() {
        // Get the default flags
        final int defaultFlags = Configurator.getInstance().getUiAutomationFlags();

        // Setup mocks
        Instrumentation mockInstrumentation = Mockito.mock(Instrumentation.class);

        // Can't mock UiAutomation, so return a real instance
        UiAutomation automation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        doReturn(automation).when(mockInstrumentation).getUiAutomation(anyInt());
        doReturn(automation).when(mockInstrumentation).getUiAutomation();

        // Get a UiDevice instance to test and use it to obtain a UiAutomation instance
        UiDevice device = Mockito.spy(new UiDevice(mockInstrumentation));
        UiAutomation uiAutomation = device.getUiAutomation();

        // Verify that the UiAutomation instance was obtained with the right arguments
        if (UiDevice.API_LEVEL_ACTUAL > Build.VERSION_CODES.M) {
            // On N+, we should obtain a UiAutomation instance using the default flags
            Mockito.verify(mockInstrumentation, atLeastOnce()).getUiAutomation(eq(defaultFlags));
        } else {
            // Prior to N, flags should be ignored
            Mockito.verify(mockInstrumentation, atLeastOnce()).getUiAutomation();
        }
    }

    @Test
    public void testGetUiAutomation_withCustomFlags() {
        // Remember the default flags
        final int defaultFlags = Configurator.getInstance().getUiAutomationFlags();

        // Set some custom flags
        final int flags = 5;
        Configurator.getInstance().setUiAutomationFlags(flags);

        // Setup mocks
        Instrumentation mockInstrumentation = Mockito.mock(Instrumentation.class);

        // Can't mock UiAutomation, so return a real instance
        UiAutomation automation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        doReturn(automation).when(mockInstrumentation).getUiAutomation(anyInt());
        doReturn(automation).when(mockInstrumentation).getUiAutomation();

        // Get a UiDevice instance to test and use it to obtain a UiAutomation instance
        UiDevice device = Mockito.spy(new UiDevice(mockInstrumentation));
        UiAutomation uiAutomation = device.getUiAutomation();

        // Verify that the UiAutomation instance was obtained with the right arguments
        if (UiDevice.API_LEVEL_ACTUAL > Build.VERSION_CODES.M) {
            // On N+, we should obtain a UiAutomation instance using the custom flags
            Mockito.verify(mockInstrumentation, atLeastOnce()).getUiAutomation(eq(flags));
        } else {
            // Prior to N, flags should be ignored
            Mockito.verify(mockInstrumentation, atLeastOnce()).getUiAutomation();
        }

        // Reset the flags
        Configurator.getInstance().setUiAutomationFlags(defaultFlags);
    }
}<commit_msg>Simplify and fix lint issues in UiDeviceTests.java<commit_after>/*
 * Copyright (C) 2016 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.test.uiautomator;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import android.app.Instrumentation;
import android.app.UiAutomation;
import android.os.Build;

import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UiDeviceTests {

    private Instrumentation mMockInstrumentation;
    private UiDevice mDevice;
    private int mDefaultFlags;

    @Before
    public void setUp() {
        mMockInstrumentation = mock(Instrumentation.class);
        UiAutomation automation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        doReturn(automation).when(mMockInstrumentation).getUiAutomation();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            doReturn(automation).when(mMockInstrumentation).getUiAutomation(anyInt());
        }
        mDevice = new UiDevice(mMockInstrumentation);
        mDefaultFlags = Configurator.getInstance().getUiAutomationFlags();
    }

    @After
    public void tearDown() {
        Configurator.getInstance().setUiAutomationFlags(mDefaultFlags);
    }

    @Test
    @SdkSuppress(maxSdkVersion = Build.VERSION_CODES.M)
    public void testGetUiAutomation_withoutFlags() {
        mDevice.getUiAutomation();
        // Verify that the UiAutomation instance was obtained without flags (prior to N).
        verify(mMockInstrumentation, atLeastOnce()).getUiAutomation();
    }

    @Test
    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
    public void testGetUiAutomation_withDefaultFlags() {
        mDevice.getUiAutomation();
        // Verify that the UiAutomation instance was obtained with default flags (N+).
        verify(mMockInstrumentation, atLeastOnce()).getUiAutomation(eq(mDefaultFlags));
    }

    @Test
    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
    public void testGetUiAutomation_withCustomFlags() {
        int customFlags = 5;
        Configurator.getInstance().setUiAutomationFlags(customFlags);
        mDevice.getUiAutomation();
        // Verify that the UiAutomation instance was obtained with custom flags (N+).
        verify(mMockInstrumentation, atLeastOnce()).getUiAutomation(eq(customFlags));
    }
}
{'fraction_non_alphanumeric': 0.08454038997214484, 'fraction_numerical': 0.0025069637883008357, 'mean_word_length': 3.612074502247913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966096', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1947, 'n_words': 537}