cmsdk: Add PerformanceManager tests, Change target permission.

Since PerformanceManager is strictly a CyanogenMod construct,
  it doesn't make sense to enforce the interfaces with an android
  specific permission (even though the implementation is delegated
  to power manager). To keep consistency with the other api's,
  modify the enforcing permission to a cm specific declaration.

  Also add test cases for the PerformanceManager public interfaces.

Change-Id: I430b69dbee73bf94bb60932d1942ab97e3ba193e
This commit is contained in:
Adnan Begovic 2016-03-07 11:49:52 -08:00
parent 7ed4fcaf8e
commit b4eafda7de
8 changed files with 128 additions and 16 deletions

View File

@ -569,6 +569,7 @@ package cyanogenmod.platform {
field public static final java.lang.String MODIFY_NETWORK_SETTINGS = "cyanogenmod.permission.MODIFY_NETWORK_SETTINGS";
field public static final java.lang.String MODIFY_PROFILES = "cyanogenmod.permission.MODIFY_PROFILES";
field public static final java.lang.String MODIFY_SOUND_SETTINGS = "cyanogenmod.permission.MODIFY_SOUND_SETTINGS";
field public static final java.lang.String PERFORMANCE_ACCESS = "cyanogenmod.permission.PERFORMANCE_ACCESS";
field public static final java.lang.String PROTECTED_APP = "cyanogenmod.permission.PROTECTED_APP";
field public static final java.lang.String PUBLISH_CUSTOM_TILE = "cyanogenmod.permission.PUBLISH_CUSTOM_TILE";
field public static final java.lang.String READ_ALARMS = "cyanogenmod.permission.READ_ALARMS";

View File

@ -218,12 +218,14 @@ public class PerformanceManagerService extends SystemService {
return false;
}
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
// Enforce the performance access permission declared by cm's res package
mContext.enforceCallingOrSelfPermission(
cyanogenmod.platform.Manifest.permission.PERFORMANCE_ACCESS, null);
long token = Binder.clearCallingIdentity();
mCurrentProfile = profile;
mHandler.removeMessages(MSG_CPU_BOOST);
mHandler.removeMessages(MSG_LAUNCH_BOOST);
mHandler.sendMessage(

View File

@ -133,10 +133,10 @@
<!-- Permission for adding/removing applications to protected apps
<p>Not for use by third-party applications. -->
<permission
android:name="cyanogenmod.permission.PROTECTED_APP"
android:label="@string/permlab_protectedApp"
android:description="@string/permdesc_protectedApp"
android:protectionLevel="signature|privileged" />
android:name="cyanogenmod.permission.PROTECTED_APP"
android:label="@string/permlab_protectedApp"
android:description="@string/permdesc_protectedApp"
android:protectionLevel="signature|privileged" />
<!-- Allows an application to be used as a third party keyguard -->
<permission android:name="android.permission.THIRD_PARTY_KEYGUARD"
@ -146,24 +146,31 @@
<!-- Allows an application to use ThemeManager -->
<permission android:name="cyanogenmod.permission.ACCESS_THEME_MANAGER"
android:label="@string/permlab_accessThemeService"
android:description="@string/permdesc_accessThemeService"
android:protectionLevel="normal" />
android:label="@string/permlab_accessThemeService"
android:description="@string/permdesc_accessThemeService"
android:protectionLevel="normal" />
<!-- Allows an application to read the current theme configuration and
get information about the various themes currently installed -->
<permission android:name="cyanogenmod.permission.READ_THEMES"
android:label="@string/permlab_readThemes"
android:description="@string/permdesc_readThemesDesc"
android:protectionLevel="normal" />
android:label="@string/permlab_readThemes"
android:description="@string/permdesc_readThemesDesc"
android:protectionLevel="normal" />
<!-- Allows an application to write the current theme configuration and
write information about the various themes currently installed.
Changing themes should be done through the service ACCESS_THEME_MANAGER -->
<permission android:name="cyanogenmod.permission.WRITE_THEMES"
android:label="@string/permlab_writeThemes"
android:description="@string/permdesc_writeThemesDesc"
android:protectionLevel="normal" />
android:label="@string/permlab_writeThemes"
android:description="@string/permdesc_writeThemesDesc"
android:protectionLevel="normal" />
<!-- Allows low-level access to performance management.
<p>Not for use by third-party applications. -->
<permission android:name="cyanogenmod.permission.PERFORMANCE_ACCESS"
android:label="@string/permlab_perfAccess"
android:description="@string/permdesc_perfAccessDesc"
android:protectionLevel="signature|privileged" />
<application android:process="system"
android:persistent="true"

View File

@ -161,4 +161,8 @@
<!-- Description of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
<string name="permdesc_writeThemesDesc">Allows the app to insert new themes and modify which theme you have applied.</string>
<!-- Performance manager permission title -->
<string name="permlab_perfAccess">access performance manager</string>
<!-- Performance manager permission description -->
<string name="permlab_perfAccessDesc">Allows an app to access the performance service. Should never be needed for normal apps</string>
</resources>

View File

@ -94,7 +94,8 @@ public class PerformanceManager {
return sInstance;
}
private static IPerformanceManager getService() {
/** @hide */
public static IPerformanceManager getService() {
if (sService != null) {
return sService;
}

View File

@ -569,6 +569,7 @@ package cyanogenmod.platform {
field public static final java.lang.String MODIFY_NETWORK_SETTINGS = "cyanogenmod.permission.MODIFY_NETWORK_SETTINGS";
field public static final java.lang.String MODIFY_PROFILES = "cyanogenmod.permission.MODIFY_PROFILES";
field public static final java.lang.String MODIFY_SOUND_SETTINGS = "cyanogenmod.permission.MODIFY_SOUND_SETTINGS";
field public static final java.lang.String PERFORMANCE_ACCESS = "cyanogenmod.permission.PERFORMANCE_ACCESS";
field public static final java.lang.String PROTECTED_APP = "cyanogenmod.permission.PROTECTED_APP";
field public static final java.lang.String PUBLISH_CUSTOM_TILE = "cyanogenmod.permission.PUBLISH_CUSTOM_TILE";
field public static final java.lang.String READ_ALARMS = "cyanogenmod.permission.READ_ALARMS";

View File

@ -20,6 +20,7 @@
<uses-permission android:name="cyanogenmod.permission.HARDWARE_ABSTRACTION_ACCESS" />
<uses-permission android:name="cyanogenmod.permission.MODIFY_PROFILES" />
<uses-permission android:name="cyanogenmod.permission.MANAGE_PERSISTENT_STORAGE" />
<uses-permission android:name="cyanogenmod.permission.PERFORMANCE_ACCESS" />
<uses-permission android:name="android.permission.STATUS_BAR_SERVICE" />
<application android:name=".CyanogenModTestApplication"

View File

@ -0,0 +1,95 @@
/**
* Copyright (c) 2016, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cyanogenmod.tests.power.unit;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import cyanogenmod.power.IPerformanceManager;
import cyanogenmod.power.PerformanceManager;
/**
* Code coverage for public facing {@link PerformanceManager} interfaces.
* The test below will save and restore the current performance profile to
* not impact successive tests.
*/
public class PerfomanceManagerTest extends AndroidTestCase {
private static final String TAG = PerfomanceManagerTest.class.getSimpleName();
private static final int IMPOSSIBLE_POWER_PROFILE = -1;
private PerformanceManager mCMPerformanceManager;
private int mSavedPerfProfile;
@Override
protected void setUp() throws Exception {
super.setUp();
mCMPerformanceManager = PerformanceManager.getInstance(mContext);
// Save the perf profile for later restore.
mSavedPerfProfile = mCMPerformanceManager.getPowerProfile();
}
@SmallTest
public void testManagerExists() {
assertNotNull(mCMPerformanceManager);
}
@SmallTest
public void testManagerServiceIsAvailable() {
IPerformanceManager icmStatusBarManager = mCMPerformanceManager.getService();
assertNotNull(icmStatusBarManager);
}
@SmallTest
public void testGetNumberOfPerformanceProfiles() {
// Assert that we can even set perf profiles
assertTrue(mCMPerformanceManager.getNumberOfProfiles() > 0);
}
@SmallTest
public void testGetPowerProfile() {
assertNotSame(IMPOSSIBLE_POWER_PROFILE, mSavedPerfProfile);
}
@SmallTest
public void testSetAndGetPowerProfile() {
int[] expectedStates = new int[] { PerformanceManager.PROFILE_POWER_SAVE,
PerformanceManager.PROFILE_BALANCED,
PerformanceManager.PROFILE_HIGH_PERFORMANCE};
// Set the state
for (int profile : expectedStates) {
// If the target perf profile is the same as the current one,
// setPowerProfile will noop, ignore that scenario
if (mCMPerformanceManager.getPowerProfile() != profile) {
mCMPerformanceManager.setPowerProfile(profile);
// Verify that it was set correctly.
assertEquals(profile, mCMPerformanceManager.getPowerProfile());
}
}
}
@SmallTest
public void testGetPerfProfileHasAppProfiles() {
// No application has power save by default
assertEquals(false, mCMPerformanceManager.getProfileHasAppProfiles(
PerformanceManager.PROFILE_POWER_SAVE));
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
// Reset
mCMPerformanceManager.setPowerProfile(mSavedPerfProfile);
}
}