开发者

Permission in Android test case

My application should have some changes after some time (in hours or days) and I would like to test that.

I try to use开发者_StackOverflow中文版 SystemClock.setCurrentTimeMillis() in my unit test to simulate the change of time/date, but without any luck.

I declared the <uses-permission android:name="android.permission.WRITE_SETTINGS" /> in the manifest of both the application and the test application, that did not change anything.

At this time, I am running those tests on the emulator if that makes any difference...

Edit : With Nick's help, also requested SET_TIME permission:

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

But now, the logcat shows :

WARN/PackageManager(59): Not granting permission android.permission.SET_TIME to package com.matthieu.tests (protectionLevel=3 flags=0xbe46)

Another Edit : With dtmilano's answer...

Added this code (with the right try/catch) in my test:

Runtime.getRuntime().exec("date \"$((10800 + $(date +%s)))\"").waitFor();

When I go on the shell, I can run that command without any problem and I see the time changing in the emulator (I am trying to add 3 hours). When I run my tests with my code, the time does not change at all... ?

What I do on the command line is :

date "$((10800 + $(date +%s)))"

I doubt I need to add the "adb shell" part in the Runtime.exec...

Seems like this is on the right path, but still not able to get it running. And it might also go along what Nick pointed to that it needs to be a system process to be able to change the time... ?


Add android.permission.SET_TIME_ZONE to your manifest if you need to change the timezone,

then from your activity (or wherever you need it.. but you'll need to grab an appropriate context..)

Context context = getContext(); // or (Context) this, or whatever depending on where you are
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.setTimeZone(timezoneid); // replace timezoneid with the time zone you need e.g. "pacific/Auckland"
mgr.setTime(timeinmillis); // replace timeinmillis with the time you need in millis, probably from a Date or Calendar object... but watch the timezone in the calendar ;-)

The whole point being that the AlarmManager system service contains members to set the system time and timezone..


http://developer.android.com/reference/android/Manifest.permission.html#SET_TIME

I searched "android permissions" then on the permissions page, did a find on the word "time". if you are past api level 8, i would recommend you request that permission.

Update: based on this link, I do not think it is possible to set the system time from a user space app. For your testing purposes, you may need to manually change the time on the emulator. The only other option I have found requires building and signing your own Android build.


From your host (assuming you are using linux) you may run:

$ adb shell date $(date --date='2011-06-11 12:10:10' +%s.0)

mainly if is the emulator. Replace the date and time with the desired values. You should find a way of synchronizing this change with your tests or you can even run it from your tests using Runtime.exec().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