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