4

I'm trying to use the new GrantPermissionRule that's a part of the latest support library.

In my Manifest I declare the following:

<uses-permission android:name="android.permission.SET_TIME"/>
And in my code I call:

@Rule
public GrantPermissionRule mPermissionRule = GrantPermissionRule.grant(Manifest.permission.SET_TIME);

But when I run the test I see the error:

12-28 14:09:35.063 7193-7215/com.blah.test E/GrantPermissionCallable: Permission: android.permission.SET_TIME cannot be granted!
12-28 14:09:35.066 7193-7215/com.blah I/TestRunner: failed: maxOfflineWithChangingDeviceTime(com.blah)
12-28 14:09:35.066 7193-7215/com.blah I/TestRunner: ----- begin exception -----
12-28 14:09:35.073 7193-7215/com.blah I/TestRunner: junit.framework.AssertionFailedError: Failed to grant permissions, see logcat for details
                                                                              at junit.framework.Assert.fail(Assert.java:50)
                                                                              at android.support.test.runner.permission.PermissionRequester.requestPermissions(PermissionRequester.java:110)
                                                                              at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:108)
                                                                              at org.junit.rules.RunRules.evaluate(RunRules.java:20)
                                                                              at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                                                                              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
                                                                              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                                                                              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                              at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                              at org.junit.runners.Suite.runChild(Suite.java:128)
                                                                              at org.junit.runners.Suite.runChild(Suite.java:27)
                                                                              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                              at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                              at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                                                                              at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                                              at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
                                                                              at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
                                                                              at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
12-28 14:09:35.073 7193-7215/com.blah I/TestRunner: ----- end exception -----

My build.gradle has the following lines updated:

androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'

Any suggestions or solutions?

Faux Pas
  • 536
  • 1
  • 8
  • 20

2 Answers2

6

SET_TIME is a signature|privileged-level permission. You cannot request it at runtime, whether using requestPermissions() or GrantPermissionRule.

And, unless you are building your own firmware or are planning on your app being installed by root users on the privileged partition, you may as well remove the <uses-permission> element from the manifest.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I figured out a way to "manipulate" the system time if you use the emulator.

If you set/change the hosts system time before you start the emulator, the emulator will take the hosts time.

This way I could, on my Windows, set the time to my desired time and then start the emulator and execute the tests.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92