1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.arch.lifecycle;
18
19import static android.arch.lifecycle.Lifecycle.Event.ON_START;
20
21import static org.hamcrest.CoreMatchers.is;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.when;
25
26import android.arch.core.executor.JunitTaskExecutorRule;
27import android.arch.core.executor.TaskExecutor;
28import android.support.annotation.Nullable;
29
30import org.junit.Before;
31import org.junit.Rule;
32import org.junit.Test;
33
34import java.util.concurrent.CountDownLatch;
35import java.util.concurrent.TimeUnit;
36
37public class ThreadedLiveDataTest {
38
39    private static final int TIMEOUT_SECS = 3;
40
41    @Rule
42    public JunitTaskExecutorRule mTaskExecutorRule = new JunitTaskExecutorRule(1, false);
43
44    private LiveData<String> mLiveData;
45    private LifecycleOwner mLifecycleOwner;
46    private LifecycleRegistry mRegistry;
47
48    @Before
49    public void init() {
50        mLiveData = new MutableLiveData<>();
51        mLifecycleOwner = mock(LifecycleOwner.class);
52        mRegistry = new LifecycleRegistry(mLifecycleOwner);
53        when(mLifecycleOwner.getLifecycle()).thenReturn(mRegistry);
54    }
55
56    @Test
57    public void testPostValue() throws InterruptedException {
58        final TaskExecutor taskExecutor = mTaskExecutorRule.getTaskExecutor();
59        final CountDownLatch finishTestLatch = new CountDownLatch(1);
60        final Observer<String> observer = new Observer<String>() {
61            @Override
62            public void onChanged(@Nullable String newValue) {
63                try {
64                    assertThat(taskExecutor.isMainThread(), is(true));
65                    assertThat(newValue, is("success"));
66                } finally {
67                    finishTestLatch.countDown();
68                }
69            }
70        };
71        taskExecutor.executeOnMainThread(new Runnable() {
72            @Override
73            public void run() {
74                mRegistry.handleLifecycleEvent(ON_START);
75                mLiveData.observe(mLifecycleOwner, observer);
76                final CountDownLatch latch = new CountDownLatch(1);
77                taskExecutor.executeOnDiskIO(new Runnable() {
78                    @Override
79                    public void run() {
80                        mLiveData.postValue("fail");
81                        mLiveData.postValue("success");
82                        latch.countDown();
83                    }
84                });
85                try {
86                    assertThat(latch.await(TIMEOUT_SECS, TimeUnit.SECONDS), is(true));
87                } catch (InterruptedException e) {
88                    throw new RuntimeException(e);
89                }
90            }
91        });
92        assertThat(finishTestLatch.await(TIMEOUT_SECS, TimeUnit.SECONDS), is(true));
93    }
94}
95