2

In my android App, there is an Activity which shows time string according to the System Time Format (12Hr. / 24Hr.). I am writing espresso test to test this behavior whether the time displays gets changed as the System time format changes.

In order to achieve this, i want to change my System time format through my Instrumentation test for pure testing purpose. and i've added permission in test project's manifest, like this

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

and wrote the following code to change system time format

Context context = InstrumentationRegistry.getContext();
Settings.System.putString(context.getContentResolver(),Settings.System.TIME_12_24, "12");

and it is throwing Permission Denial error. (you can use below link to see the stack trace)

Error StackTrace Screenshot

i am not able to understand, since the code which is trying to change the system time format in the test project itself and it has permission to change the system time setting, I verified Using this command adb shell dumpsys package com.my.app.package | grep permission

Please tell me, what i am missing here ?

Ruchi kalra
  • 21
  • 1
  • 2
  • Possible duplicate of [Set Android's date/time programmatically](http://stackoverflow.com/questions/9434239/set-androids-date-time-programmatically) – Code-Apprentice Feb 20 '16 at 19:53

1 Answers1

0

I've already found an explanation to your issue. Here is it:

The user application does not have permission to change the device time. Please read the answer by cashbash in the following post for the alternate option.

Copying here for quick reference:

According to this thread, user apps cannot set the time, regardless of the permissions we give it. Instead, the best approach is to make the user set the time manually. We will use:

startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));

Unfortunately, there is no way to link them directly to the time setting (which would save them one more click). By making use of ellapsedRealtime, we can ensure that the user sets the time correctly.

From: Set Android's date/time programmatically

Try also instead of using Espresso, use uiatomator as that instrumentation test framework can perform actions with Dialogs, Marshmallow permissions or lockscreen.

It works perfectly with Espresso. Check this site: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

Hope it help

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94