Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

* [android] The timer in Android will supports float interval/delay #699

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import android.os.Handler;
import android.os.Message;
import android.support.annotation.FloatRange;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.VisibleForTesting;
Expand Down Expand Up @@ -63,16 +64,16 @@ public WXTimerModule() {


@JSMethod(uiThread = false)
public void setTimeout(@IntRange(from = 1) int funcId, @IntRange(from = 0) int delay) {
public void setTimeout(@IntRange(from = 1) int funcId, @FloatRange(from = 0) float delay) {
if(mWXSDKInstance != null) {
postOrHoldMessage(MODULE_TIMEOUT, funcId, delay, Integer.parseInt(mWXSDKInstance.getInstanceId()));
postOrHoldMessage(MODULE_TIMEOUT, funcId, (int) delay, Integer.parseInt(mWXSDKInstance.getInstanceId()));
}
}

@JSMethod(uiThread = false)
public void setInterval(@IntRange(from = 1) int funcId, @IntRange(from = 0) int interval) {
public void setInterval(@IntRange(from = 1) int funcId, @FloatRange(from = 0) float interval) {
if(mWXSDKInstance != null) {
postOrHoldMessage(MODULE_INTERVAL, funcId, interval, Integer.parseInt(mWXSDKInstance.getInstanceId()));
postOrHoldMessage(MODULE_INTERVAL, funcId, (int) interval, Integer.parseInt(mWXSDKInstance.getInstanceId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.taobao.weex.ui.module;

import static android.R.attr.end;
import static android.R.attr.start;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -61,6 +63,7 @@ public class WXTimerModuleTest {
public final static int DELAY = 50;
public final static int IMMEDIATELY = 0;
public final static int INVALID_DELAY = -50;
public final static float FLOAT_DELAY = 20.6f;

@Rule
public PowerMockRule rule = new PowerMockRule();
Expand All @@ -84,7 +87,26 @@ public void setup() throws Exception {
@Test
public void testSetTimeoutDelay() throws Exception {
module.setTimeout(VALID_FUNC_ID, DELAY);
mLooper.idle(DELAY);
long start, end, duration;
start = mLooper.getScheduler().getCurrentTime();
mLooper.runOneTask();
end = mLooper.getScheduler().getCurrentTime();
duration = end - start;

assertThat(duration, is((long) DELAY));
Mockito.verify(module, times(1)).handleMessage(any(Message.class));
}

@Test
public void testSetTimeoutDelay2() throws Exception {
module.setTimeout(VALID_FUNC_ID, FLOAT_DELAY);
long start, end, duration;
start = mLooper.getScheduler().getCurrentTime();
mLooper.runOneTask();
end = mLooper.getScheduler().getCurrentTime();
duration = end - start;

assertThat(duration, is((long) FLOAT_DELAY));
Mockito.verify(module, times(1)).handleMessage(any(Message.class));
}

Expand Down Expand Up @@ -163,6 +185,23 @@ public void testSetIntervalDelay() {
Mockito.verify(module, times(3)).handleMessage(any(Message.class));
}

@Test
public void testSetIntervalDelay2() {
long start, end, duration;
module.setInterval(VALID_FUNC_ID, FLOAT_DELAY);

start = mLooper.getScheduler().getCurrentTime();
mLooper.runOneTask();
end = mLooper.getScheduler().getCurrentTime();
duration = end - start;

assertThat(duration, is((long) FLOAT_DELAY));

mLooper.runOneTask();
mLooper.runOneTask();
Mockito.verify(module, times(3)).handleMessage(any(Message.class));
}

@Test
public void testClearTimeout() throws Exception {
module.setTimeout(VALID_FUNC_ID, DELAY);
Expand All @@ -180,15 +219,15 @@ public void testClearInterval() throws Exception {
}

@Test
public void setClearTimeout2(){
public void testClearTimeout2(){
module.setTimeout(NO_CACHING_FUNC_ID, DELAY);
module.clearTimeout(NO_CACHING_FUNC_ID);
mLooper.idle(DELAY, TimeUnit.MILLISECONDS);
Mockito.verify(module, never()).handleMessage(any(Message.class));
}

@Test
public void setClearInterval2(){
public void testClearInterval2(){
module.setInterval(NO_CACHING_FUNC_ID, DELAY);
module.clearInterval(NO_CACHING_FUNC_ID);
mLooper.idle(DELAY, TimeUnit.MILLISECONDS);
Expand Down
14 changes: 11 additions & 3 deletions test/pages/modules/vue_timer.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<template>
<div>
<text>setTimeout timeout=3000</text>
<div class="wrapper">
<div class="wrapper-top">
<text test-id="setTimeout" class="t" @click="stimeout">SetTimeOut</text>
<text test-id="clearTimeout" class="t" @click="ctimeout">ClearTimeOut</text>
</div>
<text class="content" test-id="timeout">{{timeout_content}}</text>

<text style="margin-top: 100px">setInterval interval=3000</text>
<div style="background-color:red" class="wrapper">
<div class="wrapper-bottom">
<text test-id="setInterval" class="t" @click="sinterval">SetInterval</text>
<text test-id="clearInterval" class="t" @click="cinterval">ClearInterval</text>
</div>
Expand All @@ -17,13 +17,21 @@
</template>

<style>
.wrapper {
.wrapper-top {
height: 100px;
width: 750px;
background-color: yellow;
align-items: center;
flex-direction: row
}

.wrapper-bottom {
height: 100px;
width: 750px;
background-color: red;
align-items: center;
flex-direction: row
}

.t {
font-size: 36px;
Expand Down
14 changes: 11 additions & 3 deletions test/pages/modules/we_timer.we
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<template>
<div>
<text>setTimeout timeout=5000</text>
<div class="wrapper">
<div class="wrapper-top">
<text test-id="setTimeout" class="t" onclick="stimeout">SetTimeOut</text>
<text test-id="clearTimeout" class="t" onclick="ctimeout">ClearTimeOut</text>
</div>
<text class="content" test-id="timeout">{{timeout_content}}</text>

<text style="margin-top: 100px">setInterval interval=5000</text>
<div style="background-color:red" class="wrapper">
<div class="wrapper-bottom">
<text test-id="setInterval" class="t" onclick="sinterval">SetInterval</text>
<text test-id="clearInterval" class="t" onclick="cinterval">ClearInterval</text>
</div>
Expand All @@ -17,13 +17,21 @@
</template>

<style>
.wrapper {
.wrapper-top {
height: 100px;
width: 750px;
background-color: yellow;
align-items: center;
flex-direction: row
}

.wrapper-bottom {
height: 100px;
width: 750px;
background-color: red;
align-items: center;
flex-direction: row
}

.t {
font-size: 36px;
Expand Down