summaryrefslogtreecommitdiffstats
path: root/ui/base/ime/remote_input_method_win_unittest.cc
blob: c2346071d64b8db80742f86a18265a3fe9f09bef (plain)
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
// Copyright 2013 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 "ui/base/ime/remote_input_method_win.h"

#include <InputScope.h>

#include <vector>

#include "base/memory/scoped_ptr.h"
#include "base/scoped_observer.h"
#include "base/strings/string16.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/composition_text.h"
#include "ui/base/ime/dummy_text_input_client.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/input_method_delegate.h"
#include "ui/base/ime/input_method_observer.h"
#include "ui/base/ime/remote_input_method_delegate_win.h"
#include "ui/events/event.h"

namespace ui {
namespace {

class MockTextInputClient : public DummyTextInputClient {
 public:
  MockTextInputClient()
      : text_input_type_(TEXT_INPUT_TYPE_NONE),
        text_input_mode_(TEXT_INPUT_MODE_DEFAULT),
        call_count_set_composition_text_(0),
        call_count_insert_char_(0),
        call_count_insert_text_(0),
        emulate_pepper_flash_(false) {
  }

  size_t call_count_set_composition_text() const {
    return call_count_set_composition_text_;
  }
  const base::string16& inserted_text() const {
    return inserted_text_;
  }
  size_t call_count_insert_char() const {
    return call_count_insert_char_;
  }
  size_t call_count_insert_text() const {
    return call_count_insert_text_;
  }
  void Reset() {
    text_input_type_ = TEXT_INPUT_TYPE_NONE;
    text_input_mode_ = TEXT_INPUT_MODE_DEFAULT;
    call_count_set_composition_text_ = 0;
    inserted_text_.clear();
    call_count_insert_char_ = 0;
    call_count_insert_text_ = 0;
    caret_bounds_ = gfx::Rect();
    composition_character_bounds_.clear();
    emulate_pepper_flash_ = false;
  }
  void set_text_input_type(ui::TextInputType type) {
    text_input_type_ = type;
  }
  void set_text_input_mode(ui::TextInputMode mode) {
    text_input_mode_ = mode;
  }
  void set_caret_bounds(const gfx::Rect& caret_bounds) {
    caret_bounds_ = caret_bounds;
  }
  void set_composition_character_bounds(
      const std::vector<gfx::Rect>& composition_character_bounds) {
    composition_character_bounds_ = composition_character_bounds;
  }
  void set_emulate_pepper_flash(bool enabled) {
    emulate_pepper_flash_ = enabled;
  }

 private:
  // Overriden from DummyTextInputClient.
  void SetCompositionText(const ui::CompositionText& composition) override {
    ++call_count_set_composition_text_;
  }
  void InsertChar(const ui::KeyEvent& event) override {
    inserted_text_.append(1, event.GetCharacter());
    ++call_count_insert_char_;
  }
  void InsertText(const base::string16& text) override {
    inserted_text_.append(text);
    ++call_count_insert_text_;
  }
  ui::TextInputType GetTextInputType() const override {
    return text_input_type_;
  }
  ui::TextInputMode GetTextInputMode() const override {
    return text_input_mode_;
  }
  gfx::Rect GetCaretBounds() const override { return caret_bounds_; }
  bool GetCompositionCharacterBounds(uint32 index,
                                     gfx::Rect* rect) const override {
    // Emulate the situation of crbug.com/328237.
    if (emulate_pepper_flash_)
      return false;
    if (!rect || composition_character_bounds_.size() <= index)
      return false;
    *rect = composition_character_bounds_[index];
    return true;
  }
  bool HasCompositionText() const override {
    return !composition_character_bounds_.empty();
  }
  bool GetCompositionTextRange(gfx::Range* range) const override {
    if (composition_character_bounds_.empty())
      return false;
    *range = gfx::Range(0, composition_character_bounds_.size());
    return true;
  }

  ui::TextInputType text_input_type_;
  ui::TextInputMode text_input_mode_;
  gfx::Rect caret_bounds_;
  std::vector<gfx::Rect> composition_character_bounds_;
  base::string16 inserted_text_;
  size_t call_count_set_composition_text_;
  size_t call_count_insert_char_;
  size_t call_count_insert_text_;
  bool emulate_pepper_flash_;
  DISALLOW_COPY_AND_ASSIGN(MockTextInputClient);
};

class MockInputMethodDelegate : public internal::InputMethodDelegate {
 public:
  MockInputMethodDelegate() {}

  const std::vector<ui::KeyboardCode>& fabricated_key_events() const {
    return fabricated_key_events_;
  }
  void Reset() {
    fabricated_key_events_.clear();
  }

 private:
  ui::EventDispatchDetails DispatchKeyEventPostIME(
      ui::KeyEvent* event) override {
    EXPECT_FALSE(event->HasNativeEvent());
    fabricated_key_events_.push_back(event->key_code());
    event->SetHandled();
    return ui::EventDispatchDetails();
  }

  std::vector<ui::KeyboardCode> fabricated_key_events_;
  DISALLOW_COPY_AND_ASSIGN(MockInputMethodDelegate);
};

class MockRemoteInputMethodDelegateWin
    : public internal::RemoteInputMethodDelegateWin {
 public:
  MockRemoteInputMethodDelegateWin()
      : cancel_composition_called_(false),
        text_input_client_updated_called_(false) {
  }

  bool cancel_composition_called() const {
    return cancel_composition_called_;
  }
  bool text_input_client_updated_called() const {
    return text_input_client_updated_called_;
  }
  const std::vector<int32>& input_scopes() const {
    return input_scopes_;
  }
  const std::vector<gfx::Rect>& composition_character_bounds() const {
    return composition_character_bounds_;
  }
  void Reset() {
    cancel_composition_called_ = false;
    text_input_client_updated_called_ = false;
    input_scopes_.clear();
    composition_character_bounds_.clear();
  }

 private:
  void CancelComposition() override { cancel_composition_called_ = true; }

  void OnTextInputClientUpdated(
      const std::vector<int32>& input_scopes,
      const std::vector<gfx::Rect>& composition_character_bounds) override {
    text_input_client_updated_called_ = true;
    input_scopes_ = input_scopes;
    composition_character_bounds_ = composition_character_bounds;
  }

  bool cancel_composition_called_;
  bool text_input_client_updated_called_;
  std::vector<int32> input_scopes_;
  std::vector<gfx::Rect> composition_character_bounds_;
  DISALLOW_COPY_AND_ASSIGN(MockRemoteInputMethodDelegateWin);
};

class MockInputMethodObserver : public InputMethodObserver {
 public:
  MockInputMethodObserver()
      : on_text_input_state_changed_(0),
        on_input_method_destroyed_changed_(0) {
  }
  ~MockInputMethodObserver() override {}
  void Reset() {
    on_text_input_state_changed_ = 0;
    on_input_method_destroyed_changed_ = 0;
  }
  size_t on_text_input_state_changed() const {
    return on_text_input_state_changed_;
  }
  size_t on_input_method_destroyed_changed() const {
    return on_input_method_destroyed_changed_;
  }

 private:
  // Overriden from InputMethodObserver.
  void OnTextInputTypeChanged(const TextInputClient* client) override {}
  void OnFocus() override {}
  void OnBlur() override {}
  void OnCaretBoundsChanged(const TextInputClient* client) override {}
  void OnTextInputStateChanged(const TextInputClient* client) override {
    ++on_text_input_state_changed_;
  }
  void OnInputMethodDestroyed(const InputMethod* client) override {
    ++on_input_method_destroyed_changed_;
  }
  void OnShowImeIfNeeded() override {}

  size_t on_text_input_state_changed_;
  size_t on_input_method_destroyed_changed_;
  DISALLOW_COPY_AND_ASSIGN(MockInputMethodObserver);
};

typedef ScopedObserver<InputMethod, InputMethodObserver>
    InputMethodScopedObserver;

TEST(RemoteInputMethodWinTest, RemoteInputMethodPrivateWin) {
  InputMethod* other_ptr = static_cast<InputMethod*>(NULL) + 1;

  // Use typed NULL to make EXPECT_NE happy until nullptr becomes available.
  RemoteInputMethodPrivateWin* kNull =
      static_cast<RemoteInputMethodPrivateWin*>(NULL);
  EXPECT_EQ(kNull, RemoteInputMethodPrivateWin::Get(other_ptr));

  MockInputMethodDelegate delegate_;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));
  EXPECT_NE(kNull, RemoteInputMethodPrivateWin::Get(input_method.get()));

  InputMethod* dangling_ptr = input_method.get();
  input_method.reset(NULL);
  EXPECT_EQ(kNull, RemoteInputMethodPrivateWin::Get(dangling_ptr));
}

TEST(RemoteInputMethodWinTest, OnInputSourceChanged) {
  MockInputMethodDelegate delegate_;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));
  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);

  private_ptr->OnInputSourceChanged(
      MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN), true);
  EXPECT_EQ("ja-JP", input_method->GetInputLocale());

  private_ptr->OnInputSourceChanged(
      MAKELANGID(LANG_ARABIC, SUBLANG_ARABIC_QATAR), true);
  EXPECT_EQ("ar-QA", input_method->GetInputLocale());
}

TEST(RemoteInputMethodWinTest, OnCandidatePopupChanged) {
  MockInputMethodDelegate delegate_;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));
  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);

  // Initial value
  EXPECT_FALSE(input_method->IsCandidatePopupOpen());

  // RemoteInputMethodWin::OnCandidatePopupChanged can be called even when the
  // focused text input client is NULL.
  ASSERT_TRUE(input_method->GetTextInputClient() == NULL);
  private_ptr->OnCandidatePopupChanged(false);
  private_ptr->OnCandidatePopupChanged(true);

  MockTextInputClient mock_text_input_client;
  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  mock_text_input_client.Reset();

  private_ptr->OnCandidatePopupChanged(true);
  EXPECT_TRUE(input_method->IsCandidatePopupOpen());

  private_ptr->OnCandidatePopupChanged(false);
  EXPECT_FALSE(input_method->IsCandidatePopupOpen());
}

TEST(RemoteInputMethodWinTest, CancelComposition) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  // This must not cause a crash.
  input_method->CancelComposition(&mock_text_input_client);

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  input_method->CancelComposition(&mock_text_input_client);
  EXPECT_FALSE(mock_remote_delegate.cancel_composition_called());

  input_method->SetFocusedTextInputClient(&mock_text_input_client);
  input_method->CancelComposition(&mock_text_input_client);
  EXPECT_TRUE(mock_remote_delegate.cancel_composition_called());
}

TEST(RemoteInputMethodWinTest, SetFocusedTextInputClient) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  mock_text_input_client.set_caret_bounds(gfx::Rect(10, 0, 10, 20));
  mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_URL);
  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // Initial state must be synced.
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  ASSERT_EQ(1u, mock_remote_delegate.composition_character_bounds().size());
  EXPECT_EQ(gfx::Rect(10, 0, 10, 20),
            mock_remote_delegate.composition_character_bounds()[0]);
  ASSERT_EQ(1u, mock_remote_delegate.input_scopes().size());
  EXPECT_EQ(IS_URL, mock_remote_delegate.input_scopes()[0]);

  // State must be cleared by SetFocusedTextInputClient(NULL).
  mock_remote_delegate.Reset();
  input_method->SetFocusedTextInputClient(NULL);
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  EXPECT_TRUE(mock_remote_delegate.composition_character_bounds().empty());
  EXPECT_TRUE(mock_remote_delegate.input_scopes().empty());
}

TEST(RemoteInputMethodWinTest, DetachTextInputClient) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  mock_text_input_client.set_caret_bounds(gfx::Rect(10, 0, 10, 20));
  mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_URL);
  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // Initial state must be synced.
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  ASSERT_EQ(1u, mock_remote_delegate.composition_character_bounds().size());
  EXPECT_EQ(gfx::Rect(10, 0, 10, 20),
    mock_remote_delegate.composition_character_bounds()[0]);
  ASSERT_EQ(1u, mock_remote_delegate.input_scopes().size());
  EXPECT_EQ(IS_URL, mock_remote_delegate.input_scopes()[0]);

  // State must be cleared by DetachTextInputClient
  mock_remote_delegate.Reset();
  input_method->DetachTextInputClient(&mock_text_input_client);
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  EXPECT_TRUE(mock_remote_delegate.composition_character_bounds().empty());
  EXPECT_TRUE(mock_remote_delegate.input_scopes().empty());
}

TEST(RemoteInputMethodWinTest, OnCaretBoundsChanged) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  // This must not cause a crash.
  input_method->OnCaretBoundsChanged(&mock_text_input_client);

  mock_text_input_client.set_caret_bounds(gfx::Rect(10, 0, 10, 20));
  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // Initial state must be synced.
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  ASSERT_EQ(1u, mock_remote_delegate.composition_character_bounds().size());
  EXPECT_EQ(gfx::Rect(10, 0, 10, 20),
      mock_remote_delegate.composition_character_bounds()[0]);

  // Redundant OnCaretBoundsChanged must be ignored.
  mock_remote_delegate.Reset();
  input_method->OnCaretBoundsChanged(&mock_text_input_client);
  EXPECT_FALSE(mock_remote_delegate.text_input_client_updated_called());

  // Check OnCaretBoundsChanged is handled. (w/o composition)
  mock_remote_delegate.Reset();
  mock_text_input_client.Reset();
  mock_text_input_client.set_caret_bounds(gfx::Rect(10, 20, 30, 40));
  input_method->OnCaretBoundsChanged(&mock_text_input_client);
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  ASSERT_EQ(1u, mock_remote_delegate.composition_character_bounds().size());
  EXPECT_EQ(gfx::Rect(10, 20, 30, 40),
      mock_remote_delegate.composition_character_bounds()[0]);

  // Check OnCaretBoundsChanged is handled. (w/ composition)
  {
    mock_remote_delegate.Reset();
    mock_text_input_client.Reset();

    std::vector<gfx::Rect> bounds;
    bounds.push_back(gfx::Rect(10, 20, 30, 40));
    bounds.push_back(gfx::Rect(40, 30, 20, 10));
    mock_text_input_client.set_composition_character_bounds(bounds);
    input_method->OnCaretBoundsChanged(&mock_text_input_client);
    EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
    EXPECT_EQ(bounds, mock_remote_delegate.composition_character_bounds());
  }
}

// Test case against crbug.com/328237.
TEST(RemoteInputMethodWinTest, OnCaretBoundsChangedForPepperFlash) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));
  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  mock_remote_delegate.Reset();
  mock_text_input_client.Reset();
  mock_text_input_client.set_emulate_pepper_flash(true);

  std::vector<gfx::Rect> caret_bounds;
  caret_bounds.push_back(gfx::Rect(5, 15, 25, 35));
  mock_text_input_client.set_caret_bounds(caret_bounds[0]);

  std::vector<gfx::Rect> composition_bounds;
  composition_bounds.push_back(gfx::Rect(10, 20, 30, 40));
  composition_bounds.push_back(gfx::Rect(40, 30, 20, 10));
  mock_text_input_client.set_composition_character_bounds(composition_bounds);
  input_method->OnCaretBoundsChanged(&mock_text_input_client);
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  // The caret bounds must be used when
  // TextInputClient::GetCompositionCharacterBounds failed.
  EXPECT_EQ(caret_bounds, mock_remote_delegate.composition_character_bounds());
}

TEST(RemoteInputMethodWinTest, OnTextInputTypeChanged) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  // This must not cause a crash.
  input_method->OnCaretBoundsChanged(&mock_text_input_client);

  mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_URL);
  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // Initial state must be synced.
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  ASSERT_EQ(1u, mock_remote_delegate.input_scopes().size());
  EXPECT_EQ(IS_URL, mock_remote_delegate.input_scopes()[0]);

  // Check TEXT_INPUT_TYPE_NONE is handled.
  mock_remote_delegate.Reset();
  mock_text_input_client.Reset();
  mock_text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_NONE);
  mock_text_input_client.set_text_input_mode(ui::TEXT_INPUT_MODE_KATAKANA);
  input_method->OnTextInputTypeChanged(&mock_text_input_client);
  EXPECT_TRUE(mock_remote_delegate.text_input_client_updated_called());
  EXPECT_TRUE(mock_remote_delegate.input_scopes().empty());

  // Redundant OnTextInputTypeChanged must be ignored.
  mock_remote_delegate.Reset();
  input_method->OnTextInputTypeChanged(&mock_text_input_client);
  EXPECT_FALSE(mock_remote_delegate.text_input_client_updated_called());

  mock_remote_delegate.Reset();
  mock_text_input_client.Reset();
  mock_text_input_client.set_caret_bounds(gfx::Rect(10, 20, 30, 40));
  input_method->OnCaretBoundsChanged(&mock_text_input_client);
}

