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

Commit d280aa4a authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix a complications memory leak." into main

parents fe636f04 335533f4
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ package com.android.systemui.complication;

import static android.service.dreams.Flags.FLAG_DREAMS_V2;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyMap;
@@ -287,6 +289,29 @@ public class ComplicationHostViewControllerTest extends SysuiTestCase {
        verify(mComplicationViewModelLiveData).removeObserver(eq(observer));
    }

    @Test
    public void testComplicationsRemovedOnDestroy() {
        mController.onViewAttached();
        final Observer<Collection<ComplicationViewModel>> observer =
                captureComplicationViewModelsObserver();

        final HashSet<ComplicationViewModel> complications = new HashSet<>(
                Collections.singletonList(mComplicationViewModel));
        observer.onChanged(complications);

        // Assert that the controller has 1 complication.
        assertThat(mController.getComplicationCount()).isEqualTo(1);

        // Destroy the controller.
        mController.destroy();

        // Assert that the controller has 0 complications.
        assertThat(mController.getComplicationCount()).isEqualTo(0);

        // Verify that the complication was removed from the layout engine.
        verify(mLayoutEngine).removeComplication(mComplicationId);
    }

    private Observer<Collection<ComplicationViewModel>> captureComplicationViewModelsObserver() {
        verify(mComplicationViewModelLiveData).observe(eq(mLifecycleOwner),
                mObserverCaptor.capture());
+13 −0
Original line number Diff line number Diff line
@@ -204,6 +204,19 @@ public class ComplicationHostViewController extends ViewController<ConstraintLay
                mComplicationViewModelObserver);
    }

    @Override
    public void destroy() {
        mComplications.forEach((id, viewHolder) ->
                mLayoutEngine.removeComplication(id));
        mComplications.clear();
        super.destroy();
    }

    @VisibleForTesting
    protected int getComplicationCount() {
        return mComplications.size();
    }

    /**
     * Exposes the associated {@link View}. Since this {@link View} is instantiated through dagger
     * in the {@link ComplicationHostViewController} constructor, the