Add unit test about CalorieUtils.java. And change the visiability of some methods to make them testable.

This commit is contained in:
youtaol
2013-08-15 10:16:27 +08:00
parent d64b6a4d93
commit 75ac35bf53
2 changed files with 133 additions and 21 deletions
@@ -15,6 +15,8 @@
*/
package com.google.android.apps.mytracks.util;
import com.google.common.annotations.VisibleForTesting;
import android.location.Location;
/**
@@ -40,7 +42,8 @@ public class CalorieUtils {
/**
* Changes 4.5 miles per hour to meters per minutes.
*/
private static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM * 1000 / 60;
@VisibleForTesting
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM * 1000 / 60;
public enum ActivityType {
CYCLING, FOOT
@@ -55,7 +58,8 @@ public class CalorieUtils {
* @param grade
* @return the VO2 value in ml/kg/min
*/
private static double calculateWalkingVO2(double speed, double grade) {
@VisibleForTesting
static double calculateWalkingVO2(double speed, double grade) {
/*
* 0.1 means oxygen cost per meter of moving each kilogram (kg) of body
* weight while walking (horizontally). 1.8 means oxygen cost per meter of
@@ -73,7 +77,8 @@ public class CalorieUtils {
* @param grade
* @return the VO2 value in ml/kg/min
*/
private static double calculateRunningVO2(double speed, double grade) {
@VisibleForTesting
static double calculateRunningVO2(double speed, double grade) {
/*
* 0.2 means oxygen cost per meter of moving each kg of body weight while
@@ -105,8 +110,8 @@ public class CalorieUtils {
* @param timeUsed how many times used in second
* @return the power value watts(Joule/second)
*/
private static double calculateCyclingCalories(double speed, double grade, int weight,
double timeUsed) {
@VisibleForTesting
static double calculateCyclingCalories(double speed, double grade, int weight, double timeUsed) {
// Get the Power.
double power = 9.8 * weight * speed * (0.0053 + grade) + 0.185 * (speed * speed * speed);
// Get the calories.
@@ -122,8 +127,8 @@ public class CalorieUtils {
* @param weight the weight of user
* @return the calories expenditure between the start and stop location
*/
private static double calculateExpenditureCycling(Location start, Location stop, double grade,
int weight) {
@VisibleForTesting
static double calculateExpenditureCycling(Location start, Location stop, double grade, int weight) {
// Seconds.
double time = (double) (stop.getTime() - start.getTime()) / 1000;
// Meter per second.
@@ -144,22 +149,14 @@ public class CalorieUtils {
* @param weight the weight of user
* @return the calories expenditure between the start and stop location
*/
private static double calculateExpenditureFoot(Location start, Location stop, double grade,
int weight) {
// Seconds.
double time = (double) (stop.getTime() - start.getTime()) / 1000;
@VisibleForTesting
static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) {
// Meter per minute.
double speed = (start.getSpeed() + stop.getSpeed()) * 60 / 2;
double VO2 = 0;
if (grade < 0) {
grade = 0;
}
if (speed > CRTICAL_SPEED_RUNNING) {
VO2 = calculateRunningVO2(speed, grade);
} else {
VO2 = calculateWalkingVO2(speed, grade);
}
// Get VO2
double VO2 = getVO2(speed, grade);
// Seconds.
double time = (double) (stop.getTime() - start.getTime()) / 1000;
// Change mL/kg/min to mL/kg.
double VO2All = VO2 * time / 60;
// Change mL/kg to L/kg.
@@ -168,6 +165,29 @@ public class CalorieUtils {
return VO2All * weight * VO2H_TO_KCAL;
}
/**
* Gets the VO2 value.
*
* @param start the start location
* @param stop the stop location
* @param grade the grade to calculate
* @return the VO2 value
*/
@VisibleForTesting
static double getVO2(double speed, double grade) {
if (grade < 0) {
grade = 0;
}
double VO2 = 0;
if (speed > CRTICAL_SPEED_RUNNING) {
VO2 = calculateRunningVO2(speed, grade);
} else {
VO2 = calculateWalkingVO2(speed, grade);
}
return VO2;
}
/**
* Calculates the calories expenditure between two locations.
*