Click on Camera Shutter using UIAutomator - android-camera

I am writing an Espresso test for my app, and am trying to automate clicking the shutter button after opening of a camera in my app.
I am using Espresso and UIAutomator in the Android Emulator. I managed to dump this UI in UIAutomatorViewer.
I can't figure out why I am unable to click on the shutter button using UIAutomator using this code:
public void clickCameraShutterButton() throws UiObjectNotFoundException
{
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiSelector shutterSelector = new UiSelector().resourceId("com.android.camera:id/shutter_button");
UiObject shutterButton = device.findObject(shutterSelector);
shutterButton.click();
}
The camera just sits there, and the shutter button is never clicked. This is the stack trace I'm getting in the Android Studio monitor:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
Any advice would be appreciated.

You can try this code:
device.findObject(new UiSelector().resourceId("com.android.camera:id/shutter_button")).click();
or
device.findObject(new UiSelector().description("Shutter button")).click();
or
device.executeShellCommand("input keyevent 27");
this mean KEYCODE_CAMERA value is 27
or
device.click(x,y);

This worked for me
#Before
public void setUp() {
// Initialize UiDevice instance
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
mDevice = UiDevice.getInstance(instrumentation);
}
...
/**
* ##Test comment here##
*
* #throws Exception
*/
#Test
public void culebraGeneratedTest_CameraShutter() throws Exception {
mDevice.findObject(By.res("com.android.camera2:id/shutter_button").desc("Shutter").clazz("android.widget.ImageView").text(Pattern.compile("")).pkg("com.android.camera2")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
}
This test finds the shutter and clicks on it.
If you are interested this test was automatically generated using CulebraTester.

Just need to put the UI Automator Viewer "resource-id" value at the place of *
mdevice.findObject(new UiSelector().resourceId("*")).click();

Related

Network Client use wrong input devices when using Unity.Netcode with Starter Asset Controller (New input system)

The server is always using Keyboard and Mouse just fine.
The client however always use "xbox controller" instead of Keyboard & Mouse:
The following is the inspector view as a client:
The Start Asset input action are unchanged,
This is what I tried but client is still being assigned to controller:
private void Start()
{
if (!IsOwner)
{
Destroy(GetComponent<PlayerInput>());
}
}
How could I fix this? Other than hard coding (PlayerInput)map.SwitchCurrentControlScheme("KeyboardAndMouse");?
This issue is fixed by disabling Player Input script.
And only enable it on Network Spawn.
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (IsOwner)
{
_playerInput = GetComponent<PlayerInput>();
_playerInput.enabled = true;
}
}

Unity (Custom Editor) Save Data When Exit Unity

