WebKitTools/DumpRenderTree/chromium/TestShellMac.mm
changeset 2 303757a437d3
parent 0 4f2f89ce4247
equal deleted inserted replaced
0:4f2f89ce4247 2:303757a437d3
     1 /*
       
     2  * Copyright (C) 2010 Google Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 #include "config.h"
       
    32 
       
    33 #include "TestShell.h"
       
    34 #include "webkit/support/webkit_support.h"
       
    35 #import <AppKit/AppKit.h>
       
    36 
       
    37 // A class to be the target/selector of the "watchdog" thread that ensures
       
    38 // pages timeout if they take too long and tells the test harness via stdout.
       
    39 @interface WatchDogTarget : NSObject {
       
    40 @private
       
    41     NSTimeInterval _timeout;
       
    42 }
       
    43 // |timeout| is in seconds
       
    44 - (id)initWithTimeout:(NSTimeInterval)timeout;
       
    45 // serves as the "run" method of a NSThread.
       
    46 - (void)run:(id)sender;
       
    47 @end
       
    48 
       
    49 @implementation WatchDogTarget
       
    50 
       
    51 - (id)initWithTimeout:(NSTimeInterval)timeout
       
    52 {
       
    53     if ((self = [super init]))
       
    54         _timeout = timeout;
       
    55     return self;
       
    56 }
       
    57 
       
    58 - (void)run:(id)ignore
       
    59 {
       
    60     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
       
    61 
       
    62     // check for debugger, just bail if so. We don't want the timeouts hitting
       
    63     // when we're trying to track down an issue.
       
    64     if (webkit_support::BeingDebugged())
       
    65         return;
       
    66 
       
    67     NSThread* currentThread = [NSThread currentThread];
       
    68 
       
    69     // Wait to be cancelled. If we are that means the test finished. If it hasn't,
       
    70     // then we need to tell the layout script we timed out and start again.
       
    71     NSDate* limitDate = [NSDate dateWithTimeIntervalSinceNow:_timeout];
       
    72     while ([(NSDate*)[NSDate date] compare:limitDate] == NSOrderedAscending &&
       
    73            ![currentThread isCancelled]) {
       
    74         // sleep for a small increment then check again
       
    75         NSDate* incrementDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
       
    76         [NSThread sleepUntilDate:incrementDate];
       
    77     }
       
    78     if (![currentThread isCancelled]) {
       
    79         // Print a warning to be caught by the layout-test script.
       
    80         // Note: the layout test driver may or may not recognize
       
    81         // this as a timeout.
       
    82         puts("#TEST_TIMED_OUT\n");
       
    83         puts("#EOF\n");
       
    84         fflush(stdout);
       
    85         exit(0);
       
    86     }
       
    87 
       
    88     [pool release];
       
    89 }
       
    90 
       
    91 @end
       
    92 
       
    93 void TestShell::waitTestFinished()
       
    94 {
       
    95     ASSERT(!m_testIsPending);
       
    96 
       
    97     m_testIsPending = true;
       
    98 
       
    99     // Create a watchdog thread which just sets a timer and
       
   100     // kills the process if it times out.  This catches really
       
   101     // bad hangs where the shell isn't coming back to the
       
   102     // message loop.  If the watchdog is what catches a
       
   103     // timeout, it can't do anything except terminate the test
       
   104     // shell, which is unfortunate.
       
   105     // Windows multiplies by 2.5, but that causes us to run for far, far too
       
   106     // long. We use the passed value and let the scripts flag override
       
   107     // the value as needed.
       
   108     NSTimeInterval timeoutSeconds = layoutTestTimeoutForWatchDog() / 1000;
       
   109     WatchDogTarget* watchdog = [[[WatchDogTarget alloc]
       
   110                                     initWithTimeout:timeoutSeconds] autorelease];
       
   111     NSThread* thread = [[NSThread alloc] initWithTarget:watchdog
       
   112                                          selector:@selector(run:)
       
   113                                          object:nil];
       
   114     [thread start];
       
   115 
       
   116     // TestFinished() will post a quit message to break this loop when the page
       
   117     // finishes loading.
       
   118     while (m_testIsPending)
       
   119         webkit_support::RunMessageLoop();
       
   120 
       
   121     // Tell the watchdog that we're finished. No point waiting to re-join, it'll
       
   122     // die on its own.
       
   123     [thread cancel];
       
   124     [thread release];
       
   125 }
       
   126 
       
   127 void platformInit()
       
   128 {
       
   129 }