xpcom/reflect/xptinfo/tests/TestInterfaceInfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/reflect/xptinfo/tests/TestInterfaceInfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* Some simple smoke tests of the typelib loader. */
    1.10 +
    1.11 +#include "nscore.h"
    1.12 +
    1.13 +#include "nsISupports.h"
    1.14 +#include "nsIInterfaceInfo.h"
    1.15 +#include "nsIInterfaceInfoManager.h"
    1.16 +#include "xptinfo.h"
    1.17 +#include "nsServiceManagerUtils.h"
    1.18 +
    1.19 +#include <stdio.h>
    1.20 +
    1.21 +// This file expects the nsInterfaceInfoManager to be able to discover
    1.22 +// .xpt files corresponding to those in xpcom/idl.  Currently this
    1.23 +// means setting XPTDIR in the environment to some directory
    1.24 +// containing these files.
    1.25 +
    1.26 +int main (int argc, char **argv) {
    1.27 +    int i;
    1.28 +    nsIID *iid1, *iid2, *iid3;
    1.29 +    char *name1, *name2, *name3;
    1.30 +    nsIInterfaceInfo *info2, *info3, *info4, *info5;
    1.31 +
    1.32 +    nsCOMPtr<nsIInterfaceInfoManager> iim
    1.33 +        (do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
    1.34 +
    1.35 +    fprintf(stderr, "\ngetting iid for 'nsISupports'\n");
    1.36 +    iim->GetIIDForName("nsISupports", &iid1);
    1.37 +    iim->GetNameForIID(iid1, &name1);
    1.38 +    fprintf(stderr, "%s iid %s\n", name1, iid1->ToString());
    1.39 +
    1.40 +    fprintf(stderr, "\ngetting iid for 'nsIInputStream'\n");
    1.41 +    iim->GetIIDForName("nsIInputStream", &iid2);
    1.42 +    iim->GetNameForIID(iid2, &name2);
    1.43 +    fprintf(stderr, "%s iid %s\n", name2, iid2->ToString());
    1.44 +
    1.45 +    fprintf(stderr, "iid: %s, name: %s\n", iid1->ToString(), name1);
    1.46 +    fprintf(stderr, "iid: %s, name: %s\n", iid2->ToString(), name2);
    1.47 +
    1.48 +    fprintf(stderr, "\ngetting info for iid2 from above\n");
    1.49 +    iim->GetInfoForIID(iid2, &info2);
    1.50 +#ifdef DEBUG
    1.51 +//    ((nsInterfaceInfo *)info2)->print(stderr);
    1.52 +#endif
    1.53 +
    1.54 +    fprintf(stderr, "\ngetting iid for 'nsIInputStream'\n");
    1.55 +    iim->GetIIDForName("nsIInputStream", &iid3);
    1.56 +    iim->GetNameForIID(iid3, &name3);
    1.57 +    fprintf(stderr, "%s iid %s\n", name3, iid2->ToString());
    1.58 +    iim->GetInfoForIID(iid3, &info3);
    1.59 +#ifdef DEBUG
    1.60 +//    ((nsInterfaceInfo *)info3)->print(stderr);
    1.61 +#endif
    1.62 +
    1.63 +    fprintf(stderr, "\ngetting info for name 'nsIBidirectionalEnumerator'\n");
    1.64 +    iim->GetInfoForName("nsIBidirectionalEnumerator", &info4);
    1.65 +#ifdef DEBUG
    1.66 +//    ((nsInterfaceInfo *)info4)->print(stderr);
    1.67 +#endif
    1.68 +
    1.69 +    fprintf(stderr, "\nparams work?\n");
    1.70 +    fprintf(stderr, "\ngetting info for name 'nsIServiceManager'\n");
    1.71 +    iim->GetInfoForName("nsIComponentManager", &info5);
    1.72 +#ifdef DEBUG
    1.73 +//    ((nsInterfaceInfo *)info5)->print(stderr);
    1.74 +#endif
    1.75 +
    1.76 +    // XXX: nsIServiceManager is no more; what do we test with?
    1.77 +    if (info5 == nullptr) {
    1.78 +        fprintf(stderr, "\nNo nsIComponentManager; cannot continue.\n");
    1.79 +        return 1;
    1.80 +    }
    1.81 +
    1.82 +    uint16_t methodcount;
    1.83 +    info5->GetMethodCount(&methodcount);
    1.84 +    const nsXPTMethodInfo *mi;
    1.85 +    for (i = 0; i < methodcount; i++) {
    1.86 +        info5->GetMethodInfo(i, &mi);
    1.87 +        fprintf(stderr, "method %d, name %s\n", i, mi->GetName());
    1.88 +    }
    1.89 +
    1.90 +    // 4 is getServiceByContractID, which has juicy params.
    1.91 +    info5->GetMethodInfo(6, &mi);
    1.92 +
    1.93 +    const nsXPTParamInfo& param2 = mi->GetParam(1);
    1.94 +    // should be IID for the service
    1.95 +    nsIID *nsISL;
    1.96 +    info5->GetIIDForParam(6, &param2, &nsISL);
    1.97 +    fprintf(stderr, "iid assoc'd with param 1 of method 6 - createInstanceByContractID - %s\n", nsISL->ToString());
    1.98 +    // if we look up the name?
    1.99 +    char *nsISLname;
   1.100 +    iim->GetNameForIID(nsISL, &nsISLname);
   1.101 +    fprintf(stderr, "which is called %s\n", nsISLname);
   1.102 +
   1.103 +    fprintf(stderr, "\nNow check the last param\n");
   1.104 +    const nsXPTParamInfo& param3 = mi->GetParam(3);
   1.105 +
   1.106 +    if (param3.GetType().TagPart() != nsXPTType::T_INTERFACE_IS) {
   1.107 +        fprintf(stderr, "Param 3 is not type interface is\n");
   1.108 +        // Not returning an error, because this could legitamately change
   1.109 +    }
   1.110 +    // lets see what arg this refers to
   1.111 +    uint8_t argnum;
   1.112 +    info5->GetInterfaceIsArgNumberForParam(6, &param3, &argnum);
   1.113 +    fprintf(stderr, "param 3 referrs to param %d of method 6 - createInstanceByContractID\n", (uint32_t)argnum);
   1.114 +    // Get the type of the parameter referred to
   1.115 +    const nsXPTParamInfo& arg_param = mi->GetParam(argnum);
   1.116 +    const nsXPTType& arg_type = arg_param.GetType();
   1.117 +    // Check to make sure it refers to the proper param
   1.118 +    if(!arg_type.IsPointer() || arg_type.TagPart() != nsXPTType::T_IID) {
   1.119 +        fprintf(stderr, "Param 3 of method 6 refers to a non IID parameter\n"); 
   1.120 +        // Not returning an error, because this could legitamately change
   1.121 +    }
   1.122 +
   1.123 +
   1.124 +    return 0;
   1.125 +}    
   1.126 +

mercurial