/*
 * Copyright (C) 2016 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.support.v4.app;

import android.os.Bundle;
import android.support.v4.app.test.FragmentTestActivity;
import android.support.v4.test.R;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Miscellaneous tests for fragments that aren't big enough to belong to their own classes.
 */
public class FragmentTest extends
        ActivityInstrumentationTestCase2<FragmentTestActivity> {
    private FragmentTestActivity mActivity;

    public FragmentTest() {
        super(FragmentTestActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
    }

    @SmallTest
    @UiThreadTest
    public void testOnCreateOrder() throws Throwable {
        OrderFragment fragment1 = new OrderFragment();
        OrderFragment fragment2 = new OrderFragment();
        mActivity.getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.content, fragment1)
                .add(R.id.content, fragment2)
                .commitNow();
        assertEquals(0, fragment1.createOrder);
        assertEquals(1, fragment2.createOrder);
    }

    @SmallTest
    public void testChildFragmentManagerGone() throws Throwable {
        final FragmentA fragmentA = new FragmentA();
        final FragmentB fragmentB = new FragmentB();
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mActivity.getSupportFragmentManager().beginTransaction()
                        .add(R.id.content, fragmentA)
                        .commitNow();
            }
        });
        getInstrumentation().waitForIdleSync();
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mActivity.getSupportFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.long_fade_in, R.anim.long_fade_out,
                                R.anim.long_fade_in, R.anim.long_fade_out)
                        .replace(R.id.content, fragmentB)
                        .addToBackStack(null)
                        .commit();
            }
        });
        // Wait for the middle of the animation
        Thread.sleep(150);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mActivity.getSupportFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.long_fade_in, R.anim.long_fade_out,
                                R.anim.long_fade_in, R.anim.long_fade_out)
                        .replace(R.id.content, fragmentA)
                        .addToBackStack(null)
                        .commit();
            }
        });
        // Wait for the middle of the animation
        Thread.sleep(150);
        getInstrumentation().waitForIdleSync();
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mActivity.getSupportFragmentManager().popBackStack();
            }
        });
        // Wait for the middle of the animation
        Thread.sleep(150);
        getInstrumentation().waitForIdleSync();
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mActivity.getSupportFragmentManager().popBackStack();
            }
        });
    }

    public static class OrderFragment extends Fragment {
        private static AtomicInteger sOrder = new AtomicInteger();
        public int createOrder = -1;

        public OrderFragment() {}

        @Override
        public void onCreate(Bundle savedInstanceState) {
            createOrder = sOrder.getAndIncrement();
            super.onCreate(savedInstanceState);
        }
    }

    public static class FragmentA extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_a, container, false);
        }
    }

    public static class FragmentB extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_b, container, false);
        }
    }
}

@@ -847,24 +847,80 @@
 ThreadTest;%0A
+import android.test.suitebuilder.annotation.MediumTest;%0A
 import andro
@@ -4284,32 +4284,856 @@
      %7D);%0A    %7D%0A%0A
+    @MediumTest%0A    @UiThreadTest%0A    public void testViewOrder() throws Throwable %7B%0A        FragmentA fragmentA = new FragmentA();%0A        FragmentB fragmentB = new FragmentB();%0A        FragmentC fragmentC = new FragmentC();%0A        mActivity.getSupportFragmentManager()%0A                .beginTransaction()%0A                .add(R.id.content, fragmentA)%0A                .add(R.id.content, fragmentB)%0A                .add(R.id.content, fragmentC)%0A                .commitNow();%0A        ViewGroup content = (ViewGroup) mActivity.findViewById(R.id.content);%0A        assertEquals(3, content.getChildCount());%0A        assertNotNull(content.getChildAt(0).findViewById(R.id.textA));%0A        assertNotNull(content.getChildAt(1).findViewById(R.id.textB));%0A        assertNotNull(content.getChildAt(2).findViewById(R.id.textC));%0A    %7D%0A%0A
     public stati
@@ -6080,11 +6080,299 @@
  %7D%0A    %7D
+%0A%0A    public static class FragmentC extends Fragment %7B%0A        @Override%0A        public View onCreateView(LayoutInflater inflater, ViewGroup container,%0A                Bundle savedInstanceState) %7B%0A            return inflater.inflate(R.layout.fragment_c, container, false);%0A        %7D%0A    %7D
 %0A%7D%0A

Add test for view order when adding fragments.