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

Commit 5d99e14c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Switch fragment tests to use TestableLooper"

parents cc862820 28d5d220
Loading
Loading
Loading
Loading
+57 −42
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;
import android.widget.FrameLayout;


import com.android.systemui.utils.TestableLooper;
import com.android.systemui.utils.ViewUtils;
import com.android.systemui.utils.ViewUtils;
import com.android.systemui.utils.leaks.LeakCheckedTest;
import com.android.systemui.utils.leaks.LeakCheckedTest;


@@ -59,12 +60,14 @@ public abstract class FragmentTestCase extends LeakCheckedTest {
    }
    }


    @Before
    @Before
    public void setupFragment() throws IllegalAccessException, InstantiationException {
    public void setupFragment() throws Exception {
        mView = new FrameLayout(mContext);
        mView = new FrameLayout(mContext);
        mView.setId(VIEW_ID);
        mView.setId(VIEW_ID);
        mHandler = new Handler(Looper.getMainLooper());

        TestableLooper.get(this).runWithLooper(() -> {
            mHandler = new Handler();

            mFragment = mCls.newInstance();
            mFragment = mCls.newInstance();
        postAndWait(() -> {
            mFragments = FragmentController.createController(new HostCallbacks());
            mFragments = FragmentController.createController(new HostCallbacks());
            mFragments.attachHost(null);
            mFragments.attachHost(null);
            mFragments.getFragmentManager().beginTransaction()
            mFragments.getFragmentManager().beginTransaction()
@@ -73,30 +76,39 @@ public abstract class FragmentTestCase extends LeakCheckedTest {
        });
        });
    }
    }


    private String hex(Looper looper) {
        return Integer.toHexString(System.identityHashCode(looper));
    }

    @After
    @After
    public void tearDown() {
    public void tearDown() throws Exception {
        if (mFragments != null) {
        if (mFragments != null) {
            // Set mFragments to null to let it know not to destroy.
            // Set mFragments to null to let it know not to destroy.
            postAndWait(() -> mFragments.dispatchDestroy());
            TestableLooper.get(this).runWithLooper(() -> mFragments.dispatchDestroy());
        }
        }
    }
    }


    @Test
    @Test
    public void testCreateDestroy() {
    public void testCreateDestroy() {
        postAndWait(() -> mFragments.dispatchCreate());
        mFragments.dispatchCreate();
        processAllMessages();
        destroyFragments();
        destroyFragments();
    }
    }


    @Test
    @Test
    public void testStartStop() {
    public void testStartStop() {
        postAndWait(() -> mFragments.dispatchStart());
        mFragments.dispatchStart();
        postAndWait(() -> mFragments.dispatchStop());
        processAllMessages();
        mFragments.dispatchStop();
        processAllMessages();
    }
    }


    @Test
    @Test
    public void testResumePause() {
    public void testResumePause() {
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        postAndWait(() -> mFragments.dispatchPause());
        processAllMessages();
        mFragments.dispatchPause();
        processAllMessages();
    }
    }


    @Test
    @Test
@@ -105,24 +117,18 @@ public abstract class FragmentTestCase extends LeakCheckedTest {
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
                LayoutParams.TYPE_SYSTEM_ALERT,
                LayoutParams.TYPE_SYSTEM_ALERT,
                0, PixelFormat.TRANSLUCENT);
                0, PixelFormat.TRANSLUCENT);
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        processAllMessages();
        attachFragmentToWindow();
        attachFragmentToWindow();
        detachFragmentToWindow();
        detachFragmentToWindow();
        postAndWait(() -> mFragments.dispatchPause());
        mFragments.dispatchPause();
    }
        processAllMessages();

    protected void attachFragmentToWindow() {
        ViewUtils.attachView(mView);
    }

    protected void detachFragmentToWindow() {
        ViewUtils.detachView(mView);
    }
    }


    @Test
    @Test
    public void testRecreate() {
    public void testRecreate() {
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        postAndWait(() -> {
        processAllMessages();
        mFragments.dispatchPause();
        mFragments.dispatchPause();
        Parcelable p = mFragments.saveAllState();
        Parcelable p = mFragments.saveAllState();
        mFragments.dispatchDestroy();
        mFragments.dispatchDestroy();
@@ -131,28 +137,37 @@ public abstract class FragmentTestCase extends LeakCheckedTest {
        mFragments.attachHost(null);
        mFragments.attachHost(null);
        mFragments.restoreAllState(p, (FragmentManagerNonConfig) null);
        mFragments.restoreAllState(p, (FragmentManagerNonConfig) null);
        mFragments.dispatchResume();
        mFragments.dispatchResume();
        });
        processAllMessages();
    }
    }


    @Test
    @Test
    public void testMultipleResumes() {
    public void testMultipleResumes() {
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        postAndWait(() -> mFragments.dispatchStop());
        processAllMessages();
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchStop();
        processAllMessages();
        mFragments.dispatchResume();
        processAllMessages();
    }
    }


    protected void destroyFragments() {
    protected void attachFragmentToWindow() {
        postAndWait(() -> mFragments.dispatchDestroy());
        ViewUtils.attachView(mView);
        mFragments = null;
        TestableLooper.get(this).processMessages(1);
    }
    }


    protected void postAndWait(Runnable r) {
    protected void detachFragmentToWindow() {
        mHandler.post(r);
        ViewUtils.detachView(mView);
        waitForFragments();
        TestableLooper.get(this).processMessages(1);
    }

    protected void destroyFragments() {
        mFragments.dispatchDestroy();
        processAllMessages();
        mFragments = null;
    }
    }


    protected void waitForFragments() {
    protected void processAllMessages() {
        waitForIdleSync(mHandler);
        TestableLooper.get(this).processAllMessages();
    }
    }


    private View findViewById(int id) {
    private View findViewById(int id) {
+16 −10
Original line number Original line Diff line number Diff line
@@ -14,17 +14,23 @@


package com.android.systemui.qs;
package com.android.systemui.qs;


import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mock;


import android.os.Looper;

import com.android.systemui.Dependency;
import com.android.systemui.Dependency;
import com.android.systemui.FragmentTestCase;
import com.android.systemui.FragmentTestCase;
import com.android.systemui.R;
import com.android.systemui.R;
import com.android.systemui.SysUIRunner;
import com.android.systemui.statusbar.phone.QSTileHost;
import com.android.systemui.statusbar.phone.QSTileHost;
import com.android.systemui.statusbar.phone.QuickStatusBarHeader;
import com.android.systemui.statusbar.phone.QuickStatusBarHeader;
import com.android.systemui.statusbar.phone.StatusBarIconController;
import com.android.systemui.statusbar.phone.StatusBarIconController;
import com.android.systemui.statusbar.policy.UserSwitcherController;
import com.android.systemui.statusbar.policy.UserSwitcherController;
import com.android.systemui.tuner.TunerService;
import com.android.systemui.tuner.TunerService;
import com.android.systemui.util.LayoutInflaterBuilder;
import com.android.systemui.util.LayoutInflaterBuilder;
import com.android.systemui.utils.TestableLooper;
import com.android.systemui.utils.TestableLooper.RunWithLooper;


import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
@@ -37,7 +43,8 @@ import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout;


@RunWith(AndroidJUnit4.class)
@RunWith(SysUIRunner.class)
@RunWithLooper(setAsMainLooper = true)
public class QSFragmentTest extends FragmentTestCase {
public class QSFragmentTest extends FragmentTestCase {


    public QSFragmentTest() {
    public QSFragmentTest() {
@@ -53,32 +60,31 @@ public class QSFragmentTest extends FragmentTestCase {
                        .replace("TextClock", View.class)
                        .replace("TextClock", View.class)
                        .build());
                        .build());


        injectTestDependency(Dependency.BG_LOOPER, Looper.getMainLooper());
        injectTestDependency(Dependency.BG_LOOPER, TestableLooper.get(this).getLooper());
        injectMockDependency(UserSwitcherController.class);
        injectMockDependency(UserSwitcherController.class);
        injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES);
        injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES);
    }
    }


    @Test
    @Test
    public void testListening() {
    public void testListening() {
        assertEquals(Looper.myLooper(), Looper.getMainLooper());
        QSFragment qs = (QSFragment) mFragment;
        QSFragment qs = (QSFragment) mFragment;
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        QSTileHost host = new QSTileHost(mContext, null,
        processAllMessages();
                mock(StatusBarIconController.class));
        QSTileHost host = new QSTileHost(mContext, null, mock(StatusBarIconController.class));
        qs.setHost(host);
        qs.setHost(host);
        Handler h = new Handler((Looper) Dependency.get(Dependency.BG_LOOPER));


        qs.setListening(true);
        qs.setListening(true);
        waitForIdleSync(h);
        processAllMessages();


        qs.setListening(false);
        qs.setListening(false);
        waitForIdleSync(h);
        processAllMessages();


        // Manually push header through detach so it can handle standard cleanup it does on
        // Manually push header through detach so it can handle standard cleanup it does on
        // removed from window.
        // removed from window.
        ((QuickStatusBarHeader) qs.getView().findViewById(R.id.header)).onDetachedFromWindow();
        ((QuickStatusBarHeader) qs.getView().findViewById(R.id.header)).onDetachedFromWindow();


        host.destroy();
        host.destroy();
        // Ensure the tuner cleans up its persistent listeners.
        processAllMessages();
        Dependency.get(TunerService.class).destroy();
    }
    }
}
}
+8 −0
Original line number Original line Diff line number Diff line
@@ -14,12 +14,18 @@


