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

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

Merge "[KV] Refactor sendDataToTransport() and finishTask()"

parents ad5d61a0 0830f642
Loading
Loading
Loading
Loading
+24 −10
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.pm.PackageInfo;
import android.util.EventLog;
import android.util.Slog;

import com.android.internal.annotations.VisibleForTesting;
import com.android.server.EventLogTags;
import com.android.server.backup.BackupManagerService;
import com.android.server.backup.DataChangedJournal;
@@ -49,10 +50,19 @@ import java.util.List;
 */
// TODO: In KeyValueBackupTaskTest, remove direct assertions on logcat, observer or monitor and
//       verify calls to this object. Add these and more assertions to the test of this class.
class KeyValueBackupReporter {
    private static final String TAG = "KeyValueBackupTask";
@VisibleForTesting
public class KeyValueBackupReporter {
    @VisibleForTesting
    static final String TAG = "KeyValueBackupTask";
    private static final boolean DEBUG = BackupManagerService.DEBUG;
    private static final boolean MORE_DEBUG = BackupManagerService.MORE_DEBUG || true;
    @VisibleForTesting
    static final boolean MORE_DEBUG = BackupManagerService.MORE_DEBUG || true;

    static void onNewThread(String threadName) {
        if (DEBUG) {
            Slog.d(TAG, "Spinning thread " + threadName);
        }
    }

