blob: 06100c85654b4e12c31250da7a77c4b5d6a71b63 [file] [log] [blame]
/*
* Copyright (C) 2022 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.healthconnect.cts;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.healthconnect.HealthConnectException;
import android.healthconnect.HealthConnectManager;
import android.healthconnect.InsertRecordsResponse;
import android.healthconnect.datatypes.HydrationRecord;
import android.healthconnect.datatypes.Metadata;
import android.healthconnect.datatypes.Record;
import android.healthconnect.datatypes.units.Volume;
import android.os.OutcomeReceiver;
import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@RunWith(AndroidJUnit4.class)
public class HydrationRecordTest {
private static final String TAG = "HydrationRecordTest";
static HydrationRecord getBaseHydrationRecord() {
return new HydrationRecord.Builder(
new Metadata.Builder().build(),
Instant.now(),
Instant.now(),
Volume.fromMilliliters(10.0))
.build();
}
static HydrationRecord getCompleteHydrationRecord() {
return new HydrationRecord.Builder(
new Metadata.Builder().build(),
Instant.now(),
Instant.now(),
Volume.fromMilliliters(10.0))
.setStartZoneOffset(ZoneOffset.systemDefault().getRules().getOffset(Instant.now()))
.setEndZoneOffset(ZoneOffset.systemDefault().getRules().getOffset(Instant.now()))
.build();
}
@Test
public void testInsertHydrationRecord() throws InterruptedException {
List<Record> records = new ArrayList<>();
records.add(getBaseHydrationRecord());
records.add(getCompleteHydrationRecord());
Context context = ApplicationProvider.getApplicationContext();
CountDownLatch latch = new CountDownLatch(1);
HealthConnectManager service = context.getSystemService(HealthConnectManager.class);
assertThat(service).isNotNull();
AtomicReference<List<Record>> response = new AtomicReference<>();
service.insertRecords(
records,
Executors.newSingleThreadExecutor(),
new OutcomeReceiver<>() {
@Override
public void onResult(InsertRecordsResponse result) {
response.set(result.getRecords());
latch.countDown();
}
@Override
public void onError(HealthConnectException exception) {
Log.e(TAG, exception.getMessage());
}
});
assertThat(latch.await(3, TimeUnit.SECONDS)).isEqualTo(true);
assertThat(response.get()).hasSize(records.size());
}
}