Android,添加InstrumentationTestCase的权限

时间:2014-08-19 08:41:43

标签: android android-calendar android-permissions

我想对操作日历数据(阅读,编辑,删除)进行一些测试,但SecurityException表示测试类没有权限。

注意:读取/写入日历的权限已添加到清单文件中,我的主应用程序/活动已经可以完成这项工作。它是不能的测试类。

任何提示? TKS

更新:这里是清单文件,只是相关部分,我也要重命名包。启动器活动为StartupActivity

<permission
    android:name="com.my.android.project.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.my.android.project.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<uses-feature
    android:name="android.hardware.location"
    android:required="true" />
<uses-feature
    android:name="android.hardware.location.gps"
    android:required="false" />

<application
    android:name="com.master.project.package.app.myapp"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/config_app_name"
    android:theme="@style/V4.DarkActionBar"
    android:screenOrientation="portrait" android:exported="true">

    <!-- Receivers -->
    <receiver
        android:name="com.my.android.project.receiver.AlarmReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.my.android.project.widget.FavouriteListWidgetProvider"
        android:enabled="@bool/list_widget_enabled"
        android:icon="@drawable/ic_launcher"
        android:label="@string/myapp_fav_locations">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.my.android.project.action.list_app_widget_update" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.my.android.project.action.list_item_click" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.my.android.project.littlefluffylocationlibrary.LOCATION_CHANGED" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/favourite_list_widget_provider" />
    </receiver>

    <!-- Providers -->

    <provider
        android:name="com.my.android.project.provider.ScheduledStopsProvider"
        android:authorities="${packageName}.provider.ScheduledStopsProvider"
        android:exported="false"
        android:multiprocess="true" />
    <provider
        android:name="com.my.android.project.provider.RegionsProvider"
        android:authorities="${packageName}.provider.RegionsProvider"
        android:exported="false"
        android:multiprocess="true" />
    <provider
        android:name="com.my.android.project.provider.LocationsProvider"
        android:authorities="${packageName}.provider.LocationsProvider"
        android:exported="true"
        android:multiprocess="true" />
    <provider
        android:name="com.my.android.project.provider.TimetableProvider"
        android:authorities="${packageName}.provider.TimetableProvider"
        android:exported="false"
        android:multiprocess="true" />
    <provider
        android:name="com.my.android.project.provider.TripProvider"
        android:authorities="${packageName}.provider.TripProvider"
        android:exported="false"
        android:multiprocess="true" />
    <provider
        android:name="com.my.android.project.provider.ServiceStopsProvider"
        android:authorities="${packageName}.provider.ServiceStopsProvider"
        android:exported="false"
        android:multiprocess="true" />
    <provider
        android:name=".provider.TransportModeDataProvider"
        android:authorities="${packageName}.provider.TransportModeDataProvider"
        android:exported="false"
        android:multiprocess="true" />

    <!-- Services -->

    <service android:name="com.my.android.project.service.ScheduledStopService" />
    <service android:name="com.my.android.project.service.ServiceStopService" />
    <service android:name="com.my.android.project.service.FetchRegionDataService" />
    <service android:name="com.my.android.project.service.TimetableService" />
    <service android:name="com.master.project.common.service.DbService" />
    <service android:name="com.my.android.project.service.LocationScouringService" />
    <service android:name="com.my.android.project.service.TripService" />
    <service android:name="com.master.project.package.agenda.AgendaHandlingService" />
    <service android:name="com.master.project.package.agenda.SavingToExternalCalendarService" />
    <service android:name="com.my.android.project.service.NotificationService" />
    <service android:name="com.my.android.project.service.AlarmScheduleService" />
    <service android:name="com.my.android.project.service.SingleHitRealTimeCheckService" />
    <service android:name="com.my.android.project.service.PurgeDatabaseService" />
    <service android:name="com.my.android.project.service.PersistTripService"/>
    <service android:name="com.master.project.package.agenda.BackgroundPlanningService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </service>
    <service android:name=".service.CacheService"/>
    <service
        android:name="com.my.android.project.widget.FavouriteListWidgetService"
        android:exported="false"
        android:permission="android.permission.BIND_REMOTEVIEWS" />
    <service
        android:name="com.master.project.package.agenda.AsyncGroundyService"
        android:exported="false">
        <meta-data
            android:name="groundy:mode"
            android:value="async" />
    </service>

    <!-- Activities -->

    <activity
        android:name=".StartupActivity"
        android:label="@string/config_app_name"
        android:launchMode="singleTop"
        android:screenOrientation="sensorPortrait"
        android:theme="@style/V4.NoActionBarPortrait" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.my.android.project.HandleExternalRequestActivity"
        android:label="@string/config_app_name"
        android:screenOrientation="sensorPortrait"
        android:windowSoftInputMode="stateHidden">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="maps.google.com"
                android:scheme="https" />
            <data
                android:host="maps.google.com"
                android:scheme="http" />
            <data android:scheme="geo" />
        </intent-filter>
    </activity>

    <activity android:name="com.master.project.package.activity.EventHandlingActivity"
        android:theme="@style/V4.NoActionBarPortrait"

        >
    </activity>
    <activity android:name="com.master.project.package.activity.EventViewingActivity"
        android:label="@string/event_detail_title"
        android:screenOrientation="portrait"
        />

    <activity
            android:name="com.master.project.package.activity.ExcludeCalendarsActivity"
            android:label="@string/include_cal_act"
            android:screenOrientation="portrait"
        />
    <activity
        android:name=".WhatsNewPagerActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.myapp"
        android:windowSoftInputMode="stateHidden" />
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/V4.NoActionBarPortrait"
        android:windowSoftInputMode="adjustResize"
        />
    <activity
        android:name="com.my.android.project.ViewTimetableActivity"
        android:label="@string/view_timetable_for_stop"
        android:screenOrientation="sensorPortrait"
        android:windowSoftInputMode="stateHidden" />

    <activity
        android:name="com.my.android.project.ViewLocationActivity"
        android:label="@string/view_location_details"
        android:screenOrientation="sensorPortrait"
        android:windowSoftInputMode="stateHidden" />

    <activity
        android:name="com.my.android.project.ServiceMapPagerActivity"
        android:label="@string/page_between_upcoming_services"
        android:screenOrientation="sensorPortrait"
        android:theme="@style/V4.DarkActionBar" />
    <activity
        android:name=".TripResultActivity"
        android:theme="@style/V4.DarkActionBar"
        android:label="@string/routes"
        android:screenOrientation="sensorPortrait" />
    <activity
        android:name="com.master.project.package.activity.SingleTripResultActivity"
        android:label="@string/trip"
        android:screenOrientation="sensorPortrait"
        android:theme="@style/V4.DarkActionBar" />
    <activity
        android:name=".RemindersActivity"
        android:label="@string/reminders"
        android:screenOrientation="sensorPortrait" />
    <activity
        android:name=".SettingsActivity"
        android:label="@string/settings"
        android:screenOrientation="sensorPortrait" />
    <activity
        android:name=".TermsActivity"
        android:label="@string/about_myapp"
        android:screenOrientation="sensorPortrait" />
    <activity
        android:name=".FavouritesActivity"
        android:label="@string/favourites"
        android:screenOrientation="sensorPortrait" />
    <activity
        android:name=".WebViewActivity"
        android:label="@string/favourites"
        android:screenOrientation="sensorPortrait" />
    <activity
        android:name="com.master.project.package.activity.AgendaActivity"
        android:label="@string/v4_agenda"
        android:screenOrientation="portrait"
        android:theme="@style/V4.DarkActionBar" />
    <activity
        android:name="com.master.project.package.activity.SearchLocationActivity"
        />
    <activity
        android:name="com.master.project.package.activity.TestMapActivity"
        android:theme="@style/V4.NoActionBarPortrait" />

    <!-- UserVoice SDK -->
    <activity android:name="com.uservoice.uservoicesdk.activity.PortalActivity" />
    <activity android:name="com.uservoice.uservoicesdk.activity.ForumActivity" />
    <activity android:name="com.uservoice.uservoicesdk.activity.ArticleActivity" />
    <activity android:name="com.uservoice.uservoicesdk.activity.TopicActivity" />
    <activity
        android:name="com.uservoice.uservoicesdk.activity.ContactActivity"
        android:configChanges="orientation|keyboardHidden|screenSize" />
    <activity
        android:name="com.uservoice.uservoicesdk.activity.PostIdeaActivity"
        android:configChanges="orientation|keyboardHidden|screenSize" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/config_maps_v2_api_key" />

    <!-- Little Fluffy Location Library -->
    <service android:name="com.littlefluffytoys.littlefluffylocationlibrary.LocationBroadcastService" />

    <receiver
        android:name=".receiver.BootCompleted"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <receiver
        android:name="com.littlefluffytoys.littlefluffylocationlibrary.StartupBroadcastReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver"
        android:exported="true" />


</application>

更新2 这是错误日志:

Running tests
Test running started
java.lang.SecurityException: com.my.android.project.debug.test from uid 10110 not allowed to perform READ_CALENDAR
at android.os.Parcel.readException(Parcel.java:1465)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
at android.content.ContentResolver.query(ContentResolver.java:461)
at android.content.ContentResolver.query(ContentResolver.java:404)
at com.master.project.package.app.test.ExternalCalendarTest.getCalendarInstanceData(ExternalCalendarTest.java:56)
at com.master.project.package.app.test.ExternalCalendarTest.testDeleteInstance(ExternalCalendarTest.java:90)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

1 个答案:

答案 0 :(得分:0)

您是否曾尝试在清单文件的{{1>} 活动字段中添加android:exported="true"