-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #357 from MikeOrtiz/TouchCoordinates
Touch coordinates
- Loading branch information
Showing
6 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
app/src/androidTest/java/info/touchimage/demo/TouchTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package info.touchimage.demo | ||
|
||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.moka.utils.Screenshot | ||
import info.touchimage.demo.utils.TouchAction | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class TouchTest { | ||
|
||
@get:Rule | ||
var activityScenarioRule = ActivityScenarioRule(SingleTouchImageViewActivity::class.java) | ||
|
||
@Test | ||
fun testSingleTouch() { | ||
onView(withId(R.id.imageSingle)).perform(TouchAction(4f, 8f)) | ||
Screenshot.takeScreenshot("touch1") | ||
onView(withId(R.id.imageSingle)).perform(TouchAction(40f, 80f)) | ||
Screenshot.takeScreenshot("touch2") | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
app/src/androidTest/java/info/touchimage/demo/utils/TouchAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package info.touchimage.demo.utils | ||
|
||
import android.view.View | ||
import androidx.test.espresso.UiController | ||
import androidx.test.espresso.ViewAction | ||
import androidx.test.espresso.action.MotionEvents | ||
import androidx.test.espresso.matcher.ViewMatchers | ||
import org.hamcrest.Matcher | ||
|
||
class TouchAction(private val x: Float, private val y: Float) : ViewAction { | ||
|
||
override fun getConstraints(): Matcher<View> = ViewMatchers.isDisplayed() | ||
|
||
override fun getDescription() = "Send touch events" | ||
|
||
override fun perform(uiController: UiController, view: View) { | ||
// Get view absolute position | ||
val location = IntArray(2) | ||
view.getLocationOnScreen(location) | ||
|
||
// Offset coordinates by view position | ||
val coordinates = floatArrayOf(x + location[0], y + location[1]) | ||
val precision = floatArrayOf(1f, 1f) | ||
|
||
// Send down event, pause, and send up | ||
val down = MotionEvents.sendDown(uiController, coordinates, precision).down | ||
uiController.loopMainThreadForAtLeast(200) | ||
MotionEvents.sendUp(uiController, down, coordinates) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
touchview/src/main/java/com/ortiz/touchview/OnTouchCoordinatesListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ortiz.touchview | ||
|
||
import android.graphics.PointF | ||
import android.view.MotionEvent | ||
import android.view.View | ||
|
||
interface OnTouchCoordinatesListener { | ||
fun onTouchCoordinate(view: View, event: MotionEvent, bitmapPoint: PointF) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters