Skip to content

Commit

Permalink
Merge pull request #192 from Claudio5/fix-review-issues
Browse files Browse the repository at this point in the history
Fix review issues
  • Loading branch information
rbischof authored Dec 18, 2018
2 parents 63574a1 + 3100461 commit a6af057
Show file tree
Hide file tree
Showing 133 changed files with 3,043 additions and 2,455 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ engines:
checks:
com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck:
enabled: false
com.puppycrawl.tools.checkstyle.checks.naming.PackageNameCheck:
enabled: false
sonar-java:
enabled: true
checks:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ before_script:
- adb shell input keyevent 82
script:
- chmod +x ./gradlew
- travis_wait 45 ./gradlew connectedCheck jacocoTestReport
- travis_wait 50 ./gradlew connectedCheck jacocoTestReport
after_script:
# Report test coverage to Code Climate
- export JACOCO_SOURCE_PATH=app/src/main/java/
Expand Down
108 changes: 79 additions & 29 deletions app/src/androidTest/java/ch/epfl/sweng/SDP/MainActivityTest.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,78 @@
package ch.epfl.sweng.SDP;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.OFFLINE;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.ONLINE;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.changeOnlineStatus;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.app.Instrumentation;
import android.os.SystemClock;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.widget.TextView;
import ch.epfl.sweng.SDP.auth.Account;
import ch.epfl.sweng.SDP.auth.ConstantsWrapper;
import ch.epfl.sweng.SDP.auth.LoginActivity;
import ch.epfl.sweng.SDP.localDatabase.LocalDbHandlerForAccount;

import com.google.firebase.database.DataSnapshot;
import java.util.HashMap;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import java.util.HashMap;

import ch.epfl.sweng.SDP.auth.Account;
import ch.epfl.sweng.SDP.auth.ConstantsWrapper;
import ch.epfl.sweng.SDP.auth.LoginActivity;
import ch.epfl.sweng.SDP.home.HomeActivity;
import ch.epfl.sweng.SDP.localDatabase.LocalDbForAccount;
import ch.epfl.sweng.SDP.localDatabase.LocalDbHandlerForAccount;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static ch.epfl.sweng.SDP.auth.LoginActivityTest.executeOnUiThread;
import static ch.epfl.sweng.SDP.firebase.FbDatabase.createCompletionListener;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.OFFLINE;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.ONLINE;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.changeOnlineStatus;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

private static final String TEST_USERNAME = "TESTUSERNAME";
private static final String TEST_EMAIL = "testEmail";
private static final String TEST_USER_ID = "no_user";
private static final String TEST_LEAGUE = "leagueOne";
private static final String TEST_LEAGUE = "league1";

@Rule
public final ActivityTestRule<MainActivity> mActivityRule =
public final ActivityTestRule<MainActivity> activityRule =
new ActivityTestRule<>(MainActivity.class);

private MainActivity activity;

// Add a monitor for the login activity
private final Instrumentation.ActivityMonitor monitor = getInstrumentation()
private final Instrumentation.ActivityMonitor loginMonitor = getInstrumentation()
.addMonitor(LoginActivity.class.getName(), null, false);

// Add a monitor for the home activity
private final Instrumentation.ActivityMonitor homeMonitor = getInstrumentation()
.addMonitor(HomeActivity.class.getName(), null, false);

@Before
public void init() {
activity = mActivityRule.getActivity();
activity = activityRule.getActivity();
}

@Test
public void testCanOpenLoginActivity() {
onView(withId(R.id.login_button)).perform(click());
Activity loginActivity = getInstrumentation()
.waitForMonitorWithTimeout(monitor, 5000);
.waitForMonitorWithTimeout(loginMonitor, 5000);
assertThat(loginActivity, is(not(nullValue())));
}

