1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.federatedcompute.common; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.os.Parcel; 24 25 import androidx.test.ext.junit.runners.AndroidJUnit4; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** Tests for {@link TrainingInterval} */ 31 @RunWith(AndroidJUnit4.class) 32 public class TrainingIntervalTest { 33 @Test testRecurrentTask()34 public void testRecurrentTask() { 35 long minimumIntervalMillis = 10000000L; 36 TrainingInterval interval = 37 createInterval(TrainingInterval.SCHEDULING_MODE_RECURRENT, minimumIntervalMillis); 38 assertThat(interval.getSchedulingMode()) 39 .isEqualTo(TrainingInterval.SCHEDULING_MODE_RECURRENT); 40 assertThat(interval.getMinimumIntervalMillis()).isEqualTo(minimumIntervalMillis); 41 } 42 43 @Test testOneOffTask()44 public void testOneOffTask() { 45 TrainingInterval interval = createInterval(TrainingInterval.SCHEDULING_MODE_ONE_TIME, 0); 46 assertThat(interval.getSchedulingMode()) 47 .isEqualTo(TrainingInterval.SCHEDULING_MODE_ONE_TIME); 48 } 49 50 @Test testInvalidRecurrentTask()51 public void testInvalidRecurrentTask() { 52 assertThrows( 53 IllegalArgumentException.class, 54 () -> 55 new TrainingInterval.Builder() 56 .setSchedulingMode(TrainingInterval.SCHEDULING_MODE_RECURRENT) 57 .setMinimumIntervalMillis(0) 58 .build()); 59 } 60 61 @Test testEquals()62 public void testEquals() { 63 TrainingInterval interval = 64 createInterval(TrainingInterval.SCHEDULING_MODE_RECURRENT, 10000); 65 TrainingInterval copy = interval; 66 assertThat(interval.equals(copy)).isTrue(); 67 assertThat(interval.equals(new Object())).isFalse(); 68 TrainingInterval identical = 69 createInterval(TrainingInterval.SCHEDULING_MODE_RECURRENT, 10000); 70 assertThat(interval.equals(identical)).isTrue(); 71 assertThat(interval.hashCode()).isEqualTo(identical.hashCode()); 72 TrainingInterval differentMode = 73 createInterval(TrainingInterval.SCHEDULING_MODE_ONE_TIME, 10000); 74 assertThat(interval.equals(differentMode)).isFalse(); 75 TrainingInterval differentInterval = 76 createInterval(TrainingInterval.SCHEDULING_MODE_RECURRENT, 20000); 77 assertThat(interval.equals(differentInterval)).isFalse(); 78 } 79 80 @Test testParcelValidInterval()81 public void testParcelValidInterval() { 82 TrainingInterval interval = 83 createInterval(TrainingInterval.SCHEDULING_MODE_RECURRENT, 10000); 84 85 Parcel p = Parcel.obtain(); 86 interval.writeToParcel(p, 0); 87 p.setDataPosition(0); 88 TrainingInterval fromParcel = TrainingInterval.CREATOR.createFromParcel(p); 89 90 assertThat(interval).isEqualTo(fromParcel); 91 } 92 createInterval(int mode, long minimumIntervalMillis)93 private static TrainingInterval createInterval(int mode, long minimumIntervalMillis) { 94 return new TrainingInterval.Builder() 95 .setSchedulingMode(mode) 96 .setMinimumIntervalMillis(minimumIntervalMillis) 97 .build(); 98 } 99 } 100