Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Dev looper fixes (#174)
Browse files Browse the repository at this point in the history
* Fix issue where Lopper.loop() is called even if a looper already exists
* Force Precise machine in travis configuration because Trusty environments have a potential bug with android builds preventing jobs from ending.
  • Loading branch information
Serchinastico authored Aug 11, 2017
1 parent 0694397 commit e859eb0
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 7 deletions.
19 changes: 18 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
language: android
jdk: oraclejdk8
dist: precise
env:
global:
- ADB_INSTALL_TIMEOUT=8 # 8 minutes (2 minutes by default)

android:
components:
- tools
- build-tools-25.0.0
- platform-tools
- android-24
- android-25
- extra-android-support
- extra-google-m2repository
- extra-android-m2repository
- sys-img-armeabi-v7a-android-24
- sys-img-armeabi-v7a-android-25

before_script:
- echo no | android create avd --force -n test -t android-24 --abi armeabi-v7a
- emulator -avd test -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:
./gradlew checkstyle build test && ./gradlew assembleRelease
./gradlew checkstyle build test connectedDebugAndroidTest && ./gradlew assembleRelease

2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ buildscript {

allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'https://maven.google.com' }
}

apply plugin: 'checkstyle'
Expand Down
1 change: 1 addition & 0 deletions dexter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android {
versionCode 1
versionName "1.0"
consumerProguardFiles 'proguard-rules.pro'
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
debug {
Expand Down
4 changes: 2 additions & 2 deletions dexter/src/main/java/com/karumi/dexter/DexterException.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

final class DexterException extends IllegalStateException {

public final DexterError error;
final DexterError error;

public DexterException(String detailMessage, DexterError error) {
DexterException(String detailMessage, DexterError error) {
super(detailMessage);
this.error = error;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class MultiplePermissionsListenerToPermissionListenerAdapter

private final PermissionListener listener;

public MultiplePermissionsListenerToPermissionListenerAdapter(PermissionListener listener) {
MultiplePermissionsListenerToPermissionListenerAdapter(PermissionListener listener) {
this.listener = listener;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class PermissionRationaleToken implements PermissionToken {
private final DexterInstance dexterInstance;
private boolean isTokenResolved = false;

public PermissionRationaleToken(DexterInstance dexterInstance) {
PermissionRationaleToken(DexterInstance dexterInstance) {
this.dexterInstance = dexterInstance;
}

Expand Down
8 changes: 7 additions & 1 deletion dexter/src/main/java/com/karumi/dexter/WorkerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
final class WorkerThread implements Thread {

private final Handler handler;
private boolean wasLooperNull = false;

WorkerThread() {
//Handle the case where the current thread has not called Lopper.prepare()
if (Looper.myLooper() == null) {
wasLooperNull = true;
Looper.prepare();
}
handler = new Handler();
Expand All @@ -38,6 +41,9 @@ final class WorkerThread implements Thread {
}

@Override public void loop() {
Looper.loop();
//Handle the case where there is an already existing Looper in the current thread.
if (wasLooperNull) {
Looper.loop();
}
}
}
15 changes: 14 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
apply plugin: 'com.android.application'

configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:25.2.0'
}
}

android {
compileSdkVersion 25
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.karumi.dexter.sample"
minSdkVersion 14
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
Expand All @@ -27,4 +35,9 @@ dependencies {
compile 'com.jakewharton:butterknife:8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
compile project(':dexter')
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'org.awaitility:awaitility:3.0.0'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test:rules:1.0.0'
}
105 changes: 105 additions & 0 deletions sample/src/androidTest/java/com/karumi/dexter/sample/DexterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.karumi.dexter.sample;

import android.Manifest;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.GrantPermissionRule;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.listener.DexterError;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequestErrorListener;
import com.karumi.dexter.listener.single.BasePermissionListener;
import com.karumi.dexter.listener.single.PermissionListener;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@RunWith(AndroidJUnit4.class) public class DexterTest {

private static final String TAG = "DexterTest";
private final AtomicBoolean unblock = new AtomicBoolean(false);

private final PermissionListener permissionListener = new BasePermissionListener() {
@Override public void onPermissionGranted(PermissionGrantedResponse response) {
unblock();
}

@Override public void onPermissionDenied(PermissionDeniedResponse response) {
unblock();
}
};
private final PermissionRequestErrorListener errorListener =
new PermissionRequestErrorListener() {
@Override public void onError(DexterError error) {
Log.i(TAG, error.toString());
unblock();
}
};

@Rule public ActivityTestRule<SampleActivity> activityTestRule =
new ActivityTestRule<>(SampleActivity.class);
@Rule public GrantPermissionRule grantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.CAMERA);

private Handler handler;
private HandlerThread handlerThread;

@Before public void setUp() throws Exception {
handlerThread = new HandlerThread(TAG);
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}

@After public void tearDown() throws Exception {
handlerThread.quit();
}

@Test public void testWithLooper() {
AtomicBoolean milestone = new AtomicBoolean(false);

requestAndAcceptPermissionOnHandlerThread(milestone, Manifest.permission.CAMERA);

block();
assertThat(milestone.get(), is(true));
}

private void getPermission(String permission) {
Dexter.withActivity(activityTestRule.getActivity())
.withPermission(permission)
.withListener(permissionListener)
.withErrorListener(errorListener)
.onSameThread()
.check();
}

private void requestAndAcceptPermissionOnHandlerThread(final AtomicBoolean milestone,
final String permission) {
handler.post(new Runnable() {
@Override public void run() {
getPermission(permission);
milestone.set(true);
}
});
}

private void unblock() {
unblock.set(true);
}

private void block() {
await().atMost(20, TimeUnit.SECONDS).untilTrue(unblock);
unblock.set(false);
}
}

0 comments on commit e859eb0

Please sign in to comment.