[go: nahoru, domu]

Remove WorkManagerLiveDataTracker.

We now depend on Room 2.1.x, so it's no longer necessary.

Test: Ran existing tests.
Change-Id: I20f7722be6089cc8b879135f668ce0412b96bc56
diff --git a/work/workmanager/src/androidTest/java/androidx/work/impl/ObserveForeverTest.java b/work/workmanager/src/androidTest/java/androidx/work/impl/ObserveForeverTest.java
deleted file mode 100644
index 5c7541e..0000000
--- a/work/workmanager/src/androidTest/java/androidx/work/impl/ObserveForeverTest.java
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * Copyright 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 androidx.work.impl;
-//
-//import static org.hamcrest.CoreMatchers.is;
-//import static org.hamcrest.CoreMatchers.notNullValue;
-//import static org.hamcrest.MatcherAssert.assertThat;
-//
-//import androidx.annotation.Nullable;
-//import androidx.lifecycle.Observer;
-//import androidx.test.ext.junit.runners.AndroidJUnit4;
-//import androidx.test.filters.MediumTest;
-//import androidx.test.platform.app.InstrumentationRegistry;
-//import androidx.work.Configuration;
-//import androidx.work.ExistingWorkPolicy;
-//import androidx.work.OneTimeWorkRequest;
-//import androidx.work.WorkContinuation;
-//import androidx.work.WorkInfo;
-//import androidx.work.impl.utils.SynchronousExecutor;
-//import androidx.work.impl.utils.taskexecutor.InstantWorkTaskExecutor;
-//import androidx.work.worker.TestWorker;
-//
-//import org.junit.After;
-//import org.junit.Before;
-//import org.junit.Ignore;
-//import org.junit.Test;
-//import org.junit.runner.RunWith;
-//
-//import java.util.Collections;
-//import java.util.List;
-//import java.util.concurrent.CountDownLatch;
-//import java.util.concurrent.ExecutionException;
-//import java.util.concurrent.TimeUnit;
-//
-///**
-// * TODO remove after moving to Room 2.1.x.
-// * see: b/74477406 for details.
-// *
-// * This test suite is being @Ignored because observeForever() can no longer be called on a
-// * background thread after the move to 2.x.
-// */
-//@RunWith(AndroidJUnit4.class)
-//@MediumTest
-//@Ignore
-//public class ObserveForeverTest {
-//    private WorkManagerImpl mWorkManagerImpl;
-//    private final OneTimeWorkRequest mWork = new OneTimeWorkRequest.Builder(TestWorker.class)
-//            .addTag("foo")
-//            .setInitialDelay(1, TimeUnit.HOURS)
-//            .build();
-//
-//    @Before
-//    public void init() {
-//        Configuration configuration = new Configuration.Builder()
-//                .setExecutor(new SynchronousExecutor())
-//                .build();
-//        mWorkManagerImpl = new WorkManagerImpl(
-//                InstrumentationRegistry.getInstrumentation().getTargetContext(),
-//                configuration, new InstantWorkTaskExecutor());
-//        WorkManagerImpl.setDelegate(mWorkManagerImpl);
-//    }
-//
-//    @After
-//    public void tearDown() {
-//        WorkManagerImpl.setDelegate(null);
-//    }
-//
-//    @Test
-//    @Ignore
-//    public void observeForever_byTags() throws InterruptedException {
-//        LoggingObserver<List<WorkInfo>> observer = new LoggingObserver<>();
-//        observer.expectValue();
-//
-//        mWorkManagerImpl
-//                .getWorkInfosByTagLiveData("foo")
-//                .observeForever(observer);
-//        assertThat(observer.awaitNextValue(), is(Collections.<WorkInfo>emptyList()));
-//
-//        forceGc();
-//        observer.expectValue();
-//        mWorkManagerImpl.enqueue(mWork);
-//
-//        List<WorkInfo> received = observer.awaitNextValue();
-//        assertThat(received.size(), is(1));
-//        assertThat(received.get(0).getState(), is(WorkInfo.State.ENQUEUED));
-//    }
-//
-//    @Test
-//    @Ignore
-//    public void observeForever_byId() throws InterruptedException, ExecutionException {
-//        LoggingObserver<WorkInfo> observer = new LoggingObserver<>();
-//        observer.expectValue();
-//
-//        mWorkManagerImpl
-//                .getWorkInfoByIdLiveData(mWork.getId())
-//                .observeForever(observer);
-//
-//        mWorkManagerImpl.enqueue(mWork);
-//
-//        WorkInfo received = observer.awaitNextValue();
-//        assertThat(received, is(notNullValue()));
-//        assertThat(received.getState(), is(WorkInfo.State.ENQUEUED));
-//
-//        observer.expectValue();
-//        forceGc();
-//        mWorkManagerImpl.cancelAllWork().getResult().get();
-//
-//        assertThat(observer.awaitNextValue().getState(), is(WorkInfo.State.CANCELLED));
-//    }
-//
-//    @Test
-//    @Ignore
-//    public void observeForever_uniqueWork() throws InterruptedException {
-//        LoggingObserver<List<WorkInfo>> observer = new LoggingObserver<>();
-//        observer.expectValue();
-//
-//        mWorkManagerImpl
-//                .getWorkInfosForUniqueWorkLiveData("custom-id")
-//                .observeForever(observer);
-//        assertThat(observer.awaitNextValue(), is(Collections.<WorkInfo>emptyList()));
-//
-//        forceGc();
-//        observer.expectValue();
-//        mWorkManagerImpl.beginUniqueWork("custom-id",
-//                ExistingWorkPolicy.REPLACE,
-//                mWork).enqueue();
-//
-//        List<WorkInfo> received = observer.awaitNextValue();
-//        assertThat(received.size(), is(1));
-//        assertThat(received.get(0).getState(), is(WorkInfo.State.ENQUEUED));
-//    }
-//
-//    @Test
-//    @Ignore
-//    public void observeForever_workContinuation() throws InterruptedException {
-//        LoggingObserver<List<WorkInfo>> observer = new LoggingObserver<>();
-//        observer.expectValue();
-//
-//        WorkContinuation workContinuation = mWorkManagerImpl.beginWith(mWork);
-//        workContinuation.getWorkInfosLiveData().observeForever(observer);
-//
-//        assertThat(observer.awaitNextValue(), is(Collections.<WorkInfo>emptyList()));
-//
-//        forceGc();
-//
-//        observer.expectValue();
-//        workContinuation.enqueue();
-//
-//        List<WorkInfo> received = observer.awaitNextValue();
-//        assertThat(received.size(), is(1));
-//        assertThat(received.get(0).getState(), is(WorkInfo.State.ENQUEUED));
-//    }
-//
-//    private void forceGc() {
-//        Runtime.getRuntime().gc();
-//        Runtime.getRuntime().runFinalization();
-//        Runtime.getRuntime().gc();
-//        Runtime.getRuntime().runFinalization();
-//    }
-//
-//    static class LoggingObserver<T> implements Observer<T> {
-//        CountDownLatch mLatch;
-//        private T mValue;
-//
-//        void expectValue() {
-//            if (mLatch != null) {
-//                throw new IllegalStateException("You've not consumed previous value yet");
-//            }
-//            mLatch = new CountDownLatch(1);
-//        }
-//
-//        T awaitNextValue() throws InterruptedException {
-//            assertThat(mLatch.await(10, TimeUnit.SECONDS), is(true));
-//            mLatch = null;
-//            return mValue;
-//        }
-//
-//        @Override
-//        public void onChanged(@Nullable T t) {
-//            if (mLatch == null) {
-//                throw new IllegalStateException("Not expecting a value yet");
-//            }
-//            mValue = t;
-//            mLatch.countDown();
-//        }
-//    }
-//}
-
diff --git a/work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java b/work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java
index e5e7b21..e5120d2 100644
--- a/work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java
+++ b/work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java
@@ -81,8 +81,6 @@
     private Preferences mPreferences;
     private boolean mForceStopRunnableCompleted;
     private BroadcastReceiver.PendingResult mRescheduleReceiverResult;
-    // TODO remove after moving to X: b/74477406
-    private final WorkManagerLiveDataTracker mLiveDataTracker = new WorkManagerLiveDataTracker();
 
     private static WorkManagerImpl sDelegatedInstance = null;
     private static WorkManagerImpl sDefaultInstance = null;
@@ -462,7 +460,7 @@
         WorkSpecDao dao = mWorkDatabase.workSpecDao();
         LiveData<List<WorkSpec.WorkInfoPojo>> inputLiveData =
                 dao.getWorkStatusPojoLiveDataForIds(Collections.singletonList(id.toString()));
-        LiveData<WorkInfo> deduped = LiveDataUtils.dedupedMappedLiveDataFor(inputLiveData,
+        return LiveDataUtils.dedupedMappedLiveDataFor(inputLiveData,
                 new Function<List<WorkSpec.WorkInfoPojo>, WorkInfo>() {
                     @Override
                     public WorkInfo apply(List<WorkSpec.WorkInfoPojo> input) {
@@ -474,7 +472,6 @@
                     }
                 },
                 mWorkTaskExecutor);
-        return mLiveDataTracker.track(deduped);
     }
 
     @Override
@@ -489,11 +486,10 @@
         WorkSpecDao workSpecDao = mWorkDatabase.workSpecDao();
         LiveData<List<WorkSpec.WorkInfoPojo>> inputLiveData =
                 workSpecDao.getWorkStatusPojoLiveDataForTag(tag);
