Bug 1446732 - 2. Make @AssertCalled(count = 0) more intuitive; r=snorp

Make @AssertCalled(count = 0) the same as @AssertCalled(false), which is
more intuitive. The default behavior of @AssertCalled(true) is still one
or more calls, with an implicit count = -1.

MozReview-Commit-ID: Fy6eapTCp9

--HG--
extra : rebase_source : 9753227ea43903fcae764206f3edbdee9da8ccc7
This commit is contained in:
Jim Chen 2018-03-18 05:38:55 -04:00
parent c356702f23
commit 57f57f83d3

View File

@ -226,15 +226,16 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
@Retention(RetentionPolicy.RUNTIME)
public @interface AssertCalled {
/**
* @return True if the method must be called,
* @return True if the method must be called if count != 0,
* or false if the method must not be called.
*/
boolean value() default true;
/**
* @return If called, the number of calls called, or 0 to allow any number > 0.
* @return The number of calls allowed. Specify -1 to allow any number > 0. Specify 0 to
* assert the method is not called, even if value() is true.
*/
int count() default 0;
int count() default -1;
/**
* @return If called, the order number for each call, or 0 to allow arbitrary
@ -325,8 +326,8 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
}
/* package */ int getCount() {
return (requirement == null) ? 0 :
!requirement.allowed ? -1 : requirement.count;
return (requirement == null) ? -1 :
requirement.allowed ? requirement.count : 0;
}
/* package */ void incrementCounter() {
@ -338,12 +339,12 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
}
/* package */ boolean allowUnlimitedCalls() {
return getCount() == 0;
return getCount() == -1;
}
/* package */ boolean allowMoreCalls() {
final int count = getCount();
return count == 0 || count > currentCount;
return count == -1 || count > currentCount;
}
/* package */ CallInfo getInfo() {
@ -569,9 +570,9 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
private void assertAllowMoreCalls(final MethodCall call) {
final int count = call.getCount();
if (count != 0) {
if (count != -1) {
assertThat(call.method.getName() + " call count should be within limit",
call.getCurrentCount() + 1, lessThanOrEqualTo(Math.max(0, count)));
call.getCurrentCount() + 1, lessThanOrEqualTo(count));
}
}
@ -588,10 +589,10 @@ public class GeckoSessionTestRule extends UiThreadTestRule {
return;
}
final int count = call.getCount();
if (count < 0) {
if (count == 0) {
assertThat(call.method.getName() + " should not be called",
call.getCurrentCount(), equalTo(0));
} else if (count == 0) {
} else if (count == -1) {
assertThat(call.method.getName() + " should be called",
call.getCurrentCount(), greaterThan(0));
} else {