Skip to content

Commit

Permalink
Add another approach to disable tests from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrovgs committed Jan 11, 2016
1 parent 655e7f4 commit b2563b1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ before_script:
- echo no | android create avd --force -n test -t android-21 --abi armeabi-v7a
- emulator -avd test -no-skin -no-audio -no-window &
- android-wait-for-emulator
- adb shell settings put global window_animation_scale 0
- adb shell settings put global transition_animation_scale 0
- adb shell settings put global animator_duration_scale 0
- adb shell input keyevent 82 &

script:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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 com.karumi.katasuperheroes;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.util.Log;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
* A test rule that will fail a test if animations or transitions are enabled on a device.
*/
public class AnimationAwareTestRule implements TestRule {

private static final String TAG = "AnimationAware";

public AnimationAwareTestRule() {
}

@Override public Statement apply(Statement base, Description description) {
return new AnimationAwareStatement(base);
}

private class AnimationAwareStatement extends Statement {

private Statement mBase;

public AnimationAwareStatement(Statement base) {
mBase = base;
}

@Override public void evaluate() throws Throwable {
checkForDisabledAnimationsAndTransitions(InstrumentationRegistry.getTargetContext());
mBase.evaluate();
}
}

private static void checkForDisabledAnimationsAndTransitions(Context context)
throws AnimationsEnabledException {

ContentResolver resolver = context.getContentResolver();
final float transitionAnimationScale = getTransitionAnimationScale(resolver);
final float windowAnimationScale = getWindowAnimationScale(resolver);
final float animatorDurationScale = getAnimatorDurationScale(resolver);

final boolean isEnabled = transitionAnimationScale + animatorDurationScale
+ windowAnimationScale != 0;

if (isEnabled) {
throw new AnimationsEnabledException();
}
}

@SuppressWarnings("deprecation") @SuppressLint("InlinedApi")
private static float getTransitionAnimationScale(ContentResolver resolver) {
//noinspection deprecation
return getSetting(resolver, Settings.Global.TRANSITION_ANIMATION_SCALE,
Settings.System.TRANSITION_ANIMATION_SCALE);
}

@SuppressWarnings("deprecation") @SuppressLint("InlinedApi")
private static float getWindowAnimationScale(ContentResolver resolver) {
//noinspection deprecation
return getSetting(resolver, Settings.Global.WINDOW_ANIMATION_SCALE,
Settings.System.WINDOW_ANIMATION_SCALE);
}

@SuppressWarnings("deprecation") @SuppressLint("InlinedApi")
private static float getAnimatorDurationScale(ContentResolver resolver) {
return getSetting(resolver, Settings.Global.ANIMATOR_DURATION_SCALE,
Settings.System.ANIMATOR_DURATION_SCALE);
}

private static float getSetting(ContentResolver resolver, String current, String deprecated) {
if (isJellyBeanOrHigher()) {
return getGlobalSetting(resolver, current);
} else {
//noinspection deprecation
return getSystemSetting(resolver, deprecated);
}
}

private static boolean isJellyBeanOrHigher() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static float getGlobalSetting(ContentResolver resolver, String setting) {
try {
return Settings.Global.getFloat(resolver, setting);
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "getSystemSetting: Setting not found", e);
return 0;
}
}

private static float getSystemSetting(ContentResolver resolver, String setting) {
try {
return Settings.System.getFloat(resolver, setting);
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "getSystemSetting: Setting not found", e);
return 0;
}
}

/**
* Error that's being thrown when your system has animations enabled and should not.
*/
private static class AnimationsEnabledException extends RuntimeException {
public AnimationsEnabledException() {
super("To properly execute this test, disable animations and transitions "
+ "on your device.\n"
+ "For more info check: http://goo.gl/qVu1yV");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
@Rule public IntentsTestRule<MainActivity> activityRule =
new IntentsTestRule<>(MainActivity.class, true, false);

@Rule public AnimationAwareTestRule mAnimationAwareTestRule = new AnimationAwareTestRule();

@Mock SuperHeroesRepository repository;

@Test public void showsEmptyCaseIfThereAreNoSuperHeroes() {
Expand Down

0 comments on commit b2563b1

Please sign in to comment.