我有一个ListFragment活动。 我需要实施场景(在Espresso的帮助下):
我有第四点的问题。 我已阅读文章[https://google.github.io/android-testing-support-library/docs/espresso/advanced/#asserting-that-a-view-is-not-present][1]。
作者建议实施匹配器:
private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("with class name: ");
dataMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof AdapterView)) {
return false;
}
@SuppressWarnings("rawtypes")
Adapter adapter = ((AdapterView) view).getAdapter();
for (int i = 0; i < adapter.getCount(); i++) {
if (dataMatcher.matches(adapter.getItem(i))) {
return true;
}
}
return false;
}
};
}
然后打电话:
onView(withId(R.id.list))
.check(matches(not(withAdaptedData(withItemContent("item: 168")))));
问题是在我的情况下我没有AdapterView。我在函数withId()中传递ListFragment的id。所以匹配器不起作用。 问题是如何将withAdaptedData的代码更改为与ListFragment一起使用?或者,可能有另一种方法来解决我的任务?
答案 0 :(得分:0)
不幸的是,我没有找到比不使用Espresso解决这个问题更好的事情。我只是实现了检查数据不在ListFragment中的方法。
package com.zetcode;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
public class GroupLayoutMultipleColumns extends JFrame {
public GroupLayoutMultipleColumns() {
initUI();
}
private void initUI() {
JPanel green1 = createPanel(new Color(60, 170, 70));
JPanel green2 = createPanel(new Color(60, 170, 70));
JPanel red1 = createPanel(new Color(230, 0, 50));
JPanel red2 = createPanel(new Color(230, 0, 50));
JPanel red3 = createPanel(new Color(230, 0, 50));
JPanel red4 = createPanel(new Color(230, 0, 50));
JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();
createLayout(green1, red1, red2, red3, red4,
combo1, combo2, green2);
setTitle("Multiple columns");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public JPanel createPanel(Color col) {
JPanel panel = new JPanel();
panel.setBackground(col);
return panel;
}
private void createLayout(JComponent... arg) {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup()
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
.addComponent(arg[4]))
.addGroup(gl.createParallelGroup()
.addComponent(arg[5])
.addComponent(arg[6])))
.addComponent(arg[7])
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addGroup(gl.createSequentialGroup()
.addGroup(gl.createParallelGroup()
.addGroup(gl.createSequentialGroup()
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
.addComponent(arg[4]))
.addGroup(gl.createSequentialGroup()
.addComponent(arg[5], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(arg[6], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
.addComponent(arg[7])
);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
GroupLayoutMultipleColumns ex = new GroupLayoutMultipleColumns();
ex.setVisible(true);
});
}
}
但我相信有更好的解决方案。