|
1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 |
|
4 #include "TestCommon.h" |
|
5 #include "nsCOMPtr.h" |
|
6 #include "nsIServiceManager.h" |
|
7 #include "nsNetCID.h" |
|
8 #include "nsIURL.h" |
|
9 #include "prinrval.h" |
|
10 #include "nsStringAPI.h" |
|
11 #include "nsComponentManagerUtils.h" |
|
12 |
|
13 static nsIURL *test_url = 0; |
|
14 static nsCString test_param; |
|
15 |
|
16 static void run_test(const char *testname, int count, void (* testfunc)()) |
|
17 { |
|
18 PRIntervalTime start, end; |
|
19 start = PR_IntervalNow(); |
|
20 for (; count; --count) |
|
21 testfunc(); |
|
22 end = PR_IntervalNow(); |
|
23 printf("completed %s test in %u milliseconds\n", testname, |
|
24 PR_IntervalToMilliseconds(end - start)); |
|
25 } |
|
26 |
|
27 static void set_spec_test() |
|
28 { |
|
29 test_url->SetSpec(test_param); |
|
30 } |
|
31 |
|
32 static void get_spec_test() |
|
33 { |
|
34 nsAutoCString spec; |
|
35 test_url->GetSpec(spec); |
|
36 } |
|
37 |
|
38 static void resolve_test() |
|
39 { |
|
40 nsAutoCString spec; |
|
41 test_url->Resolve(NS_LITERAL_CSTRING("foo.html?q=45"), spec); |
|
42 } |
|
43 |
|
44 static void set_scheme_test() |
|
45 { |
|
46 test_url->SetScheme(NS_LITERAL_CSTRING("foo")); |
|
47 } |
|
48 |
|
49 static void get_scheme_test() |
|
50 { |
|
51 nsAutoCString scheme; |
|
52 test_url->GetScheme(scheme); |
|
53 } |
|
54 |
|
55 static void host_test() |
|
56 { |
|
57 nsAutoCString host; |
|
58 test_url->GetHost(host); |
|
59 test_url->SetHost(NS_LITERAL_CSTRING("www.yahoo.com")); |
|
60 test_url->SetHost(host); |
|
61 } |
|
62 |
|
63 static void set_path_test() |
|
64 { |
|
65 test_url->SetPath(NS_LITERAL_CSTRING("/some-path/one-the-net/about.html?with-a-query#for-you")); |
|
66 } |
|
67 |
|
68 static void get_path_test() |
|
69 { |
|
70 nsAutoCString path; |
|
71 test_url->GetPath(path); |
|
72 } |
|
73 |
|
74 static void query_test() |
|
75 { |
|
76 nsAutoCString query; |
|
77 test_url->GetQuery(query); |
|
78 test_url->SetQuery(NS_LITERAL_CSTRING("a=b&d=c&what-ever-you-want-to-be-called=45")); |
|
79 test_url->SetQuery(query); |
|
80 } |
|
81 |
|
82 static void ref_test() |
|
83 { |
|
84 nsAutoCString ref; |
|
85 test_url->GetRef(ref); |
|
86 test_url->SetRef(NS_LITERAL_CSTRING("#some-book-mark")); |
|
87 test_url->SetRef(ref); |
|
88 } |
|
89 |
|
90 int main(int argc, char **argv) |
|
91 { |
|
92 if (test_common_init(&argc, &argv) != 0) |
|
93 return -1; |
|
94 |
|
95 if (argc < 2) { |
|
96 printf("usage: TestURL url [count]\n"); |
|
97 return -1; |
|
98 } |
|
99 |
|
100 int count = 1000; |
|
101 if (argc == 3) |
|
102 count = atoi(argv[2]); |
|
103 else |
|
104 printf("using a default count of %d\n", count); |
|
105 |
|
106 nsCOMPtr<nsIURL> url( do_CreateInstance(NS_STANDARDURL_CONTRACTID) ); |
|
107 if (!url) { |
|
108 printf("failed to instantiate component: %s\n", NS_STANDARDURL_CONTRACTID); |
|
109 return -1; |
|
110 } |
|
111 |
|
112 test_url = url; |
|
113 test_param = argv[1]; |
|
114 |
|
115 run_test("SetSpec", count, set_spec_test); |
|
116 run_test("GetSpec", count, get_spec_test); |
|
117 run_test("Resolve", count, resolve_test); |
|
118 run_test("SetScheme", count, set_scheme_test); |
|
119 run_test("GetScheme", count, get_scheme_test); |
|
120 run_test("[GS]etHost", count, host_test); |
|
121 run_test("SetPath", count, set_path_test); |
|
122 run_test("GetPath", count, get_path_test); |
|
123 run_test("[GS]etQuery", count, query_test); |
|
124 run_test("[GS]etRef", count, ref_test); |
|
125 |
|
126 return 0; |
|
127 } |