fix: 首次提交
This commit is contained in:
@@ -0,0 +1,348 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 org.robolectric.shadows.androidx.fragment;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import org.robolectric.android.controller.ActivityController;
|
||||
import org.robolectric.android.controller.ComponentController;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
/** A Controller that can be used to drive the lifecycle of a {@link Fragment} */
|
||||
public class FragmentController<F extends Fragment>
|
||||
extends ComponentController<FragmentController<F>, F> {
|
||||
|
||||
private final F mFragment;
|
||||
private final ActivityController<? extends FragmentActivity> mActivityController;
|
||||
|
||||
private FragmentController(F fragment, Class<? extends FragmentActivity> activityClass) {
|
||||
this(fragment, activityClass, null /*intent*/, null /*arguments*/);
|
||||
}
|
||||
|
||||
private FragmentController(
|
||||
F fragment, Class<? extends FragmentActivity> activityClass, Intent intent) {
|
||||
this(fragment, activityClass, intent, null /*arguments*/);
|
||||
}
|
||||
|
||||
private FragmentController(
|
||||
F fragment, Class<? extends FragmentActivity> activityClass, Bundle arguments) {
|
||||
this(fragment, activityClass, null /*intent*/, arguments);
|
||||
}
|
||||
|
||||
private FragmentController(
|
||||
F fragment,
|
||||
Class<? extends FragmentActivity> activityClass,
|
||||
Intent intent,
|
||||
Bundle arguments) {
|
||||
super(fragment, intent);
|
||||
this.mFragment = fragment;
|
||||
if (arguments != null) {
|
||||
this.mFragment.setArguments(arguments);
|
||||
}
|
||||
this.mActivityController =
|
||||
ActivityController.of(ReflectionHelpers.callConstructor(activityClass), intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(F fragment) {
|
||||
return new FragmentController<>(fragment, FragmentControllerActivity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment and intent.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param intent the intent which will be retained by activity
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(F fragment, Intent intent) {
|
||||
return new FragmentController<>(fragment, FragmentControllerActivity.class, intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment and arguments.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param arguments the arguments which will be retained by fragment
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(F fragment, Bundle arguments) {
|
||||
return new FragmentController<>(fragment, FragmentControllerActivity.class, arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment and activity class.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param activityClass the activity which will be attached by fragment
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(
|
||||
F fragment, Class<? extends FragmentActivity> activityClass) {
|
||||
return new FragmentController<>(fragment, activityClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment, intent and arguments.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param intent the intent which will be retained by activity
|
||||
* @param arguments the arguments which will be retained by fragment
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(
|
||||
F fragment, Intent intent, Bundle arguments) {
|
||||
return new FragmentController<>(fragment, FragmentControllerActivity.class, intent,
|
||||
arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment, activity class and intent.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param activityClass the activity which will be attached by fragment
|
||||
* @param intent the intent which will be retained by activity
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(
|
||||
F fragment, Class<? extends FragmentActivity> activityClass, Intent intent) {
|
||||
return new FragmentController<>(fragment, activityClass, intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment, activity class and arguments.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param activityClass the activity which will be attached by fragment
|
||||
* @param arguments the arguments which will be retained by fragment
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(
|
||||
F fragment, Class<? extends FragmentActivity> activityClass, Bundle arguments) {
|
||||
return new FragmentController<>(fragment, activityClass, arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the {@link FragmentController} for specific fragment, activity class, intent and
|
||||
* arguments.
|
||||
*
|
||||
* @param fragment the fragment which you'd like to drive lifecycle
|
||||
* @param activityClass the activity which will be attached by fragment
|
||||
* @param intent the intent which will be retained by activity
|
||||
* @param arguments the arguments which will be retained by fragment
|
||||
* @return {@link FragmentController}
|
||||
*/
|
||||
public static <F extends Fragment> FragmentController<F> of(
|
||||
F fragment,
|
||||
Class<? extends FragmentActivity> activityClass,
|
||||
Intent intent,
|
||||
Bundle arguments) {
|
||||
return new FragmentController<>(fragment, activityClass, intent, arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the given fragment by attaching it to an activity, calling its onCreate() through
|
||||
* onResume() lifecycle methods, and then making it visible. Note that the fragment will be
|
||||
* added
|
||||
* to the view with ID 1.
|
||||
*/
|
||||
public static <F extends Fragment> F setupFragment(F fragment) {
|
||||
return FragmentController.of(fragment).create().start().resume().visible().get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the given fragment by attaching it to an activity, calling its onCreate() through
|
||||
* onResume() lifecycle methods, and then making it visible. Note that the fragment will be
|
||||
* added
|
||||
* to the view with ID 1.
|
||||
*/
|
||||
public static <F extends Fragment> F setupFragment(
|
||||
F fragment, Class<? extends FragmentActivity> fragmentActivityClass) {
|
||||
return FragmentController.of(fragment, fragmentActivityClass)
|
||||
.create()
|
||||
.start()
|
||||
.resume()
|
||||
.visible()
|
||||
.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the given fragment by attaching it to an activity created with the given bundle,
|
||||
* calling its onCreate() through onResume() lifecycle methods, and then making it visible. Note
|
||||
* that the fragment will be added to the view with ID 1.
|
||||
*/
|
||||
public static <F extends Fragment> F setupFragment(
|
||||
F fragment, Class<? extends FragmentActivity> fragmentActivityClass, Bundle bundle) {
|
||||
return FragmentController.of(fragment, fragmentActivityClass)
|
||||
.create(bundle)
|
||||
.start()
|
||||
.resume()
|
||||
.visible()
|
||||
.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the given fragment by attaching it to an activity created with the given bundle and
|
||||
* container id, calling its onCreate() through onResume() lifecycle methods, and then making it
|
||||
* visible.
|
||||
*/
|
||||
public static <F extends Fragment> F setupFragment(
|
||||
F fragment,
|
||||
Class<? extends FragmentActivity> fragmentActivityClass,
|
||||
int containerViewId,
|
||||
Bundle bundle) {
|
||||
return FragmentController.of(fragment, fragmentActivityClass)
|
||||
.create(containerViewId, bundle)
|
||||
.start()
|
||||
.resume()
|
||||
.visible()
|
||||
.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the activity with {@link Bundle} and adds the fragment to the view with ID {@code
|
||||
* contentViewId}.
|
||||
*/
|
||||
public FragmentController<F> create(final int contentViewId, final Bundle bundle) {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController
|
||||
.create(bundle)
|
||||
.get()
|
||||
.getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.add(contentViewId, mFragment)
|
||||
.commit();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the activity with {@link Bundle} and adds the fragment to it. Note that the fragment
|
||||
* will be added to the view with ID 1.
|
||||
*/
|
||||
public FragmentController<F> create(final Bundle bundle) {
|
||||
return create(1, bundle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the {@link Fragment} in a newly initialized state and hence will receive a null
|
||||
* savedInstanceState {@link Bundle parameter}
|
||||
*/
|
||||
@Override
|
||||
public FragmentController<F> create() {
|
||||
return create(null);
|
||||
}
|
||||
|
||||
/** Drive lifecycle of activity to Start lifetime */
|
||||
public FragmentController<F> start() {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController.start();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Drive lifecycle of activity to Resume lifetime */
|
||||
public FragmentController<F> resume() {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController.resume();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Drive lifecycle of activity to Pause lifetime */
|
||||
public FragmentController<F> pause() {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController.pause();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Drive lifecycle of activity to Stop lifetime */
|
||||
public FragmentController<F> stop() {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController.stop();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Drive lifecycle of activity to Destroy lifetime */
|
||||
@Override
|
||||
public FragmentController<F> destroy() {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController.destroy();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Let activity can be visible lifetime */
|
||||
public FragmentController<F> visible() {
|
||||
shadowMainLooper.runPaused(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mActivityController.visible();
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
private static class FragmentControllerActivity extends FragmentActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
LinearLayout view = new LinearLayout(this);
|
||||
view.setId(1);
|
||||
|
||||
setContentView(view);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Testing infrastructure for androidx.fragment library.
|
||||
*
|
||||
* <p>To use this in your project, add the artifact {@code
|
||||
* org.robolectric:shadows-androidx-fragment} to your project.
|
||||
*/
|
||||
package org.robolectric.shadows.androidx.fragment;
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.robolectric.shadows.androidx.fragment">
|
||||
|
||||
<uses-sdk android:targetSdkVersion="28"/>
|
||||
</manifest>
|
||||
@@ -0,0 +1,360 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 org.robolectric.shadows.androidx.fragment;
|
||||
|
||||
import static android.os.Looper.getMainLooper;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Tests for {@link FragmentController} */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class FragmentControllerTest {
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
TranscriptFragment.clearLifecycleEvents();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initialNotAttached() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment());
|
||||
|
||||
assertThat(controller.get().getView()).isNull();
|
||||
assertThat(controller.get().getActivity()).isNull();
|
||||
assertThat(controller.get().isAdded()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initialNotAttached_customActivity() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
assertThat(controller.get().getView()).isNull();
|
||||
assertThat(controller.get().getActivity()).isNull();
|
||||
assertThat(controller.get().isAdded()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachedAfterCreate() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment());
|
||||
|
||||
controller.create();
|
||||
shadowOf(getMainLooper()).idle();
|
||||
|
||||
assertThat(controller.get().getActivity()).isNotNull();
|
||||
assertThat(controller.get().isAdded()).isTrue();
|
||||
assertThat(controller.get().isResumed()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachedAfterCreate_customActivity() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
controller.create();
|
||||
shadowOf(getMainLooper()).idle();
|
||||
|
||||
assertThat(controller.get().getActivity()).isNotNull();
|
||||
assertThat(controller.get().getActivity()).isInstanceOf(TestActivity.class);
|
||||
assertThat(controller.get().isAdded()).isTrue();
|
||||
assertThat(controller.get().isResumed()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachedAfterCreate_customizedViewId() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), CustomizedViewIdTestActivity.class);
|
||||
|
||||
controller.create(R.id.custom_activity_view, null).start();
|
||||
|
||||
assertThat(controller.get().getView()).isNotNull();
|
||||
assertThat(controller.get().getActivity()).isNotNull();
|
||||
assertThat(controller.get().isAdded()).isTrue();
|
||||
assertThat(controller.get().isResumed()).isFalse();
|
||||
assertThat((TextView) controller.get().getView().findViewById(R.id.tacos)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasViewAfterStart() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment());
|
||||
|
||||
controller.create().start();
|
||||
|
||||
assertThat(controller.get().getView()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isResumed() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
controller.create().start().resume();
|
||||
|
||||
assertThat(controller.get().getView()).isNotNull();
|
||||
assertThat(controller.get().getActivity()).isNotNull();
|
||||
assertThat(controller.get().isAdded()).isTrue();
|
||||
assertThat(controller.get().isResumed()).isTrue();
|
||||
assertThat((TextView) controller.get().getView().findViewById(R.id.tacos)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isPaused() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
controller.create().start().resume().pause();
|
||||
|
||||
assertThat(controller.get().getView()).isNotNull();
|
||||
assertThat(controller.get().getActivity()).isNotNull();
|
||||
assertThat(controller.get().isAdded()).isTrue();
|
||||
assertThat(controller.get().isResumed()).isFalse();
|
||||
assertThat(controller.get().getLifecycleEvents())
|
||||
.containsExactly("onCreate", "onStart", "onResume", "onPause")
|
||||
.inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isStopped() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
controller.create().start().resume().pause().stop();
|
||||
|
||||
assertThat(controller.get().getView()).isNotNull();
|
||||
assertThat(controller.get().getActivity()).isNotNull();
|
||||
assertThat(controller.get().isAdded()).isTrue();
|
||||
assertThat(controller.get().isResumed()).isFalse();
|
||||
assertThat(controller.get().getLifecycleEvents())
|
||||
.containsExactly("onCreate", "onStart", "onResume", "onPause", "onStop")
|
||||
.inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withIntent() {
|
||||
final Intent intent = generateTestIntent();
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class, intent);
|
||||
|
||||
controller.create();
|
||||
shadowOf(getMainLooper()).idle();
|
||||
final Intent intentInFragment = controller.get().getActivity().getIntent();
|
||||
|
||||
assertThat(intentInFragment.getAction()).isEqualTo("test_action");
|
||||
assertThat(intentInFragment.getExtras().getString("test_key")).isEqualTo("test_value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withArguments() {
|
||||
final Bundle bundle = generateTestBundle();
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class, bundle);
|
||||
|
||||
controller.create();
|
||||
final Bundle args = controller.get().getArguments();
|
||||
|
||||
assertThat(args.getString("test_key")).isEqualTo("test_value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withIntentAndArguments() {
|
||||
final Bundle bundle = generateTestBundle();
|
||||
final Intent intent = generateTestIntent();
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class, intent, bundle);
|
||||
|
||||
controller.create();
|
||||
shadowOf(getMainLooper()).idle();
|
||||
final Intent intentInFragment = controller.get().getActivity().getIntent();
|
||||
final Bundle args = controller.get().getArguments();
|
||||
|
||||
assertThat(intentInFragment.getAction()).isEqualTo("test_action");
|
||||
assertThat(intentInFragment.getExtras().getString("test_key")).isEqualTo("test_value");
|
||||
assertThat(args.getString("test_key")).isEqualTo("test_value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void visible() {
|
||||
final FragmentController<TranscriptFragment> controller =
|
||||
FragmentController.of(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
controller.create().start().resume();
|
||||
|
||||
assertThat(controller.get().isVisible()).isFalse();
|
||||
|
||||
controller.visible();
|
||||
|
||||
assertThat(controller.get().isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setupFragmentWithFragment_fragmentHasCorrectLifecycle() {
|
||||
TranscriptFragment fragment = FragmentController.setupFragment(new TranscriptFragment());
|
||||
|
||||
assertThat(fragment.getLifecycleEvents())
|
||||
.containsExactly("onCreate", "onStart", "onResume")
|
||||
.inOrder();
|
||||
assertThat(fragment.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setupFragmentWithFragmentAndActivity_fragmentHasCorrectLifecycle() {
|
||||
TranscriptFragment fragment =
|
||||
FragmentController.setupFragment(new TranscriptFragment(), TestActivity.class);
|
||||
|
||||
assertThat(fragment.getLifecycleEvents())
|
||||
.containsExactly("onCreate", "onStart", "onResume")
|
||||
.inOrder();
|
||||
assertThat(fragment.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setupFragmentWithFragmentAndActivityAndBundle_HasCorrectLifecycle() {
|
||||
Bundle testBundle = generateTestBundle();
|
||||
TranscriptFragment fragment =
|
||||
FragmentController.setupFragment(new TranscriptFragment(), TestActivity.class,
|
||||
testBundle);
|
||||
|
||||
assertThat(fragment.getLifecycleEvents())
|
||||
.containsExactly("onCreate", "onStart", "onResume")
|
||||
.inOrder();
|
||||
assertThat(fragment.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void
|
||||
setupFragmentWithFragment_Activity_ContainViewIdAndBundle_HasCorrectLifecycle() {
|
||||
Bundle testBundle = generateTestBundle();
|
||||
TranscriptFragment fragment =
|
||||
FragmentController.setupFragment(
|
||||
new TranscriptFragment(),
|
||||
CustomizedViewIdTestActivity.class,
|
||||
R.id.custom_activity_view,
|
||||
testBundle);
|
||||
|
||||
assertThat(fragment.getLifecycleEvents())
|
||||
.containsExactly("onCreate", "onStart", "onResume")
|
||||
.inOrder();
|
||||
assertThat(fragment.isVisible()).isTrue();
|
||||
}
|
||||
|
||||
private Intent generateTestIntent() {
|
||||
final Intent testIntent = new Intent("test_action").putExtra("test_key", "test_value");
|
||||
return testIntent;
|
||||
}
|
||||
|
||||
private Bundle generateTestBundle() {
|
||||
final Bundle testBundle = new Bundle();
|
||||
testBundle.putString("test_key", "test_value");
|
||||
|
||||
return testBundle;
|
||||
}
|
||||
|
||||
/** A Fragment which can record lifecycle status for test. */
|
||||
public static class TranscriptFragment extends Fragment {
|
||||
|
||||
public static final List<String> sLifecycleEvents = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
sLifecycleEvents.add("onCreate");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
sLifecycleEvents.add("onStart");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
sLifecycleEvents.add("onResume");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
sLifecycleEvents.add("onPause");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
sLifecycleEvents.add("onStop");
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(
|
||||
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_contents, container, false);
|
||||
}
|
||||
|
||||
public List<String> getLifecycleEvents() {
|
||||
return sLifecycleEvents;
|
||||
}
|
||||
|
||||
public static void clearLifecycleEvents() {
|
||||
sLifecycleEvents.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/** A Activity which set a default view for test. */
|
||||
public static class TestActivity extends FragmentActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
LinearLayout view = new LinearLayout(this);
|
||||
view.setId(1);
|
||||
|
||||
setContentView(view);
|
||||
}
|
||||
}
|
||||
|
||||
/** A Activity which has a custom view for test. */
|
||||
public static class CustomizedViewIdTestActivity extends FragmentActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.custom_activity_view);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/custom_activity_view"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tacos"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="TACOS"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/burritos"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="BURRITOS"/>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user