Skip to content

Commit

Permalink
Sprint2: Implement test case (#51)
Browse files Browse the repository at this point in the history
* test cases added

* test case change from mainmodel to mainviewmodel

* null string, stored in database, invalid new username format test case added

* Sprint2: Implement test case

---------

Co-authored-by: Sam-zzZ <[email protected]>
  • Loading branch information
tianrui-qi and Sam-zzZ authored Oct 26, 2024
1 parent 3d9498e commit 523a8f4
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,156 @@
package com.example.sprint1;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.example.sprint1.viewmodel.MainViewModel;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private static final MainViewModel mainViewModel = new MainViewModel();

@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.sprint1", appContext.getPackageName());
}

@Test
public void testUserSignUpWithExistingUsername() {
mainViewModel.userSignUp("testUser", "testPassword", Assert::assertFalse);
}

@Test
public void testUserSignInWithCorrectCredentials() {
mainViewModel.userSignIn("tianrui", "1", Assert::assertTrue);
}

@Test
public void testUserSignInWithIncorrectPassword() {
mainViewModel.userSignIn("tianrui", "2", Assert::assertFalse);
}

@Test
public void testAddDestinationWithValidData() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.addDestination("Paris", "01/01/2023", "01/11/2023", Assert::assertTrue);
}

@Test
public void testAddDestinationWithAdditionalValidData() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.addDestination("New York", "01/04/2023", "01/06/2023", Assert::assertTrue);
}

@Test
public void testAddDestinationWithDuplicateLocation() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.addDestination("Atlanta", "01/01/2023", "01/11/2023", Assert::assertTrue);
}

@Test
public void testGetDestinationsWithExistingDestinations() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.getDestinations(result -> assertTrue(result.containsKey("Paris")));
}

@Test
public void testGetDestinationsWithNoDestinations() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.getDestinations(Assert::assertNull);
}

@Test
public void testSetVacationWithValidDatesAndDuration() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.setVacation("01/01/2023", "01/10/2023", "10", Assert::assertTrue);
mainViewModel.getVacation(result -> {
assertEquals("01/01/2023", result.get("startDate"));
assertEquals("01/10/2023", result.get("endDate"));
assertEquals("10", result.get("duration"));
});
}

@Test
public void testSetVacationWithMismatchedDuration() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.setVacation("01/10/2023", "01/01/2023", "5", Assert::assertFalse);
}

@Test
public void testCalculateOccupiedDaysWithValidVacationAndDestinations() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.setVacation("01/01/2023", "01/21/2023", "21", Assert::assertTrue);
mainViewModel.addDestination("Paris", "01/01/2023", "01/11/2023", Assert::assertTrue);
}

@Test
public void testCalculateOccupiedDaysWithNoOverlap() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
mainViewModel.userSignIn("testUser", "testPassword", result -> {
assertTrue(result);
latch.countDown();
});
assertTrue(latch.await(5, TimeUnit.SECONDS));

mainViewModel.setVacation("01/01/2023", "01/30/2023", "30", Assert::assertTrue);
mainViewModel.addDestination("Paris", "01/15/2023", "01/20/2023", Assert::assertTrue);
}

@Test
public void testLogTravelWithEmptyLocation() {
mainViewModel.addDestination("", "01/01/2023", "01/10/2023", Assert::assertFalse);
}

@Test
public void testLogTravelEntryStoredInDatabase() {
mainViewModel.userSignIn("testUser", "testPassword", Assert::assertTrue);
mainViewModel.getDestinations(result -> {
assertNotNull(result);
assertTrue(result.containsKey("TestLocation"));
});
}

@Test
public void testAccountCreationWithWhitespaceOnlyInput() {
mainViewModel.userSignUp(" ", "validPassword", Assert::assertFalse);
}

@Test
public void testAccountCreationWithNullInput() {
mainViewModel.userSignUp(null, "validPassword", Assert::assertFalse);
}

@Test
public void testAccountCreationWithEmptyStringInput() {
mainViewModel.userSignUp("", "validPassword", Assert::assertFalse);
}

@Test
public void testAccountLoginWithWhitespaceOnlyInput() {
mainViewModel.userSignIn(" ", "validPassword", Assert::assertFalse);
}

@Test
public void testAccountLoginWithNullInput() {
mainViewModel.userSignIn(null, "validPassword", Assert::assertFalse);
}

@Test
public void testAccountLoginWithEmptyStringInput() {
mainViewModel.userSignIn("", "validPassword", Assert::assertFalse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ private void formCalculate() {
startDate.setText("");
endDate.setText("");
duration.setText("");
mainViewModel.calVacation(
occupiedDays -> {
// Update the result view with the occupied days
if (occupiedDays != null) {
resultView.setText(getString(
R.string.HomeDestination_Calculate_Message, occupiedDays
));
} else {
resultView.setText(R.string.HomeDestination_Calculate_Error);
}
}
);
mainViewModel.calVacation(occupiedDays -> {
// Update the result view with the occupied days
if (occupiedDays != null) {
resultView.setText(getString(
R.string.HomeDestination_Calculate_Message,
occupiedDays
));
} else {
resultView.setText(
R.string.HomeDestination_Calculate_Error
);
}
});
formCalculate.setVisibility(View.GONE);
resultCalculate.setVisibility(View.VISIBLE);
}
}
)
})
);
}

Expand Down

0 comments on commit 523a8f4

Please sign in to comment.