我的片段如下
public class NerdLauncherFragment extends Fragment {
private RecyclerView mRecyclerView;
private final String TAG = this.getTag();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_nerd_launcher, container, false);
mRecyclerView = view.findViewById(R.id.app_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
setAdapter();
return view;
}
private void setAdapter() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager packageManager = getActivity().getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
Collections.sort(resolveInfos, new Comparator<ResolveInfo>() {
@Override
public int compare(ResolveInfo first, ResolveInfo second) {
PackageManager pm = getActivity().getPackageManager();
return String.CASE_INSENSITIVE_ORDER.compare(
first.loadLabel(pm).toString(), second.loadLabel(pm).toString());
}
});
mRecyclerView.setAdapter(new ActivityAdapter(resolveInfos));
Log.i(TAG, "Found : " + resolveInfos.size() + " activities.");
}
}
我的 RecyclerView 适配器
public class ActivityAdapter extends RecyclerView.Adapter<ActivityHolder> {
private List<ResolveInfo> mResolveInfos;
public ActivityAdapter(List<ResolveInfo> resolveInfos) {
mResolveInfos = resolveInfos;
}
@NonNull
@Override
public ActivityHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.simple_list_item_1, parent, false);
return new ActivityHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ActivityHolder holder, int position) {
ResolveInfo resolveInfo = mResolveInfos.get(position);
holder.bindView(resolveInfo);
}
@Override
public int getItemCount() {
return mResolveInfos.size();
}
}
查看持有人
public class ActivityHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mTextView;
private ImageView mImageView;
private ResolveInfo mResolveInfo;
public ActivityHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.id_app_name);
mImageView = itemView.findViewById(R.id.id_app_image);
}
public void bindView(ResolveInfo resolveInfo) {
mResolveInfo = resolveInfo;
PackageManager pm = getActivity().getPackageManager();
String appName = mResolveInfo.loadLabel(pm).toString();
mImageView.setImageDrawable(mResolveInfo.loadIcon(pm));
mTextView.setText(appName);
mTextView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ActivityInfo activityInfo = mResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN).setClassName(
activityInfo.applicationInfo.packageName, activityInfo.name).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
布局文件:fragment_nerd_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/app_recycler_view">
</androidx.recyclerview.widget.RecyclerView>
ViewAdapter 布局:simple_list_item_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_width="match_parent">
<TextView
android:id="@+id/id_app_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat"
android:layout_weight="1">
</TextView>
<ImageView
android:id="@+id/id_app_image"
android:layout_width="wrap_content"
android:layout_height="48dp" />
</LinearLayout>
espresso 测试不调用 onclick 方法。
@Override
public void onClick(View v) {
ActivityInfo activityInfo = mResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN).setClassName(
activityInfo.applicationInfo.packageName, activityInfo.name).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
当我在 Android Studio 中运行应用程序时,会调用 onclick 侦听器,但不会在我编写 espresso 测试时调用。我的浓缩咖啡测试如下
@RunWith(AndroidJUnit4.class)
@LargeTest
public class NerdLauncherActivityTest {
@Rule
public ActivityScenarioRule<NerdLauncherActivity> activityScenarioRule =
new ActivityScenarioRule<NerdLauncherActivity>(NerdLauncherActivity.class);
@Before
public void setUp() {
Intents.init();
}
@After
public void tearDown() {
Intents.release();
}
@Test
public void testLaunchApp() {
Espresso.onView(ViewMatchers.withId(R.id.app_recycler_view)).perform(
RecyclerViewActions.actionOnItemAtPosition(0, ViewActions.click()));
Matcher<Intent> expectedIntent = Matchers.allOf(IntentMatchers.hasAction(Intent.ACTION_MAIN),
IntentMatchers.hasFlag(Intent.FLAG_ACTIVITY_NEW_TASK));
Intents.intended(expectedIntent);
Intents.intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
}
}
它在这条线上失败
Intents.intended(expectedIntent);
堆栈跟踪
IntentMatcher: (has action: is "android.intent.action.SEND" and flags: 10000000)
Matched intents:[]
Recorded intents:[]
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1720)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:324)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:306)
at androidx.test.espresso.intent.Intents.intended(Intents.java:190)
at androidx.test.espresso.intent.Intents.intended(Intents.java:170)
at
. . . . .
. . . . .
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.
我不知道我做错了什么。