1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "TestCommon.h"
8 #include "nsStringAPI.h"
9 #include "nsIPersistentProperties2.h"
10 #include "nsIServiceManager.h"
11 #include "nsIComponentRegistrar.h"
13 #include "nsIIOService.h"
15 #include "nsIChannel.h"
16 #include "nsIComponentManager.h"
18 #include "nsComponentManagerUtils.h"
19 #include "nsServiceManagerUtils.h"
20 #include "nsISimpleEnumerator.h"
22 #define TEST_URL "resource:/res/test.properties"
23 static NS_DEFINE_CID(kPersistentPropertiesCID
, NS_IPERSISTENTPROPERTIES_CID
);
25 static NS_DEFINE_CID(kIOServiceCID
, NS_IOSERVICE_CID
);
27 /***************************************************************************/
30 main(int argc
, char* argv
[])
32 if (test_common_init(&argc
, &argv
) != 0)
37 nsCOMPtr
<nsIServiceManager
> servMan
;
38 NS_InitXPCOM2(getter_AddRefs(servMan
), nullptr, nullptr);
40 nsIInputStream
* in
= nullptr;
42 nsCOMPtr
<nsIIOService
> service(do_GetService(kIOServiceCID
, &ret
));
43 if (NS_FAILED(ret
)) return 1;
45 nsIChannel
*channel
= nullptr;
46 ret
= service
->NewChannel(NS_LITERAL_CSTRING(TEST_URL
), nullptr, nullptr, &channel
);
47 if (NS_FAILED(ret
)) return 1;
49 ret
= channel
->Open(&in
);
50 if (NS_FAILED(ret
)) return 1;
52 nsIPersistentProperties
* props
;
53 ret
= CallCreateInstance(kPersistentPropertiesCID
, &props
);
54 if (NS_FAILED(ret
) || (!props
)) {
55 printf("create nsIPersistentProperties failed\n");
58 ret
= props
->Load(in
);
60 printf("cannot load properties\n");
67 sprintf(name
, "%d", i
);
69 ret
= props
->GetStringProperty(nsDependentCString(name
), v
);
70 if (NS_FAILED(ret
) || (!v
.Length())) {
73 printf("\"%d\"=\"%s\"\n", i
, NS_ConvertUTF16toUTF8(v
).get());
77 nsCOMPtr
<nsISimpleEnumerator
> propEnum
;
78 ret
= props
->Enumerate(getter_AddRefs(propEnum
));
81 printf("cannot enumerate properties\n");
86 printf("\nKey\tValue\n");
87 printf( "---\t-----\n");
90 while (NS_SUCCEEDED(propEnum
->HasMoreElements(&hasMore
)) &&
92 nsCOMPtr
<nsISupports
> sup
;
93 ret
= propEnum
->GetNext(getter_AddRefs(sup
));
95 nsCOMPtr
<nsIPropertyElement
> propElem
= do_QueryInterface(sup
, &ret
);
97 printf("failed to get current item\n");
104 ret
= propElem
->GetKey(key
);
105 if (NS_FAILED(ret
)) {
106 printf("failed to get current element's key\n");
109 ret
= propElem
->GetValue(value
);
110 if (NS_FAILED(ret
)) {
111 printf("failed to get current element's value\n");
115 printf("%s\t%s\n", key
.get(), NS_ConvertUTF16toUTF8(value
).get());