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

Commit

Permalink
Fix duplicated permission report (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrovgs authored Nov 9, 2020
1 parent 11c6351 commit e52b7d0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,37 @@

import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
* An in detail report of the request permission process
*/
public final class MultiplePermissionsReport {

private final List<PermissionGrantedResponse> grantedPermissionResponses;
private final List<PermissionDeniedResponse> deniedPermissionResponses;
private final Set<PermissionGrantedResponse> grantedPermissionResponses;
private final Set<PermissionDeniedResponse> deniedPermissionResponses;

MultiplePermissionsReport() {
grantedPermissionResponses = new LinkedList<>();
deniedPermissionResponses = new LinkedList<>();
grantedPermissionResponses = new LinkedHashSet<>();
deniedPermissionResponses = new LinkedHashSet<>();
}

/**
* Returns a collection with all the permissions that has been granted
*/
public List<PermissionGrantedResponse> getGrantedPermissionResponses() {
return grantedPermissionResponses;
return new LinkedList<>(grantedPermissionResponses);
}

/**
* Returns a collection with all the permissions that has been denied
*/
public List<PermissionDeniedResponse> getDeniedPermissionResponses() {
return deniedPermissionResponses;
return new LinkedList<>(deniedPermissionResponses);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,21 @@ public String getPermissionName() {
public boolean isPermanentlyDenied() {
return permanentlyDenied;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PermissionDeniedResponse that = (PermissionDeniedResponse) o;
return requestedPermission.getName().equals(that.requestedPermission.getName());
}

@Override
public int hashCode() {
return requestedPermission.getName().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@ public PermissionRequest getRequestedPermission() {
public String getPermissionName() {
return requestedPermission.getName();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PermissionGrantedResponse that = (PermissionGrantedResponse) o;
return requestedPermission.getName().equals(that.requestedPermission.getName());
}

@Override
public int hashCode() {
return requestedPermission.getName().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2015 Karumi.
*
* 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.dexter;

import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;

import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static junit.framework.TestCase.assertEquals;

public class MultiplePermissionsReportTest {

private static final PermissionGrantedResponse GRANTED_RESPONSE =
PermissionGrantedResponse.from("CAMERA");
private static final PermissionGrantedResponse OTHER_GRANTED_RESPONSE =
PermissionGrantedResponse.from("CONTACTS");
private static final PermissionDeniedResponse DENIED_RESPONSE =
PermissionDeniedResponse.from("MICROPHONE", true);
private static final PermissionDeniedResponse OTHER_DENIED_RESPONSE =
PermissionDeniedResponse.from("STORAGE", false);

@Test
public void shouldReplaceOldPermissionGrantedReportsWithTheNewOnes() {
MultiplePermissionsReport report = new MultiplePermissionsReport();

report.addGrantedPermissionResponse(GRANTED_RESPONSE);
report.addGrantedPermissionResponse(OTHER_GRANTED_RESPONSE);
report.addGrantedPermissionResponse(GRANTED_RESPONSE);

List<PermissionGrantedResponse> expectedPermissions = new LinkedList<>();
expectedPermissions.add(GRANTED_RESPONSE);
expectedPermissions.add(OTHER_GRANTED_RESPONSE);
assertEquals(expectedPermissions, report.getGrantedPermissionResponses());
}

@Test
public void shouldReplaceOldPermissionDeniedReportsWithTheNewOnes() {
MultiplePermissionsReport report = new MultiplePermissionsReport();

report.addDeniedPermissionResponse(DENIED_RESPONSE);
report.addDeniedPermissionResponse(OTHER_DENIED_RESPONSE);
report.addDeniedPermissionResponse(DENIED_RESPONSE);

List<PermissionDeniedResponse> expectedPermissions = new LinkedList<>();
expectedPermissions.add(DENIED_RESPONSE);
expectedPermissions.add(OTHER_DENIED_RESPONSE);
assertEquals(expectedPermissions, report.getDeniedPermissionResponses());
}
}

0 comments on commit e52b7d0

Please sign in to comment.