TEST(RemoteInputMethodWinTest, DispatchKeyEvent_NativeKeyEvent) {
  // Basically RemoteInputMethodWin does not handle native keydown event.

  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  const MSG wm_keydown = { NULL, WM_KEYDOWN, ui::VKEY_A };
  ui::KeyEvent new_keydown(wm_keydown);
  ui::KeyEvent native_keydown(new_keydown);

  // This must not cause a crash.
  input_method->DispatchKeyEvent(&native_keydown);
  EXPECT_FALSE(native_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // TextInputClient is not focused yet here.
  native_keydown = new_keydown;
  input_method->DispatchKeyEvent(&native_keydown);
  EXPECT_FALSE(native_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  // TextInputClient is now focused here.
  native_keydown = new_keydown;
  input_method->DispatchKeyEvent(&native_keydown);
  EXPECT_FALSE(native_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();
}

TEST(RemoteInputMethodWinTest, DispatchKeyEvent_NativeCharEvent) {
  // RemoteInputMethodWin handles native char event if possible.

  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  const MSG wm_char = { NULL, WM_CHAR, 'A', 0 };
  ui::KeyEvent new_char(wm_char);
  ui::KeyEvent native_char(new_char);

  // This must not cause a crash.
  input_method->DispatchKeyEvent(&native_char);
  EXPECT_FALSE(native_char.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // TextInputClient is not focused yet here.
  native_char = new_char;
  input_method->DispatchKeyEvent(&native_char);
  EXPECT_FALSE(native_char.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  // TextInputClient is now focused here.
  native_char = new_char;
  input_method->DispatchKeyEvent(&native_char);
  EXPECT_TRUE(native_char.handled());
  EXPECT_EQ(L"A", mock_text_input_client.inserted_text());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();
}

TEST(RemoteInputMethodWinTest, DispatchKeyEvent_FabricatedKeyDown) {
  // Fabricated non-char event will be delegated to
  // InputMethodDelegate::DispatchFabricatedKeyEventPostIME as long as the
  // delegate is installed.

  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  ui::KeyEvent new_keydown(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  new_keydown.set_character(L'A');
  ui::KeyEvent fabricated_keydown(new_keydown);

  // This must not cause a crash.
  input_method->DispatchKeyEvent(&fabricated_keydown);
  EXPECT_TRUE(fabricated_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  ASSERT_EQ(1u, delegate_.fabricated_key_events().size());
  EXPECT_EQ(L'A', delegate_.fabricated_key_events()[0]);
  delegate_.Reset();
  mock_text_input_client.Reset();

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // TextInputClient is not focused yet here.
  fabricated_keydown = new_keydown;
  input_method->DispatchKeyEvent(&fabricated_keydown);
  EXPECT_TRUE(fabricated_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  ASSERT_EQ(1u, delegate_.fabricated_key_events().size());
  EXPECT_EQ(L'A', delegate_.fabricated_key_events()[0]);
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetFocusedTextInputClient(&mock_text_input_client);
  // TextInputClient is now focused here.
  fabricated_keydown = new_keydown;
  input_method->DispatchKeyEvent(&fabricated_keydown);
  EXPECT_TRUE(fabricated_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  ASSERT_EQ(1u, delegate_.fabricated_key_events().size());
  EXPECT_EQ(L'A', delegate_.fabricated_key_events()[0]);
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetDelegate(NULL);
  // RemoteInputMethodDelegateWin is no longer set here.
  fabricated_keydown = new_keydown;
  input_method->DispatchKeyEvent(&fabricated_keydown);
  EXPECT_FALSE(fabricated_keydown.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
}

TEST(RemoteInputMethodWinTest, DispatchKeyEvent_FabricatedChar) {
  // Note: RemoteInputMethodWin::DispatchKeyEvent should always return true
  // for fabricated character events.

  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  ui::KeyEvent new_char(L'A', ui::VKEY_A, ui::EF_NONE);
  ui::KeyEvent fabricated_char(new_char);

  // This must not cause a crash.
  input_method->DispatchKeyEvent(&fabricated_char);
  EXPECT_TRUE(fabricated_char.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  // TextInputClient is not focused yet here.
  fabricated_char = new_char;
  input_method->DispatchKeyEvent(&fabricated_char);
  EXPECT_TRUE(fabricated_char.handled());
  EXPECT_TRUE(mock_text_input_client.inserted_text().empty());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  // TextInputClient is now focused here.
  fabricated_char = new_char;
  input_method->DispatchKeyEvent(&fabricated_char);
  EXPECT_TRUE(fabricated_char.handled());
  EXPECT_EQ(L"A", mock_text_input_client.inserted_text());
  EXPECT_TRUE(delegate_.fabricated_key_events().empty());
  delegate_.Reset();
  mock_text_input_client.Reset();
}

TEST(RemoteInputMethodWinTest, OnCompositionChanged) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  CompositionText composition_text;

  // TextInputClient is not focused yet here.

  private_ptr->OnCompositionChanged(composition_text);
  EXPECT_EQ(0u, mock_text_input_client.call_count_set_composition_text());
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  // TextInputClient is now focused here.

  private_ptr->OnCompositionChanged(composition_text);
  EXPECT_EQ(1u, mock_text_input_client.call_count_set_composition_text());
  delegate_.Reset();
  mock_text_input_client.Reset();
}

TEST(RemoteInputMethodWinTest, OnTextCommitted) {
  MockInputMethodDelegate delegate_;
  MockTextInputClient mock_text_input_client;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));

  RemoteInputMethodPrivateWin* private_ptr =
      RemoteInputMethodPrivateWin::Get(input_method.get());
  ASSERT_TRUE(private_ptr != NULL);
  MockRemoteInputMethodDelegateWin mock_remote_delegate;
  private_ptr->SetRemoteDelegate(&mock_remote_delegate);

  base::string16 committed_text = L"Hello";

  // TextInputClient is not focused yet here.

  mock_text_input_client.set_text_input_type(TEXT_INPUT_TYPE_TEXT);
  private_ptr->OnTextCommitted(committed_text);
  EXPECT_EQ(0u, mock_text_input_client.call_count_insert_char());
  EXPECT_EQ(0u, mock_text_input_client.call_count_insert_text());
  EXPECT_EQ(L"", mock_text_input_client.inserted_text());
  delegate_.Reset();
  mock_text_input_client.Reset();

  input_method->SetFocusedTextInputClient(&mock_text_input_client);

  // TextInputClient is now focused here.

  mock_text_input_client.set_text_input_type(TEXT_INPUT_TYPE_TEXT);
  private_ptr->OnTextCommitted(committed_text);
  EXPECT_EQ(0u, mock_text_input_client.call_count_insert_char());
  EXPECT_EQ(1u, mock_text_input_client.call_count_insert_text());
  EXPECT_EQ(committed_text, mock_text_input_client.inserted_text());
  delegate_.Reset();
  mock_text_input_client.Reset();

  // When TextInputType is TEXT_INPUT_TYPE_NONE, TextInputClient::InsertText
  // should not be used.
  mock_text_input_client.set_text_input_type(TEXT_INPUT_TYPE_NONE);
  private_ptr->OnTextCommitted(committed_text);
  EXPECT_EQ(committed_text.size(),
            mock_text_input_client.call_count_insert_char());
  EXPECT_EQ(0u, mock_text_input_client.call_count_insert_text());
  EXPECT_EQ(committed_text, mock_text_input_client.inserted_text());
  delegate_.Reset();
  mock_text_input_client.Reset();
}

TEST(RemoteInputMethodWinTest, OnTextInputStateChanged_Observer) {
  DummyTextInputClient text_input_client;
  DummyTextInputClient text_input_client_the_other;

  MockInputMethodObserver input_method_observer;
  MockInputMethodDelegate delegate_;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));
  InputMethodScopedObserver scoped_observer(&input_method_observer);
  scoped_observer.Add(input_method.get());

  input_method->SetFocusedTextInputClient(&text_input_client);
  ASSERT_EQ(&text_input_client, input_method->GetTextInputClient());
  EXPECT_EQ(1u, input_method_observer.on_text_input_state_changed());
  input_method_observer.Reset();

  input_method->SetFocusedTextInputClient(&text_input_client);
  ASSERT_EQ(&text_input_client, input_method->GetTextInputClient());
  EXPECT_EQ(0u, input_method_observer.on_text_input_state_changed());
  input_method_observer.Reset();

  input_method->SetFocusedTextInputClient(&text_input_client_the_other);
  ASSERT_EQ(&text_input_client_the_other, input_method->GetTextInputClient());
  EXPECT_EQ(1u, input_method_observer.on_text_input_state_changed());
  input_method_observer.Reset();

  input_method->DetachTextInputClient(&text_input_client_the_other);
  ASSERT_TRUE(input_method->GetTextInputClient() == NULL);
  EXPECT_EQ(1u, input_method_observer.on_text_input_state_changed());
  input_method_observer.Reset();
}

TEST(RemoteInputMethodWinTest, OnInputMethodDestroyed_Observer) {
  DummyTextInputClient text_input_client;
  DummyTextInputClient text_input_client_the_other;

  MockInputMethodObserver input_method_observer;
  InputMethodScopedObserver scoped_observer(&input_method_observer);

  MockInputMethodDelegate delegate_;
  scoped_ptr<InputMethod> input_method(CreateRemoteInputMethodWin(&delegate_));
  input_method->AddObserver(&input_method_observer);

  EXPECT_EQ(0u, input_method_observer.on_input_method_destroyed_changed());
  input_method.reset();
  EXPECT_EQ(1u, input_method_observer.on_input_method_destroyed_changed());
}

}  // namespace
}  // namespace ui