/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sejda.sambox.pdmodel;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Test;
import org.sejda.sambox.cos.COSArray;
import org.sejda.sambox.cos.COSDictionary;
import org.sejda.sambox.cos.COSName;
import org.sejda.sambox.cos.COSNull;
import org.sejda.sambox.cos.COSNumber;
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotationLink;

/**
 * @author Andrea Vacondio
 *
 */
public class PDPageTest
{

    @Test
    public void nullBeads()
    {
        PDPage victim = new PDPage();
        victim.getCOSObject().setItem(COSName.B, null);
        assertTrue(victim.getThreadBeads().isEmpty());
    }

    @Test
    public void cosNullBeadsItem()
    {
        PDPage victim = new PDPage();
        COSArray beads = new COSArray(COSNull.NULL);
        victim.getCOSObject().setItem(COSName.B, beads);
        assertTrue(victim.getThreadBeads().isEmpty());
    }

    @Test
    public void wrongTypeBeadsItem() throws IOException
    {
        PDPage victim = new PDPage();
        COSArray beads = new COSArray(COSNumber.get("2"));
        victim.getCOSObject().setItem(COSName.B, beads);
        assertTrue(victim.getThreadBeads().isEmpty());
    }

    @Test
    public void nonNullBeadsItem()
    {
        PDPage victim = new PDPage();
        COSArray beads = new COSArray(new COSDictionary());
        victim.getCOSObject().setItem(COSName.B, beads);
        assertFalse(victim.getThreadBeads().isEmpty());
    }

    @Test
    public void nullAnnotations()
    {
        PDPage victim = new PDPage();
        victim.getCOSObject().setItem(COSName.ANNOTS, null);
        assertTrue(victim.getAnnotations().isEmpty());
    }

    @Test
    public void cosNullAnnotsItem()
    {
        PDPage victim = new PDPage();
        COSArray beads = new COSArray(COSNull.NULL);
        victim.getCOSObject().setItem(COSName.ANNOTS, beads);
        assertTrue(victim.getAnnotations().isEmpty());
    }

    @Test
    public void wrongTypeAnnotsItem() throws IOException
    {
        PDPage victim = new PDPage();
        COSArray beads = new COSArray(COSNumber.get("2"));
        victim.getCOSObject().setItem(COSName.ANNOTS, beads);
        assertTrue(victim.getAnnotations().isEmpty());
    }

    @Test
    public void nonNullAnnotsItem()
    {
        PDPage victim = new PDPage();
        COSArray beads = new COSArray(new PDAnnotationLink().getCOSObject());
        victim.getCOSObject().setItem(COSName.ANNOTS, beads);
        assertFalse(victim.getAnnotations().isEmpty());
    }
}

@@ -831,16 +831,61 @@
 model;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -920,16 +920,16 @@
 tFalse;%0A
-
 import s
@@ -1044,24 +1044,17 @@
 box.cos.
-COSArray
+*
 ;%0Aimport
@@ -1075,138 +1075,34 @@
 box.
-cos.COSDictionary;%0Aimport org.sejda.sambox.cos.COSName;%0Aimport org.sejda.sambox.cos.COSNull;%0Aimport org.sejda.sambox.cos.COSNumber
+pdmodel.common.PDRectangle
 ;%0Aim
@@ -3319,11 +3319,639 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void cropBoxSlighlyOutOfMediaBoxBounds()%0A    %7B%0A        PDPage page = new PDPage();%0A        page.setMediaBox(new PDRectangle(toArray(0, 0, 287.07f, 831)));%0A        page.setCropBox(new PDRectangle(toArray(1, 214, 294, 624)));%0A        assertEquals(page.getCropBox(), new PDRectangle(toArray(1, 214, 287.07f, 624)));%0A    %7D%0A%0A    COSArray toArray(float n1, float n2, float n3, float n4) %7B%0A        COSArray result = new COSArray();%0A        result.add(new COSFloat(n1));%0A        result.add(new COSFloat(n2));%0A        result.add(new COSFloat(n3));%0A        result.add(new COSFloat(n4));%0A        return result;%0A    %7D
 %0A%7D%0A

Test handling crop box slightly out of mediabox