package com.android.example.github.util;

import com.android.example.github.testing.SingleFragmentActivity;

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

import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v4.app.Fragment;

import java.util.concurrent.CountDownLatch;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

@RunWith(AndroidJUnit4.class)
public class AutoClearedValueTest {
    @Rule
    public ActivityTestRule<SingleFragmentActivity> activityRule =
            new ActivityTestRule<>(SingleFragmentActivity.class, true, true);
    public TestFragment testFragment;

    @Before
    public void init() {
        testFragment = new TestFragment();
        activityRule.getActivity().setFragment(testFragment);
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
    }

    @Test
    public void testClear() throws Throwable {
        testFragment.testValue = new AutoClearedValue<>(testFragment, "foo");
        activityRule.getActivity().replaceFragment(new TestFragment());
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        assertThat(testFragment.testValue.get(), nullValue());
    }

    @Test
    public void testDontClearForChildFragment() throws Throwable {
        testFragment.testValue = new AutoClearedValue<>(testFragment, "foo");
        testFragment.getChildFragmentManager().beginTransaction()
                .add(new Fragment(), "foo").commit();
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        assertThat(testFragment.testValue.get(), is("foo"));
    }

    public static class TestFragment extends Fragment {
        AutoClearedValue<String> testValue;
    }
}
@@ -1,8 +1,628 @@
+/*%0A * Copyright (C) 2017 The Android Open Source Project%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -884,61 +884,8 @@
 ry;%0A
-import android.support.test.annotation.UiThreadTest;%0A
 impo
@@ -1026,53 +1026,8 @@
 t;%0A%0A
-import java.util.concurrent.CountDownLatch;%0A%0A
 impo
@@ -1075,16 +1075,16 @@
 rtThat;%0A
+
 import s
@@ -1119,58 +1119,8 @@
 is;%0A
-import static org.hamcrest.Matchers.notNullValue;%0A
 impo

add copyright header to the new file