Advertisement
Guest User

Untitled

a guest
Aug 7th, 2010
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5.         <script type="text/javascript">
  6.  
  7.             /* #### PLEASE SET THIS TO THE CORRECT PROXY OR COMMENT IT OUT #### */
  8.             // Titanium.Network.setHTTPProxy("http://HOST:PORT");
  9.  
  10.         </script>
  11.         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  12.         <style>
  13.         </style>
  14.     </head>
  15.     <body>
  16.         <div id="main" style="width:100%; height:100%">
  17.         Duck Duck Go Query Simulation
  18.  
  19.         <script type="text/javascript">
  20.  
  21.  
  22.             /* +----------------------+
  23.              * |  BEGIN TEST HARNESS  |
  24.              * +----------------------+
  25.              */
  26.  
  27.  
  28.             /* +--------------------------+
  29.              * |  BEGIN HELPER FUNCTIONS  |
  30.              * +--------------------------+
  31.              */
  32.             function parseArguments(args, handlers) {
  33.                 var al = parseInt(args.length);
  34.                 for (var i = 0; i < al; ++i) {
  35.                     if (args[i] in handlers) {
  36.                         var argData = null;
  37.                         if (i+1 < args.length) {
  38.                             argData = args[i+1];
  39.                         }
  40.                         var eatNext = handlers[args[i]](argData);
  41.                         i += 1;
  42.                         if (eatNext) {
  43.                             i += 1;
  44.                         }
  45.                     }
  46.                 }
  47.             }
  48.  
  49.  
  50.             function hitch(obj, fn){
  51.                 return function(){
  52.                     return fn.apply(obj, arguments);
  53.                 };
  54.             }
  55.  
  56.             /* END HELPER FUNCTIONS */
  57.  
  58.  
  59.             function TestRunner(testCases, css, removeWhenDone) {
  60.                 var iframe = document.createElement("iframe");
  61.                 for (var attr in css) {
  62.                     $(iframe).css(attr, css[attr]);
  63.                 }
  64.                 this._iframe = iframe;
  65.                 this.testIndex = -1;
  66.                 this.testCases = testCases;
  67.                 this.context = { };
  68.                 this.removeWhenDone = removeWhenDone;
  69.  
  70.                 iframe.src = "about:blank";
  71.                 iframe.onload = hitch(this, this._onIFrameLoaded);
  72.             }
  73.  
  74.            
  75.             TestRunner.prototype = {
  76.                 _onIFrameLoaded: function() {
  77.                     ++this.testIndex;
  78.                     if (this.testIndex >= this.testCases.length) {
  79.                         if (this.removeWhenDone) {
  80.                             $(this._iframe).remove();
  81.                         }
  82.                         return;
  83.                     }
  84.  
  85.                     this.testCases[this.testIndex](this._iframe, this._iframe.contentWindow, this.context);
  86.                 },
  87.  
  88.                 run: function() {
  89.                     document.body.appendChild(this._iframe);
  90.                 }
  91.             };
  92.  
  93.  
  94.             /* END TEST HARNESS */
  95.  
  96.  
  97.             /* +-------------------------+
  98.              * |  BEGIN TEST DDG SEARCH  |
  99.              * +-------------------------+
  100.              */
  101.  
  102.             /* This use case is the one where the user logs in */
  103.             var test_ddg_search = function(query, removeWhenDone, callback) {
  104.                 var cases = [ ];
  105.  
  106.                 cases.push(function(iframe, win, context, harness) {
  107.                     /* Load Duck Duck Go's home page */
  108.                     iframe.src = "http://duckduckgo.com/";
  109.                 });
  110.  
  111.                 cases.push(function(iframe, win, context) {
  112.                     /* Make the query */
  113.                     var jQueryInclude = document.createElement('script');
  114.                     jQueryInclude.setAttribute('type', 'text/javascript');
  115.                     jQueryInclude.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
  116.                     win.document.body.appendChild(jQueryInclude);
  117.                     console.log(win.$);
  118.  
  119.                     /* Wait for the jQuery script to be included in the context of
  120.                      * Duck Duck Go's home page
  121.                      */
  122.                     setTimeout(function() {
  123.                         try {
  124.                             /* Simulate typing... ;) */
  125.                             var currPos = 1;
  126.                             var corrected = false;
  127.                             var ival = setInterval(function() {
  128.                                 if (currPos > query.length) {
  129.                                     clearInterval(ival);
  130.                                     win.$("form input[type=button]")[0].click();
  131.                                 }
  132.                                 else {
  133.                                     if (currPos == 6 && !corrected) {
  134.                                         currPos -= 2;
  135.                                         corrected = true;
  136.                                     }
  137.                                     win.$("form input[type=text]").val(query.slice(0, currPos));
  138.                                     currPos += 1;
  139.                                 }
  140.                             }, 300);
  141.  
  142.                         }
  143.                         catch (ex) {
  144.                             console.error("Exception: " + ex.toString());
  145.                         }
  146.                     }, 2000);
  147.                 });
  148.  
  149.                 cases.push(function(iframe, win, context) {
  150.                     /* Fire the callback */
  151.                     callback();
  152.                 });
  153.  
  154.                 var tester = new TestRunner(cases, { width: "700px", height: "500px" }, removeWhenDone);
  155.                 tester.run();
  156.             };
  157.             /* END TEST DDG SEARCH */
  158.  
  159.  
  160.  
  161.  
  162.             /* +-------------------+
  163.              * |  Our Entry Point  |
  164.              * +-------------------+
  165.              */
  166.             function main(argc, argv) {
  167.  
  168.                 var query_string = false;
  169.  
  170.                 var argHandlers = {
  171.                     "--query": function(qs) {
  172.                         query_string = qs;
  173.                         /* Tell the arguments parser that we consumed a token
  174.                          * from the input. This enables the arguments parser
  175.                          * to skip that token while processing.
  176.                          */
  177.                         return true;
  178.                     }
  179.                 };
  180.  
  181.                 parseArguments(argv, argHandlers);
  182.  
  183.  
  184.                 test_ddg_search(query_string, true, function() { alert("done..."); });
  185.  
  186.                 return 0;
  187.             }
  188.  
  189.  
  190.             $().ready(function() {
  191.                 main(Titanium.App.getArguments().length, Titanium.App.getArguments());
  192.             });
  193.  
  194.         </script>
  195.     </body>
  196. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement