Back to index

lightning-sunbird  0.9+nobinonly
TestStandardURL.cpp
Go to the documentation of this file.
00001 #include "TestCommon.h"
00002 #include <stdlib.h>
00003 #include "nsCOMPtr.h"
00004 #include "nsIServiceManager.h"
00005 #include "nsNetCID.h"
00006 #include "nsIURL.h"
00007 #include "prinrval.h"
00008 #include "nsString.h"
00009 
00010 static nsIURL     *test_url = 0;
00011 static nsCString   test_param;
00012 
00013 static void run_test(const char *testname, int count, void (* testfunc)())
00014 {
00015     PRIntervalTime start, end;
00016     start = PR_IntervalNow();
00017     for (; count; --count)
00018         testfunc();
00019     end = PR_IntervalNow();
00020     printf("completed %s test in %u milliseconds\n", testname,
00021             PR_IntervalToMilliseconds(end - start));
00022 }
00023 
00024 static void set_spec_test()
00025 {
00026     test_url->SetSpec(test_param);
00027 }
00028 
00029 static void get_spec_test()
00030 {
00031     nsCAutoString spec;
00032     test_url->GetSpec(spec);
00033 }
00034 
00035 static void resolve_test()
00036 {
00037     nsCAutoString spec;
00038     test_url->Resolve(NS_LITERAL_CSTRING("foo.html?q=45"), spec);
00039 }
00040 
00041 static void set_scheme_test()
00042 {
00043     test_url->SetScheme(NS_LITERAL_CSTRING("foo"));
00044 }
00045 
00046 static void get_scheme_test()
00047 {
00048     nsCAutoString scheme;
00049     test_url->GetScheme(scheme);
00050 }
00051 
00052 static void host_test()
00053 {
00054     nsCAutoString host;
00055     test_url->GetHost(host);
00056     test_url->SetHost(NS_LITERAL_CSTRING("www.yahoo.com"));
00057     test_url->SetHost(host);
00058 }
00059 
00060 static void set_path_test()
00061 {
00062     test_url->SetPath(NS_LITERAL_CSTRING("/some-path/one-the-net/about.html?with-a-query#for-you"));
00063 }
00064 
00065 static void get_path_test()
00066 {
00067     nsCAutoString path;
00068     test_url->GetPath(path);
00069 }
00070 
00071 static void query_test()
00072 {
00073     nsCAutoString query;
00074     test_url->GetQuery(query);
00075     test_url->SetQuery(NS_LITERAL_CSTRING("a=b&d=c&what-ever-you-want-to-be-called=45"));
00076     test_url->SetQuery(query);
00077 }
00078 
00079 static void ref_test()
00080 {
00081     nsCAutoString ref;
00082     test_url->GetRef(ref);
00083     test_url->SetRef(NS_LITERAL_CSTRING("#some-book-mark"));
00084     test_url->SetRef(ref);
00085 }
00086 
00087 int main(int argc, char **argv)
00088 {
00089     if (test_common_init(&argc, &argv) != 0)
00090         return -1;
00091 
00092     if (argc < 2) {
00093         printf("usage: TestURL url [count]\n");
00094         return -1;
00095     }
00096 
00097     int count = 1000;
00098     if (argc == 3)
00099         count = atoi(argv[2]);
00100     else
00101         printf("using a default count of %d\n", count);
00102 
00103     nsCOMPtr<nsIURL> url( do_CreateInstance(NS_STANDARDURL_CONTRACTID) );
00104     if (!url) {
00105         printf("failed to instantiate component: %s\n", NS_STANDARDURL_CONTRACTID);
00106         return -1;
00107     }
00108 
00109     test_url = url;
00110     test_param = argv[1];
00111 
00112     run_test("SetSpec", count, set_spec_test);
00113     run_test("GetSpec", count, get_spec_test);
00114     run_test("Resolve", count, resolve_test);
00115     run_test("SetScheme", count, set_scheme_test);
00116     run_test("GetScheme", count, get_scheme_test);
00117     run_test("[GS]etHost", count, host_test);
00118     run_test("SetPath", count, set_path_test);
00119     run_test("GetPath", count, get_path_test);
00120     run_test("[GS]etQuery", count, query_test);
00121     run_test("[GS]etRef", count, ref_test);
00122 
00123     return 0;
00124 }