1 /*
2  * Copyright 2019 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.media.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.media.Session2Command;
24 import android.media.Session2CommandGroup;
25 import android.os.Parcel;
26 
27 import androidx.test.filters.SmallTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.Set;
34 
35 /**
36  * Tests {@link android.media.Session2CommandGroup}.
37  */
38 @RunWith(AndroidJUnit4.class)
39 @SmallTest
40 public class Session2CommandGroupTest {
41     private final int TEST_COMMAND_CODE_1 = 10000;
42     private final int TEST_COMMAND_CODE_2 = 10001;
43     private final int TEST_COMMAND_CODE_3 = 10002;
44 
45     @Test
testHasCommand()46     public void testHasCommand() {
47         Session2Command testCommand = new Session2Command(TEST_COMMAND_CODE_1);
48         Session2CommandGroup.Builder builder = new Session2CommandGroup.Builder()
49                 .addCommand(testCommand);
50         Session2CommandGroup commandGroup = builder.build();
51         assertTrue(commandGroup.hasCommand(TEST_COMMAND_CODE_1));
52         assertTrue(commandGroup.hasCommand(testCommand));
53         assertFalse(commandGroup.hasCommand(TEST_COMMAND_CODE_2));
54     }
55 
56     @Test
testGetCommands()57     public void testGetCommands() {
58         Session2Command command1 = new Session2Command(TEST_COMMAND_CODE_1);
59         Session2Command command2 = new Session2Command(TEST_COMMAND_CODE_2);
60         Session2CommandGroup.Builder builder = new Session2CommandGroup.Builder()
61                 .addCommand(command1);
62         Session2CommandGroup commandGroup = builder.build();
63 
64         Set<Session2Command> commands = commandGroup.getCommands();
65         assertTrue(commands.contains(command1));
66         assertFalse(commands.contains(command2));
67     }
68 
69     @Test
testDescribeContents()70     public void testDescribeContents() {
71         final int expected = 0;
72         Session2Command command = new Session2Command(TEST_COMMAND_CODE_1);
73         Session2CommandGroup.Builder builder = new Session2CommandGroup.Builder()
74                 .addCommand(command);
75         Session2CommandGroup commandGroup = builder.build();
76         assertEquals(expected, commandGroup.describeContents());
77     }
78 
79     @Test
testWriteToParcel()80     public void testWriteToParcel() {
81         Session2CommandGroup.Builder builder = new Session2CommandGroup.Builder()
82                 .addCommand(new Session2Command(TEST_COMMAND_CODE_1))
83                 .addCommand(new Session2Command(TEST_COMMAND_CODE_2));
84         Session2CommandGroup commandGroup = builder.build();
85         Parcel dest = Parcel.obtain();
86         commandGroup.writeToParcel(dest, 0);
87         dest.setDataPosition(0);
88         Session2CommandGroup commandGroupFromParcel =
89             Session2CommandGroup.CREATOR.createFromParcel(dest);
90         assertEquals(commandGroup.getCommands(), commandGroupFromParcel.getCommands());
91         dest.recycle();
92     }
93 
94     @Test
testBuilder()95     public void testBuilder() {
96         Session2CommandGroup.Builder builder = new Session2CommandGroup.Builder()
97                 .addCommand(new Session2Command(TEST_COMMAND_CODE_1));
98         Session2CommandGroup commandGroup = builder.build();
99         Session2CommandGroup.Builder newBuilder = new Session2CommandGroup.Builder(commandGroup);
100         Session2CommandGroup newCommandGroup = newBuilder.build();
101         assertEquals(commandGroup.getCommands(), newCommandGroup.getCommands());
102     }
103 
104     @Test
testAddAndRemoveCommand()105     public void testAddAndRemoveCommand() {
106         Session2Command testCommand1 = new Session2Command(TEST_COMMAND_CODE_1);
107         Session2Command testCommand2 = new Session2Command(TEST_COMMAND_CODE_2);
108         Session2Command testCommand3 = new Session2Command(TEST_COMMAND_CODE_3);
109         Session2CommandGroup.Builder builder = new Session2CommandGroup.Builder()
110                 .addCommand(testCommand1)
111                 .addCommand(testCommand2)
112                 .addCommand(testCommand3);
113         builder.removeCommand(testCommand1)
114                 .removeCommand(testCommand2);
115         Session2CommandGroup commandGroup = builder.build();
116         assertFalse(commandGroup.hasCommand(testCommand1));
117         assertFalse(commandGroup.hasCommand(testCommand2));
118         assertTrue(commandGroup.hasCommand(testCommand3));
119     }
120 }
121