I created a simple Custom Editor that shows how much time I spent on Unity. When the button on it is pressed, it records the start time in a Scriptable Object (It's dirty). When the button is pressed again, it records the end time. If the window is closed before the button is pressed, I use the OnDestroy() method to complete the recording. It works (I also use "ExecuteInEditMode").
Here is the problem: If I close Unity without pressing the button, the OnDestroy() method does not work this time. Is there any way to fix this?
You could add a callback to EditorApplication.quitting
Unity raises this event when the editor application is quitting.
Add an event handler to this event to receive a notification that the application is quitting.
Note that this will not fire if the Editor is forced to quit or if there is a crash. This event is raised when the quitting process cannot be cancelled.
Note btw that you could do it completely automated without a button or a ScriptableObject:
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
public static class LogEditorTime
{
private static bool isLogging;
private static readonly StopWatch sw = new StopWatch ();
static void OnQuit()
{
sw.Stop();
var elapsedTime = sw.Elapsed;
Debug.Log($"Quitting the Editor after {elapsedTime.Hours}h {elapsedTime.Minutes}m {elapsedTime.Seconds}.{elapsedTime.Milliseconds / 10}s");
}
[InitializeOnLoadMethod]
static void OnLoad()
{
if(isLogging) return;
sw.Restart();
EditorApplication.quitting -= OnQuit;
EditorApplication.quitting += OnQuit;
isLogging = true;
}
}

Loader during Unity IAP Callback

I want to put loader in between dialog boxes come up for the purchase. What is the way for this?
Because when game player press Buy button, he should require to wait for 5 to 10 second depends on internet speed and server response and this process happed 2 to 3 times because multiple dialogs come up within screen.
So in this case, may be player can leave the screen. I want to put the loader so that game player realise that some processing is running in background, he required to wait for some time.
At present I was following completely this code for Unity IAP setup.
Integrating Unity IAP In Your Game
I assume this is for mobile platform but even if its not still the following can be considered:
Simple solution is to create a full screen Image (UI/Panel) object in your UI to block clicks. I would use Animator component (with triggers) to display this panel in front of other UI when there is a background process running.
public class Loader : MonoBehaviour
{
public static Loader Instance;
Animator m_Animator;
public bool Loading {get; private set;}
void Awake()
{
Instance = this; // However make sure there is only one object containing this script in the scene all time.
}
void Start()
{
//This gets the Animator, which should be attached to the GameObject you are intending to animate.
m_Animator = gameObject.GetComponent<Animator>();
Loading = false;
}
public void Show()
{
Loading = true;
m_Animator.SetBool("Loading", Loading); // this will show the panel.
}
public void Hide()
{
Loading = false;
m_Animator.SetBool("Loading", Loading); // this will hide the panel.
}
}
Then in any script which manipulates UI:
public void BuyButtonClicked()
{
Loader.Instance.Show();
// process time taking stuff
Loader.Instance.Hide();
}
You can also create any kind of loading animation as child of panel object using simple images and animation tool inside Unity (for example rotating animation (use fidget spinner, its cool)).
And in case of Android where user have option to leave screen by pressing OS back button you can prevent going back by checking if any loading is in progress by following example:
// code for back button
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
BackButtonPressed();
}
}
void BackButtonPressed()
{
if(Loader.Instance.Loading)
return;
// use back button event. (For example to leave screen)
}
Hope this helps ;)

Using BluetoothLEAdvertisementWatcher in background application?

I try to scan BLE devices in my Windows 10 IOT background (headless) app running on Raspberry PI 3.
I also tried to use BluetoothLEAdvertisementWatcher in a headed app (with UI) on the same RaspBerry PI machine and it worked.
My headless app is the simplest as it can be:
public sealed class StartupTask : IBackgroundTask
{
private readonly BluetoothLEAdvertisementWatcher _bleWatcher =
new BluetoothLEAdvertisementWatcher();
public void Run(IBackgroundTaskInstance taskInstance)
{
_bleWatcher.Received += _bleWatcher_Received;
_bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
_bleWatcher.Start();
}
private void _bleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
}
}
_bleWatcher_Received is never hit. Capabilities are set (Bluetooth, Internet, Proximity).
What is the problem? What do I miss?
You app shuts down when the run method completes. That's why _bleWatcher_Received is never hit.
To prevent you app from exiting you need call the “GetDeferral” method like this:
public void Run(IBackgroundTaskInstance taskInstance)
{
deferral = taskInstance.GetDeferral();
//YOUR CODE HERE
}
For more information please reference "Developing Background Applications".

Google Plus Profile Picture Google Play Game Services

I've already seen all post in this forum but i haven't found anything.
How can i show in an ImageView the Profile picture of google plus of person who log in my app? with ImageManager?
can you show some code please?
The method below does what you want.
public class AvatarImage extends ImageView {
Bitmap image;
public static final String TAG = "AvatarImage";
public AvatarImage(Context context) {
super(context);
setImageResource(R.drawable.avatar);
}
/**
* This method takes the participant object and attempts to
* download avatar of the participant from google plus
* if this is unsuccesful default avatar is shown
* #param p
*/
public void setImageFromParticipant(Participant p) {
ImageManager im = ImageManager.create(getContext());
im.loadImage(this, p.getIconImageUri(), R.drawable.avatar);
try{
Bitmap bitmap = ((BitmapDrawable)this.getDrawable()).getBitmap();
setImageBitmap(bitmap);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
The code as shown for the other answer from John is correct, however, there is currently an issue with Google Play Service Library as shown here Google Issue Tracker for anyone using ver 10 of the library (and perhaps ver 9.. version 8 is good). Unfortunately for this issue, if you even attempt to get the URI decoded by ImageManager, Google Play Services quits working. Either wait for next ver of the library, or try to get ahold of ver 8.
(I couldn't put this in as a comment on the answer above, so had to go with a new answer...)