|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 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 "config.h" |
| 27 | + |
| 28 | +#if ENABLE(CFPREFS_DIRECT_MODE) |
| 29 | + |
| 30 | +#import "PlatformUtilities.h" |
| 31 | +#import "TestWKWebView.h" |
| 32 | +#import "WKWebViewConfigurationExtras.h" |
| 33 | +#import <WebKit/PreferenceObserver.h> |
| 34 | +#import <WebKit/WKProcessPoolPrivate.h> |
| 35 | +#import <WebKit/_WKProcessPoolConfiguration.h> |
| 36 | + |
| 37 | +#if PLATFORM(MAC) |
| 38 | +#import <pal/spi/mac/HIServicesSPI.h> |
| 39 | +#endif |
| 40 | + |
| 41 | +#if PLATFORM(IOS_FAMILY) |
| 42 | +#include <pal/spi/cocoa/AccessibilitySupportSPI.h> |
| 43 | +#include <wtf/SoftLinking.h> |
| 44 | + |
| 45 | +#define NOTIFICATION_CENTER CFNotificationCenterGetDarwinNotifyCenter() |
| 46 | +#define INCREASE_CONTRAST_PREFERENCE CFSTR("DarkenSystemColors") |
| 47 | +#define ACCESSIBILITY_DOMAIN CFSTR("com.apple.Accessibility") |
| 48 | +#else |
| 49 | +#define NOTIFICATION_CENTER CFNotificationCenterGetDistributedCenter() |
| 50 | +#define INCREASE_CONTRAST_PREFERENCE kAXInterfaceIncreaseContrastKey |
| 51 | +#define ACCESSIBILITY_DOMAIN CFSTR("com.apple.universalaccess") |
| 52 | +#endif |
| 53 | + |
| 54 | +static bool receivedPreferenceNotification = false; |
| 55 | + |
| 56 | +@interface WKPreferenceObserverForTestingIncreaseContrast : WKPreferenceObserver |
| 57 | +- (void)preferenceDidChange:(NSString *)domain key:(NSString *)key encodedValue:(NSString *)encodedValue; |
| 58 | +@end |
| 59 | + |
| 60 | +@implementation WKPreferenceObserverForTestingIncreaseContrast |
| 61 | +- (void)preferenceDidChange:(NSString *)domain key:(NSString *)key encodedValue:(NSString *)encodedValue |
| 62 | +{ |
| 63 | + receivedPreferenceNotification = true; |
| 64 | + [super preferenceDidChange:domain key:key encodedValue:encodedValue]; |
| 65 | +} |
| 66 | +@end |
| 67 | + |
| 68 | +TEST(WebKit, AccessibilityIncreaseContrast) |
| 69 | +{ |
| 70 | + WKWebViewConfiguration *configuration = [WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"WebProcessPlugInWithInternals" configureJSCForTesting:YES]; |
| 71 | + auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration addToWindow:YES]); |
| 72 | + |
| 73 | + CFPreferencesSetAppValue(INCREASE_CONTRAST_PREFERENCE, kCFBooleanFalse, ACCESSIBILITY_DOMAIN); |
| 74 | + |
| 75 | + auto observer = adoptNS([[WKPreferenceObserverForTestingIncreaseContrast alloc] init]); |
| 76 | + |
| 77 | + [webView synchronouslyLoadTestPageNamed:@"simple"]; |
| 78 | + |
| 79 | + auto contrastCheck = [&] { |
| 80 | + return [webView stringByEvaluatingJavaScript:@"window.internals.userPrefersContrast()"].boolValue; |
| 81 | + }; |
| 82 | + |
| 83 | + ASSERT_FALSE(contrastCheck()); |
| 84 | + |
| 85 | + CFPreferencesSetAppValue(INCREASE_CONTRAST_PREFERENCE, kCFBooleanTrue, ACCESSIBILITY_DOMAIN); |
| 86 | + |
| 87 | + TestWebKitAPI::Util::run(&receivedPreferenceNotification); |
| 88 | + |
| 89 | + [webView synchronouslyLoadTestPageNamed:@"simple"]; |
| 90 | + |
| 91 | + ASSERT_TRUE(contrastCheck()); |
| 92 | + |
| 93 | + CFPreferencesSetAppValue(INCREASE_CONTRAST_PREFERENCE, nullptr, ACCESSIBILITY_DOMAIN); |
| 94 | +} |
| 95 | + |
| 96 | +#endif |
0 commit comments