1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.cronet_test_apk; 6 7 import android.test.InstrumentationTestCase; 8 import android.test.suitebuilder.annotation.SmallTest; 9 10 import org.chromium.base.test.util.Feature; 11 import org.chromium.net.ChunkedWritableByteChannel; 12 13 import java.io.IOException; 14 import java.nio.ByteBuffer; 15 import java.nio.channels.ClosedChannelException; 16 import java.nio.charset.Charset; 17 import java.util.Arrays; 18 19 /** 20 * Tests for {@link ChunkedWritableByteChannel} 21 */ 22 public class ChunkedWritableByteChannelTest extends InstrumentationTestCase { 23 private ChunkedWritableByteChannel mChannel; 24 25 @Override 26 public void setUp() { 27 mChannel = new ChunkedWritableByteChannel(); 28 } 29 30 @SmallTest 31 @Feature({"Cronet"}) 32 public void testSetCapacity() { 33 int capacity = 100; 34 mChannel.setCapacity(capacity); 35 assertEquals("Bytebuffer capacity wasn't set properly", capacity, 36 mChannel.getByteBuffer().capacity()); 37 mChannel.close(); 38 } 39 40 @SmallTest 41 @Feature({"Cronet"}) 42 public void testWrite() throws IOException { 43 String test = "Write in the buffer."; 44 mChannel.write( 45 ByteBuffer.wrap(test.getBytes(Charset.forName("UTF-8")))); 46 mChannel.write( 47 ByteBuffer.wrap(test.getBytes(Charset.forName("UTF-8")))); 48 assertEquals("Buffer didn't write the bytes properly", test + test, 49 new String(mChannel.getBytes(), "UTF-8")); 50 mChannel.close(); 51 } 52 53 @SmallTest 54 @Feature({"Cronet"}) 55 public void testCloseChannel() { 56 assertTrue("Channel should be open", mChannel.isOpen()); 57 mChannel.close(); 58 assertFalse("Channel shouldn't be open", mChannel.isOpen()); 59 } 60 61 @SmallTest 62 @Feature({"Cronet"}) 63 public void testWriteToClosedChannel() throws IOException { 64 try { 65 mChannel.close(); 66 mChannel.write(ByteBuffer.wrap(new byte[1])); 67 fail("ClosedChannelException should have been thrown."); 68 } catch (ClosedChannelException e) { 69 // Intended 70 } 71 } 72 73 @SmallTest 74 @Feature({"Cronet"}) 75 public void testCapacityGrows() throws Exception { 76 mChannel.setCapacity(123); 77 byte[] data = new byte[1234]; 78 Arrays.fill(data, (byte) 'G'); 79 mChannel.write(ByteBuffer.wrap(data)); 80 assertTrue(Arrays.equals(data, mChannel.getBytes())); 81 } 82 } 83