Skip to content

Commit

Permalink
Merge pull request #177 from Claudio5/Sharing
Browse files Browse the repository at this point in the history
Sharing and saving picture
  • Loading branch information
LudoHoff authored Dec 16, 2018
2 parents 7f2d6de + 859772f commit 63574a1
Show file tree
Hide file tree
Showing 41 changed files with 1,008 additions and 355 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ dependencies {
implementation 'com.google.firebase:firebase-functions:15.0.0'
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.15'

implementation 'com.facebook.android:facebook-share:[4,5)'

implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package ch.epfl.sweng.SDP.game;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.intent.Intents;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.GrantPermissionRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.widget.RatingBar;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.storage.FirebaseStorage;

import org.junit.After;
import org.junit.Before;
Expand All @@ -30,16 +35,23 @@
import ch.epfl.sweng.SDP.auth.ConstantsWrapper;
import ch.epfl.sweng.SDP.firebase.Database;
import ch.epfl.sweng.SDP.home.HomeActivity;
import ch.epfl.sweng.SDP.localDatabase.LocalDbHandlerForImages;
import ch.epfl.sweng.SDP.utils.BitmapManipulator;
import ch.epfl.sweng.SDP.utils.ImageSharer;
import ch.epfl.sweng.SDP.utils.ImageStorageManager;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread;
import static ch.epfl.sweng.SDP.game.drawing.DrawingOnlineTest.initializedBitmap;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
Expand All @@ -52,13 +64,18 @@ public class VotingPageActivityTest {
private static final String USER_ID = "userA";
private static final String ROOM_ID_TEST = "0123457890";
private static final String TOP_ROOM_ID = "realRooms";
private static final int PERMISSION_WRITE_STORAGE = 1;

private DataSnapshot dataSnapshotMock;
private DatabaseError databaseErrorMock;
private StarAnimationView starsAnimation;

@Rule
public final ActivityTestRule<VotingPageActivity> mActivityRule =
public GrantPermissionRule runtimePermissionRule = GrantPermissionRule
.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);

@Rule
public final ActivityTestRule<VotingPageActivity> activityRule =
new ActivityTestRule<VotingPageActivity>(VotingPageActivity.class) {
@Override
protected void beforeActivityLaunched() {
Expand All @@ -82,7 +99,7 @@ protected Intent getActivityIntent() {
public void init() {
dataSnapshotMock = Mockito.mock(DataSnapshot.class);
databaseErrorMock = Mockito.mock(DatabaseError.class);
starsAnimation = mActivityRule.getActivity()
starsAnimation = activityRule.getActivity()
.findViewById(R.id.starsAnimation);
}

Expand All @@ -92,17 +109,56 @@ public void end() {
}

@Test
public void ratingUsingRatingBarShouldBeSaved() {
public void testSharingImage() {
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(6);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(2000);

RankingFragment myFragment = (RankingFragment) activityRule.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.votingPageLayout);
assertThat(myFragment.isVisible(), is(true));

Bitmap bitmap = BitmapFactory.decodeResource(
activityRule.getActivity().getResources(), R.drawable.league_1);
LocalDbHandlerForImages localDbHandler = new LocalDbHandlerForImages(
activityRule.getActivity().getApplicationContext(), null, 1);
localDbHandler.addBitmapToDb(bitmap, 2);
onView(withId(R.id.shareButton)).perform(click());
assertThat(myFragment.isVisible(), is(true));
}

@Test
public void testSaveImage() {
// Open fragment
SystemClock.sleep(1000);
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(6);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(2000);

RankingFragment myFragment = (RankingFragment) activityRule.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.votingPageLayout);
assertThat(myFragment.isVisible(), is(true));

// Save image
Bitmap bitmap = initializedBitmap();
LocalDbHandlerForImages localDbHandler = new LocalDbHandlerForImages(
activityRule.getActivity().getApplicationContext(), null, 1);
localDbHandler.addBitmapToDb(bitmap, 2);
onView(withId(R.id.saveButton)).perform(click());
assertThat(myFragment.isVisible(), is(true));
}

@Test
public void ratingUsingRatingBarShouldBeSaved() {
// To ensure that the rating value does not get above 20
Database.getReference(TOP_ROOM_ID + "." + ROOM_ID_TEST + ".ranking." + USER_ID)
.setValue(0);
.setValue(0);

short counter = mActivityRule.getActivity().getChangeDrawingCounter();
short counter = activityRule.getActivity().getChangeDrawingCounter();
SystemClock.sleep(5000);
((RatingBar) mActivityRule.getActivity().findViewById(R.id.ratingBar)).setRating(3);
((RatingBar) activityRule.getActivity().findViewById(R.id.ratingBar)).setRating(3);
SystemClock.sleep(5000);
assertThat(mActivityRule.getActivity().getRatings()[counter], is(3));
assertThat(activityRule.getActivity().getRatings()[counter], is(3));
}

@Test
Expand Down Expand Up @@ -162,33 +218,33 @@ public void addStarsHandlesNegativeNumber() {
@Test
public void startHomeActivityStartsHomeActivity() {
Intents.init();
mActivityRule.getActivity().startHomeActivity(null);
activityRule.getActivity().startHomeActivity();
SystemClock.sleep(2000);
intended(hasComponent(HomeActivity.class.getName()));
Intents.release();
Database.getReference(TOP_ROOM_ID + "." + ROOM_ID_TEST + ".users." + USER_ID)
.setValue(USER_ID);
.setValue(USER_ID);
Database.getReference(TOP_ROOM_ID + "." + ROOM_ID_TEST + ".ranking." + USER_ID)
.setValue(0);
.setValue(0);
SystemClock.sleep(2000);
}

@Test
public void testState6Change() {
SystemClock.sleep(1000);
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(6);
mActivityRule.getActivity().callOnStateChange(dataSnapshotMock);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(2500);

RankingFragment myFragment = (RankingFragment) mActivityRule.getActivity()
RankingFragment myFragment = (RankingFragment) activityRule.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.votingPageLayout);
assertThat(myFragment.isVisible(), is(true));
}

@Test
public void testState5Change() {
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(5);
mActivityRule.getActivity().callOnStateChange(dataSnapshotMock);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(2500);

onView(withId(R.id.playerNameView)).check(matches(not(isDisplayed())));
Expand All @@ -198,44 +254,44 @@ public void testState5Change() {
public void testState4Change() {
SystemClock.sleep(1000);
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(4);
mActivityRule.getActivity().callOnStateChange(dataSnapshotMock);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(6000);
}

@Test
public void testShowDrawingImage() {
Bitmap image = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
mActivityRule.getActivity().callShowWinnerDrawing(image, "Champion");
activityRule.getActivity().callShowWinnerDrawing(image, "Champion");
}

@Test
public void testChangeImage() {
short counter = mActivityRule.getActivity().getChangeDrawingCounter();
mActivityRule.getActivity().callChangeImage();
short counter = activityRule.getActivity().getChangeDrawingCounter();
activityRule.getActivity().callChangeImage();

SystemClock.sleep(6000);

assertThat((int) mActivityRule.getActivity().getChangeDrawingCounter(),
assertThat((int) activityRule.getActivity().getChangeDrawingCounter(),
greaterThanOrEqualTo(counter + 1));
}

@Test(expected = DatabaseException.class)
public void testOnCancelledListenerState() {
when(databaseErrorMock.toException()).thenReturn(new DatabaseException("Cancelled"));
mActivityRule.getActivity().listenerState.onCancelled(databaseErrorMock);
activityRule.getActivity().listenerState.onCancelled(databaseErrorMock);
}

@Test(expected = DatabaseException.class)
public void testOnCancelledListenerCounter() {
when(databaseErrorMock.toException()).thenReturn(new DatabaseException("Cancelled"));
mActivityRule.getActivity().listenerCounter.onCancelled(databaseErrorMock);
activityRule.getActivity().listenerCounter.onCancelled(databaseErrorMock);
}

@Test
public void testDecodeSampledBitmapFromResource() {
Bitmap bitmap = BitmapManipulator.decodeSampledBitmapFromResource(
mActivityRule.getActivity().getResources(), R.drawable.default_image, 2, 2);
activityRule.getActivity().getResources(), R.drawable.default_image, 2, 2);
assertThat(bitmap, is(not(nullValue())));
}

Expand All @@ -249,7 +305,70 @@ public void testDecodeSampledBitmapFromByteArray() {
bitmap.compress(Bitmap.CompressFormat.JPEG,
1, byteArrayOutputStream);
byte[] data = byteArrayOutputStream.toByteArray();
Bitmap newBitmap = BitmapManipulator.decodeSampledBitmapFromByteArray(data, 0, data.length, 2, 2);
Bitmap newBitmap = BitmapManipulator.decodeSampledBitmapFromByteArray(
data, 0, data.length, 2, 2);
assertThat(newBitmap, is(not(nullValue())));
}

@Test
public void testImageSharerShareToAppFails() {
ImageSharer imageSharer = ImageSharer.getInstance(activityRule.getActivity());
imageSharer.getUrl(FirebaseStorage.getInstance().getReference().child("TestImage"));
imageSharer.shareDrawingToFacebook(Uri.EMPTY);
assertThat(imageSharer.shareImageToFacebookApp(initializedBitmap()), is(false));
}

@Test
public void testToastAfterSuccessfulDownload() {
activityRule.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ImageStorageManager.successfullyDownloadedImageToast(activityRule.getActivity());
}
});

onView(withText(activityRule.getActivity().getString(R.string.successfulImageDownload)))
.inRoot(withDecorView(not(is(activityRule.getActivity()
.getWindow().getDecorView())))).check(matches(isDisplayed()));
}

@Test
public void testOnRequestWritePermissionsAccepted() {
// Open fragment
SystemClock.sleep(1000);
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(6);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(2000);

RankingFragment myFragment = (RankingFragment) activityRule.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.votingPageLayout);
assertThat(myFragment.isVisible(), is(true));

// Save image
Bitmap bitmap = initializedBitmap();
LocalDbHandlerForImages localDbHandler = new LocalDbHandlerForImages(
activityRule.getActivity().getApplicationContext(), null, 1);
localDbHandler.addBitmapToDb(bitmap, 2);
activityRule.getActivity().onRequestPermissionsResult(PERMISSION_WRITE_STORAGE,
new String[]{}, new int[]{});

assertThat(myFragment.isVisible(), is(true));
}

@Test
public void testOnRandomRequestPermissionsAccepted() {
SystemClock.sleep(1000);
when(dataSnapshotMock.getValue(Integer.class)).thenReturn(6);
activityRule.getActivity().callOnStateChange(dataSnapshotMock);
SystemClock.sleep(2000);

RankingFragment myFragment = (RankingFragment) activityRule.getActivity()
.getSupportFragmentManager().findFragmentById(R.id.votingPageLayout);
assertThat(myFragment.isVisible(), is(true));

activityRule.getActivity().onRequestPermissionsResult(42,
new String[]{}, new int[]{});

assertThat(myFragment.isVisible(), is(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testSwapAxisItemSwapsSpeedPaintView() {
@Test
public void testBumpingItemReplacesPaintViewCoordinatesCorrectly() {
paintView.setCircle(202, 202);
collisionItem(new BumpingItem(200, 200, 10));
collisionItem(new BumpingItem(200, 200, RandomItemGenerator.ITEM_RADIUS));
int dx = paintView.getCircleX() - 200;
int dy = paintView.getCircleY() - 200;
double radius = Math.sqrt(dx * dx + dy * dy) + paintView.getCircleRadius();
Expand All @@ -123,7 +123,7 @@ public void testBumpingItemReplacesPaintViewCoordinatesCorrectly() {
@Test
public void testBumpingItemChangesItsDrawable() {
paintView.setCircle(200, 200);
BumpingItem item = new BumpingItem(200, 200, 10);
BumpingItem item = new BumpingItem(200, 200, RandomItemGenerator.ITEM_RADIUS);
ImageView view = new ImageView(activity);
view.setX(item.getX() - item.getRadius());
view.setY(item.getY() - item.getRadius());
Expand Down
Loading

0 comments on commit 63574a1

Please sign in to comment.