How to accept dialog in unit testing? - android

I have a unit test that opens up a custom dialog and enters some text. This works, but I am unable to accept the dialog or get hold of the ok button. Please can anyone tell me how to automate the dialog acceptance using junit.
ActivityMonitor activityMonitor = instrumentation.addMonitor(
EditItem.class.getName(), null, false);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
instrumentation.invokeContextMenuAction(gridList, R.id.add, 0);
Activity activity = instrumentation.waitForMonitorWithTimeout(
activityMonitor, 10);
assertNotNull("Make sure the edit item activity was called", activity);
assertEquals("Make sure the edit item activity was called",
EditItem.class, activity.getClass());
final TextView nameView = (TextView) activity.findViewById(R.id.name);
// this opens the dialog
TouchUtils.clickView(this, nameView);
// this adds some text
for (int i = 0; i < 3; i++)
{
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_X);
}
// here I would like to accept the ok button on the dialog
OK I seem to have worked round this with a combination of key presses
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
The problem is now I can't tell when the dialog has finished being dismissed and returned to the parent Activity to continue the test with a
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Go a work around for this too now:
instrumentation.waitForIdleSync();
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
Seems a bit clunky, is this how you are supposed to use these tools?

I'm having the same problem. What it looks like is that you will have to create a custom dialog. This will allow you to retrieve the buttons you added using the findViewById(). Here is a link that I found that might get you on the right path.
http://www.mkyong.com/android/android-custom-dialog-example/

Related

android- How to prevent enter key/click coming from barcode scanner

