1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "ui/base/cocoa/defaults_utils.h"
#include <AppKit/AppKit.h>
#include "base/time/time.h"
#import "ui/base/test/cocoa_helper.h"
namespace ui::cocoa {
namespace {
const int k750MS = 750;
const int k250MS = 250;
const int kTwoHoursInMS = 2 * 60 * base::Time::kMillisecondsPerSecond;
const base::TimeDelta kInfiniteBlinkTime = base::Milliseconds(0);
NSString* const kInsertionPointBlinkPeriodOn =
@"NSTextInsertionPointBlinkPeriodOn";
NSString* const kInsertionPointBlinkPeriodOff =
@"NSTextInsertionPointBlinkPeriodOff";
NSArray<NSString*>* blink_period_keys =
@[ kInsertionPointBlinkPeriodOn, kInsertionPointBlinkPeriodOff ];
class DefaultsUtilsTest : public CocoaTest {
public:
void SetUp() override {
CocoaTest::SetUp();
// Capture the (static) flag's value after the first time the function gets
// called.
if (!refresh_flag_initial_value_) {
refresh_flag_initial_value_ = BlinkPeriodRefreshFlagForTesting();
}
// Preserve the environment before clearing it out (in case we're running on
// a personal machine).
int i = 0;
for (NSString* next_key in blink_period_keys) {
orig_blink_period_values_[i++] =
[NSUserDefaults.standardUserDefaults integerForKey:next_key];
[NSUserDefaults.standardUserDefaults removeObjectForKey:next_key];
}
// Make sure the test's blink period changes get picked up.
BlinkPeriodRefreshFlagForTesting() = true;
}
bool BlinkPeriodRefreshFlagInitialValue() {
return *refresh_flag_initial_value_;
}
bool WillRefreshBlinkPeriod() { return BlinkPeriodRefreshFlagForTesting(); }
void RefreshBlinkPeriod() { BlinkPeriodRefreshFlagForTesting() = true; }
// Sets the blink period to `milliseconds`. Removes all values from defaults.
void SetBlinkPeriod(const int milliseconds) {
NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults;
[defaults removeObjectForKey:kInsertionPointBlinkPeriodOn];
[defaults removeObjectForKey:kInsertionPointBlinkPeriodOff];
[defaults setInteger:milliseconds forKey:kInsertionPointBlinkPeriodOn];
[defaults setInteger:milliseconds forKey:kInsertionPointBlinkPeriodOff];
RefreshBlinkPeriod();
EXPECT_EQ(base::Milliseconds(milliseconds),
*TextInsertionCaretBlinkPeriodFromDefaults());
[defaults removeObjectForKey:kInsertionPointBlinkPeriodOn];
[defaults removeObjectForKey:kInsertionPointBlinkPeriodOff];
}
void TearDown() override {
int i = 0;
for (NSString* next_key in blink_period_keys) {
if (orig_blink_period_values_[i]) {
[NSUserDefaults.standardUserDefaults
setInteger:orig_blink_period_values_[i]
forKey:next_key];
} else {
[NSUserDefaults.standardUserDefaults removeObjectForKey:next_key];
}
i++;
}
CocoaTest::TearDown();
}
private:
std::optional<bool> refresh_flag_initial_value_;
NSInteger orig_blink_period_values_[2];
};
// Tests that the flag which tells
// TextInsertionCaretBlinkPeriodFromDefaults() to refresh the blink period is
// set to true the first time BlinkPeriodRefreshFlagForTesting() gets called.
TEST_F(DefaultsUtilsTest, RefreshFlagTrueAfterFirstCall) {
EXPECT_TRUE(BlinkPeriodRefreshFlagInitialValue());
}
// Tests that the flag which tells
// TextInsertionCaretBlinkPeriodFromDefaults() to refresh the blink period is
// set to true whenever the app becomes active.
TEST_F(DefaultsUtilsTest, RefreshFlagResetsOnAppActivate) {
// Confirm that after retrieving the blink period we aren't set to refresh.
TextInsertionCaretBlinkPeriodFromDefaults();
EXPECT_FALSE(WillRefreshBlinkPeriod());
// Simulate the app becoming active, as if the user switched away and back.
[NSNotificationCenter.defaultCenter
postNotificationName:NSApplicationWillBecomeActiveNotification
object:nil];
EXPECT_TRUE(WillRefreshBlinkPeriod());
TextInsertionCaretBlinkPeriodFromDefaults();
EXPECT_FALSE(WillRefreshBlinkPeriod());
}
// Tests that we don't return a blink period if there's nothing set in defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodNoDefaults) {
EXPECT_FALSE(TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests returning the blink period from defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodFromDefaults) {
[NSUserDefaults.standardUserDefaults setInteger:k750MS
forKey:kInsertionPointBlinkPeriodOn];
[NSUserDefaults.standardUserDefaults
setInteger:k750MS
forKey:kInsertionPointBlinkPeriodOff];
EXPECT_EQ(base::Milliseconds(k750MS),
*TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests returning the blink period when a double is stored in defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodFromDefaultsDouble) {
const double k750WithFractionalMS = 750.6;
[NSUserDefaults.standardUserDefaults setDouble:k750WithFractionalMS
forKey:kInsertionPointBlinkPeriodOn];
[NSUserDefaults.standardUserDefaults setDouble:k750WithFractionalMS
forKey:kInsertionPointBlinkPeriodOff];
EXPECT_EQ(base::Milliseconds(k750MS),
*TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests returning the blink period derived from just the on time setting in
// defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodFromOnTime) {
[NSUserDefaults.standardUserDefaults setInteger:k750MS
forKey:kInsertionPointBlinkPeriodOn];
EXPECT_EQ(base::Milliseconds((k750MS + 0) / 2),
*TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests returning the blink period derived from just the off time setting in
// defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodFromOffTime) {
[NSUserDefaults.standardUserDefaults
setInteger:k250MS
forKey:kInsertionPointBlinkPeriodOff];
EXPECT_EQ(base::Milliseconds((0 + k250MS) / 2),
*TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests returning the blink period derived from the on and off times in
// defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodFromOnOffTime) {
[NSUserDefaults.standardUserDefaults setInteger:k750MS
forKey:kInsertionPointBlinkPeriodOn];
[NSUserDefaults.standardUserDefaults
setInteger:k250MS
forKey:kInsertionPointBlinkPeriodOff];
EXPECT_EQ(base::Milliseconds((k750MS + k250MS) / 2),
*TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests returning "infinite" blink period for a long on time in defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodFromLongOnTime) {
[NSUserDefaults.standardUserDefaults setInteger:kTwoHoursInMS
forKey:kInsertionPointBlinkPeriodOn];
EXPECT_EQ(kInfiniteBlinkTime, *TextInsertionCaretBlinkPeriodFromDefaults());
}
// Tests handling of bad blink period times from defaults.
TEST_F(DefaultsUtilsTest, InsertionPointBlinkPeriodNegativeTimes) {
const int kNegativeMS = -500;
NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults;
// By setting the blink period we cause
// TextInsertionCaretBlinkPeriodFromDefaults() to evaluate to true so
// we're sure that setting a negative value causes it to evaluate to false.
SetBlinkPeriod(k250MS);
[defaults setInteger:kNegativeMS forKey:kInsertionPointBlinkPeriodOn];
RefreshBlinkPeriod();
EXPECT_FALSE(TextInsertionCaretBlinkPeriodFromDefaults());
SetBlinkPeriod(k250MS);
[defaults setInteger:k750MS forKey:kInsertionPointBlinkPeriodOn];
[defaults setInteger:kNegativeMS forKey:kInsertionPointBlinkPeriodOff];
RefreshBlinkPeriod();
EXPECT_FALSE(TextInsertionCaretBlinkPeriodFromDefaults());
}
} // namespace
} // namespace ui::cocoa
|