-        LiveData<List<WorkInfo>> deduped = LiveDataUtils.dedupedMappedLiveDataFor(
+        return LiveDataUtils.dedupedMappedLiveDataFor(
                 inputLiveData,
                 WorkSpec.WORK_INFO_MAPPER,
                 mWorkTaskExecutor);
-        return mLiveDataTracker.track(deduped);
     }
 
     @Override
@@ -509,11 +505,10 @@
         WorkSpecDao workSpecDao = mWorkDatabase.workSpecDao();
         LiveData<List<WorkSpec.WorkInfoPojo>> inputLiveData =
                 workSpecDao.getWorkStatusPojoLiveDataForName(name);
-        LiveData<List<WorkInfo>> deduped = LiveDataUtils.dedupedMappedLiveDataFor(
+        return LiveDataUtils.dedupedMappedLiveDataFor(
                 inputLiveData,
                 WorkSpec.WORK_INFO_MAPPER,
                 mWorkTaskExecutor);
-        return mLiveDataTracker.track(deduped);
     }
 
     @Override
@@ -529,11 +524,10 @@
         WorkSpecDao dao = mWorkDatabase.workSpecDao();
         LiveData<List<WorkSpec.WorkInfoPojo>> inputLiveData =
                 dao.getWorkStatusPojoLiveDataForIds(workSpecIds);