Expand All @@ -78,7 +90,7 @@ public void testCloneAccountFromFirebase() {
activity.cloneAccountFromFirebase(snapshot);

Account.deleteAccount();
LocalDbHandlerForAccount localDbHandlerForAccount = new LocalDbHandlerForAccount(
LocalDbForAccount localDbHandlerForAccount = new LocalDbHandlerForAccount(
activity, null, 1);
Account.createAccount(activity, new ConstantsWrapper(),
TEST_USERNAME, TEST_EMAIL);
Expand All @@ -90,7 +102,7 @@ public void testCloneAccountFromFirebase() {

@Test
public void testHandleUserStatusOnline() {
changeOnlineStatus(TEST_USER_ID, ONLINE);
changeOnlineStatus(TEST_USER_ID, ONLINE, createCompletionListener());
SystemClock.sleep(3000);

TextView errorMessage = new TextView(activity);
Expand All @@ -100,12 +112,12 @@ public void testHandleUserStatusOnline() {
assertThat(errorMessage.getText().toString(),
is(activity.getString(R.string.already_logged_in)));
assertThat(errorMessage.getVisibility(), is(View.VISIBLE));
changeOnlineStatus(TEST_USER_ID, OFFLINE);
changeOnlineStatus(TEST_USER_ID, OFFLINE, createCompletionListener());
}

@Test
public void testHandleUserStatusOffline() {
changeOnlineStatus(TEST_USER_ID, OFFLINE);
changeOnlineStatus(TEST_USER_ID, OFFLINE, createCompletionListener());
SystemClock.sleep(3000);

TextView errorMessage = new TextView(activity);
Expand All @@ -115,6 +127,45 @@ public void testHandleUserStatusOffline() {
assertThat(activity.isFinishing(), is(true));
}

@Test
public void testRedirectionToHome() {
HashMap<String, Object> values = new HashMap<>();
initializeAccountHashMap(values);

HashMap<String, HashMap<String, Object>> account = new HashMap<>();
account.put(TEST_USER_ID, values);

final DataSnapshot snapshot = Mockito.mock(DataSnapshot.class);
when(snapshot.getValue()).thenReturn(account);

executeOnUiThread(new Runnable() {
@Override
public void run() {
activityRule.getActivity().handleRedirection(snapshot);
}
});

Activity homeActivity = getInstrumentation()
.waitForMonitorWithTimeout(homeMonitor, 5000);
assertThat(homeActivity, is(nullValue()));
}

@Test
public void testRedirectionToMain() {

final DataSnapshot snapshot = Mockito.mock(DataSnapshot.class);
when(snapshot.exists()).thenReturn(false);

executeOnUiThread(new Runnable() {
@Override
public void run() {
activityRule.getActivity().handleRedirection(snapshot);
}
});

assertThat(this.activity.isFinishing(), is(false));
}

/**
* Populates the given HashMap with test values.
*
Expand Down Expand Up @@ -150,5 +201,4 @@ private void assertThatAccountWasInitializedCorrectly(Account newAccount) {
assertThat(newAccount.getCurrentLeague(), is(equalTo(TEST_LEAGUE)));
assertThat(newAccount.getMaxTrophies(), is(100));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

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

import ch.epfl.sweng.SDP.R;
import ch.epfl.sweng.SDP.firebase.Database;
import ch.epfl.sweng.SDP.firebase.FbDatabase;
import ch.epfl.sweng.SDP.firebase.OnSuccessValueEventListener;
import ch.epfl.sweng.SDP.home.FriendsRequestState;
import ch.epfl.sweng.SDP.shop.ColorsShop;
import ch.epfl.sweng.SDP.shop.ShopItem;
Expand Down Expand Up @@ -150,12 +148,6 @@ public void testSetGetUsername() {
assertThat(account.getUsername(), is("user"));
}

@Test
public void testSetUsersRef() {
DatabaseReference databaseReference = Mockito.mock(DatabaseReference.class);
account.setUsersRef(databaseReference);
}

@Test
public void testIncreaseMatchesWon() {
account.increaseMatchesWon();
Expand Down Expand Up @@ -207,9 +199,7 @@ public void testConfirmFriend() {
}

private void friendsTestHelper(int state) {
Database.getReference(USERS_TAG
+ USER_ID + TEST_FRIEND_TAG)
.setValue(state);
FbDatabase.setFriendValue(USER_ID, TEST_FRIEND, state);
setListenerAndAssertToFirebaseForFriendsTest(true,
USERS_TAG + USER_ID + TEST_FRIEND_TAG);
account.addFriend(TEST_FRIEND);
Expand All @@ -234,6 +224,13 @@ public void testCreateAccountWhenAlreadyCreated() {
TEST_EMAIL);
}

@Test
public void testHandleResponseAndRedirect() {
Account.deleteAccount();
activityRule.getActivity().createAccountAndRedirect(USERNAME);
assertThat(activityRule.getActivity().isFinishing(), is(true));
}

@Test(expected = IllegalArgumentException.class)
public void testCreateAccountWithNullContext() {
Account.createAccount(null, new ConstantsWrapper(), USERNAME, null);
Expand Down Expand Up @@ -415,20 +412,15 @@ private void setListenerAndAssertToFirebaseForFriendsTest(
final CountingIdlingResource countingResource =
new CountingIdlingResource("WaitForFirebase");
countingResource.increment();
final ValueEventListener valueEventListener = new ValueEventListener() {
final ValueEventListener valueEventListener = new OnSuccessValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
assertThat(dataSnapshot.exists(), is(state));
countingResource.decrement();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
};

Database.getReference(path)
FbDatabase.getReference(path)
.addListenerForSingleValueEvent(valueEventListener);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package ch.epfl.sweng.SDP.auth;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.OFFLINE;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.changeOnlineStatus;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.SystemClock;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.widget.TextView;
import ch.epfl.sweng.SDP.R;
import ch.epfl.sweng.SDP.home.HomeActivity;

import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.util.ExtraConstants;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

import ch.epfl.sweng.SDP.R;
import ch.epfl.sweng.SDP.home.HomeActivity;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread;
import static ch.epfl.sweng.SDP.firebase.FbDatabase.createCompletionListener;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.OFFLINE;
import static ch.epfl.sweng.SDP.utils.OnlineStatus.changeOnlineStatus;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

@RunWith(JUnit4.class)
public class LoginActivityTest {

Expand Down Expand Up @@ -83,7 +87,8 @@ public void testSuccessfulLoginNewUser() {

@Test
public void testSuccessfulLoginExistingUser() {
changeOnlineStatus(Account.getInstance(loginActivity).getUserId(), OFFLINE);
changeOnlineStatus(Account.getInstance(loginActivity).getUserId(), OFFLINE,
createCompletionListener());
SystemClock.sleep(3000);
Account.deleteAccount();
Mockito.when(mockIdpResponse.isNewUser()).thenReturn(false);
Expand Down Expand Up @@ -128,7 +133,10 @@ public void run() {
loginActivity.getResources().getString(expectedErrorMessageId))));
}

private void executeOnUiThread(Runnable runnable) {
/**
* Executes a runnable on the UI thread.
*/
public static void executeOnUiThread(Runnable runnable) {
try {
runOnUiThread(runnable);
} catch (Throwable throwable) {
Expand Down
Loading

0 comments on commit a6af057

Please sign in to comment.