Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 46f01c16 authored by felkachang's avatar felkachang
Browse files

Fix flaky of DocumentLoaderTest by mock context

DocumentLoaderTest is impacted by the whole system stability. To
make the original test to be DocumentLoaderPerftest as LargeTest.
The default atest DocumentsUITests run DocumentLoaderPerftest.

To fix the flaky test by using mock Context, limit the context only
return the mocked ContentResolver that return InspectorProvider's
instance.

Fixes: 120431745
Bug: 121002105
Test: atest \
  DocumentsUITests:com.android.documentsui.inspector.DocumentLoaderTest
Test: adb shell am instrument -w -e class \
  com.android.documentsui.inspector.DocumentLoaderPerfTest,\
  com.android.documentsui.inspector.DocumentLoaderTest \
  com.android.documentsui.tests/androidx.test.runner.AndroidJUnitRunner

Test: adb shell am instrument -w \
  -e notAnnotation androidx.test.filters.LargeTest \
  -e class com.android.documentsui.inspector.DocumentLoaderPerfTest,\
  com.android.documentsui.inspector.DocumentLoaderTest \
  com.android.documentsui.tests/androidx.test.runner.AndroidJUnitRunner

Change-Id: I5cb0c1f98d96d81b22a26b00c7df67bc15334cf5
parent a9cfc1c5
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.documentsui;
import static android.provider.DocumentsContract.Document.FLAG_SUPPORTS_METADATA;
import static android.provider.DocumentsContract.Document.FLAG_SUPPORTS_SETTINGS;

import android.content.Context;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder;
@@ -77,6 +79,17 @@ public class InspectorProvider extends TestRootProvider {
        return c;
    }

    @Override
    public void attachInfo(Context context, ProviderInfo info) {
        // prevent the testing from Security Exception comes from DocumentsProvider
        info.exported = true;
        info.grantUriPermissions = true;
        info.writePermission = android.Manifest.permission.MANAGE_DOCUMENTS;
        info.readPermission = android.Manifest.permission.MANAGE_DOCUMENTS;

        super.attachInfo(context, info);
    }

    @Override
    public Cursor queryChildDocuments(String s, String[] projection, String s1)
            throws FileNotFoundException {
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.documentsui.inspector;

import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;

import org.junit.Before;

/**
 * This test just like DocumentLoaderTest except that this test runs in real context environment.
 */
@LargeTest
public class DocumentLoaderPerfTest extends DocumentLoaderTest {
    @Override
    protected Context prepareContentResolverSource() {
        return InstrumentationRegistry.getTargetContext();
    }
}
+23 −2
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
 */
package com.android.documentsui.inspector;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.media.ExifInterface;
@@ -24,7 +27,7 @@ import android.os.Looper;
import android.provider.DocumentsContract;
import android.test.suitebuilder.annotation.MediumTest;

import androidx.test.InstrumentationRegistry;
import androidx.test.rule.provider.ProviderTestRule;

import com.android.documentsui.InspectorProvider;
import com.android.documentsui.base.DocumentInfo;
@@ -35,6 +38,7 @@ import com.android.documentsui.testing.TestSupportLoaderManager;
import junit.framework.TestCase;

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

import java.util.concurrent.TimeUnit;
@@ -54,10 +58,15 @@ public class DocumentLoaderTest extends TestCase {
    private DataSupplier mLoader;
    private ContentResolver mResolver;

    @Rule
    private ProviderTestRule mProviderTestRule = new ProviderTestRule.Builder(
            InspectorProvider.class, InspectorProvider.AUTHORITY).build();

    @Before
    public void setUp() throws Exception {
        super.setUp();
        mContext = InstrumentationRegistry.getTargetContext();

        mContext = prepareContentResolverSource();
        mResolver = mContext.getContentResolver();
        mLoaderManager = new TestSupportLoaderManager();
        mLoader = new RuntimeDataSupplier(mContext, mLoaderManager);
@@ -67,6 +76,18 @@ public class DocumentLoaderTest extends TestCase {
        }
    }

    protected Context prepareContentResolverSource() {
        ContentResolver contentResolver = mProviderTestRule.getResolver();
        Context context = mock(Context.class);
        // inject ContentResolver
        when(context.getContentResolver()).thenReturn(contentResolver);
        // inject ContentResolver and prevent CursorLoader.loadInBackground from
        // NullPointerException
        when(context.getApplicationContext()).thenReturn(context);
        return context;

    }

    /**
     * Tests the loader using the Inspector Content provider. This test that we got valid info back
     * from the loader.