Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from CSE-110-Winter-2023/story_7_task_3
Story 7 Task 3 #50
- Loading branch information
Showing
5 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/test/java/com/team34/cse_110_project_team_34/CalculationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.team34.cse_110_project_team_34; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import utilities.Calculation; | ||
|
||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class CalculationTest { | ||
double lat1 = 0; | ||
double long1 = 0; | ||
double lat2 = 40; | ||
double long2 = 30; | ||
|
||
@Test | ||
public void checkAngle() { | ||
assertEquals(Math.toDegrees(Math.asin(3/(float) 5)), Calculation.getAngle(lat1,long1,lat2,long2), 1); | ||
} | ||
|
||
@Test | ||
public void checkDistance() { | ||
assertEquals(69*Math.sqrt((lat2-lat1)*(lat2-lat1) + (long2-long1)*(long2-long1)), Calculation.getDistance(lat1,long1,lat2,long2),1); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/test/java/com/team34/cse_110_project_team_34/DatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.team34.cse_110_project_team_34; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.core.app.ApplicationProvider; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
|
||
import java.util.List; | ||
|
||
import database.Database; | ||
import database.UserDao; | ||
import model.User; | ||
|
||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class DatabaseTest { | ||
|
||
private UserDao userDao; | ||
|
||
@Before | ||
public void createDb() { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
userDao = Database.getInstance(context).getUserDao(); | ||
Database.getInstance(context).clearAllTables(); | ||
} | ||
|
||
@Test | ||
public void emptyDatabaseTest() { | ||
List<User> users = userDao.getAllList(); | ||
assertEquals(0, users.size()); | ||
} | ||
|
||
@Test | ||
public void oneEntryTest() { | ||
User u = new User("name","public_code", 0,0); | ||
userDao.upsert(u); | ||
|
||
List<User> users = userDao.getAllList(); | ||
assertEquals(1, users.size()); | ||
assertTrue(users.get(0).equals(u)); | ||
} | ||
|
||
@Test | ||
public void multipleEntryTest() { | ||
User u1 = new User("u1", "public1", 0,0); | ||
userDao.upsert(u1); | ||
User u2 = new User("u2", "public2", 0,0); | ||
userDao.upsert(u2); | ||
User u3 = new User("u3", "public3", 0,0); | ||
userDao.upsert(u3); | ||
|
||
|
||
User db1 = userDao.get("public1"); | ||
assertTrue(db1.equals(u1)); | ||
User db2 = userDao.get("public2"); | ||
assertTrue(db2.equals(u2)); | ||
User db3 = userDao.get("public3"); | ||
assertTrue(db3.equals(u3)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
app/src/test/java/com/team34/cse_110_project_team_34/OrientationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
|
||
package com.team34.cse_110_project_team_34; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import android.content.Context; | ||
import android.content.pm.ActivityInfo; | ||
import android.media.Image; | ||
import android.provider.SyncStateContract; | ||
import android.widget.ImageView; | ||
|
||
import androidx.lifecycle.Lifecycle; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.test.core.app.ActivityScenario; | ||
import androidx.test.core.app.ApplicationProvider; | ||
|
||
import com.ibm.icu.impl.units.UnitsData; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
import utilities.OrientationService; | ||
|
||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class OrientationTest { | ||
ActivityScenario<CompassActivity> scenario; | ||
|
||
@Before | ||
public void preTest() { | ||
scenario = ActivityScenario.launch(CompassActivity.class); | ||
scenario.moveToState(Lifecycle.State.CREATED); | ||
scenario.moveToState(Lifecycle.State.STARTED); | ||
scenario.moveToState(Lifecycle.State.RESUMED); | ||
} | ||
|
||
// OrientationService's azimuth is in radians | ||
// Compass's rotation is in degrees | ||
@Test | ||
public void testOrientation() { | ||
float testValue = (float) Math.PI; | ||
ActivityScenario<CompassActivity> scenario = ActivityScenario.launch(CompassActivity.class); | ||
|
||
scenario.onActivity(activity -> { | ||
OrientationService orientationService = OrientationService.getInstance(activity); | ||
MutableLiveData<Float> mockOrientation = new MutableLiveData<Float>(); | ||
|
||
mockOrientation.setValue(testValue); | ||
orientationService.setMockOrientationSource(mockOrientation); | ||
activity.observeOrientation(); | ||
|
||
ImageView compass = activity.findViewById(R.id.compass); | ||
assertEquals(Math.toDegrees(testValue), compass.getRotation(), 1); | ||
}); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
app/src/test/java/com/team34/cse_110_project_team_34/PositionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package com.team34.cse_110_project_team_34; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
import androidx.constraintlayout.widget.ConstraintLayout; | ||
import androidx.constraintlayout.widget.ConstraintSet; | ||
import androidx.lifecycle.Lifecycle; | ||
import androidx.test.core.app.ActivityScenario; | ||
import androidx.test.core.app.ApplicationProvider; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import database.Database; | ||
import database.UserDao; | ||
import database.UserRepository; | ||
import model.User; | ||
import view.LocationView; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class PositionTest { | ||
|
||
UserRepository repo; | ||
ActivityScenario<CompassActivity> scenario; | ||
Map<String, LocationView> views; | ||
SharedPreferences preferences; | ||
|
||
UserDao dao; | ||
User mainUser; | ||
String public_id; | ||
|
||
@Before | ||
public void preTest() { | ||
Context context = ApplicationProvider.getApplicationContext(); | ||
dao = Database.getInstance(context).getUserDao(); | ||
//repo = new UserRepository(Database.getInstance(context).getUserDao()); | ||
repo = new UserRepository(dao); | ||
Database.getInstance(context).clearAllTables(); | ||
|
||
scenario = ActivityScenario.launch(CompassActivity.class); | ||
scenario.moveToState(Lifecycle.State.CREATED); | ||
scenario.moveToState(Lifecycle.State.STARTED); | ||
scenario.moveToState(Lifecycle.State.RESUMED); | ||
} | ||
|
||
/** | ||
* Checking that the position of a friend changes on CompassActivity | ||
* when the latitude and longitude of that friend is changed. | ||
*/ | ||
@Test | ||
public void changePosition() { | ||
scenario.onActivity(activity -> { | ||
preferences = activity.getSharedPreferences("preferences", Context.MODE_PRIVATE); | ||
views = activity.locationViews; | ||
|
||
//adding the mainUser to sharedPreferences | ||
String public_code = UUID.randomUUID().toString(); | ||
mainUser = new User("main", public_code, 0,0); | ||
|
||
//adding new user | ||
User user = new User("user", "code1", 30, 40); | ||
LocationView locationViewbefore = activity.addLocationView(activity.findViewById(R.id.mainLayout), user); | ||
|
||
repo.upsertLocal(mainUser); | ||
repo.getLocal(public_code); | ||
public_id = preferences.getString("Public", ""); | ||
|
||
//upserting test friend into the dao | ||
repo.upsertLocal(user); | ||
|
||
ConstraintLayout.LayoutParams lp1 = (ConstraintLayout.LayoutParams) locationViewbefore.itemView.getLayoutParams(); | ||
float before = lp1.circleAngle; | ||
|
||
//User user2 = new User("user2", "code2", 100, -130); | ||
//changing location and reupdating the dao | ||
user.latitude = 100; | ||
user.longitude = -130; | ||
repo.updateLocal(user); | ||
|
||
LocationView locationViewafter = activity.addLocationView(activity.findViewById(R.id.mainLayout), user); | ||
ConstraintLayout.LayoutParams lp2 = (ConstraintLayout.LayoutParams) locationViewafter.itemView.getLayoutParams(); | ||
float after = lp2.circleAngle; | ||
|
||
//asserting that the distances are not the same after moving position | ||
assertTrue(before == after); | ||
}); | ||
} | ||
|
||
/** | ||
* Checking the angle between the main user and two friends. | ||
* The friend with the higher latitude should be displayed at a bigger angle to the main user | ||
* than the friend with the smaller latitude. | ||
*/ | ||
@Test | ||
public void checkAngle() { | ||
scenario.onActivity(activity -> { | ||
preferences = activity.getSharedPreferences("preferences", Context.MODE_PRIVATE); | ||
|
||
String public_code = UUID.randomUUID().toString(); | ||
mainUser = new User("main", public_code, 0,0); | ||
repo.upsertLocal(mainUser); | ||
repo.getLocal(public_code); | ||
|
||
public_id = preferences.getString("Public", ""); | ||
|
||
|
||
User testfriend1 = new User("friend1", "code1", 30, 40); | ||
LocationView locationView1 = activity.addLocationView(activity.findViewById(R.id.mainLayout), testfriend1); | ||
User testfriend2 = new User("friend2", "code2", 50,300); | ||
LocationView locationView2 = activity.addLocationView(activity.findViewById(R.id.mainLayout), testfriend2); | ||
repo.upsertLocal(testfriend1); | ||
repo.upsertLocal(testfriend2); | ||
|
||
ConstraintLayout.LayoutParams lp1 = (ConstraintLayout.LayoutParams) locationView1.itemView.getLayoutParams(); | ||
float angle1 = lp1.circleAngle; | ||
|
||
ConstraintLayout.LayoutParams lp2 = (ConstraintLayout.LayoutParams) locationView2.itemView.getLayoutParams(); | ||
float angle2 = lp2.circleAngle; | ||
assertTrue(angle1 <= angle2); | ||
}); | ||
} | ||
|
||
|
||
} |