Hello I have an application which I got entries by barcode scanner. I use Zebra TC56 as testing device.
I need to show a warning message to the user and that is why I have a custom dialog box.
Dialog box is being showed when user gets an error. Picture of my dialog box can be seen here :
Below red part is a button and when button is clicked , dialog box will be closed and user will turn the latest screen.
Everything works fine but there is somethin I dont want. When user scans a barcode(enters data) button is trigered and dialog box is closed.
I want dialog box to be closed only by clicking the button(TAMAM) from the screen. But when I scan anything, dialog box is closed.
Here is the code of the dialog box class :
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.customdialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
I tried to get focus to somewhere except the button(TAMAM) but it didnt help. If someone knows how to dismiss enter key from the barcode scanner, I really need some help and will be appreciated. (I already set the device to send Enter key because I need in other screens)
Few ways to do this that I can think of.
Firstly, I presume you are using DataWedge to automatically append the enter key to scanned data, you could dynamically switch to a profile which did not send the enter key but was identical in every other way using the SWITCH_PROFILE API: http://techdocs.zebra.com/datawedge/6-3/guide/api/switchtoprofile/. This is presuming that you still need to be able to scan when the dialog is visible, if you wish to disable scanning entirely you could use the SCANNER_INPUT_PLUGIN API: http://techdocs.zebra.com/datawedge/6-3/guide/api/scannerinputplugin/.
Secondly, you could use the EMDK profile API to change the parameters of the KeyStroke output plugin (http://techdocs.zebra.com/emdk-for-android/6-3/mx/data-capture/keystroke/#keystrokeoutput) then apply that newly modified profile. I've never tried that myself but it should work - check out the following sample for the principles behind that: http://techdocs.zebra.com/emdk-for-android/6-3/samples/data-capture/
Thirdly, you could use the Java SDK for the scanner which gives you more control over how the scanner behaves (http://techdocs.zebra.com/emdk-for-android/6-3/api/)

YouTube Android Player API - Programatically perform a click on "skip ad"

Intro
I'm working in an android app that receives commands through http network commands (not through touch).
What I need to accomplish
I want to be able to, whenever a user instructs via http command that wants to perform a click on the skip add, programmatically instruct the youtube-player (embedded in my app), to skip the add.
Looking to the available functions I only can check if the player is showing the advertising or not, but nothing related to being able to skip it through a method.
How I'm trying to solve it
I'm trying to go via:
Get all child views and their childs of the activity
Figure out the view that I need to perform the click
Perform the click through view.performClick()
My problem is that it rarely shows advertising, meaning that I'm unable to complete the task or even know if this will work in a useful time.
Any ideas that can help to solve the main problem?
Thanks in advance.
Ideally, Google can provide an api call that can allow skip ad as it is displayed on the screen "You can skip ad in 5, 4, 3, 2, 1s".
For the TV set top boxs, it has the same problem too. It is common for them not having a mouse, so you cannot move the cursor to the "skip ad" button and click.
My solution is to create a simulated touch event as you can find from this discussion: How to simulate a touch event in Android?.
The trick will be in calculating the x , y for the "skip ad" button though.
I was able to find the view on runtime:
List<View> views = getAllChildrenBFS(mYouTubeView);
for (final View v: views) {
if (v.toString().contains("skip_ad_button")){
v.setFocusable(true);
v.setFocusableInTouchMode(true);
v.requestFocus();
break;
}
}
I set focus on the button, so the user can skip it.
(you can try using performClick though i'm not sure youtube will be very happy about this...)
The BFS search function:
private List<View> getAllChildrenBFS(View v) {
List<View> visited = new ArrayList<View>();
List<View> unvisited = new ArrayList<View>();
unvisited.add(v);
while (!unvisited.isEmpty()) {
View child = unvisited.remove(0);
visited.add(child);
if (!(child instanceof ViewGroup)) continue;
ViewGroup group = (ViewGroup) child;
final int childCount = group.getChildCount();
for (int i=0; i<childCount; i++) unvisited.add(group.getChildAt(i));
}
return visited;
}
Copied from:
https://stackoverflow.com/a/18818750
Thanks to this wonderfull community and thank the lord!

Mono Droid Modal Popup

I am wondering how I go about (if it is possible) creating a modal popup in a mono for droid application.
Scenario: The application talks to the customers hosted web server (so this location will be different customer to customer). To use the app the user must specify the connection string of their web server. So when the application starts and it hits the main activity, the first task I do is check if there is a connection string set in the devices application settings. If not I want to throw up a simple modal popup that allows the user to specify a connection to their server.
I dont really want to start a normal activity because the user will be able to click the back button and just go back to the main menu and the app is than in an invalid state because it doesnt know what server to talk to.
Any ideas on how I go about this?
Or should I be structuring the activity chain so that the connection string is entered on the first activity so that if they click back it actually goes out of the app?
Im a little confused.
Thanks in advance
This is possible with AlertDialog. It can create dialogs for simple input with lists, checkboxes, yes/no buttons and custom views.
There is a sample in the Xamarin Sample Repository for different type of dialogs and in the bottom you can find one where a custom view with a username and password field has been added.
So first define your custom view you want to put in the AlertDialog. alert_dialog_connection_entry.xml and is a Layout:
Somewhere in your activity add the code:
var connection_string_view = LayoutInflater.Inflate(Resource.Layout.alert_dialog_connection_entry, null);
var builder = new AlertDialog.Builder(this);
builder.SetTitle("Connection String");
builder.SetView(connection_string_view);
builder.SetPositiveButton("OK", OkClicked);
builder.SetNegativeButton("Cancel", CancelClicked);
builder.Create();
builder.Show();
Add some handlers for the buttons:
private void CancelClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
//Todo
}
private void OkClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
var dialog = sender as AlertDialog;
if (null != dialog)
{
var connectionEdit = dialog.FindViewById(Resource.Id.connectionstring_edit) as EditText;
if (null != connectionEdit)
Console.WriteLine("Connection String: {0}", connectionEdit.Text);
}
}
That should be it. You should be able to put any kind of custom view in the dialog.
If you just want to display a modal popup for letting users put their connection string, you could try this.
First, you need to have a simple layout for how the dialog is presented. In this case, a TextView displaying something like "Connection string:" and an EditText to let the user put connection string is probably enough to go.
Then, you can put this code somewhere in your MainActivity, like after checking application settings or something similar.
var builder = new AlertDialog.Builder(this);
var view = LayoutInflater.Inflate(Resource.Layout.ModalDialog, null);
builder.SetView(view);
string connectionString = view.FindViewById<EditText>(Resource.Id.ConnectionString).Text;
AlertDialog alert = builder.Create();
alert.SetCancelable(false); //This prevents the dialog from being dismissed by either hit back button or hit out side of the dialog
alert.SetButton("OK", (s,e)=> ToDo(connectionString)); //Now you have the connection string, to do whatever you want.
alert.Show();
As you said, the alternative could be allowing user specify the connection string in the first screen. This is a good approach too. I assume you know how to do it, so I didn't post code here.

Android: How to obtain an overlay on top of acitivity

when my application is installed for the first time by the user , i want to explain him what each activity does (like a quick tip) , it should look like the below picture
it should have the cancel button , and should not repeat after the first install,
I need suggestion or any useful links ,how to implement it, Any help is appreciated.
You could use SharePreference to store the state whether or not user have viewed your instruction. Just add a boolean value named as 'instructionViewed' to SharePreference. Every time your application launched, check the value. If true, don't show instruction. If false, show instruction. When user click the cancel button, set the value to true.
SharePreference is a very easy-using class to help store some information.You could google how use it.
Hope this helpful.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testpage);
//Check SharePreference
boolean instructionViewed = checkInstrunctionState();
//If the instruction is not viewed, show instruction.
if(!instructionViewed){
RelativeLayout yourLayout = createYourOwnUi();
FrameLayout contentView = (FrameLayout)(getWindow().getDecorView().findViewById(android.R.id.content));
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(MATCH_PARENT,MATCH_PARENT);
contentView.addView(yourLayout, lp);
}
}

Android PlusOneButton

I'm trying to make plusone button works inside my android app.
I did this:
m_plusClient = new PlusClient(this, this, this);
Then:
m_plusOneButton = (PlusOneButton) m_view.findViewById(R.id.plus_one_button);
m_plusOneButton.initialize(((SinglePlace)getActivity()).m_plusClient, url, 0);
Button is there, but if I click it, it shows an indefinite progress bar and does nothing. No callbacks are called on plus client...
You need to follow these steps first:
https://developers.google.com/+/mobile/android/getting-started
before you jump to +1 button section.

Categories

Resources