gpt4 book ai didi

android - 如何在 Robolectric 的 ViewPager 上模拟拖动事件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:46 26 4
gpt4 key购买 nike

我是 Android 和 Robolectric 的新手。我正在尝试模拟(如主题所示)viewpager 上的拖动事件。

我曾经实现过这段代码并且它在模拟器上运行。

问题主要是:如何对它进行单元测试?

这是单元测试:

/** imports are skipped **/

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "/src/main/AndroidManifest.xml")
public class TestImages {

private ImageActivity activity;

@InjectResource(R.drawable.ic_launcher)
private static Drawable image;

@Spy
private final PicturesModel picturesModel = spy(new PicturesModelImpl());

/**
* Last touched view
*/
private View lastTouchView;

/**
* Last touched X coordinate
*/
private float lastTouchX;
/**
* Last touched Y coordinate
*/
private float lastTouchY;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);

final Module roboGuiceModule = RoboGuice.newDefaultRoboModule(Robolectric.application);
final Module pictureModule = Modules.override(roboGuiceModule).with(new PictureModule());
final Module testModule = Modules.override(pictureModule).with(new TestPictureModule(picturesModel));

RoboGuice.setBaseApplicationInjector(Robolectric.application, RoboGuice.DEFAULT_STAGE, testModule);
RoboInjector injector = RoboGuice.getInjector(Robolectric.application);
injector.injectMembersWithoutViews(this);
}

@Test
public void scroll_image() {
final Intent intent = new Intent(Robolectric.getShadowApplication().getApplicationContext(), ImageActivity.class);
final Album album = new Album(1, "album_1", image);
intent.putExtra("album", album);
intent.putExtra("position", 2);
intent.putExtra("imageId", 2L);

// Get the activity
activity = Robolectric.buildActivity(ImageActivity.class).withIntent(intent).create().get();
final Point point = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(point);

// Get a spy viewer otherwise viewPager's width is always 0
final ViewPager viewPager = spy((ViewPager) activity.findViewById(R.id.viewPager));
viewPager.setVisibility(View.VISIBLE);
when(viewPager.getWidth()).thenReturn(point.x - 50);

// First item sent by viewPager before swipe
final int firstItem = viewPager.getCurrentItem();

// Swipe
drag(viewPager, 10F, 10F, point.x - 60F, 10F);

// Next item after swipe
final int secondItem = viewPager.getCurrentItem();

// Comparison
assertThat(firstItem).isEqualTo(2);
assertThat(secondItem).isEqualTo(3);
}

public void touchDown(View view, float x, float y) {
lastTouchX = x;
lastTouchY = y;
lastTouchView = view;

sendMotionEvent(view, MotionEvent.ACTION_DOWN, x, y);
}

public void touchMove(float x, float y) {
lastTouchX = x;
lastTouchY = y;

sendMotionEvent(lastTouchView, MotionEvent.ACTION_MOVE, x, y);
}

public void touchUp() {
sendMotionEvent(
lastTouchView, MotionEvent.ACTION_UP, lastTouchX, lastTouchY);

lastTouchView = null;
}

public void drag(View view, float xStart, float yStart,
float xEnd, float yEnd) {
touchDown(view, xStart, yStart);
touchMove(xEnd, yEnd);
touchUp();
}

private void sendMotionEvent(View view, int action, float x, float y) {

int[] screenOffset = new int[2];
view.getLocationOnScreen(screenOffset);

MotionEvent event = MotionEvent.obtain(100, 200, action,
x + screenOffset[0], y + screenOffset[1], 0);

shadowOf(event).setPointerIds(1, 2);
shadowOf(event).setPointerIndex(1);

view.onTouchEvent(event);
view.dispatchTouchEvent(event);
}
}

和 Activity :

public class ImageActivity extends RoboActivity {

@Inject
private PicturesModel picturesModel;

@InjectView(R.id.viewPager)
private ViewPager viewPager;

private PagerAdapter pagerAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);

final Bundle bundle = getIntent().getExtras();
if (bundle != null && !bundle.isEmpty()) {
final long id = (long) bundle.get("imageId");
final int position = (int) bundle.get("position");
final Picture picture = picturesModel.getPicture(id);
if (picture != null) {

pagerAdapter = new ImageAdapter(this, picturesModel.getAllPictures((Album) bundle.get("album")));
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(position, true);
}
}
}
}

这是布局:

    <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewPager" />

有人知道如何进行或告诉我我的错误在哪里吗?

最佳答案

我想你要找的是 Espresso图书馆。

它模拟 UI 事件,让您在抛出事件后检查 View 的状态,它非常易于使用。只是不要忘记在开发人员设置中关闭动画。

关于android - 如何在 Robolectric 的 ViewPager 上模拟拖动事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com