1 /*
2  * Copyright 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 androidx.webkit;
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4;
20 import androidx.test.filters.SmallTest;
21 
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.junit.function.ThrowingRunnable;
25 import org.junit.runner.RunWith;
26 
27 /**
28  * Test {@link WebMessageCompat} basic usages.
29  */
30 @SmallTest
31 @RunWith(AndroidJUnit4.class)
32 public class WebMessageCompatUnitTest {
33 
34     @Test
testArrayBufferUsage()35     public void testArrayBufferUsage() {
36         final byte[] bytes = {1, 2, 3};
37         final WebMessageCompat message = new WebMessageCompat(bytes);
38         Assert.assertEquals(WebMessageCompat.TYPE_ARRAY_BUFFER, message.getType());
39         Assert.assertArrayEquals(bytes, message.getArrayBuffer());
40         Assert.assertThrows(IllegalStateException.class, new ThrowingRunnable() {
41             @Override
42             public void run() throws Throwable {
43                 message.getData();
44             }
45         });
46     }
47 
48     @Test
testStringUsage()49     public void testStringUsage() {
50         final String string = "Hello";
51         final WebMessageCompat message = new WebMessageCompat(string);
52         Assert.assertEquals(WebMessageCompat.TYPE_STRING, message.getType());
53         Assert.assertEquals(string, message.getData());
54         Assert.assertThrows(IllegalStateException.class, new ThrowingRunnable() {
55             @Override
56             public void run() throws Throwable {
57                 message.getArrayBuffer();
58             }
59         });
60     }
61 }
62