[android-developers] Re: Android JUnit Testing of Paste
Can you clarify? You want to simulate a long press in an edit box, followed by a selection of the paste menu item? If so, get hold of the Android 2.2 SDK documentation. In it, under the Resources tab, there's a tutorial on unit testing an Activity: http://developer.android.com/resources/tutorials/testing/activity_test.html This uses the test case class ActivityInstrumentationTestCase2, which inherits from InstrumentationTestCase. Part of the tutorial shows how to get focus on a UI widget. In your case, you'd get focus on the edit box. From there, you need to bring up the context menu. I think you could use TouchUtils.longClickView() to do that. That puts focus on the menu, I think. From there, send DPAD_DOWN key events to move the highlight to the paste option, then DPAD_ENTER to select it. Keep in mind that I've never tried to do this on a menu. I just provide this as a guideline for trying to solve the problem. Also keep in mind that Android JUnit testing is really focused on *unit* testing. If you're really trying to test functionality, there are better options. IMHO, the difference is that unit testing goes method-by-method, with the assumption that all methods and classes are independent. You inject all external dependencies, either with mocks or actual objects. Functional testing goes from the beginning of a user task to the end. For example, a functional test of a calculator app would enter a 2, enter a +, enter another 2, enter an =, and check to see that 4 is displayed. On Nov 4, 12:41 pm, Zymurgeek zymurg...@gmail.com wrote: How do I go about simulating a context menu paste command using the Android JUnit tests? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Android JUnit Testing of Paste
Hi Elk, Yes, I wanted to do a paste into a field just as a user would: long press for context menu, then select Paste. Particularly, I had identified a problem with pasting a value into a field rather than entering with individual key presses (my bad, not an Android problem). This isn't a functional test really, rather a component test focused on the UI for a single field--I have detailed JUnit tests for the app's data model. I find the Android JUnit framework to be quite handy for testing this. But if you have a favorite tool for functional testing, I'd like to know about it. Your suggestion was spot on. Thank you! The code for the working test follows. --Z /* Verify proper formatting of values in the bill total field * when data is pasted into the field. */ public void testBillTotalPaste() { mActivity.runOnUiThread( new Runnable() { public void run() { billTotalEntryView.requestFocus(); } } ); mInstrumentation.waitForIdleSync(); mActivity.runOnUiThread( new Runnable() { public void run() { ClipboardManager clipboardManager = (ClipboardManager) mActivity.getSystemService (android.content.Context.CLIPBOARD_SERVICE); clipboardManager.setText(120.56); } } ); mInstrumentation.waitForIdleSync(); TouchUtils.longClickView(this, billTotalEntryView); mInstrumentation.waitForIdleSync(); int[] keyCodes = { KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_CENTER }; for (int keyCode : keyCodes) { this.sendKeys(keyCode); } mInstrumentation.waitForIdleSync(); assertEquals(Incorrect pasted value, 120.56, billTotalEntryView.getText().toString()); } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en