1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81

content / public / android / junit / src / org / chromium / content_public / browser / MessagePayloadTest.java [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content_public.browser;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import org.chromium.base.test.BaseRobolectricTestRunner;

import java.io.UnsupportedEncodingException;

/**
 * Unit tests for MessagePayload.
 * Note: After new type is added, please add a test case here.
 */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class MessagePayloadTest {
    @Test
    public void testString() {
        final String testStr = "TestStr";
        MessagePayload messagePayload = new MessagePayload(testStr);
        Assert.assertEquals(messagePayload.getAsString(), testStr);
        Assert.assertEquals(messagePayload.getType(), MessagePayloadType.STRING);

        Assert.assertEquals(messagePayload.getType(), MessagePayloadType.STRING);
    }

    @Test
    public void testStringCanBeNull() {
        MessagePayload jsValue = new MessagePayload((String) null);
        Assert.assertNull(jsValue.getAsString());
        Assert.assertEquals(jsValue.getType(), MessagePayloadType.STRING);
    }

    @Test
    public void testArrayBuffer() throws UnsupportedEncodingException {
        final byte[] bytes = "TestStr".getBytes("UTF-8");
        MessagePayload jsValue = new MessagePayload(bytes);
        Assert.assertEquals(jsValue.getAsArrayBuffer(), bytes);
        Assert.assertEquals(jsValue.getType(), MessagePayloadType.ARRAY_BUFFER);
    }

    @Test
    public void testArrayBufferCannotBeNull() {
        try {
            new MessagePayload((byte[]) null);
            Assert.fail("Should throw exception");
        } catch (NullPointerException e) {
            // Expected
        }
    }

    @Test
    public void testWrongValueTypeString() throws UnsupportedEncodingException {
        MessagePayload jsValue = new MessagePayload("TestStr".getBytes("UTF-8"));
        Assert.assertEquals(jsValue.getType(), MessagePayloadType.ARRAY_BUFFER);
        try {
            jsValue.getAsString();
            Assert.fail("Should throw exception");
        } catch (IllegalStateException e) {
            // Expected
        }
    }

    @Test
    public void testWrongValueTypeArrayBuffer() {
        MessagePayload jsValue = new MessagePayload("TestStr");
        Assert.assertEquals(jsValue.getType(), MessagePayloadType.STRING);
        try {
            jsValue.getAsArrayBuffer();
            Assert.fail("Should throw exception");
        } catch (IllegalStateException e) {
            // Expected
        }
    }
}