• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.federatedcompute.common.TrainingInterval.SCHEDULING_MODE_ONE_TIME;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.junit.Assert.assertThrows;
24 
25 import android.os.Parcel;
26 
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 /** Tests for {@link TrainingOptions} */
34 @RunWith(AndroidJUnit4.class)
35 public final class TrainingOptionsTest {
36     private String mTestPopulation;
37     private int mTestJobId;
38     private TrainingInterval mOneOffInterval;
39 
40     @Before
setUp()41     public void setUp() {
42         mTestPopulation = "population";
43         mTestJobId = 10086;
44         mOneOffInterval =
45                 new TrainingInterval.Builder().setSchedulingMode(SCHEDULING_MODE_ONE_TIME).build();
46     }
47 
48     @Test
testFederatedTask()49     public void testFederatedTask() {
50         TrainingOptions options =
51                 new TrainingOptions.Builder()
52                         .setPopulationName(mTestPopulation)
53                         .setJobSchedulerJobId(mTestJobId)
54                         .setTrainingInterval(mOneOffInterval)
55                         .build();
56         assertThat(options.getPopulationName()).isEqualTo(mTestPopulation);
57         assertThat(options.getJobSchedulerJobId()).isEqualTo(mTestJobId);
58         assertThat(options.getTrainingInterval()).isEqualTo(mOneOffInterval);
59     }
60 
61     @Test
testNullPopulation()62     public void testNullPopulation() {
63         assertThrows(
64                 IllegalArgumentException.class,
65                 () ->
66                         new TrainingOptions.Builder()
67                                 .setPopulationName(null)
68                                 .setJobSchedulerJobId(mTestJobId)
69                                 .setTrainingInterval(mOneOffInterval)
70                                 .build());
71     }
72 
73     @Test
testEmptyPopulation()74     public void testEmptyPopulation() {
75         assertThrows(
76                 IllegalArgumentException.class,
77                 () ->
78                         new TrainingOptions.Builder()
79                                 .setPopulationName("")
80                                 .setJobSchedulerJobId(mTestJobId)
81                                 .setTrainingInterval(mOneOffInterval)
82                                 .build());
83     }
84 
85     @Test
testJobIdCannotBeZero()86     public void testJobIdCannotBeZero() {
87         assertThrows(
88                 IllegalArgumentException.class,
89                 () ->
90                         new TrainingOptions.Builder()
91                                 .setPopulationName(mTestPopulation)
92                                 .setJobSchedulerJobId(0)
93                                 .setTrainingInterval(mOneOffInterval)
94                                 .build());
95     }
96 
97     @Test
testNullTrainingIntervalIsAllowed()98     public void testNullTrainingIntervalIsAllowed() {
99         TrainingOptions options =
100                 new TrainingOptions.Builder()
101                         .setPopulationName(mTestPopulation)
102                         .setJobSchedulerJobId(mTestJobId)
103                         .setTrainingInterval(null)
104                         .build();
105         assertThat(options.getTrainingInterval()).isNull();
106     }
107 
108     @Test
testParcelValidInterval()109     public void testParcelValidInterval() {
110         TrainingOptions options =
111                 new TrainingOptions.Builder()
112                         .setPopulationName(mTestPopulation)
113                         .setJobSchedulerJobId(mTestJobId)
114                         .setTrainingInterval(null)
115                         .build();
116 
117         Parcel p = Parcel.obtain();
118         options.writeToParcel(p, 0);
119         p.setDataPosition(0);
120         TrainingOptions fromParcel = TrainingOptions.CREATOR.createFromParcel(p);
121 
122         assertThat(options).isEqualTo(fromParcel);
123     }
124 }
125