fix: 首次提交
This commit is contained in:
40
SettingsLib/tests/robotests/fragment/Android.bp
Normal file
40
SettingsLib/tests/robotests/fragment/Android.bp
Normal file
@@ -0,0 +1,40 @@
|
||||
//#############################################
|
||||
// Compile Robolectric shadows framework misapplied to androidx
|
||||
//#############################################
|
||||
|
||||
package {
|
||||
// See: http://go/android-license-faq
|
||||
// A large-scale-change added 'default_applicable_licenses' to import
|
||||
// all of the 'license_kinds' from "frameworks_base_license"
|
||||
// to get the below license kinds:
|
||||
// SPDX-license-identifier-Apache-2.0
|
||||
default_applicable_licenses: ["frameworks_base_license"],
|
||||
}
|
||||
|
||||
java_library {
|
||||
name: "Robolectric_shadows_androidx_fragment_upstream",
|
||||
srcs: [
|
||||
"src/main/java/**/*.java",
|
||||
"src/main/java/**/*.kt",
|
||||
],
|
||||
javacflags: [
|
||||
"-Aorg.robolectric.annotation.processing.shadowPackage=org.robolectric.shadows.androidx.fragment",
|
||||
"-Aorg.robolectric.annotation.processing.sdkCheckMode=ERROR",
|
||||
// Uncomment the below to debug annotation processors not firing.
|
||||
//"-verbose",
|
||||
//"-XprintRounds",
|
||||
//"-XprintProcessorInfo",
|
||||
//"-Xlint",
|
||||
//"-J-verbose",
|
||||
],
|
||||
libs: [
|
||||
"Robolectric_all-target_upstream",
|
||||
"androidx.fragment_fragment",
|
||||
],
|
||||
plugins: [
|
||||
"auto_value_plugin_1.9",
|
||||
"auto_value_builder_plugin_1.9",
|
||||
"Robolectric_processor_upstream",
|
||||
],
|
||||
|
||||
}
|
||||
69
SettingsLib/tests/robotests/fragment/BUILD
Normal file
69
SettingsLib/tests/robotests/fragment/BUILD
Normal file
@@ -0,0 +1,69 @@
|
||||
load("//third_party/java/android/android_sdk_linux/extras/android/compatibility/jetify:jetify.bzl", "jetify_android_library", "jetify_android_local_test")
|
||||
|
||||
package(
|
||||
default_applicable_licenses = ["//third_party/java_src/robolectric:license"],
|
||||
default_visibility = ["//third_party/java_src/robolectric:__subpackages__"],
|
||||
)
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
#==============================================================================
|
||||
# Test resources library
|
||||
#==============================================================================
|
||||
jetify_android_library(
|
||||
name = "test_resources",
|
||||
custom_package = "org.robolectric.shadows.androidx.fragment",
|
||||
manifest = "src/test/AndroidManifest.xml",
|
||||
resource_files = glob(
|
||||
["src/test/resources/**/*"],
|
||||
),
|
||||
)
|
||||
|
||||
#==============================================================================
|
||||
# AndroidX fragment module library
|
||||
#==============================================================================
|
||||
jetify_android_library(
|
||||
name = "androidx_fragment",
|
||||
testonly = 1,
|
||||
srcs = glob(
|
||||
["src/main/java/**"],
|
||||
),
|
||||
custom_package = "org.robolectric.shadows.androidx.fragment",
|
||||
javacopts = [
|
||||
"-Aorg.robolectric.annotation.processing.shadowPackage=org.robolectric.shadows.androidx.fragment",
|
||||
],
|
||||
jetify_sources = True,
|
||||
plugins = [
|
||||
"//java/com/google/thirdparty/robolectric/processor",
|
||||
],
|
||||
deps = [
|
||||
"//third_party/java/androidx/core",
|
||||
"//third_party/java/androidx/fragment",
|
||||
"//third_party/java/androidx/lifecycle",
|
||||
"//third_party/java_src/robolectric/shadowapi",
|
||||
"//third_party/java_src/robolectric/shadows/framework",
|
||||
],
|
||||
)
|
||||
|
||||
[
|
||||
jetify_android_local_test(
|
||||
name = "test_" + src.rstrip(".java"),
|
||||
size = "small",
|
||||
srcs = glob(
|
||||
["src/test/java/**/*.java"],
|
||||
),
|
||||
jetify_sources = True,
|
||||
deps = [
|
||||
":androidx_fragment",
|
||||
":test_resources",
|
||||
"//third_party/java/androidx/fragment",
|
||||
"//third_party/java/androidx/loader",
|
||||
"//third_party/java/mockito",
|
||||
"//third_party/java/robolectric",
|
||||
"//third_party/java/truth",
|
||||
],
|
||||
)
|
||||
for src in glob(
|
||||
["src/test/java/**/*Test.java"],
|
||||
)
|
||||
]
|
||||
48
SettingsLib/tests/robotests/fragment/build.gradle
Normal file
48
SettingsLib/tests/robotests/fragment/build.gradle
Normal file
@@ -0,0 +1,48 @@
|
||||
plugins {
|
||||
id "net.ltgt.errorprone" version "0.0.13"
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
|
||||
android {
|
||||
sourceSets {
|
||||
main {
|
||||
res.srcDirs = ['src/test/resources/res']
|
||||
}
|
||||
}
|
||||
testOptions {
|
||||
unitTests {
|
||||
includeAndroidResources = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Project dependencies
|
||||
compileOnly project(":robolectric")
|
||||
|
||||
// Compile dependencies
|
||||
compileOnly AndroidSdk.MAX_SDK.coordinates
|
||||
compileOnly "androidx.core:core:1.0.0-rc02"
|
||||
compileOnly 'androidx.fragment:fragment:1.0.0-rc02'
|
||||
compileOnly "androidx.lifecycle:lifecycle-viewmodel:2.0.0-rc01"
|
||||
compileOnly "androidx.lifecycle:lifecycle-common:2.0.0-beta01"
|
||||
|
||||
// Testing dependencies
|
||||
testImplementation "com.google.truth:truth:0.44"
|
||||
testImplementation "org.mockito:mockito-core:2.5.4"
|
||||
testImplementation "androidx.arch.core:core-common:2.0.0-beta01"
|
||||
testImplementation "androidx.arch.core:core-runtime:2.0.0-rc01"
|
||||
testImplementation "androidx.collection:collection:1.0.0-rc01"
|
||||
testImplementation "androidx.core:core:1.0.0-rc02"
|
||||
testImplementation 'androidx.fragment:fragment:1.0.0-rc02'
|
||||
testImplementation "androidx.lifecycle:lifecycle-viewmodel:2.0.0-rc01"
|
||||
testImplementation "androidx.lifecycle:lifecycle-common:2.0.0-beta01"
|
||||
testImplementation "androidx.lifecycle:lifecycle-runtime:2.0.0-rc01"
|
||||
testImplementation "androidx.lifecycle:lifecycle-livedata-core:2.0.0-rc01"
|
||||
testImplementation "androidx.loader:loader:1.0.0-rc02"
|
||||
}
|
||||
@@ -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