    private final BackupManagerService mBackupManagerService;
    private final IBackupObserver mObserver;
@@ -61,7 +71,7 @@ class KeyValueBackupReporter {
    KeyValueBackupReporter(
            BackupManagerService backupManagerService,
            IBackupObserver observer,
            IBackupManagerMonitor monitor) {
            @Nullable IBackupManagerMonitor monitor) {
        mBackupManagerService = backupManagerService;
        mObserver = observer;
        mMonitor = monitor;
@@ -73,6 +83,10 @@ class KeyValueBackupReporter {
        return mMonitor;
    }

    IBackupObserver getObserver() {
        return mObserver;
    }

    void onSkipBackup() {
        if (DEBUG) {
            Slog.d(TAG, "Skipping backup since one is already in progress");
@@ -237,10 +251,6 @@ class KeyValueBackupReporter {
        }
    }

    void onTruncateDataError() {
        Slog.w(TAG, "Unable to roll back");
    }

    void onSendDataToTransport(String packageName) {
        if (MORE_DEBUG) {
            Slog.v(TAG, "Sending non-empty data to transport for " + packageName);
@@ -357,7 +367,7 @@ class KeyValueBackupReporter {
        return (packageInfo != null) ? packageInfo.packageName : "no_package_yet";
    }

    void onRevertBackup() {
    void onRevertTask() {
        if (MORE_DEBUG) {
            Slog.i(TAG, "Reverting backup queue, re-staging everything");
        }
@@ -391,6 +401,10 @@ class KeyValueBackupReporter {
        Slog.w(TAG, "Failed to query transport name for pending init: " + e);
    }

    /**
     * This is a bit different from {@link #onTaskFinished()}, it's only called if there is no
     * full-backup requests associated with the key-value task.
     */
    void onBackupFinished(int status) {
        BackupObserverUtils.sendBackupFinished(mObserver, status);
    }
@@ -399,7 +413,7 @@ class KeyValueBackupReporter {
        Slog.d(TAG, "Starting full backups for: " + pendingFullBackups);
    }

    void onKeyValueBackupFinished() {
    void onTaskFinished() {
        Slog.i(TAG, "K/V backup pass finished");
    }
}
+175 −171

File changed.

Preview size limit exceeded, changes collapsed.

+80 −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.server.backup.keyvalue;

import static com.android.server.backup.keyvalue.KeyValueBackupReporter.TAG;
import static com.android.server.backup.testing.TestUtils.assertLogcat;

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

import android.app.backup.IBackupManagerMonitor;
import android.app.backup.IBackupObserver;
import android.platform.test.annotations.Presubmit;
import android.util.Log;

import com.android.server.backup.BackupManagerService;
import com.android.server.testing.FrameworkRobolectricTestRunner;
import com.android.server.testing.SystemLoaderPackages;
import com.android.server.testing.shadows.ShadowEventLog;
import com.android.server.testing.shadows.ShadowSlog;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.annotation.Config;

@RunWith(FrameworkRobolectricTestRunner.class)
@Config(
        manifest = Config.NONE,
        sdk = 26,
        shadows = {ShadowEventLog.class, ShadowSlog.class})
@SystemLoaderPackages({"com.android.server.backup"})
@Presubmit
public class KeyValueBackupReporterTest {
    @Mock private BackupManagerService mBackupManagerService;
    @Mock private IBackupObserver mObserver;
    @Mock private IBackupManagerMonitor mMonitor;

    private KeyValueBackupReporter mReporter;

    @Before
    public void setUp() throws Exception {
        mReporter = new KeyValueBackupReporter(mBackupManagerService, mObserver, mMonitor);
    }

    @Test
    public void testOnNewThread_logsCorrectly() throws Exception {
        KeyValueBackupReporter.onNewThread("foo");

        assertLogcat(TAG, Log.DEBUG);
    }

    @Test
    public void testGetMonitor_returnsMonitor() throws Exception {
        IBackupManagerMonitor monitor = mReporter.getMonitor();

        assertThat(monitor).isEqualTo(mMonitor);
    }

    @Test
    public void testGetObserver_returnsObserver() throws Exception {
        IBackupObserver observer = mReporter.getObserver();

        assertThat(observer).isEqualTo(mObserver);
    }
}
+338 −316

File changed.

Preview size limit exceeded, changes collapsed.

+30 −2
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat;

import static org.robolectric.Shadows.shadowOf;

import static java.util.stream.Collectors.toSet;

import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
@@ -36,6 +38,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.IntStream;

public class TestUtils {
    private static final long TIMEOUT_MS = 3000;
@@ -87,20 +90,45 @@ public class TestUtils {
        ShadowSystemClock.setCurrentTimeMillis(shadowLooper.getScheduler().getCurrentTime());
    }

    /** Reset logcat with {@link ShadowLog#reset()} before the test case. */
    /**
     * Reset logcat with {@link ShadowLog#reset()} before the test case if you do anything that uses
     * logcat before that.
     */
    public static void assertLogcatAtMost(String tag, int level) {
        assertThat(ShadowLog.getLogsForTag(tag).stream().allMatch(logItem -> logItem.type <= level))
                .named("All logs <= " + level)
                .isTrue();
    }

    /** Reset logcat with {@link ShadowLog#reset()} before the test case. */
    /**
     * Reset logcat with {@link ShadowLog#reset()} before the test case if you do anything that uses
     * logcat before that.
     */
    public static void assertLogcatAtLeast(String tag, int level) {
        assertThat(ShadowLog.getLogsForTag(tag).stream().anyMatch(logItem -> logItem.type >= level))
                .named("Any log >= " + level)
                .isTrue();
    }

    /**
     * Verifies that logcat has produced log items as specified per level in {@code logs} (with
     * repetition).
     *
     * <p>So, if you call {@code assertLogcat(TAG, Log.ERROR, Log.ERROR)}, you assert that there are
     * exactly 2 log items, each with level ERROR.
     *
     * <p>Reset logcat with {@link ShadowLog#reset()} before the test case if you do anything
     * that uses logcat before that.
     */
    public static void assertLogcat(String tag, int... logs) {
        assertThat(
                        ShadowLog.getLogsForTag(tag).stream()
                                .map(logItem -> logItem.type)
                                .collect(toSet()))
                .named("Log items (specified per level)")
                .containsExactly(IntStream.of(logs).boxed().toArray());
    }

    public static void assertLogcatContains(String tag, Predicate<ShadowLog.LogItem> predicate) {
        assertThat(ShadowLog.getLogsForTag(tag).stream().anyMatch(predicate)).isTrue();
    }
Loading