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 223 224
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
#include <string>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#import "content/app_shim_remote_cocoa/render_widget_host_view_cocoa.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "third_party/blink/public/platform/web_text_input_type.h"
#include "ui/events/test/cocoa_test_event_utils.h"
@interface TextInputFlagChangeWaiter : NSObject
@end
@implementation TextInputFlagChangeWaiter {
RenderWidgetHostViewCocoa* _rwhv_cocoa;
std::unique_ptr<base::RunLoop> _run_loop;
}
- (instancetype)initWithRenderWidgetHostViewCocoa:
(RenderWidgetHostViewCocoa*)rwhv_cocoa {
if ((self = [super init])) {
_rwhv_cocoa = rwhv_cocoa;
[_rwhv_cocoa addObserver:self
forKeyPath:@"textInputFlags"
options:NSKeyValueObservingOptionNew
context:nullptr];
[self reset];
}
return self;
}
- (void)dealloc {
[_rwhv_cocoa removeObserver:self forKeyPath:@"textInputFlags"];
}
- (void)reset {
_run_loop = std::make_unique<base::RunLoop>();
}
- (void)wait {
_run_loop->Run();
[self reset];
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id>*)change
context:(void*)context {
_run_loop->Quit();
}
@end
namespace content {
namespace {
class TextCallbackWaiter {
public:
TextCallbackWaiter() = default;
TextCallbackWaiter(const TextCallbackWaiter&) = delete;
TextCallbackWaiter& operator=(const TextCallbackWaiter&) = delete;
void Wait() { run_loop_.Run(); }
const std::u16string& text() const { return text_; }
void GetText(const std::u16string& text) {
text_ = text;
run_loop_.Quit();
}
private:
std::u16string text_;
base::RunLoop run_loop_;
};
class TextSelectionWaiter : public TextInputManager::Observer {
public:
explicit TextSelectionWaiter(RenderWidgetHostViewMac* rwhv) {
rwhv->GetTextInputManager()->AddObserver(this);
}
void OnTextSelectionChanged(TextInputManager* text_input_manager,
RenderWidgetHostViewBase* updated_view) override {
text_input_manager->RemoveObserver(this);
run_loop_.Quit();
}
void Wait() { run_loop_.Run(); }
private:
base::RunLoop run_loop_;
};
} // namespace
class RenderWidgetHostViewMacTest : public ContentBrowserTest {};
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewMacTest, GetPageTextForSpeech) {
GURL url(
"data:text/html,<span>Hello</span>"
"<span style='display:none'>Goodbye</span>"
"<span>World</span>");
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderWidgetHostView* rwhv =
shell()->web_contents()->GetPrimaryMainFrame()->GetView();
RenderWidgetHostViewMac* rwhv_mac =
static_cast<RenderWidgetHostViewMac*>(rwhv);
TextCallbackWaiter waiter;
rwhv_mac->GetPageTextForSpeech(
base::BindOnce(&TextCallbackWaiter::GetText, base::Unretained(&waiter)));
waiter.Wait();
EXPECT_EQ(u"Hello\nWorld", waiter.text());
}
// Test that -firstRectForCharacterRange:actualRange: works when the range
// isn't in the active selection, which requires a sync IPC to the renderer.
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewMacTest,
GetFirstRectForCharacterRangeUncached) {
GURL url("data:text/html,Hello");
EXPECT_TRUE(NavigateToURL(shell(), url));
auto* web_contents_impl =
static_cast<WebContentsImpl*>(shell()->web_contents());
auto* root = web_contents_impl->GetPrimaryFrameTree().root();
web_contents_impl->GetPrimaryFrameTree().SetFocusedFrame(
root, root->current_frame_host()->GetSiteInstance()->group());
RenderWidgetHostView* rwhv =
shell()->web_contents()->GetPrimaryMainFrame()->GetView();
RenderWidgetHostViewMac* rwhv_mac =
static_cast<RenderWidgetHostViewMac*>(rwhv);
NSRect rect = [rwhv_mac->GetInProcessNSView()
firstRectForCharacterRange:NSMakeRange(2, 1)
actualRange:nullptr];
EXPECT_GT(NSMinX(rect), 0);
EXPECT_GT(NSWidth(rect), 0);
EXPECT_GT(NSHeight(rect), 0);
}
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewMacTest, UpdateInputFlags) {
class InputMethodObserver {};
GURL url("data:text/html,<!doctype html><textarea id=ta></textarea>");
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderWidgetHostView* rwhv =
shell()->web_contents()->GetPrimaryMainFrame()->GetView();
RenderWidgetHostViewMac* rwhv_mac =
static_cast<RenderWidgetHostViewMac*>(rwhv);
RenderWidgetHostViewCocoa* rwhv_cocoa = rwhv_mac->GetInProcessNSView();
TextInputFlagChangeWaiter* flag_change_waiter =
[[TextInputFlagChangeWaiter alloc]
initWithRenderWidgetHostViewCocoa:rwhv_cocoa];
EXPECT_TRUE(ExecJs(shell(), "ta.focus();"));
[flag_change_waiter wait];
EXPECT_FALSE(rwhv_cocoa.textInputFlags &
blink::kWebTextInputFlagAutocorrectOff);
EXPECT_TRUE(ExecJs(
shell(),
"ta.setAttribute('autocorrect', 'off'); console.log(ta.outerHTML);"));
[flag_change_waiter wait];
EXPECT_TRUE(rwhv_cocoa.textInputFlags &
blink::kWebTextInputFlagAutocorrectOff);
}
IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewMacTest,
InputTextPreventedSyncsCursorLocation) {
class InputMethodObserver {};
GURL url("data:text/html,<!doctype html><textarea id=ta></textarea>");
EXPECT_TRUE(NavigateToURL(shell(), url));
RenderWidgetHostView* rwhv =
shell()->web_contents()->GetPrimaryMainFrame()->GetView();
RenderWidgetHostViewMac* rwhv_mac =
static_cast<RenderWidgetHostViewMac*>(rwhv);
RenderWidgetHostViewCocoa* rwhv_cocoa = rwhv_mac->GetInProcessNSView();
EXPECT_TRUE(ExecJs(
shell(),
"ta.addEventListener('keypress', (evt) => {evt.preventDefault();}); "
"ta.focus();"));
// Before typing anything, we're at position 0.
EXPECT_EQ(0lu, [rwhv_cocoa selectedRange].location);
TextSelectionWaiter waiter(rwhv_mac);
NSEvent* key_a = cocoa_test_event_utils::KeyEventWithKeyCode(
'a', 'a', NSEventTypeKeyDown, 0);
[rwhv_cocoa keyEvent:key_a];
// After typing 'a', the browser process assumes that the text was entered and
// we are at position 1.
EXPECT_EQ(1lu, [rwhv_cocoa selectedRange].location);
// Wait for the renderer to sync the selection to the browser.
waiter.Wait();
// The synced selection updates the selected position back to 0.
EXPECT_EQ(0lu, [rwhv_cocoa selectedRange].location);
}
} // namespace content
|