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

Commit 081676ef authored by Winson Chung's avatar Winson Chung
Browse files

Wake task persister whenever a new task is added

- The creation of a new task will trigger writing the task to disk in
  several places.  However, when a task is added to the TaskPersister
  write queue (ie. in ActivityRecord.onParentChanged), the task may
  not yet have been added to the set of RecentTasks.

  The task persister, as a part of onPreProcessItem() will try to fetch
  the persistable task ids to write from RecentTasks, which may not have
  the new task, resulting in it skipping the write of the item (because
  the set of persistable tasks have not changed).

  The task is then not persisted until the next time a new item is added
  to the write queue (can repro by starting a couple tasks, then rebooting
  and opening recents again).

  Instead, we should ensure that whenever a task is added to the recent
  task list, we poke the task persister to ensure that it will have the
  latest set of persisted tasks.

Bug: 146014211
Bug: 141560745
Test: atest RecentTasksTest
Test: Open an app, go home, open another app, wait for the task persister
      delay and verify that the tasks are in recents
      (adb shell dumpsys activity recents)
      and also that the persisted tasks have been updated with all tasks
      (adb shell cat /data/system_de/0/persisted_taskIds.txt)

Change-Id: I95f80d5fe2ef8d6c3128562bd36d4c95cd582eba
parent c6ded22c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1144,6 +1144,7 @@ class RecentTasks {

        // Trim the set of tasks to the active set
        trimInactiveRecentTasks();
        notifyTaskPersisterLocked(task, false /* flush */);
    }

    /**
+45 −0
Original line number Diff line number Diff line
@@ -41,8 +41,12 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@@ -112,6 +116,7 @@ public class RecentTasksTest extends ActivityTestsBase {
    @Before
    public void setUp() throws Exception {
        mTaskPersister = new TestTaskPersister(mContext.getFilesDir());
        spyOn(mTaskPersister);
        mDisplay = mRootActivityContainer.getActivityDisplay(DEFAULT_DISPLAY);

        // Set the recent tasks we should use for testing in this class.
@@ -170,6 +175,46 @@ public class RecentTasksTest extends ActivityTestsBase {
        assertThat(mCallbacksRecorder.mRemoved).isEmpty();
    }

    @Test
    public void testPersister() {
        // Add some tasks, ensure the persister is woken
        mRecentTasks.add(mTasks.get(0));
        mRecentTasks.add(mTasks.get(1));
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(0)), anyBoolean());
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(1)), anyBoolean());
        reset(mTaskPersister);

        // Update a task, ensure the persister is woken
        mRecentTasks.add(mTasks.get(0));
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(0)), anyBoolean());
        reset(mTaskPersister);

        // Remove some tasks, ensure the persister is woken
        mRecentTasks.remove(mTasks.get(0));
        mRecentTasks.remove(mTasks.get(1));
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(0)), anyBoolean());
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(1)), anyBoolean());
        reset(mTaskPersister);
    }

    @Test
    public void testPersisterTrimmed() {
        mRecentTasks.setOnlyTestVisibleRange();

        // Limit the global maximum number of recent tasks to a fixed size
        mRecentTasks.setGlobalMaxNumTasks(1 /* globalMaxNumTasks */);

        mRecentTasks.add(mTasks.get(0));
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(0)), anyBoolean());
        reset(mTaskPersister);

        // Add N+1 tasks to ensure the previous task is trimmed
        mRecentTasks.add(mTasks.get(1));
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(0)), anyBoolean());
        verify(mTaskPersister, times(1)).wakeup(eq(mTasks.get(1)), anyBoolean());
        assertTrimmed(mTasks.get(0));
    }

    @Test
    public void testAddTasksNoMultiple_expectNoTrim() {
        // Add same non-multiple-task document tasks will remove the task (to re-add it) but not