Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9d5664ed authored by George Mount's avatar George Mount Committed by Android (Google) Code Review
Browse files

Merge "Use accurate time to update Chronometer value" into main

parents 88248b6b 5c45aeef
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -328,7 +328,7 @@ public class Chronometer extends TextView {
            if (running) {
                updateText(SystemClock.elapsedRealtime());
                dispatchChronometerTick();
                postDelayed(mTickRunnable, 1000);
                postTickOnNextSecond();
            } else {
                removeCallbacks(mTickRunnable);
            }
@@ -342,11 +342,17 @@ public class Chronometer extends TextView {
            if (mRunning) {
                updateText(SystemClock.elapsedRealtime());
                dispatchChronometerTick();
                postDelayed(mTickRunnable, 1000);
                postTickOnNextSecond();
            }
        }
    };

    private void postTickOnNextSecond() {
        long nowMillis = SystemClock.elapsedRealtime();
        int millis = (int) ((nowMillis - mBase) % 1000);
        postDelayed(mTickRunnable, 1000 - millis);
    }

    void dispatchChronometerTick() {
        if (mOnChronometerTickListener != null) {
            mOnChronometerTickListener.onChronometerTick(this);
+11 −0
Original line number Diff line number Diff line
@@ -265,6 +265,17 @@
            </intent-filter>
        </activity>

        <activity android:name="android.widget.ChronometerActivity"
                android:label="ChronometerActivity"
                android:screenOrientation="portrait"
                android:exported="true"
                android:theme="@android:style/Theme.Material.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
            </intent-filter>
        </activity>

        <activity android:name="android.widget.DatePickerActivity"
                android:label="DatePickerActivity"
                android:screenOrientation="portrait"
+25 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <Chronometer
            android:id="@+id/chronometer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</FrameLayout>
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.widget;

import android.app.Activity;
import android.os.Bundle;

import com.android.frameworks.coretests.R;

/**
 * A minimal application for DatePickerFocusTest.
 */
public class ChronometerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chronometer_layout);
    }
}
+83 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.widget;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import androidx.test.filters.LargeTest;

import com.android.frameworks.coretests.R;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Test {@link DatePicker} focus changes.
 */
@SuppressWarnings("deprecation")
@LargeTest
public class ChronometerTest extends ActivityInstrumentationTestCase2<ChronometerActivity> {

    private Activity mActivity;
    private Chronometer mChronometer;

    public ChronometerTest() {
        super(ChronometerActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mActivity = getActivity();
        mChronometer = mActivity.findViewById(R.id.chronometer);
    }

    public void testChronometerTicksSequentially() throws Throwable {
        final CountDownLatch latch = new CountDownLatch(5);
        ArrayList<String> ticks = new ArrayList<>();
        runOnUiThread(() -> {
            mChronometer.setOnChronometerTickListener((chronometer) -> {
                ticks.add(chronometer.getText().toString());
                latch.countDown();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }
            });
            mChronometer.start();
        });
        assertTrue(latch.await(6, TimeUnit.SECONDS));
        assertTrue(ticks.size() >= 5);
        assertEquals("00:00", ticks.get(0));
        assertEquals("00:01", ticks.get(1));
        assertEquals("00:02", ticks.get(2));
        assertEquals("00:03", ticks.get(3));
        assertEquals("00:04", ticks.get(4));
    }

    private void runOnUiThread(Runnable runnable) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        mActivity.runOnUiThread(() -> {
            runnable.run();
            latch.countDown();
        });
        latch.await();
    }
}