编辑:小心!我已经删除了这个问题中提到的旧存储库。请看我自己对问题的答案,以获得一个可能的解决方案,并随时改进它!
我指的是我的职位。现在我走得更远了。我还提到了github项目中的两个分支:
@PerActivity
@Component(modules = ActivityModule.class)
public interface ActivityComponent {
// TODO: Comment this out for switching back to the old approach
void inject(MainFragment mainFragment);
// TODO: Leave that for witching to the new approach
void inject(MainActivity mainActivity);
}
ActivityModule
提供了MainInteractor
@Module
public class ActivityModule {
@Provides
@PerActivity
MainInteractor provideMainInteractor () {
return new MainInteractor();
}
}
我的TestActivityComponent
使用了TestActivityModule
:
@PerActivity
@Component(modules = TestActivityModule.class)
public interface TestActivityComponent extends ActivityComponent {
void inject(MainActivityTest mainActivityTest);
}
TestActVityModule
提供了FakeInteractor
:
@Module
public class TestActivityModule {
@Provides
@PerActivity
MainInteractor provideMainInteractor () {
return new FakeMainInteractor();
}
}
我的MainActivity
有一个getComponent()
方法和一个setComponent()
方法。使用后者,您可以将组件交换到检测测试中的测试组件。活动内容如下:
public class MainActivity extends BaseActivity implements MainFragment.OnFragmentInteractionListener {
private static final String TAG = "MainActivity";
private Fragment currentFragment;
private ActivityComponent activityComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeInjector();
if (savedInstanceState == null) {
currentFragment = new MainFragment();
addFragment(R.id.fragmentContainer, currentFragment);
}
}
private void initializeInjector() {
Log.i(TAG, "injectDagger initializeInjector()");
activityComponent = DaggerActivityComponent.builder()
.activityModule(new ActivityModule())
.build();
activityComponent.inject(this);
}
@Override
public void onFragmentInteraction(final Uri uri) {
}
ActivityComponent getActivityComponent() {
return activityComponent;
}
@VisibleForTesting
public void setActivityComponent(ActivityComponent activityComponent) {
Log.w(TAG, "injectDagger Only call this method to swap test doubles");
this.activityComponent = activityComponent;
}
}
如您所见,本练习使用mainfragment
。在片段的oncreate()
中注入组件:
public class MainFragment extends BaseFragment implements MainView {
private static final String TAG = "MainFragment";
@Inject
MainPresenter mainPresenter;
private View view;
public MainFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "injectDagger onCreate()");
super.onCreate(savedInstanceState);
// TODO: That approach works
// ((AndroidApplication)((MainActivity) getActivity()).getApplication()).getApplicationComponent().inject(this);
// TODO: This approach is NOT working, see MainActvityTest
((MainActivity) getActivity()).getActivityComponent().inject(this);
}
}
然后在测试中,我将ActivityComponent
与TestApplicationComponent
交换:
public class MainActivityTest{
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class, true, false);
private MainActivity mActivity;
private TestActivityComponent mTestActivityComponent;
// TODO: That approach works
// private TestApplicationComponent mTestApplicationComponent;
//
// private void initializeInjector() {
// mTestApplicationComponent = DaggerTestApplicationComponent.builder()
// .testApplicationModule(new TestApplicationModule(getApp()))
// .build();
//
// getApp().setApplicationComponent(mTestApplicationComponent);
// mTestApplicationComponent.inject(this);
// }
// TODO: This approach does NOT work because mActivity.setActivityComponent() is called after MainInteractor has already been injected!
private void initializeInjector() {
mTestActivityComponent = DaggerTestActivityComponent.builder()
.testActivityModule(new TestActivityModule())
.build();
mActivity.setActivityComponent(mTestActivityComponent);
mTestActivityComponent.inject(this);
}
public AndroidApplication getApp() {
return (AndroidApplication) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
}
// TODO: That approach works
// @Before
// public void setUp() throws Exception {
//
// initializeInjector();
// mActivityRule.launchActivity(null);
// mActivity = mActivityRule.getActivity();
// }
// TODO: That approach does not works because mActivity.setActivityComponent() is called after MainInteractor has already been injected!
@Before
public void setUp() throws Exception {
mActivityRule.launchActivity(null);
mActivity = mActivityRule.getActivity();
initializeInjector();
}
@Test
public void testOnClick_Fake() throws Exception {
onView(withId(R.id.edittext)).perform(typeText("John"));
onView(withId(R.id.button)).perform(click());
onView(withId(R.id.textview_greeting)).check(matches(withText(containsString("Hello Fake"))));
}
@Test
public void testOnClick_Real() throws Exception {
onView(withId(R.id.edittext)).perform(typeText("John"));
onView(withId(R.id.button)).perform(click());
onView(withId(R.id.textview_greeting)).check(matches(withText(containsString("Hello John"))));
}
}
活动测试运行,但使用了错误的组件
。这是因为活动和片段oncreate()
是在组件交换之前运行的。
正如您所看到的,如果我将ApplicationComponent
绑定到application类,那么我有一种经过注释的旧方法。这是因为我可以在启动活动之前构建依赖项。但是现在有了activitycomponent
之后,我必须在初始化注入器之前启动活动。因为否则我无法设置
mActivity.setActivityComponent(mTestActivityComponent);
因为mactivity
如果在注入器初始化后启动活动,则会为空。(请参阅mainactivitytest
)
那么如何截取MainActivity
和MainFragment
来使用TestActivityComponent
呢?
现在我通过混合一些示例发现了如何交换一个活动范围的组件和一个片段范围的组件。在这篇文章中,我将向您展示如何做到这两个方面。但我将更详细地描述如何在InstrumentationTest期间交换片段范围的组件。我的全部代码都托管在GitHub上。您可以运行MainFragmentTest
类,但要注意,您必须在Android Studio中将de.xappo.presenterinjection.runner.AndroidApplicationJunitRunner
设置为TestRunner。
现在我简要描述如何用假交互器交换交互器。在本例中,我试图尽可能地尊重干净的体系结构。但它们可能是一些小东西,打破了这个架构。所以请随意改进。
那么,我们开始吧。首先需要一个自己的JUnitRunner:
/**
* Own JUnit runner for intercepting the ActivityComponent injection and swapping the
* ActivityComponent with the TestActivityComponent
*/
public class AndroidApplicationJUnitRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(ClassLoader classLoader, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(classLoader, TestAndroidApplication.class.getName(), context);
}
@Override
public Activity newActivity(ClassLoader classLoader, String className, Intent intent)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Activity activity = super.newActivity(classLoader, className, intent);
return swapActivityGraph(activity);
}
@SuppressWarnings("unchecked")
private Activity swapActivityGraph(Activity activity) {
if (!(activity instanceof HasComponent) || !TestActivityComponentHolder.hasComponentCreator()) {
return activity;
}
((HasComponent<ActivityComponent>) activity).
setComponent(TestActivityComponentHolder.getComponent(activity));
return activity;
}
}
@PerFragment
@Component(modules = TestFragmentModule.class)
public interface TestFragmentComponent extends FragmentComponent{
void inject(MainActivityTest mainActivityTest);
void inject(MainFragmentTest mainFragmentTest);
}
@Module
public class TestFragmentModule {
@Provides
@PerFragment
MainInteractor provideMainInteractor () {
return new FakeMainInteractor();
}
}
@Module
public class FragmentModule {
@Provides
@PerFragment
MainInteractor provideMainInteractor () {
return new MainInteractor();
}
}
public class MainInteractor {
private static final String TAG = "MainInteractor";
public MainInteractor() {
Log.i(TAG, "constructor");
}
public Person createPerson(final String name) {
return new Person(name);
}
}
public class FakeMainInteractor extends MainInteractor {
private static final String TAG = "FakeMainInteractor";
public FakeMainInteractor() {
Log.i(TAG, "constructor");
}
public Person createPerson(final String name) {
return new Person("Fake Person");
}
}
现在,我们使用一个自定义的FragmentTestRule
来测试片段,该片段独立于产品中包含它的活动:
public class FragmentTestRule<F extends Fragment> extends ActivityTestRule<TestActivity> {
private static final String TAG = "FragmentTestRule";
private final Class<F> mFragmentClass;
private F mFragment;
public FragmentTestRule(final Class<F> fragmentClass) {
super(TestActivity.class, true, false);
mFragmentClass = fragmentClass;
}
@Override
protected void beforeActivityLaunched() {
super.beforeActivityLaunched();
try {
mFragment = mFragmentClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
protected void afterActivityLaunched() {
super.afterActivityLaunched();
//Instantiate and insert the fragment into the container layout
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragmentContainer, mFragment);
transaction.commit();
}
public F getFragment() {
return mFragment;
}
}
testactivity
非常简单:
public class TestActivity extends BaseActivity implements
HasComponent<ActivityComponent> {
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setId(R.id.fragmentContainer);
setContentView(frameLayout);
}
}
但现在如何互换组件呢?有几个小技巧可以实现这一点。首先,我们需要一个holder类来保存TestFragmentComponent
:
/**
* Because neither the Activity nor the ActivityTest can hold the TestActivityComponent (due to
* runtime order problems we need to hold it statically
**/
public class TestFragmentComponentHolder {
private static TestFragmentComponent sComponent;
private static ComponentCreator sCreator;
public interface ComponentCreator {
TestFragmentComponent createComponent(Fragment fragment);
}
/**
* Configures an ComponentCreator that is used to create an activity graph. Call that in @Before.
*
* @param creator The creator
*/
public static void setCreator(ComponentCreator creator) {
sCreator = creator;
}
/**
* Releases the static instances of our creator and graph. Call that in @After.
*/
public static void release() {
sCreator = null;
sComponent = null;
}
/**
* Returns the {@link TestFragmentComponent} or creates a new one using the registered {@link
* ComponentCreator}
*
* @throws IllegalStateException if no creator has been registered before
*/
@NonNull
public static TestFragmentComponent getComponent(Fragment fragment) {
if (sComponent == null) {
checkRegistered(sCreator != null, "no creator registered");
sComponent = sCreator.createComponent(fragment);
}
return sComponent;
}
/**
* Returns true if a custom activity component creator was configured for the current test run,
* false otherwise
*/
public static boolean hasComponentCreator() {
return sCreator != null;
}
/**
* Returns a previously instantiated {@link TestFragmentComponent}.
*
* @throws IllegalStateException if none has been instantiated
*/
@NonNull
public static TestFragmentComponent getComponent() {
checkRegistered(sComponent != null, "no component created");
return sComponent;
}
}
public class MainFragment extends BaseFragment implements MainView {
private static final String TAG = "MainFragment";
@Inject
MainPresenter mainPresenter;
private View view;
// TODO: Rename and change types and number of parameters
public static MainFragment newInstance() {
MainFragment fragment = new MainFragment();
return fragment;
}
public MainFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//((MainActivity)getActivity()).getComponent().inject(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
return view;
}
public void onClick(final String s) {
mainPresenter.onClick(s);
}
@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getComponent().inject(this);
final EditText editText = (EditText) view.findViewById(R.id.edittext);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
MainFragment.this.onClick(editText.getText().toString());
}
});
mainPresenter.attachView(this);
}
@Override
public void updatePerson(final Person person) {
TextView textView = (TextView) view.findViewById(R.id.textview_greeting);
textView.setText("Hello " + person.getName());
}
@Override
public void onDestroy() {
super.onDestroy();
mainPresenter.detachView();
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
前面描述的所有步骤(第二到第四个技巧)都可以在这个MainFragmentTest
类的@before
注释的setup()
-方法中找到:
public class MainFragmentTest implements
InjectsComponent<TestFragmentComponent>, TestFragmentComponentHolder.ComponentCreator {
private static final String TAG = "MainFragmentTest";
@Rule
public FragmentTestRule<MainFragment> mFragmentTestRule = new FragmentTestRule<>(MainFragment.class);
public AndroidApplication getApp() {
return (AndroidApplication) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
}
@Before
public void setUp() throws Exception {
TestFragmentComponentHolder.setCreator(this);
mFragmentTestRule.launchActivity(null);
MainFragment fragment = mFragmentTestRule.getFragment();
if (!(fragment instanceof HasComponent) || !TestFragmentComponentHolder.hasComponentCreator()) {
return;
} else {
((HasComponent<FragmentComponent>) fragment).
setComponent(TestFragmentComponentHolder.getComponent(fragment));
injectFragmentGraph();
waitForFragment(R.id.fragmentContainer, 2000);
}
}
@After
public void tearDown() throws Exception {
TestFragmentComponentHolder.release();
mFragmentTestRule = null;
}
@SuppressWarnings("unchecked")
private void injectFragmentGraph() {
((InjectsComponent<TestFragmentComponent>) this).injectComponent(TestFragmentComponentHolder.getComponent());
}
protected Fragment waitForFragment(@IdRes int id, int timeout) {
long endTime = SystemClock.uptimeMillis() + timeout;
while (SystemClock.uptimeMillis() <= endTime) {
Fragment fragment = mFragmentTestRule.getActivity().getSupportFragmentManager().findFragmentById(id);
if (fragment != null) {
return fragment;
}
}
return null;
}
@Override
public TestFragmentComponent createComponent(final Fragment fragment) {
return DaggerTestFragmentComponent.builder()
.testFragmentModule(new TestFragmentModule())
.build();
}
@Test
public void testOnClick_Fake() throws Exception {
onView(withId(R.id.edittext)).perform(typeText("John"));
onView(withId(R.id.button)).perform(click());
onView(withId(R.id.textview_greeting)).check(matches(withText(containsString("Hello Fake"))));
}
@Test
public void testOnClick_Real() throws Exception {
onView(withId(R.id.edittext)).perform(typeText("John"));
onView(withId(R.id.button)).perform(click());
onView(withId(R.id.textview_greeting)).check(matches(withText(containsString("Hello John"))));
}
@Override
public void injectComponent(final TestFragmentComponent component) {
component.inject(this);
}
}
除了时间问题。这个测试在我的环境中运行了10次,在API级别为23的仿真Android上运行了10次。在一台搭载Android6的真正三星Galaxy S5 Neo设备上,10次测试中有9次可以运行。
正如我在上面所写的,您可以从github下载整个示例,如果您找到一种方法来解决这个小的计时问题,可以随时改进。
就是这样!
我看了几篇不同的文章,这些文章似乎建议在Dagger 2中使用两种不同的方法进行自定义范围界定: > 在配置更改第2部分(Github repo)中幸存的MVP演示者: 为每个片段使用唯一的自定义作用域,例如分别为和的
问题内容: 我在小组活动中有一个片段,我想用另一个片段替换它: 在不使用活动组的情况下作为单独的项目完成时,它工作正常,当控件进入getview()时,每件事在日志猫中都可以正常工作,但是没有视图可见,甚至没有任何异常出现,我希望将书详细信息片段由部分详细信息片段代替。 图书详细信息片段的XML具有id book_description_fragment,而部分描述片段的xml具有id secti
我在android项目中使用Dagger2我有两个作用域:ActivityScope和FragmentScope我读了一些示例代码,他们说定义并使用ActivityScope,所以对象将在activity lifecycle中销毁。因为活动和片段有不同的生命周期,所以我们应该有两个作用域。 我的问题是:我是否需要做一些事情让代码知道,当我使用ActivityScope时,对象应该随活动生命周期一起
我一直在看谷歌Android架构的MVP与匕首2的例子: https://github.com/googlesamples/android-architecture/blob/todo-mvp-dagger/todoapp/app/src/main/java/com/example/android/architecture/blueprents/todoapp/tasksactivity.java
问题内容: 我可能错过了一些东西,但我认为像@Singleton这样的作用域用于定义“作用域生命周期”。 我在Android应用程序中使用了Dagger 2(但我认为问题根本与android相关)。 我有1个模块: 我有两个与范围不同的组件: 两者,并且,有一个构造函数。虽然MenuPresenter期望将其作为参数,但LoginPresenter却采用了: 但是每次我使用这些组件创建一个或时,都
我有个错误 //类别2是片段