OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/client/core/contents/ime_feature.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/callback_helpers.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "blimp/client/core/contents/mock_ime_feature_delegate.h" | |
12 #include "blimp/common/create_blimp_message.h" | |
13 #include "blimp/net/input_message_converter.h" | |
14 #include "blimp/net/test_common.h" | |
15 #include "net/base/test_completion_callback.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using testing::_; | |
20 using ::testing::SaveArg; | |
21 | |
22 namespace blimp { | |
23 namespace client { | |
24 namespace { | |
25 | |
26 void SendMockImeMessage(BlimpMessageProcessor* processor, | |
27 int tab_id, | |
28 int render_widget_id, | |
29 ui::TextInputType input_type, | |
30 const std::string& existing_text, | |
31 ImeMessage::Type message_type) { | |
32 ImeMessage* ime_message; | |
33 std::unique_ptr<BlimpMessage> message = | |
34 CreateBlimpMessage(&ime_message, tab_id); | |
35 ime_message->set_render_widget_id(render_widget_id); | |
36 ime_message->set_type(message_type); | |
37 | |
38 if (message_type == ImeMessage::SHOW_IME) { | |
39 ime_message->set_text_input_type( | |
40 InputMessageConverter::TextInputTypeToProto(input_type)); | |
41 ime_message->set_ime_text(existing_text); | |
42 } | |
43 | |
44 net::TestCompletionCallback cb; | |
45 processor->ProcessMessage(std::move(message), cb.callback()); | |
46 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
47 } | |
48 | |
49 MATCHER_P4(EqualsOnImeTextEntered, tab_id, rwid, text, submit, "") { | |
50 return arg.target_tab_id() == tab_id && | |
51 arg.ime().render_widget_id() == rwid && arg.ime().ime_text() == text && | |
52 arg.ime().auto_submit() == submit; | |
53 } | |
54 | |
55 class ImeFeatureTest : public testing::Test { | |
56 public: | |
57 ImeFeatureTest() : out_processor_(nullptr) {} | |
58 | |
59 void SetUp() override { | |
60 out_processor_ = new MockBlimpMessageProcessor(); | |
61 feature_.set_outgoing_message_processor(base::WrapUnique(out_processor_)); | |
62 feature_.set_delegate(&delegate_); | |
63 } | |
64 | |
65 protected: | |
66 // This is a raw pointer to a class that is owned by the ControlFeature. | |
67 MockBlimpMessageProcessor* out_processor_; | |
68 | |
69 MockImeFeatureDelegate delegate_; | |
70 | |
71 ImeFeature feature_; | |
72 }; | |
73 | |
74 TEST_F(ImeFeatureTest, HideImeCalls) { | |
75 MockImeFeatureDelegate delegate1; | |
76 MockImeFeatureDelegate delegate2; | |
77 | |
78 EXPECT_CALL(delegate1, OnHideImeRequested()); | |
79 EXPECT_CALL(delegate2, OnHideImeRequested()); | |
80 | |
81 feature_.set_delegate(&delegate1); | |
82 SendMockImeMessage(&feature_, 0, 1, ui::TEXT_INPUT_TYPE_NONE, "", | |
83 ImeMessage::HIDE_IME); | |
84 feature_.set_delegate(&delegate2); | |
85 SendMockImeMessage(&feature_, 0, 1, ui::TEXT_INPUT_TYPE_NONE, "", | |
86 ImeMessage::HIDE_IME); | |
87 } | |
88 | |
89 TEST_F(ImeFeatureTest, RepeatedShowAndHideImeCalls) { | |
90 ImeFeature::WebInputRequest request; | |
91 | |
92 EXPECT_CALL(delegate_, OnHideImeRequested()); | |
93 EXPECT_CALL(delegate_, OnShowImeRequested(_)).WillOnce(SaveArg<0>(&request)); | |
94 | |
95 SendMockImeMessage(&feature_, 0, 1, ui::TEXT_INPUT_TYPE_TEXT, "some text", | |
96 ImeMessage::SHOW_IME); | |
97 SendMockImeMessage(&feature_, 0, 1, ui::TEXT_INPUT_TYPE_NONE, "", | |
98 ImeMessage::HIDE_IME); | |
99 ASSERT_EQ("some text", request.text); | |
100 ASSERT_EQ(ui::TEXT_INPUT_TYPE_TEXT, request.input_type); | |
101 | |
102 EXPECT_CALL(delegate_, OnShowImeRequested(_)).WillOnce(SaveArg<0>(&request)); | |
103 | |
104 SendMockImeMessage(&feature_, 0, 1, ui::TEXT_INPUT_TYPE_TEXT_AREA, | |
105 "modified text", ImeMessage::SHOW_IME); | |
106 ASSERT_EQ("modified text", request.text); | |
107 ASSERT_EQ(ui::TEXT_INPUT_TYPE_TEXT_AREA, request.input_type); | |
108 } | |
109 | |
110 TEST_F(ImeFeatureTest, CallbackHasFieldsPopulatedCorrectly) { | |
111 ImeFeature::WebInputRequest request; | |
112 ImeFeature::WebInputResponse response; | |
113 | |
114 EXPECT_CALL(delegate_, OnShowImeRequested(_)).WillOnce(SaveArg<0>(&request)); | |
115 SendMockImeMessage(&feature_, 1, 3, ui::TEXT_INPUT_TYPE_TEXT, "old test", | |
116 ImeMessage::SHOW_IME); | |
117 | |
118 EXPECT_CALL(*out_processor_, | |
119 MockableProcessMessage( | |
120 EqualsOnImeTextEntered(1, 3, "new text", true), _)); | |
121 response.text = "new text"; | |
122 response.submit = true; | |
123 base::ResetAndReturn(&request.show_ime_callback).Run(response); | |
124 } | |
125 | |
126 } // namespace | |
127 } // namespace client | |
128 } // namespace blimp | |
OLD | NEW |