#!/usr/bin/python3

import pyatspi

def findMenuItems(root):
    print("finding menu items...")
    collection = root.queryCollection()
    rule = collection.createMatchRule(
        pyatspi.StateSet().raw(), collection.MATCH_ANY, # states to match
        [], collection.MATCH_ANY, # object attributes to match
        [pyatspi.ROLE_MENU_ITEM], collection.MATCH_ANY, # roles to match
        "", collection.MATCH_ALL, # interfaces to match
        False)

    rv = collection.getMatches(rule, collection.SORT_ORDER_CANONICAL, 0, True)
    collection.freeMatchRule(rule)
    print("%i items found\n" % len(rv))

def findFocusableItems(root):
    print("finding focusable items...")
    states = pyatspi.StateSet()
    states.add(pyatspi.STATE_FOCUSABLE)

    collection = root.queryCollection()
    rule = collection.createMatchRule(
        states.raw(), collection.MATCH_ANY, # states to match
        [], collection.MATCH_ANY, # object attributes to match
        [pyatspi.ROLE_MENU_ITEM], collection.MATCH_ANY, # roles to match
        "", collection.MATCH_ALL, # interfaces to match
        False)

    rv = collection.getMatches(rule, collection.SORT_ORDER_CANONICAL, 0, True)
    collection.freeMatchRule(rule)
    print("%i items found\n" % len(rv))

def stringFindItemsWithActionsMatchAll(root):
    print("finding items with actions via string with match all...")
    interfaces = pyatspi.Atspi.DBUS_INTERFACE_ACTION

    collection = root.queryCollection()
    rule = collection.createMatchRule(
        pyatspi.StateSet().raw(), collection.MATCH_ANY, # states to match
        [], collection.MATCH_ANY, # object attributes to match
        [], collection.MATCH_ANY, # roles to match
        interfaces, collection.MATCH_ALL, # interfaces to match
        False)

    rv = collection.getMatches(rule, collection.SORT_ORDER_CANONICAL, 0, True)
    collection.freeMatchRule(rule)
    print("%i items found\n" % len(rv))

def listFindItemsWithActionsMatchAll(root):
    print("finding items with actions via list with match all...")

    interfaces = [pyatspi.Atspi.DBUS_INTERFACE_ACTION]

    collection = root.queryCollection()
    rule = collection.createMatchRule(
        pyatspi.StateSet().raw(), collection.MATCH_ANY, # states to match
        [], collection.MATCH_ANY, # object attributes to match
        [], collection.MATCH_ANY, # roles to match
        interfaces, collection.MATCH_ALL, # interfaces to match
        False)

    rv = collection.getMatches(rule, collection.SORT_ORDER_CANONICAL, 0, True)
    collection.freeMatchRule(rule)
    print("%i items found\n" % len(rv))

def stringFindItemsWithActionsMatchAny(root):
    print("finding items with actions via string with match any...")
    interfaces = pyatspi.Atspi.DBUS_INTERFACE_ACTION

    collection = root.queryCollection()
    rule = collection.createMatchRule(
        pyatspi.StateSet().raw(), collection.MATCH_ANY, # states to match
        [], collection.MATCH_ANY, # object attributes to match
        [], collection.MATCH_ANY, # roles to match
        interfaces, collection.MATCH_ANY, # interfaces to match
        False)

    rv = collection.getMatches(rule, collection.SORT_ORDER_CANONICAL, 0, True)
    collection.freeMatchRule(rule)
    print("%i items found\n" % len(rv))

def listFindItemsWithActionsMatchAny(root):
    print("finding items with actions via list with match any...")

    interfaces = [pyatspi.Atspi.DBUS_INTERFACE_ACTION]

    collection = root.queryCollection()
    rule = collection.createMatchRule(
        pyatspi.StateSet().raw(), collection.MATCH_ANY, # states to match
        [], collection.MATCH_ANY, # object attributes to match
        [], collection.MATCH_ANY, # roles to match
        interfaces, collection.MATCH_ANY, # interfaces to match
        False)

    rv = collection.getMatches(rule, collection.SORT_ORDER_CANONICAL, 0, True)
    collection.freeMatchRule(rule)
    print("%i items found\n" % len(rv))

def runTests(e):
    findMenuItems(e.source)
    findFocusableItems(e.source)
    stringFindItemsWithActionsMatchAll(e.source)
    listFindItemsWithActionsMatchAll(e.source)
    stringFindItemsWithActionsMatchAny(e.source)
    listFindItemsWithActionsMatchAny(e.source)

pyatspi.Registry.registerEventListener(runTests, "window:activate")
pyatspi.Registry.start()