-        LiveData<List<WorkInfo>> deduped = LiveDataUtils.dedupedMappedLiveDataFor(
+        return LiveDataUtils.dedupedMappedLiveDataFor(
                 inputLiveData,
                 WorkSpec.WORK_INFO_MAPPER,
                 mWorkTaskExecutor);
-        return mLiveDataTracker.track(deduped);
     }
 
     /**
diff --git a/work/workmanager/src/main/java/androidx/work/impl/WorkManagerLiveDataTracker.java b/work/workmanager/src/main/java/androidx/work/impl/WorkManagerLiveDataTracker.java
deleted file mode 100644
index 2357429..0000000
--- a/work/workmanager/src/main/java/androidx/work/impl/WorkManagerLiveDataTracker.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright 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 androidx.work.impl;
-
-import androidx.annotation.Nullable;
-import androidx.annotation.VisibleForTesting;
-import androidx.lifecycle.LiveData;
-import androidx.lifecycle.MediatorLiveData;
-import androidx.lifecycle.Observer;
-
-import java.util.Collections;
-import java.util.IdentityHashMap;
-import java.util.Set;
-
-/**
- * Utility class that create LiveData instances which stay in memory as long as there is an
- * active observer.
- * see: b/74477406 for details.
- */
-class WorkManagerLiveDataTracker {
-    @VisibleForTesting
-    final Set<LiveData> mLiveDataSet = Collections.newSetFromMap(
-            new IdentityHashMap<LiveData, Boolean>());
-
-    public <T> LiveData<T> track(LiveData<T> other) {
-        return new TrackedLiveData<>(this, other);
-    }
-
-    void onActive(LiveData liveData) {
-        mLiveDataSet.add(liveData);
-    }
-
-    void onInactive(LiveData liveData) {
-        mLiveDataSet.remove(liveData);
-    }
-
-    static class TrackedLiveData<T> extends MediatorLiveData<T> {
-        private final WorkManagerLiveDataTracker mContainer;
-        TrackedLiveData(WorkManagerLiveDataTracker container, LiveData<T> wrapped) {
-            mContainer = container;
-            addSource(wrapped, new Observer<T>() {
-                @Override
-                public void onChanged(@Nullable T t) {
-                    setValue(t);
-                }
-            });
-        }
-
-        @Override
-        protected void onActive() {
-            super.onActive();
-            mContainer.onActive(this);
-        }
-
-
-        @Override
-        protected void onInactive() {
-            super.onInactive();
-            mContainer.onInactive(this);
-        }
-    }
-}
diff --git a/work/workmanager/src/test/java/androidx/work/impl/WorkManagerLiveDataTrackerTest.java b/work/workmanager/src/test/java/androidx/work/impl/WorkManagerLiveDataTrackerTest.java
deleted file mode 100644
index dbc71a4..0000000
--- a/work/workmanager/src/test/java/androidx/work/impl/WorkManagerLiveDataTrackerTest.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Copyright 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 androidx.work.impl;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-import androidx.lifecycle.LiveData;
-import androidx.lifecycle.MutableLiveData;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
-
-@RunWith(JUnit4.class)
-public class WorkManagerLiveDataTrackerTest {
-    private WorkManagerLiveDataTracker mContainer = new WorkManagerLiveDataTracker();
-
-    @Test
-    public void add() {
-        LiveData liveData = createLiveData();
-        assertThat(mContainer.mLiveDataSet, is(setOf()));
-        mContainer.onActive(liveData);
-        assertThat(mContainer.mLiveDataSet, is(setOf(liveData)));
-    }
-
-    @Test
-    public void add_twice() {
-        LiveData liveData = createLiveData();
-        mContainer.onActive(liveData);
-        mContainer.onActive(liveData);
-        assertThat(mContainer.mLiveDataSet, is(setOf(liveData)));
-    }
-
-    @Test
-    public void remove() {
-        LiveData liveData = createLiveData();
-        mContainer.onActive(liveData);
-        mContainer.onInactive(liveData);
-        assertThat(mContainer.mLiveDataSet, is(setOf()));
-    }
-
-    @Test
-    public void remove_twice() {
-        LiveData liveData = createLiveData();
-        mContainer.onActive(liveData);
-        mContainer.onInactive(liveData);
-        mContainer.onInactive(liveData);
-        assertThat(mContainer.mLiveDataSet, is(setOf()));
-    }
-
-    @Test
-    public void addRemoveMultiple() {
-        LiveData ld1 = createLiveData();
-        LiveData ld2 = createLiveData();
-        assertThat(mContainer.mLiveDataSet, is(setOf()));
-        mContainer.onActive(ld1);
-        mContainer.onActive(ld2);
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld1, ld2)));
-        mContainer.onInactive(ld1);
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld2)));
-        mContainer.onInactive(ld1); // intentional
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld2)));
-        mContainer.onActive(ld1);
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld1, ld2)));
-        mContainer.onActive(ld1); // intentional
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld1, ld2)));
-        mContainer.onInactive(ld2);
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld1)));
-        mContainer.onInactive(ld1);
-        assertThat(mContainer.mLiveDataSet, is(setOf()));
-        mContainer.onActive(ld1);
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld1)));
-        mContainer.onActive(ld2);
-        assertThat(mContainer.mLiveDataSet, is(setOf(ld1, ld2)));
-    }
-
-    private LiveData<String> createLiveData() {
-        return mContainer.track(new MutableLiveData<String>());
-    }
-
-    private static Set<LiveData> setOf(LiveData... items) {
-        return new HashSet<>(Arrays.asList(items));
-    }
-}