forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_view_browsertest.cc
1851 lines (1675 loc) · 72.4 KB
/
render_view_browsertest.cc
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "base/shared_memory.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "content/common/view_messages.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/url_constants.h"
#include "content/public/renderer/document_state.h"
#include "content/public/renderer/navigation_state.h"
#include "content/public/test/render_view_test.h"
#include "content/renderer/render_view_impl.h"
#include "content/shell/shell_content_browser_client.h"
#include "content/shell/shell_content_client.h"
#include "content/test/mock_keyboard.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebData.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebHTTPBody.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebHistoryItem.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebWindowFeatures.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/base/range/range.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "webkit/glue/glue_serialize.h"
#include "webkit/glue/web_io_operators.h"
#if defined(OS_LINUX) && !defined(USE_AURA)
#include "ui/base/gtk/event_synthesis_gtk.h"
#endif
#if defined(USE_AURA)
#include "ui/base/events/event.h"
#endif
#if defined(USE_AURA) && defined(USE_X11)
#include <X11/Xlib.h>
#include "ui/base/events/event_constants.h"
#include "ui/base/keycodes/keyboard_code_conversion.h"
#include "ui/base/x/x11_util.h"
#endif
using WebKit::WebFrame;
using WebKit::WebInputEvent;
using WebKit::WebMouseEvent;
using WebKit::WebString;
using WebKit::WebTextDirection;
using WebKit::WebURLError;
namespace content {
namespace {
#if defined(USE_AURA) && defined(USE_X11)
// Converts MockKeyboard::Modifiers to ui::EventFlags.
int ConvertMockKeyboardModifier(MockKeyboard::Modifiers modifiers) {
static struct ModifierMap {
MockKeyboard::Modifiers src;
int dst;
} kModifierMap[] = {
{ MockKeyboard::LEFT_SHIFT, ui::EF_SHIFT_DOWN },
{ MockKeyboard::RIGHT_SHIFT, ui::EF_SHIFT_DOWN },
{ MockKeyboard::LEFT_CONTROL, ui::EF_CONTROL_DOWN },
{ MockKeyboard::RIGHT_CONTROL, ui::EF_CONTROL_DOWN },
{ MockKeyboard::LEFT_ALT, ui::EF_ALT_DOWN },
{ MockKeyboard::RIGHT_ALT, ui::EF_ALT_DOWN },
};
int flags = 0;
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kModifierMap); ++i) {
if (kModifierMap[i].src & modifiers) {
flags |= kModifierMap[i].dst;
}
}
return flags;
}
#endif
class WebUITestWebUIControllerFactory : public WebUIControllerFactory {
public:
virtual WebUIController* CreateWebUIControllerForURL(
WebUI* web_ui, const GURL& url) const OVERRIDE {
return NULL;
}
virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
const GURL& url) const OVERRIDE {
return WebUI::kNoWebUI;
}
virtual bool UseWebUIForURL(BrowserContext* browser_context,
const GURL& url) const OVERRIDE {
return HasWebUIScheme(url);
}
virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context,
const GURL& url) const OVERRIDE {
return HasWebUIScheme(url);
}
};
} // namespace
class RenderViewImplTest : public RenderViewTest {
public:
RenderViewImplTest() {
// Attach a pseudo keyboard device to this object.
mock_keyboard_.reset(new MockKeyboard());
}
RenderViewImpl* view() {
return static_cast<RenderViewImpl*>(view_);
}
// Sends IPC messages that emulates a key-press event.
int SendKeyEvent(MockKeyboard::Layout layout,
int key_code,
MockKeyboard::Modifiers modifiers,
string16* output) {
#if defined(OS_WIN)
// Retrieve the Unicode character for the given tuple (keyboard-layout,
// key-code, and modifiers).
// Exit when a keyboard-layout driver cannot assign a Unicode character to
// the tuple to prevent sending an invalid key code to the RenderView
// object.
CHECK(mock_keyboard_.get());
CHECK(output);
int length = mock_keyboard_->GetCharacters(layout, key_code, modifiers,
output);
if (length != 1)
return -1;
// Create IPC messages from Windows messages and send them to our
// back-end.
// A keyboard event of Windows consists of three Windows messages:
// WM_KEYDOWN, WM_CHAR, and WM_KEYUP.
// WM_KEYDOWN and WM_KEYUP sends virtual-key codes. On the other hand,
// WM_CHAR sends a composed Unicode character.
MSG msg1 = { NULL, WM_KEYDOWN, key_code, 0 };
#if defined(USE_AURA)
ui::KeyEvent evt1(msg1, false);
NativeWebKeyboardEvent keydown_event(&evt1);
#else
NativeWebKeyboardEvent keydown_event(msg1);
#endif
SendNativeKeyEvent(keydown_event);
MSG msg2 = { NULL, WM_CHAR, (*output)[0], 0 };
#if defined(USE_AURA)
ui::KeyEvent evt2(msg2, true);
NativeWebKeyboardEvent char_event(&evt2);
#else
NativeWebKeyboardEvent char_event(msg2);
#endif
SendNativeKeyEvent(char_event);
MSG msg3 = { NULL, WM_KEYUP, key_code, 0 };
#if defined(USE_AURA)
ui::KeyEvent evt3(msg3, false);
NativeWebKeyboardEvent keyup_event(&evt3);
#else
NativeWebKeyboardEvent keyup_event(msg3);
#endif
SendNativeKeyEvent(keyup_event);
return length;
#elif defined(USE_AURA) && defined(USE_X11)
// We ignore |layout|, which means we are only testing the layout of the
// current locale. TODO(mazda): fix this to respect |layout|.
CHECK(output);
const int flags = ConvertMockKeyboardModifier(modifiers);
XEvent xevent1;
InitXKeyEventForTesting(ui::ET_KEY_PRESSED,
static_cast<ui::KeyboardCode>(key_code),
flags,
&xevent1);
ui::KeyEvent event1(&xevent1, false);
NativeWebKeyboardEvent keydown_event(&event1);
SendNativeKeyEvent(keydown_event);
XEvent xevent2;
InitXKeyEventForTesting(ui::ET_KEY_PRESSED,
static_cast<ui::KeyboardCode>(key_code),
flags,
&xevent2);
ui::KeyEvent event2(&xevent2, true);
NativeWebKeyboardEvent char_event(&event2);
SendNativeKeyEvent(char_event);
XEvent xevent3;
InitXKeyEventForTesting(ui::ET_KEY_RELEASED,
static_cast<ui::KeyboardCode>(key_code),
flags,
&xevent3);
ui::KeyEvent event3(&xevent3, false);
NativeWebKeyboardEvent keyup_event(&event3);
SendNativeKeyEvent(keyup_event);
long c = GetCharacterFromKeyCode(static_cast<ui::KeyboardCode>(key_code),
flags);
output->assign(1, static_cast<char16>(c));
return 1;
#elif defined(OS_LINUX)
// We ignore |layout|, which means we are only testing the layout of the
// current locale. TODO(estade): fix this to respect |layout|.
std::vector<GdkEvent*> events;
ui::SynthesizeKeyPressEvents(
NULL, static_cast<ui::KeyboardCode>(key_code),
modifiers & (MockKeyboard::LEFT_CONTROL | MockKeyboard::RIGHT_CONTROL),
modifiers & (MockKeyboard::LEFT_SHIFT | MockKeyboard::RIGHT_SHIFT),
modifiers & (MockKeyboard::LEFT_ALT | MockKeyboard::RIGHT_ALT),
&events);
guint32 unicode_key = 0;
for (size_t i = 0; i < events.size(); ++i) {
// Only send the up/down events for key press itself (skip the up/down
// events for the modifier keys).
if ((i + 1) == (events.size() / 2) || i == (events.size() / 2)) {
unicode_key = gdk_keyval_to_unicode(events[i]->key.keyval);
NativeWebKeyboardEvent webkit_event(events[i]);
SendNativeKeyEvent(webkit_event);
// Need to add a char event after the key down.
if (webkit_event.type == WebKit::WebInputEvent::RawKeyDown) {
NativeWebKeyboardEvent char_event = webkit_event;
char_event.type = WebKit::WebInputEvent::Char;
char_event.skip_in_browser = true;
SendNativeKeyEvent(char_event);
}
}
gdk_event_free(events[i]);
}
output->assign(1, static_cast<char16>(unicode_key));
return 1;
#else
NOTIMPLEMENTED();
return L'\0';
#endif
}
private:
scoped_ptr<MockKeyboard> mock_keyboard_;
};
// Test that we get form state change notifications when input fields change.
TEST_F(RenderViewImplTest, DISABLED_OnNavStateChanged) {
// Don't want any delay for form state sync changes. This will still post a
// message so updates will get coalesced, but as soon as we spin the message
// loop, it will generate an update.
view()->set_send_content_state_immediately(true);
LoadHTML("<input type=\"text\" id=\"elt_text\"></input>");
// We should NOT have gotten a form state change notification yet.
EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching(
ViewHostMsg_UpdateState::ID));
render_thread_->sink().ClearMessages();
// Change the value of the input. We should have gotten an update state
// notification. We need to spin the message loop to catch this update.
ExecuteJavaScript("document.getElementById('elt_text').value = 'foo';");
ProcessPendingMessages();
EXPECT_TRUE(render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID));
}
TEST_F(RenderViewImplTest, OnNavigationHttpPost) {
ViewMsg_Navigate_Params nav_params;
// An http url will trigger a resource load so cannot be used here.
nav_params.url = GURL("data:text/html,<div>Page</div>");
nav_params.navigation_type = ViewMsg_Navigate_Type::NORMAL;
nav_params.transition = PAGE_TRANSITION_TYPED;
nav_params.page_id = -1;
nav_params.is_post = true;
// Set up post data.
const unsigned char* raw_data = reinterpret_cast<const unsigned char*>(
"post \0\ndata");
const unsigned int length = 11;
const std::vector<unsigned char> post_data(raw_data, raw_data + length);
nav_params.browser_initiated_post_data = post_data;
view()->OnNavigate(nav_params);
ProcessPendingMessages();
const IPC::Message* frame_navigate_msg =
render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_FrameNavigate::ID);
EXPECT_TRUE(frame_navigate_msg);
ViewHostMsg_FrameNavigate::Param host_nav_params;
ViewHostMsg_FrameNavigate::Read(frame_navigate_msg, &host_nav_params);
EXPECT_TRUE(host_nav_params.a.is_post);
// Check post data sent to browser matches
EXPECT_FALSE(host_nav_params.a.content_state.empty());
const WebKit::WebHistoryItem item = webkit_glue::HistoryItemFromString(
host_nav_params.a.content_state);
WebKit::WebHTTPBody body = item.httpBody();
WebKit::WebHTTPBody::Element element;
bool successful = body.elementAt(0, element);
EXPECT_TRUE(successful);
EXPECT_EQ(WebKit::WebHTTPBody::Element::TypeData, element.type);
EXPECT_EQ(length, element.data.size());
EXPECT_EQ(0, memcmp(raw_data, element.data.data(), length));
}
TEST_F(RenderViewImplTest, DecideNavigationPolicy) {
WebUITestWebUIControllerFactory factory;
WebUIControllerFactory::RegisterFactory(&factory);
DocumentState state;
state.set_navigation_state(NavigationState::CreateContentInitiated());
// Navigations to normal HTTP URLs can be handled locally.
WebKit::WebURLRequest request(GURL("http://foo.com"));
WebKit::WebNavigationPolicy policy = view()->decidePolicyForNavigation(
GetMainFrame(),
&state,
request,
WebKit::WebNavigationTypeLinkClicked,
WebKit::WebNavigationPolicyCurrentTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyCurrentTab, policy);
// Verify that form posts to WebUI URLs will be sent to the browser process.
WebKit::WebURLRequest form_request(GURL("chrome://foo"));
form_request.setHTTPMethod("POST");
policy = view()->decidePolicyForNavigation(
GetMainFrame(),
&state,
form_request,
WebKit::WebNavigationTypeFormSubmitted,
WebKit::WebNavigationPolicyCurrentTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyIgnore, policy);
// Verify that popup links to WebUI URLs also are sent to browser.
WebKit::WebURLRequest popup_request(GURL("chrome://foo"));
policy = view()->decidePolicyForNavigation(
GetMainFrame(),
&state,
popup_request,
WebKit::WebNavigationTypeLinkClicked,
WebKit::WebNavigationPolicyNewForegroundTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyIgnore, policy);
}
TEST_F(RenderViewImplTest, DecideNavigationPolicyForWebUI) {
// Enable bindings to simulate a WebUI view.
view()->OnAllowBindings(BINDINGS_POLICY_WEB_UI);
DocumentState state;
state.set_navigation_state(NavigationState::CreateContentInitiated());
// Navigations to normal HTTP URLs will be sent to browser process.
WebKit::WebURLRequest request(GURL("http://foo.com"));
WebKit::WebNavigationPolicy policy = view()->decidePolicyForNavigation(
GetMainFrame(),
&state,
request,
WebKit::WebNavigationTypeLinkClicked,
WebKit::WebNavigationPolicyCurrentTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyIgnore, policy);
// Navigations to WebUI URLs will also be sent to browser process.
WebKit::WebURLRequest webui_request(GURL("chrome://foo"));
policy = view()->decidePolicyForNavigation(
GetMainFrame(),
&state,
webui_request,
WebKit::WebNavigationTypeLinkClicked,
WebKit::WebNavigationPolicyCurrentTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyIgnore, policy);
// Verify that form posts to data URLs will be sent to the browser process.
WebKit::WebURLRequest data_request(GURL("data:text/html,foo"));
data_request.setHTTPMethod("POST");
policy = view()->decidePolicyForNavigation(
GetMainFrame(),
&state,
data_request,
WebKit::WebNavigationTypeFormSubmitted,
WebKit::WebNavigationPolicyCurrentTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyIgnore, policy);
// Verify that a popup that creates a view first and then navigates to a
// normal HTTP URL will be sent to the browser process, even though the
// new view does not have any enabled_bindings_.
WebKit::WebURLRequest popup_request(GURL("http://foo.com"));
WebKit::WebView* new_web_view = view()->createView(
GetMainFrame(), popup_request, WebKit::WebWindowFeatures(), "foo",
WebKit::WebNavigationPolicyNewForegroundTab);
RenderViewImpl* new_view = RenderViewImpl::FromWebView(new_web_view);
policy = new_view->decidePolicyForNavigation(
new_web_view->mainFrame(),
&state,
popup_request,
WebKit::WebNavigationTypeLinkClicked,
WebKit::WebNavigationPolicyNewForegroundTab,
false);
EXPECT_EQ(WebKit::WebNavigationPolicyIgnore, policy);
// Clean up after the new view so we don't leak it.
new_view->Close();
new_view->Release();
}
// Ensure the RenderViewImpl sends an ACK to a SwapOut request, even if it is
// already swapped out. http://crbug.com/93427.
TEST_F(RenderViewImplTest, SendSwapOutACK) {
LoadHTML("<div>Page A</div>");
int initial_page_id = view()->GetPageId();
// Respond to a swap out request.
ViewMsg_SwapOut_Params params;
params.closing_process_id = 10;
params.closing_route_id = 11;
params.new_render_process_host_id = 12;
params.new_request_id = 13;
view()->OnSwapOut(params);
// Ensure the swap out commits synchronously.
EXPECT_NE(initial_page_id, view()->GetPageId());
// Check for a valid OnSwapOutACK with echoed params.
const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_SwapOut_ACK::ID);
ASSERT_TRUE(msg);
ViewHostMsg_SwapOut_ACK::Param reply_params;
ViewHostMsg_SwapOut_ACK::Read(msg, &reply_params);
EXPECT_EQ(params.closing_process_id, reply_params.a.closing_process_id);
EXPECT_EQ(params.closing_route_id, reply_params.a.closing_route_id);
EXPECT_EQ(params.new_render_process_host_id,
reply_params.a.new_render_process_host_id);
EXPECT_EQ(params.new_request_id, reply_params.a.new_request_id);
// It is possible to get another swap out request. Ensure that we send
// an ACK, even if we don't have to do anything else.
render_thread_->sink().ClearMessages();
view()->OnSwapOut(params);
const IPC::Message* msg2 = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_SwapOut_ACK::ID);
ASSERT_TRUE(msg2);
// If we navigate back to this RenderView, ensure we don't send a state
// update for the swapped out URL. (http://crbug.com/72235)
ViewMsg_Navigate_Params nav_params;
nav_params.url = GURL("data:text/html,<div>Page B</div>");
nav_params.navigation_type = ViewMsg_Navigate_Type::NORMAL;
nav_params.transition = PAGE_TRANSITION_TYPED;
nav_params.current_history_list_length = 1;
nav_params.current_history_list_offset = 0;
nav_params.pending_history_list_offset = 1;
nav_params.page_id = -1;
view()->OnNavigate(nav_params);
ProcessPendingMessages();
const IPC::Message* msg3 = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
EXPECT_FALSE(msg3);
}
// Ensure the RenderViewImpl reloads the previous page if a reload request
// arrives while it is showing swappedout://. http://crbug.com/143155.
TEST_F(RenderViewImplTest, ReloadWhileSwappedOut) {
// Load page A.
LoadHTML("<div>Page A</div>");
// Load page B, which will trigger an UpdateState message for page A.
LoadHTML("<div>Page B</div>");
// Check for a valid UpdateState message for page A.
ProcessPendingMessages();
const IPC::Message* msg_A = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_A);
int page_id_A;
std::string state_A;
ViewHostMsg_UpdateState::Read(msg_A, &page_id_A, &state_A);
EXPECT_EQ(1, page_id_A);
render_thread_->sink().ClearMessages();
// Back to page A (page_id 1) and commit.
ViewMsg_Navigate_Params params_A;
params_A.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params_A.transition = PAGE_TRANSITION_FORWARD_BACK;
params_A.current_history_list_length = 2;
params_A.current_history_list_offset = 1;
params_A.pending_history_list_offset = 0;
params_A.page_id = 1;
params_A.state = state_A;
view()->OnNavigate(params_A);
ProcessPendingMessages();
// Respond to a swap out request.
ViewMsg_SwapOut_Params params;
params.closing_process_id = 10;
params.closing_route_id = 11;
params.new_render_process_host_id = 12;
params.new_request_id = 13;
view()->OnSwapOut(params);
// Check for a OnSwapOutACK.
const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_SwapOut_ACK::ID);
ASSERT_TRUE(msg);
render_thread_->sink().ClearMessages();
// It is possible to get a reload request at this point, containing the
// params.state of the initial page (e.g., if the new page fails the
// provisional load in the renderer process, after we unload the old page).
// Ensure the old page gets reloaded, not swappedout://.
ViewMsg_Navigate_Params nav_params;
nav_params.url = GURL("data:text/html,<div>Page A</div>");
nav_params.navigation_type = ViewMsg_Navigate_Type::RELOAD;
nav_params.transition = PAGE_TRANSITION_RELOAD;
nav_params.current_history_list_length = 2;
nav_params.current_history_list_offset = 0;
nav_params.pending_history_list_offset = 0;
nav_params.page_id = 1;
nav_params.state = state_A;
view()->OnNavigate(nav_params);
ProcessPendingMessages();
// Verify page A committed, not swappedout://.
const IPC::Message* frame_navigate_msg =
render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_FrameNavigate::ID);
EXPECT_TRUE(frame_navigate_msg);
// Read URL out of the parent trait of the params object.
ViewHostMsg_FrameNavigate::Param commit_params;
ViewHostMsg_FrameNavigate::Read(frame_navigate_msg, &commit_params);
EXPECT_NE(GURL("swappedout://"), commit_params.a.url);
}
// Test that we get the correct UpdateState message when we go back twice
// quickly without committing. Regression test for http://crbug.com/58082.
// Disabled: http://crbug.com/157357 .
TEST_F(RenderViewImplTest, DISABLED_LastCommittedUpdateState) {
// Load page A.
LoadHTML("<div>Page A</div>");
// Load page B, which will trigger an UpdateState message for page A.
LoadHTML("<div>Page B</div>");
// Check for a valid UpdateState message for page A.
ProcessPendingMessages();
const IPC::Message* msg_A = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_A);
int page_id_A;
std::string state_A;
ViewHostMsg_UpdateState::Read(msg_A, &page_id_A, &state_A);
EXPECT_EQ(1, page_id_A);
render_thread_->sink().ClearMessages();
// Load page C, which will trigger an UpdateState message for page B.
LoadHTML("<div>Page C</div>");
// Check for a valid UpdateState for page B.
ProcessPendingMessages();
const IPC::Message* msg_B = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_B);
int page_id_B;
std::string state_B;
ViewHostMsg_UpdateState::Read(msg_B, &page_id_B, &state_B);
EXPECT_EQ(2, page_id_B);
EXPECT_NE(state_A, state_B);
render_thread_->sink().ClearMessages();
// Load page D, which will trigger an UpdateState message for page C.
LoadHTML("<div>Page D</div>");
// Check for a valid UpdateState for page C.
ProcessPendingMessages();
const IPC::Message* msg_C = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_C);
int page_id_C;
std::string state_C;
ViewHostMsg_UpdateState::Read(msg_C, &page_id_C, &state_C);
EXPECT_EQ(3, page_id_C);
EXPECT_NE(state_B, state_C);
render_thread_->sink().ClearMessages();
// Go back to C and commit, preparing for our real test.
ViewMsg_Navigate_Params params_C;
params_C.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params_C.transition = PAGE_TRANSITION_FORWARD_BACK;
params_C.current_history_list_length = 4;
params_C.current_history_list_offset = 3;
params_C.pending_history_list_offset = 2;
params_C.page_id = 3;
params_C.state = state_C;
view()->OnNavigate(params_C);
ProcessPendingMessages();
render_thread_->sink().ClearMessages();
// Go back twice quickly, such that page B does not have a chance to commit.
// This leads to two changes to the back/forward list but only one change to
// the RenderView's page ID.
// Back to page B (page_id 2), without committing.
ViewMsg_Navigate_Params params_B;
params_B.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params_B.transition = PAGE_TRANSITION_FORWARD_BACK;
params_B.current_history_list_length = 4;
params_B.current_history_list_offset = 2;
params_B.pending_history_list_offset = 1;
params_B.page_id = 2;
params_B.state = state_B;
view()->OnNavigate(params_B);
// Back to page A (page_id 1) and commit.
ViewMsg_Navigate_Params params;
params.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params.transition = PAGE_TRANSITION_FORWARD_BACK;
params_B.current_history_list_length = 4;
params_B.current_history_list_offset = 2;
params_B.pending_history_list_offset = 0;
params.page_id = 1;
params.state = state_A;
view()->OnNavigate(params);
ProcessPendingMessages();
// Now ensure that the UpdateState message we receive is consistent
// and represents page C in both page_id and state.
const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg);
int page_id;
std::string state;
ViewHostMsg_UpdateState::Read(msg, &page_id, &state);
EXPECT_EQ(page_id_C, page_id);
EXPECT_NE(state_A, state);
EXPECT_NE(state_B, state);
EXPECT_EQ(state_C, state);
}
// Test that the history_page_ids_ list can reveal when a stale back/forward
// navigation arrives from the browser and can be ignored. See
// http://crbug.com/86758.
TEST_F(RenderViewImplTest, StaleNavigationsIgnored) {
// Load page A.
LoadHTML("<div>Page A</div>");
EXPECT_EQ(1, view()->history_list_length_);
EXPECT_EQ(0, view()->history_list_offset_);
EXPECT_EQ(1, view()->history_page_ids_[0]);
// Load page B, which will trigger an UpdateState message for page A.
LoadHTML("<div>Page B</div>");
EXPECT_EQ(2, view()->history_list_length_);
EXPECT_EQ(1, view()->history_list_offset_);
EXPECT_EQ(2, view()->history_page_ids_[1]);
// Check for a valid UpdateState message for page A.
ProcessPendingMessages();
const IPC::Message* msg_A = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_A);
int page_id_A;
std::string state_A;
ViewHostMsg_UpdateState::Read(msg_A, &page_id_A, &state_A);
EXPECT_EQ(1, page_id_A);
render_thread_->sink().ClearMessages();
// Back to page A (page_id 1) and commit.
ViewMsg_Navigate_Params params_A;
params_A.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params_A.transition = PAGE_TRANSITION_FORWARD_BACK;
params_A.current_history_list_length = 2;
params_A.current_history_list_offset = 1;
params_A.pending_history_list_offset = 0;
params_A.page_id = 1;
params_A.state = state_A;
view()->OnNavigate(params_A);
ProcessPendingMessages();
// A new navigation commits, clearing the forward history.
LoadHTML("<div>Page C</div>");
EXPECT_EQ(2, view()->history_list_length_);
EXPECT_EQ(1, view()->history_list_offset_);
EXPECT_EQ(3, view()->history_page_ids_[1]);
// The browser then sends a stale navigation to B, which should be ignored.
ViewMsg_Navigate_Params params_B;
params_B.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params_B.transition = PAGE_TRANSITION_FORWARD_BACK;
params_B.current_history_list_length = 2;
params_B.current_history_list_offset = 0;
params_B.pending_history_list_offset = 1;
params_B.page_id = 2;
params_B.state = state_A; // Doesn't matter, just has to be present.
view()->OnNavigate(params_B);
// State should be unchanged.
EXPECT_EQ(2, view()->history_list_length_);
EXPECT_EQ(1, view()->history_list_offset_);
EXPECT_EQ(3, view()->history_page_ids_[1]);
}
// Test that we do not ignore navigations after the entry limit is reached,
// in which case the browser starts dropping entries from the front. In this
// case, we'll see a page_id mismatch but the RenderView's id will be older,
// not newer, than params.page_id. Use this as a cue that we should update the
// state and not treat it like a navigation to a cropped forward history item.
// See http://crbug.com/89798.
TEST_F(RenderViewImplTest, DontIgnoreBackAfterNavEntryLimit) {
// Load page A.
LoadHTML("<div>Page A</div>");
EXPECT_EQ(1, view()->history_list_length_);
EXPECT_EQ(0, view()->history_list_offset_);
EXPECT_EQ(1, view()->history_page_ids_[0]);
// Load page B, which will trigger an UpdateState message for page A.
LoadHTML("<div>Page B</div>");
EXPECT_EQ(2, view()->history_list_length_);
EXPECT_EQ(1, view()->history_list_offset_);
EXPECT_EQ(2, view()->history_page_ids_[1]);
// Check for a valid UpdateState message for page A.
ProcessPendingMessages();
const IPC::Message* msg_A = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_A);
int page_id_A;
std::string state_A;
ViewHostMsg_UpdateState::Read(msg_A, &page_id_A, &state_A);
EXPECT_EQ(1, page_id_A);
render_thread_->sink().ClearMessages();
// Load page C, which will trigger an UpdateState message for page B.
LoadHTML("<div>Page C</div>");
EXPECT_EQ(3, view()->history_list_length_);
EXPECT_EQ(2, view()->history_list_offset_);
EXPECT_EQ(3, view()->history_page_ids_[2]);
// Check for a valid UpdateState message for page B.
ProcessPendingMessages();
const IPC::Message* msg_B = render_thread_->sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID);
ASSERT_TRUE(msg_B);
int page_id_B;
std::string state_B;
ViewHostMsg_UpdateState::Read(msg_B, &page_id_B, &state_B);
EXPECT_EQ(2, page_id_B);
render_thread_->sink().ClearMessages();
// Suppose the browser has limited the number of NavigationEntries to 2.
// It has now dropped the first entry, but the renderer isn't notified.
// Ensure that going back to page B (page_id 2) at offset 0 is successful.
ViewMsg_Navigate_Params params_B;
params_B.navigation_type = ViewMsg_Navigate_Type::NORMAL;
params_B.transition = PAGE_TRANSITION_FORWARD_BACK;
params_B.current_history_list_length = 2;
params_B.current_history_list_offset = 1;
params_B.pending_history_list_offset = 0;
params_B.page_id = 2;
params_B.state = state_B;
view()->OnNavigate(params_B);
ProcessPendingMessages();
EXPECT_EQ(2, view()->history_list_length_);
EXPECT_EQ(0, view()->history_list_offset_);
EXPECT_EQ(2, view()->history_page_ids_[0]);
}
// Test that our IME backend sends a notification message when the input focus
// changes.
TEST_F(RenderViewImplTest, OnImeStateChanged) {
// Enable our IME backend code.
view()->OnSetInputMethodActive(true);
// Load an HTML page consisting of two input fields.
view()->set_send_content_state_immediately(true);
LoadHTML("<html>"
"<head>"
"</head>"
"<body>"
"<input id=\"test1\" type=\"text\" value=\"some text\"></input>"
"<input id=\"test2\" type=\"password\"></input>"
"</body>"
"</html>");
render_thread_->sink().ClearMessages();
const int kRepeatCount = 10;
for (int i = 0; i < kRepeatCount; i++) {
// Move the input focus to the first <input> element, where we should
// activate IMEs.
ExecuteJavaScript("document.getElementById('test1').focus();");
ProcessPendingMessages();
render_thread_->sink().ClearMessages();
// Update the IME status and verify if our IME backend sends an IPC message
// to activate IMEs.
view()->UpdateTextInputState(RenderWidget::DO_NOT_SHOW_IME);
const IPC::Message* msg = render_thread_->sink().GetMessageAt(0);
EXPECT_TRUE(msg != NULL);
EXPECT_EQ(ViewHostMsg_TextInputStateChanged::ID, msg->type());
ViewHostMsg_TextInputStateChanged::Param params;
ViewHostMsg_TextInputStateChanged::Read(msg, ¶ms);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_TEXT, params.a.type);
EXPECT_EQ(true, params.a.can_compose_inline);
EXPECT_EQ("some text", params.a.value);
EXPECT_EQ(0, params.a.selection_start);
EXPECT_EQ(9, params.a.selection_end);
EXPECT_EQ(-1, params.a.composition_start);
EXPECT_EQ(-1, params.a.composition_end);
// Move the input focus to the second <input> element, where we should
// de-activate IMEs.
ExecuteJavaScript("document.getElementById('test2').focus();");
ProcessPendingMessages();
render_thread_->sink().ClearMessages();
// Update the IME status and verify if our IME backend sends an IPC message
// to de-activate IMEs.
view()->UpdateTextInputState(RenderWidget::DO_NOT_SHOW_IME);
msg = render_thread_->sink().GetMessageAt(0);
EXPECT_TRUE(msg != NULL);
EXPECT_EQ(ViewHostMsg_TextInputStateChanged::ID, msg->type());
ViewHostMsg_TextInputStateChanged::Read(msg, ¶ms);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_PASSWORD, params.a.type);
}
}
// Test that our IME backend can compose CJK words.
// Our IME front-end sends many platform-independent messages to the IME backend
// while it composes CJK words. This test sends the minimal messages captured
// on my local environment directly to the IME backend to verify if the backend
// can compose CJK words without any problems.
// This test uses an array of command sets because an IME composotion does not
// only depends on IME events, but also depends on window events, e.g. moving
// the window focus while composing a CJK text. To handle such complicated
// cases, this test should not only call IME-related functions in the
// RenderWidget class, but also call some RenderWidget members, e.g.
// ExecuteJavaScript(), RenderWidget::OnSetFocus(), etc.
TEST_F(RenderViewImplTest, ImeComposition) {
enum ImeCommand {
IME_INITIALIZE,
IME_SETINPUTMODE,
IME_SETFOCUS,
IME_SETCOMPOSITION,
IME_CONFIRMCOMPOSITION,
IME_CANCELCOMPOSITION
};
struct ImeMessage {
ImeCommand command;
bool enable;
int selection_start;
int selection_end;
const wchar_t* ime_string;
const wchar_t* result;
};
static const ImeMessage kImeMessages[] = {
// Scenario 1: input a Chinese word with Microsoft IME (on Vista).
{IME_INITIALIZE, true, 0, 0, NULL, NULL},
{IME_SETINPUTMODE, true, 0, 0, NULL, NULL},
{IME_SETFOCUS, true, 0, 0, NULL, NULL},
{IME_SETCOMPOSITION, false, 1, 1, L"n", L"n"},
{IME_SETCOMPOSITION, false, 2, 2, L"ni", L"ni"},
{IME_SETCOMPOSITION, false, 3, 3, L"nih", L"nih"},
{IME_SETCOMPOSITION, false, 4, 4, L"niha", L"niha"},
{IME_SETCOMPOSITION, false, 5, 5, L"nihao", L"nihao"},
{IME_CONFIRMCOMPOSITION, false, -1, -1, L"\x4F60\x597D", L"\x4F60\x597D"},
// Scenario 2: input a Japanese word with Microsoft IME (on Vista).
{IME_INITIALIZE, true, 0, 0, NULL, NULL},
{IME_SETINPUTMODE, true, 0, 0, NULL, NULL},
{IME_SETFOCUS, true, 0, 0, NULL, NULL},
{IME_SETCOMPOSITION, false, 0, 1, L"\xFF4B", L"\xFF4B"},
{IME_SETCOMPOSITION, false, 0, 1, L"\x304B", L"\x304B"},
{IME_SETCOMPOSITION, false, 0, 2, L"\x304B\xFF4E", L"\x304B\xFF4E"},
{IME_SETCOMPOSITION, false, 0, 3, L"\x304B\x3093\xFF4A",
L"\x304B\x3093\xFF4A"},
{IME_SETCOMPOSITION, false, 0, 3, L"\x304B\x3093\x3058",
L"\x304B\x3093\x3058"},
{IME_SETCOMPOSITION, false, 0, 2, L"\x611F\x3058", L"\x611F\x3058"},
{IME_SETCOMPOSITION, false, 0, 2, L"\x6F22\x5B57", L"\x6F22\x5B57"},
{IME_CONFIRMCOMPOSITION, false, -1, -1, L"", L"\x6F22\x5B57"},
{IME_CANCELCOMPOSITION, false, -1, -1, L"", L"\x6F22\x5B57"},
// Scenario 3: input a Korean word with Microsot IME (on Vista).
{IME_INITIALIZE, true, 0, 0, NULL, NULL},
{IME_SETINPUTMODE, true, 0, 0, NULL, NULL},
{IME_SETFOCUS, true, 0, 0, NULL, NULL},
{IME_SETCOMPOSITION, false, 0, 1, L"\x3147", L"\x3147"},
{IME_SETCOMPOSITION, false, 0, 1, L"\xC544", L"\xC544"},
{IME_SETCOMPOSITION, false, 0, 1, L"\xC548", L"\xC548"},
{IME_CONFIRMCOMPOSITION, false, -1, -1, L"", L"\xC548"},
{IME_SETCOMPOSITION, false, 0, 1, L"\x3134", L"\xC548\x3134"},
{IME_SETCOMPOSITION, false, 0, 1, L"\xB140", L"\xC548\xB140"},
{IME_SETCOMPOSITION, false, 0, 1, L"\xB155", L"\xC548\xB155"},
{IME_CANCELCOMPOSITION, false, -1, -1, L"", L"\xC548"},
{IME_SETCOMPOSITION, false, 0, 1, L"\xB155", L"\xC548\xB155"},
{IME_CONFIRMCOMPOSITION, false, -1, -1, L"", L"\xC548\xB155"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kImeMessages); i++) {
const ImeMessage* ime_message = &kImeMessages[i];
switch (ime_message->command) {
case IME_INITIALIZE:
// Load an HTML page consisting of a content-editable <div> element,
// and move the input focus to the <div> element, where we can use
// IMEs.
view()->OnSetInputMethodActive(ime_message->enable);
view()->set_send_content_state_immediately(true);
LoadHTML("<html>"
"<head>"
"</head>"
"<body>"
"<div id=\"test1\" contenteditable=\"true\"></div>"
"</body>"
"</html>");
ExecuteJavaScript("document.getElementById('test1').focus();");
break;
case IME_SETINPUTMODE:
// Activate (or deactivate) our IME back-end.
view()->OnSetInputMethodActive(ime_message->enable);
break;
case IME_SETFOCUS:
// Update the window focus.
view()->OnSetFocus(ime_message->enable);
break;
case IME_SETCOMPOSITION:
view()->OnImeSetComposition(
WideToUTF16Hack(ime_message->ime_string),
std::vector<WebKit::WebCompositionUnderline>(),
ime_message->selection_start,
ime_message->selection_end);
break;
case IME_CONFIRMCOMPOSITION:
view()->OnImeConfirmComposition(
WideToUTF16Hack(ime_message->ime_string),
ui::Range::InvalidRange());
break;
case IME_CANCELCOMPOSITION:
view()->OnImeSetComposition(
string16(),
std::vector<WebKit::WebCompositionUnderline>(),
0, 0);
break;
}
// Update the status of our IME back-end.
// TODO(hbono): we should verify messages to be sent from the back-end.
view()->UpdateTextInputState(RenderWidget::DO_NOT_SHOW_IME);
ProcessPendingMessages();
render_thread_->sink().ClearMessages();
if (ime_message->result) {
// Retrieve the content of this page and compare it with the expected
// result.
const int kMaxOutputCharacters = 128;
std::wstring output = UTF16ToWideHack(
GetMainFrame()->contentAsText(kMaxOutputCharacters));
EXPECT_EQ(output, ime_message->result);
}
}
}
// Test that the RenderView::OnSetTextDirection() function can change the text
// direction of the selected input element.
TEST_F(RenderViewImplTest, OnSetTextDirection) {
// Load an HTML page consisting of a <textarea> element and a <div> element.
// This test changes the text direction of the <textarea> element, and
// writes the values of its 'dir' attribute and its 'direction' property to
// verify that the text direction is changed.
view()->set_send_content_state_immediately(true);
LoadHTML("<html>"
"<head>"
"</head>"
"<body>"
"<textarea id=\"test\"></textarea>"
"<div id=\"result\" contenteditable=\"true\"></div>"
"</body>"
"</html>");
render_thread_->sink().ClearMessages();
static const struct {
WebTextDirection direction;
const wchar_t* expected_result;