#
# PWG 5100.5-2024: IPP Document Object v1.2 (DOCOBJECT)
#
# https://ftp.pwg.org/pub/pwg/candidates/cs-ippdocobject12-20240517-5100.5.pdf
#
# Copyright © 2024 by OpenPrinting.
# Copyright © 2014-2022 by the Printer Working Group.
#
# Licensed under Apache License v2.0.  See the file "LICENSE" for more
# information.
#
# Usage:
#
#   ./ipptool [OPTIONS] -f FILENAME -t PRINTER-URI pwg5100.5.test
#
# Options:
#
#   -d NOPRINT=1              Disable the print tests.
#   -d REQUIRE_DOCOBJECT=1    Require support for the document object.
#

{
    NAME "PWG 5100.5-2024: Operations, Attributes, and Values"
    OPERATION Get-Printer-Attributes
    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri

    STATUS successful-ok

    # Table 1 - IPP Operations
    EXPECT operations-supported OF-TYPE enum IN-GROUP printer-attributes-tag

    EXPECT operations-supported WITH-VALUE 0x0034 DEFINE-MATCH PRINTER_HAS_DOCOBJECT

    EXPECT operations-supported WITH-VALUE 0x0033 IF-DEFINED REQUIRE_DOCOBJECT # REQUIRED Cancel-Document
    EXPECT operations-supported WITH-VALUE 0x0005 IF-DEFINED REQUIRE_DOCOBJECT # REQUIRED Create-Job
    EXPECT operations-supported WITH-VALUE 0x0034 IF-DEFINED REQUIRE_DOCOBJECT # REQUIRED Get-Document-Attributes
    EXPECT operations-supported WITH-VALUE 0x0035 IF-DEFINED REQUIRE_DOCOBJECT # REQUIRED Get-Documents
    EXPECT operations-supported WITH-VALUE 0x0002 IF-DEFINED REQUIRE_DOCOBJECT # REQUIRED Print-Job
    EXPECT operations-supported WITH-VALUE 0x0006 IF-DEFINED REQUIRE_DOCOBJECT # REQUIRED Send-Document
    EXPECT operations-supported WITH-VALUE 0x0007 DEFINE-MATCH PRINTER_HAS_SEND_URI # OPTIONAL Send-URI
    EXPECT operations-supported WITH-VALUE 0x0037 DEFINE-MATCH PRINTER_HAS_SET_DOCUMENT_ATTRIBUTES	# OPTIONAL Set-Document-Attributes

    # Section 6.5 Printer Description Attributes
    EXPECT ?document-creation-attributes-supported OF-TYPE keyword IN-GROUP printer-attributes-tag
    EXPECT document-creation-attributes-supported IF-DEFINED REQUIRE_DOCOBJECT
    EXPECT document-creation-attributes-supported WITH-VALUE document-uri IF-DEFINED REQUIRE_DOCOBJECT IF-DEFINED PRINTER_HAS_SEND_URI
    EXPECT !document-creation-attributes-supported WITH-VALUE document-uri IF-NOT-DEFINED PRINTER_HAS_SEND_URI
}