package com.android.systemui.statusbar;
package com.android.systemui.statusbar;


import com.android.systemui.SysUIRunner;
import com.android.systemui.utils.TestableLooper;
import com.android.systemui.utils.TestableLooper.RunWithLooper;
import com.android.systemui.utils.ViewUtils;
import com.android.systemui.utils.ViewUtils;
import com.android.systemui.utils.leaks.LeakCheckedTest;
import com.android.systemui.utils.leaks.LeakCheckedTest;


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


@RunWith(SysUIRunner.class)
@RunWithLooper
public class NotificationMenuRowTest extends LeakCheckedTest {
public class NotificationMenuRowTest extends LeakCheckedTest {


    @Before
    @Before
@@ -31,6 +37,8 @@ public class NotificationMenuRowTest extends LeakCheckedTest {
    public void testAttachDetach() {
    public void testAttachDetach() {
        NotificationMenuRow row = new NotificationMenuRow(mContext);
        NotificationMenuRow row = new NotificationMenuRow(mContext);
        ViewUtils.attachView(row);
        ViewUtils.attachView(row);
        TestableLooper.get(this).processAllMessages();
        ViewUtils.detachView(row);
        ViewUtils.detachView(row);
        TestableLooper.get(this).processAllMessages();
    }
    }
}
}
+11 −3
Original line number Original line Diff line number Diff line
@@ -25,16 +25,21 @@ import android.view.ViewPropertyAnimator;


import com.android.systemui.FragmentTestCase;
import com.android.systemui.FragmentTestCase;
import com.android.systemui.R;
import com.android.systemui.R;
import com.android.systemui.SysUIRunner;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.policy.KeyguardMonitor;
import com.android.systemui.statusbar.policy.KeyguardMonitor;
import com.android.systemui.statusbar.policy.NetworkController;
import com.android.systemui.statusbar.policy.NetworkController;
import com.android.systemui.statusbar.policy.SecurityController;
import com.android.systemui.statusbar.policy.SecurityController;
import com.android.systemui.tuner.TunerService;
import com.android.systemui.tuner.TunerService;
import com.android.systemui.utils.TestableLooper.RunWithLooper;


import org.junit.Before;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.Mockito;


@RunWith(SysUIRunner.class)
@RunWithLooper(setAsMainLooper = true)
public class CollapsedStatusBarFragmentTest extends FragmentTestCase {
public class CollapsedStatusBarFragmentTest extends FragmentTestCase {


