[OS X] Main frame scrollbars should appear on the left on RTL systems
[WebKit-https.git] / Tools / WebKitTestRunner / InjectedBundle / mac / InjectedBundleMac.mm
1 /*
2  * Copyright (C) 2011 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "InjectedBundle.h"
27
28 #import <Foundation/Foundation.h>
29
30 @interface NSURLRequest (PrivateThingsWeShouldntReallyUse)
31 +(void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString *)host;
32 @end
33
34 @interface NSSound ()
35 + (void)_setAlertType:(NSUInteger)alertType;
36 @end
37
38 namespace WTR {
39
40 static bool shouldUseRTLScrollbars(WKTypeRef initializationUserData)
41 {
42     if (!initializationUserData || WKGetTypeID(initializationUserData) != WKDictionaryGetTypeID())
43         return false;
44
45     WKTypeRef item = WKDictionaryGetItemForKey(static_cast<WKDictionaryRef>(initializationUserData), adoptWK(WKStringCreateWithUTF8CString("UseRTLScrollbars")).get());
46     if (!item || WKGetTypeID(item) != WKBooleanGetTypeID())
47         return false;
48
49     return WKBooleanGetValue(static_cast<WKBooleanRef>(item));
50 }
51
52 void InjectedBundle::platformInitialize(WKTypeRef initializationUserData)
53 {
54     static const int NoFontSmoothing = 0;
55     static const int BlueTintedAppearance = 1;
56
57     // Work around missing /etc/catalog <rdar://problem/4292995>.
58     setenv("XML_CATALOG_FILES", "", 0);
59     setenv("XTYPE_ALLOW_AUTOACTIVATION", "1", 0);
60
61     // Language was set up earlier in main(). Don't clobber it.
62     NSArray *languages = [[[NSUserDefaults standardUserDefaults] volatileDomainForName:NSArgumentDomain] valueForKey:@"AppleLanguages"];
63     if (!languages)
64         languages = @[ @"en" ];
65
66     NSDictionary *dict = @{
67         @"AppleAntiAliasingThreshold": @4,
68         // FIXME: Setting AppleFontSmoothing is likely unnecessary and ineffective. WebKit2 has its own preference for font smoothing, which is
69         // applied to each context via CGContextSetShouldSmoothFonts, presumably overriding the default.
70         @"AppleFontSmoothing": @(NoFontSmoothing),
71         @"AppleAquaColorVariant": @(BlueTintedAppearance),
72         @"AppleHighlightColor": @"0.709800 0.835300 1.000000",
73         @"AppleOtherHighlightColor": @"0.500000 0.500000 0.500000",
74         @"NSScrollAnimationEnabled": @NO,
75         @"NSOverlayScrollersEnabled": @NO,
76         @"AppleShowScrollBars": @"Always",
77         @"NSButtonAnimationsEnabled": @NO, // Ideally, we should find a way to test animations, but for now, make sure that the dumped snapshot matches actual state.
78         // FIXME (<rdar://problem/13396515>): It is too late to set AppleLanguages here, as loaded frameworks localizations cannot be changed.
79         // This breaks some accessibility tests on machines with non-English user language.
80         @"AppleLanguages": languages,
81         @"NSPreferredSpellServerLanguage": @"en_US",
82         @"NSUserDictionaryReplacementItems": @[],
83         @"NSTestCorrectionDictionary": @{
84             @"notationl": @"notational",
85             @"mesage": @"message",
86             @"wouldn": @"would",
87             @"wellcome": @"welcome",
88             @"hellolfworld": @"hello\nworld"
89         },
90 #if __MAC_OS_X_VERSION_MIN_REQUIRED > 101000
91         @"AppleSystemFontOSSubversion": @(10),
92 #endif
93         @"AppleEnableSwipeNavigateWithScrolls": @YES,
94         @"com.apple.swipescrolldirection": @1,
95     };
96
97     if (shouldUseRTLScrollbars(initializationUserData)) {
98         NSMutableDictionary *newDictionary = [dict mutableCopy];
99         [newDictionary setValue:@YES forKey:@"AppleTextDirection"];
100         [newDictionary setValue:@YES forKey:@"NSForceRightToLeftWritingDirection"];
101         dict = [newDictionary autorelease];
102     }
103
104     [[NSUserDefaults standardUserDefaults] setVolatileDomain:dict forName:NSArgumentDomain];
105
106     // Make NSFont use the new defaults.
107     [NSFont initialize];
108
109     // Underlying frameworks have already read AppleAntiAliasingThreshold default before we changed it.
110     // A distributed notification is delivered to all applications, but it should be harmless, and it's the only way to update all underlying frameworks anyway.
111     [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"AppleAquaAntiAliasingChanged" object:nil userInfo:nil deliverImmediately:YES];
112
113     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"localhost"];
114     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"127.0.0.1"];
115
116     [NSSound _setAlertType:0];
117 }
118
119 } // namespace WTR