OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "testing/platform_test.h" | |
12 #include "ui/base/cocoa/text_services_context_menu.h" | |
Alexei Svitkine (slow)
2017/02/03 15:43:32
This should be the first include.
spqchan
2017/02/03 23:32:11
Done.
| |
13 #import "ui/gfx/test/ui_cocoa_test_helper.h" | |
14 #include "ui/strings/grit/ui_strings.h" | |
15 | |
16 namespace ui { | |
17 | |
18 class TextServicesContextMenuTest : public CocoaTest, | |
19 public TextServicesContextMenu::Delegate, | |
20 public SimpleMenuModel::Delegate { | |
21 public: | |
22 TextServicesContextMenuTest() | |
23 : menu_(this), | |
24 menu_model_(this), | |
25 text_direction_(base::i18n::UNKNOWN_DIRECTION) {} | |
26 | |
27 // TextServicesContextMenu::Delegate: | |
28 base::string16 GetSelectedText() const override { return base::string16(); } | |
29 | |
30 bool IsTextDirectionEnabled( | |
31 base::i18n::TextDirection direction) const override { | |
32 return true; | |
33 } | |
34 | |
35 bool IsTextDirectionChecked( | |
36 base::i18n::TextDirection direction) const override { | |
37 return text_direction_ == direction; | |
38 } | |
39 | |
40 void UpdateTextDirection(base::i18n::TextDirection direction) override {} | |
41 | |
42 // SimpleMenuModel::Delegate: | |
43 bool IsCommandIdChecked(int command_id) const override { return false; } | |
44 | |
45 bool IsCommandIdEnabled(int command_id) const override { return true; } | |
46 | |
47 void ExecuteCommand(int command_id, int event_flags) override {} | |
48 | |
49 ui::TextServicesContextMenu menu_; | |
tapted
2017/02/03 10:14:51
protected: before this
spqchan
2017/02/03 23:32:11
Done.
| |
50 | |
51 ui::SimpleMenuModel menu_model_; | |
52 | |
53 base::i18n::TextDirection text_direction_; | |
54 }; | |
tapted
2017/02/03 10:14:51
nit:
private:
DISALLOW_COPY_AND_ASSIGN(..)
spqchan
2017/02/03 23:32:11
Done.
| |
55 | |
56 // Tests to see if the BiDi and Speech menu gets appended. | |
57 TEST_F(TextServicesContextMenuTest, AppendMenu) { | |
58 menu_model_.AddItem(0, base::UTF8ToUTF16("test item")); | |
59 | |
60 menu_.AppendToContextMenu(&menu_model_); | |
61 menu_.AppendEditableItems(&menu_model_); | |
62 | |
63 int menu_index = 1; | |
64 | |
65 // Separator item. | |
66 EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); | |
67 EXPECT_EQ(MenuModel::ItemType::TYPE_SEPARATOR, | |
68 menu_model_.GetTypeAt(menu_index)); | |
69 menu_index++; | |
70 | |
71 // Speech Submenu item. | |
72 EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); | |
73 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, | |
74 menu_model_.GetTypeAt(menu_index)); | |
75 MenuModel* speech_menu = menu_model_.GetSubmenuModelAt(menu_index); | |
76 ASSERT_TRUE(speech_menu); | |
77 menu_index++; | |
78 | |
79 // Check each item in the Speech submenu. | |
80 EXPECT_EQ(speech_menu->GetItemCount(), 2); | |
81 EXPECT_EQ(IDS_SPEECH_START_SPEAKING_MAC, speech_menu->GetCommandIdAt(0)); | |
82 EXPECT_EQ(IDS_SPEECH_STOP_SPEAKING_MAC, speech_menu->GetCommandIdAt(1)); | |
83 | |
84 // BiDi Submenu item. | |
85 EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); | |
86 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, | |
87 menu_model_.GetTypeAt(menu_index)); | |
88 MenuModel* bidi_menu = menu_model_.GetSubmenuModelAt(menu_index); | |
89 ASSERT_TRUE(bidi_menu); | |
90 | |
91 // Check each item in the BiDi submenu. | |
92 EXPECT_EQ(bidi_menu->GetItemCount(), 3); | |
93 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, | |
94 bidi_menu->GetCommandIdAt(0)); | |
95 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, | |
96 bidi_menu->GetCommandIdAt(1)); | |
97 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, | |
98 bidi_menu->GetCommandIdAt(2)); | |
99 } | |
100 | |
101 // Tests to see if the Speech API works | |
102 TEST_F(TextServicesContextMenuTest, SpeechApi) { | |
103 menu_.SpeakText(base::UTF8ToUTF16("boxfish")); | |
tapted
2017/02/03 10:14:51
nit: ASCIIToUTF16
spqchan
2017/02/03 23:32:11
Done.
| |
104 EXPECT_TRUE(menu_.IsSpeaking()); | |
tapted
2017/02/03 10:14:51
It's likely this will be OK - I'm not sure how it
Alexei Svitkine (slow)
2017/02/03 15:43:33
Yeah - maybe worth making the text string a bit lo
spqchan
2017/02/03 23:32:11
Done.
| |
105 menu_.StopSpeaking(); | |
106 } | |
107 | |
108 // Tests to see if the Speech submenu items are correct. | |
109 TEST_F(TextServicesContextMenuTest, SpeechSubmenu) { | |
110 const int kStartSpeakingIndex = 0; | |
111 const int kStopSpeakingIndex = 1; | |
112 | |
113 menu_.AppendToContextMenu(&menu_model_); | |
114 | |
115 // Get and check the speech submenu item. | |
116 EXPECT_TRUE(menu_model_.IsEnabledAt(0)); | |
117 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, menu_model_.GetTypeAt(0)); | |
118 MenuModel* speech_menu = menu_model_.GetSubmenuModelAt(0); | |
119 ASSERT_TRUE(speech_menu); | |
120 | |
121 // Check each item in the Speech submenu. | |
122 EXPECT_EQ(speech_menu->GetItemCount(), 2); | |
123 EXPECT_EQ(IDS_SPEECH_START_SPEAKING_MAC, | |
124 speech_menu->GetCommandIdAt(kStartSpeakingIndex)); | |
125 EXPECT_EQ(IDS_SPEECH_STOP_SPEAKING_MAC, | |
126 speech_menu->GetCommandIdAt(kStopSpeakingIndex)); | |
127 EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); | |
128 EXPECT_FALSE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); | |
129 | |
130 menu_.SpeakText(base::UTF8ToUTF16("boxfish")); | |
tapted
2017/02/03 10:14:51
ASCII
Also I think IsSpeaking() depends on global
spqchan
2017/02/03 23:32:11
That's a good point. I removed SpeechAPI test sinc
| |
131 EXPECT_TRUE(menu_.IsSpeaking()); | |
132 | |
133 EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); | |
134 EXPECT_TRUE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); | |
135 | |
136 menu_.StopSpeaking(); | |
137 EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); | |
138 EXPECT_FALSE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); | |
139 } | |
140 | |
141 // Tests to see if the BiDi submenu items are correct. | |
142 TEST_F(TextServicesContextMenuTest, BiDiSubmenu) { | |
143 const int kTextDirectionDefaultIndex = 0; | |
144 const int kTextDirectionLTRIndex = 1; | |
145 const int kTextDirectionRTLIndex = 2; | |
146 | |
147 menu_.AppendEditableItems(&menu_model_); | |
148 | |
149 // BiDi Submenu item. | |
150 EXPECT_TRUE(menu_model_.IsEnabledAt(0)); | |
151 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, menu_model_.GetTypeAt(0)); | |
152 MenuModel* bidi_menu = menu_model_.GetSubmenuModelAt(0); | |
153 ASSERT_TRUE(bidi_menu); | |
154 | |
155 // Check each item in the BiDi submenu. | |
156 EXPECT_EQ(bidi_menu->GetItemCount(), 3); | |
157 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, | |
158 bidi_menu->GetCommandIdAt(kTextDirectionDefaultIndex)); | |
159 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, | |
160 bidi_menu->GetCommandIdAt(kTextDirectionLTRIndex)); | |
161 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, | |
162 bidi_menu->GetCommandIdAt(kTextDirectionRTLIndex)); | |
163 | |
164 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | |
165 EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); | |
166 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); | |
167 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); | |
168 | |
169 text_direction_ = base::i18n::LEFT_TO_RIGHT; | |
170 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); | |
171 EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); | |
172 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); | |
173 | |
174 text_direction_ = base::i18n::RIGHT_TO_LEFT; | |
175 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); | |
176 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); | |
177 EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); | |
178 } | |
179 | |
180 } // namespace | |
tapted
2017/02/03 10:14:51
namespace ui
| |
OLD | NEW |