    private NotificationIconAreaController mMockNotificiationAreaController;
    private NotificationIconAreaController mMockNotificiationAreaController;
@@ -59,7 +64,8 @@ public class CollapsedStatusBarFragmentTest extends FragmentTestCase {


    @Test
    @Test
    public void testDisableNone() throws Exception {
    public void testDisableNone() throws Exception {
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        processAllMessages();


        CollapsedStatusBarFragment fragment = (CollapsedStatusBarFragment) mFragment;
        CollapsedStatusBarFragment fragment = (CollapsedStatusBarFragment) mFragment;
        fragment.initNotificationIconArea(mMockNotificiationAreaController);
        fragment.initNotificationIconArea(mMockNotificiationAreaController);
@@ -71,7 +77,8 @@ public class CollapsedStatusBarFragmentTest extends FragmentTestCase {


    @Test
    @Test
    public void testDisableSystemInfo() throws Exception {
    public void testDisableSystemInfo() throws Exception {
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        processAllMessages();


        CollapsedStatusBarFragment fragment = (CollapsedStatusBarFragment) mFragment;
        CollapsedStatusBarFragment fragment = (CollapsedStatusBarFragment) mFragment;
        fragment.initNotificationIconArea(mMockNotificiationAreaController);
        fragment.initNotificationIconArea(mMockNotificiationAreaController);
@@ -88,7 +95,8 @@ public class CollapsedStatusBarFragmentTest extends FragmentTestCase {


    @Test
    @Test
    public void testDisableNotifications() throws Exception {
    public void testDisableNotifications() throws Exception {
        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        processAllMessages();


        CollapsedStatusBarFragment fragment = (CollapsedStatusBarFragment) mFragment;
        CollapsedStatusBarFragment fragment = (CollapsedStatusBarFragment) mFragment;
        fragment.initNotificationIconArea(mMockNotificiationAreaController);
        fragment.initNotificationIconArea(mMockNotificiationAreaController);
+7 −1
Original line number Original line Diff line number Diff line
@@ -24,13 +24,18 @@ import android.view.WindowManager;


import com.android.systemui.Dependency;
import com.android.systemui.Dependency;
import com.android.systemui.FragmentTestCase;
import com.android.systemui.FragmentTestCase;
import com.android.systemui.SysUIRunner;
import com.android.systemui.recents.Recents;
import com.android.systemui.recents.Recents;
import com.android.systemui.stackdivider.Divider;
import com.android.systemui.stackdivider.Divider;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.utils.TestableLooper.RunWithLooper;


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


@RunWith(SysUIRunner.class)
@RunWithLooper(setAsMainLooper = true)
public class NavigationBarFragmentTest extends FragmentTestCase {
public class NavigationBarFragmentTest extends FragmentTestCase {


    public NavigationBarFragmentTest() {
    public NavigationBarFragmentTest() {
@@ -56,7 +61,8 @@ public class NavigationBarFragmentTest extends FragmentTestCase {
    public void testHomeLongPress() {
    public void testHomeLongPress() {
        NavigationBarFragment navigationBarFragment = (NavigationBarFragment) mFragment;
        NavigationBarFragment navigationBarFragment = (NavigationBarFragment) mFragment;


        postAndWait(() -> mFragments.dispatchResume());
        mFragments.dispatchResume();
        processAllMessages();
        navigationBarFragment.onHomeLongClick(navigationBarFragment.getView());
        navigationBarFragment.onHomeLongClick(navigationBarFragment.getView());
    }
    }


Loading