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

Commit baa38111 authored by Daniel Nishi's avatar Daniel Nishi
Browse files

Fix a bug where the last used times could be wrong.

If a newer UsageStats came along, it would wipe away
the older UsageStats' last used times because it was
newer and, therefore, a source of truth. This is an
improper assumption, however. The newer UsageStat
may cover a timeframe which does not even have a last
use and, thus, destroys valid data.

Bug: 34857041
Test: FrameworksCoreTest
Change-Id: I5111b0d5a184a31a0a8fbdf12984cc7ef90ccd6e
parent c78d74e3
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -138,13 +138,13 @@ public final class UsageStats implements Parcelable {
                    mPackageName + "' with UsageStats for package '" + right.mPackageName + "'.");
        }

        if (right.mBeginTimeStamp > mBeginTimeStamp) {
            // The incoming UsageStat begins after this one, so use its last time used fields
            // as the source of truth.
        // We use the mBeginTimeStamp due to a bug where UsageStats files can overlap with
        // regards to their mEndTimeStamp.
            mLastEvent = right.mLastEvent;
            mLastTimeUsed = right.mLastTimeUsed;
        if (right.mBeginTimeStamp > mBeginTimeStamp) {
            // Even though incoming UsageStat begins after this one, its last time used fields
            // may somehow be empty or chronologically preceding the older UsageStat.
            mLastEvent = Math.max(mLastEvent, right.mLastEvent);
            mLastTimeUsed = Math.max(mLastTimeUsed, right.mLastTimeUsed);
        }
        mBeginTimeStamp = Math.min(mBeginTimeStamp, right.mBeginTimeStamp);
        mEndTimeStamp = Math.max(mEndTimeStamp, right.mEndTimeStamp);
+2 −1
Original line number Diff line number Diff line
@@ -34,7 +34,8 @@ LOCAL_STATIC_JAVA_LIBRARIES := \
    espresso-core \
    ub-uiautomator \
    platform-test-annotations \
    compatibility-device-util
    compatibility-device-util \
    truth-prebuilt

LOCAL_JAVA_LIBRARIES := android.test.runner conscrypt telephony-common org.apache.http.legacy
LOCAL_PACKAGE_NAME := FrameworksCoreTests
+105 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 android.app.usage;

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

import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

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

@RunWith(AndroidJUnit4.class)
@SmallTest
public class UsageStatsTest {
    private UsageStats left;
    private UsageStats right;

    @Before
    public void setUp() throws Exception {
        left = new UsageStats();
        right = new UsageStats();
    }

    @Test
    public void testEarlierBeginTimeTakesPriorityOnAdd() {
        left.mPackageName = "com.test";
        left.mBeginTimeStamp = 100000;
        right.mPackageName = "com.test";
        right.mBeginTimeStamp = 99999;

        left.add(right);

        assertThat(left.getFirstTimeStamp()).isEqualTo(99999);
    }

    @Test
    public void testLaterEndTimeTakesPriorityOnAdd() {
        left.mPackageName = "com.test";
        left.mEndTimeStamp = 100000;
        right.mPackageName = "com.test";
        right.mEndTimeStamp = 100001;

        left.add(right);

        assertThat(left.getLastTimeStamp()).isEqualTo(100001);
    }

    @Test
    public void testLastUsedTimeIsOverriddenByLaterStats() {
        left.mPackageName = "com.test";
        left.mBeginTimeStamp = 100000;
        left.mLastTimeUsed = 200000;
        right.mPackageName = "com.test";
        right.mBeginTimeStamp = 100001;
        right.mLastTimeUsed = 200001;

        left.add(right);

        assertThat(left.getLastTimeUsed()).isEqualTo(200001);
    }

    @Test
    public void testLastUsedTimeIsNotOverriddenByLaterStatsIfUseIsEarlier() {
        left.mPackageName = "com.test";
        left.mBeginTimeStamp = 100000;
        left.mLastTimeUsed = 200000;
        right.mPackageName = "com.test";
        right.mBeginTimeStamp = 100001;
        right.mLastTimeUsed = 150000;

        left.add(right);

        assertThat(left.getLastTimeUsed()).isEqualTo(200000);
    }

    @Test
    public void testForegroundTimeIsSummed() {
        left.mPackageName = "com.test";
        left.mBeginTimeStamp = 100000;
        left.mTotalTimeInForeground = 10;
        right.mPackageName = "com.test";
        right.mBeginTimeStamp = 100001;
        right.mTotalTimeInForeground = 1;

        left.add(right);

        assertThat(left.getTotalTimeInForeground()).isEqualTo(11);
    }
}