# testing document operations

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "print-job to create document object"
    OPERATION Print-Job

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR name requesting-user-name $user
    #ATTR mimeMediaType document-format $filetype

    GROUP job-attributes-tag
    ATTR integer copies 1

    ATTR name document-name "test-document-1"

    FILE $filename

    STATUS successful-ok
    STATUS successful-ok-ignored-or-substituted-attributes

    EXPECT job-id OF-TYPE integer IN-GROUP job-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT ?document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE 1 COUNT 1
    EXPECT ?document-state OF-TYPE enum IN-GROUP document-attributes-tag
    EXPECT ?document-state-reasons OF-TYPE keyword IN-GROUP document-attributes-tag
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "Get document attributes"
    OPERATION Get-Document-Attributes

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR integer document-number 1
    ATTR name requesting-user-name $user
    GROUP document-attributes-tag
    ATTR integer printer-up-time 1
    STATUS successful-ok

    #Document description
    EXPECT document-name OF-TYPE name IN-GROUP document-attributes-tag WITH-VALUE "test-document-1" COUNT 1

    #Document STATUS
    EXPECT document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE 1 COUNT 1
    EXPECT last-document OF-TYPE boolean IN-GROUP document-attributes-tag



    EXPECT document-job-id OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT document-job-uri OF-TYPE uri IN-GROUP document-attributes-tag COUNT 1


    EXPECT document-printer-uri OF-TYPE uri IN-GROUP document-attributes-tag WITH-VALUE $uri COUNT 1
    EXPECT document-state OF-TYPE enum IN-GROUP document-attributes-tag
    EXPECT document-state-reasons OF-TYPE keyword IN-GROUP document-attributes-tag

    EXPECT printer-up-time OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT time-at-completed OF-TYPE integer|no-value IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT time-at-created OF-TYPE integer|no-value IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT time-at-processing OF-TYPE integer|no-value IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_SET_DOCUMENT_ATTRIBUTES

    NAME "Set Document Attributes"
    OPERATION Set-Document-Attributes

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR integer document-number 1
    ATTR name requesting-user-name $user

    GROUP document-attributes-tag
    ATTR name document-name "test-document-2"

    STATUS successful-ok
    STATUS client-error-not-possible
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_SET_DOCUMENT_ATTRIBUTES

    NAME "To Set New Document Description Values"
    OPERATION Get-Document-Attributes

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR integer document-number 1
    ATTR name requesting-user-name $user

    STATUS successful-ok

    EXPECT document-job-id OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT document-job-uri OF-TYPE uri IN-GROUP document-attributes-tag COUNT 1
    EXPECT document-name OF-TYPE name IN-GROUP document-attributes-tag COUNT 1
    EXPECT document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE 1 COUNT 1
    EXPECT document-printer-uri OF-TYPE uri IN-GROUP document-attributes-tag WITH-VALUE $uri COUNT 1
    EXPECT document-state OF-TYPE enum IN-GROUP document-attributes-tag
    EXPECT document-state-reasons OF-TYPE keyword IN-GROUP document-attributes-tag
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "Create job"

    OPERATION create-job

    GROUP operation
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR name requesting-user-name $user

    GROUP job
    ATTR integer copies 1

    STATUS successful-ok

    EXPECT job-id OF-TYPE integer IN-GROUP job-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT job-uri OF-TYPE uri IN-GROUP job-attributes-tag COUNT 1
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "testing document-attributes in send-document operation"
    OPERATION send-document

    GROUP operation
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR name requesting-user-name $user
    ATTR boolean last-document true

    GROUP job
    ATTR integer copies 1

    FILE $filename

    STATUS successful-ok

    EXPECT ?document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT ?document-state OF-TYPE enum IN-GROUP document-attributes-tag
    EXPECT ?document-state-reasons OF-TYPE keyword IN-GROUP document-attributes-tag
	EXPECT ?document-state-message
    #ISSUE: document-number, document-state, and document-state-reasons NOT PRESENT.
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_SEND_URI

    NAME "testing document-attributes in send-uri operation"
    OPERATION Send-URI

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR naturalLanguage attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR name requesting-user-name $user
    ATTR boolean last-document true
    ATTR uri document-uri $document-uri

    GROUP job-attributes-tag
    ATTR integer copies 1
    FILE $filename
    STATUS successful-ok
    EXPECT ?document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT ?document-state OF-TYPE enum IN-GROUP document-attributes-tag
    EXPECT ?document-state-reasons OF-TYPE keyword IN-GROUP document-attributes-tag

    #ISSUE: Multiple document jobs are not supported
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "testing cancel-document operation"
    OPERATION Cancel-Document

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR integer document-number 1
    ATTR name requesting-user-name $user

    STATUS successful-ok
}


# To test Get-Documents operation
{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "Get Documents"
    OPERATION Get-Documents

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR name requesting-user-name $user

    STATUS successful-ok

    #Document STATUS
    EXPECT document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE 1 COUNT 1
    EXPECT last-document OF-TYPE boolean IN-GROUP document-attributes-tag
    EXPECT ?document-charset OF-TYPE charset IN-GROUP document-attributes-tag
    EXPECT ?document-format-detected OF-TYPE mimeMediaType IN-GROUP document-attributes-tag
    EXPECT ?document-message OF-TYPE text IN-GROUP document-attributes-tag
}

{
    SKIP-IF-DEFINED NOPRINT
    SKIP-IF-NOT-DEFINED PRINTER_HAS_DOCOBJECT

    NAME "Get document attributes"
    OPERATION Get-Document-Attributes

    GROUP operation-attributes-tag
    ATTR charset attributes-charset utf-8
    ATTR language attributes-natural-language en
    ATTR uri printer-uri $uri
    ATTR integer job-id $job-id
    ATTR integer document-number 1
    ATTR name requesting-user-name $user

    STATUS successful-ok

    EXPECT document-job-id OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE >0 COUNT 1
    EXPECT document-job-uri OF-TYPE uri IN-GROUP document-attributes-tag COUNT 1
    EXPECT document-number OF-TYPE integer IN-GROUP document-attributes-tag WITH-VALUE 1 COUNT 1
    EXPECT document-printer-uri OF-TYPE uri IN-GROUP document-attributes-tag WITH-VALUE $uri COUNT 1
    EXPECT ?document-state OF-TYPE enum IN-GROUP document-attributes-tag WITH-VALUE 5 COUNT 1 DEFINE-MATCH DOCUMENT_PROCESSING			# processing
    EXPECT document-state OF-TYPE enum IN-GROUP document-attributes-tag WITH-VALUE 7 COUNT 1 IF-NOT-DEFINED DOCUMENT_PROCESSING 		# cancelled
    EXPECT document-state-reasons OF-TYPE keyword IN-GROUP document-attributes-tag WITH-VALUE "processing-to-stop-print" IF-DEFINED DOCUMENT_PROCESSING
}
