| 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 #include "ui/base/cocoa/text_services_context_menu.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include "testing/platform_test.h" | |
| 14 #import "ui/gfx/test/ui_cocoa_test_helper.h" | |
| 15 #include "ui/strings/grit/ui_strings.h" | |
| 16 | |
| 17 namespace ui { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // To reduce the chance that the API finished speaking before the test is | |
| 22 // completed, we want to use a large string to stall the process. | |
| 23 const base::string16 kSpeechText = base::ASCIIToUTF16( | |
| 24 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam " | |
| 25 "faucibus vulputate dui ut molestie. Curabitur varius, tortor consectetur " | |
| 26 "ultrices blandit, eros arcu efficitur nisl, eget cursus odio purus id " | |
| 27 "augue. Nunc velit urna, porttitor et condimentum nec, imperdiet mattis " | |
| 28 "tortor. Aliquam vitae mattis urna. Mauris vulputate hendrerit nulla, " | |
| 29 "quis rutrum urna egestas eu. Vivamus porttitor ligula lectus, non " | |
| 30 "dapibus arcu rutrum eu. Curabitur placerat tincidunt sem, vel rutrum " | |
| 31 "nibh porta non. Nunc lacinia, turpis et maximus commodo, odio est " | |
| 32 "viverra ligula, finibus pharetra tellus lacus eget ante. Integer " | |
| 33 "venenatis hendrerit tellus eget tincidunt. Proin tempor quam ut purus " | |
| 34 "vulputate tempor. Donec commodo urna ut tortor congue, ut gravida ligula." | |
| 35 "The yellow boxfish is a species of boxfish. It is box-shaped. It is " | |
| 36 "bright yellow as a juvenile. As it ages, the brightness fades. Very " | |
| 37 "old specimens will have blue-grey colouration with faded yellow. It " | |
| 38 "feeds mainly on algae, but will also feed on sponges and shellfish." | |
| 39 "Testing testing testing testing testing testing testing testing " | |
| 40 "testing testing testing testing testing testing testing testing " | |
| 41 "testing testing testing testing testing testing testing testing " | |
| 42 "testing testing testing testing testing testing testing testing " | |
| 43 "and some more testing."); | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 class TextServicesContextMenuTest : public CocoaTest, | |
| 48 public TextServicesContextMenu::Delegate, | |
| 49 public SimpleMenuModel::Delegate { | |
| 50 public: | |
| 51 TextServicesContextMenuTest() | |
| 52 : menu_(this), | |
| 53 menu_model_(this), | |
| 54 text_direction_(base::i18n::UNKNOWN_DIRECTION) {} | |
| 55 | |
| 56 // TextServicesContextMenu::Delegate: | |
| 57 base::string16 GetSelectedText() const override { return base::string16(); } | |
| 58 | |
| 59 bool IsTextDirectionEnabled( | |
| 60 base::i18n::TextDirection direction) const override { | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 bool IsTextDirectionChecked( | |
| 65 base::i18n::TextDirection direction) const override { | |
| 66 return text_direction_ == direction; | |
| 67 } | |
| 68 | |
| 69 void UpdateTextDirection(base::i18n::TextDirection direction) override {} | |
| 70 | |
| 71 // SimpleMenuModel::Delegate: | |
| 72 bool IsCommandIdChecked(int command_id) const override { return false; } | |
| 73 | |
| 74 bool IsCommandIdEnabled(int command_id) const override { return true; } | |
| 75 | |
| 76 void ExecuteCommand(int command_id, int event_flags) override {} | |
| 77 | |
| 78 protected: | |
| 79 ui::TextServicesContextMenu menu_; | |
| 80 | |
| 81 ui::SimpleMenuModel menu_model_; | |
| 82 | |
| 83 base::i18n::TextDirection text_direction_; | |
| 84 | |
| 85 private: | |
| 86 DISALLOW_COPY_AND_ASSIGN(TextServicesContextMenuTest); | |
| 87 }; | |
| 88 | |
| 89 // Tests to see if the BiDi and Speech menu gets appended. | |
| 90 TEST_F(TextServicesContextMenuTest, AppendMenu) { | |
| 91 menu_model_.AddItem(0, base::ASCIIToUTF16("test item")); | |
| 92 | |
| 93 menu_.AppendToContextMenu(&menu_model_); | |
| 94 menu_.AppendEditableItems(&menu_model_); | |
| 95 | |
| 96 int menu_index = 1; | |
| 97 | |
| 98 // Separator item. | |
| 99 EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); | |
| 100 EXPECT_EQ(MenuModel::ItemType::TYPE_SEPARATOR, | |
| 101 menu_model_.GetTypeAt(menu_index)); | |
| 102 menu_index++; | |
| 103 | |
| 104 // Speech Submenu item. | |
| 105 EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); | |
| 106 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, | |
| 107 menu_model_.GetTypeAt(menu_index)); | |
| 108 MenuModel* speech_menu = menu_model_.GetSubmenuModelAt(menu_index); | |
| 109 ASSERT_TRUE(speech_menu); | |
| 110 menu_index++; | |
| 111 | |
| 112 // Check each item in the Speech submenu. | |
| 113 EXPECT_EQ(speech_menu->GetItemCount(), 2); | |
| 114 EXPECT_EQ(IDS_SPEECH_START_SPEAKING_MAC, speech_menu->GetCommandIdAt(0)); | |
| 115 EXPECT_EQ(IDS_SPEECH_STOP_SPEAKING_MAC, speech_menu->GetCommandIdAt(1)); | |
| 116 | |
| 117 // BiDi Submenu item. | |
| 118 EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); | |
| 119 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, | |
| 120 menu_model_.GetTypeAt(menu_index)); | |
| 121 MenuModel* bidi_menu = menu_model_.GetSubmenuModelAt(menu_index); | |
| 122 ASSERT_TRUE(bidi_menu); | |
| 123 | |
| 124 // Check each item in the BiDi submenu. | |
| 125 EXPECT_EQ(bidi_menu->GetItemCount(), 3); | |
| 126 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, | |
| 127 bidi_menu->GetCommandIdAt(0)); | |
| 128 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, | |
| 129 bidi_menu->GetCommandIdAt(1)); | |
| 130 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, | |
| 131 bidi_menu->GetCommandIdAt(2)); | |
| 132 } | |
| 133 | |
| 134 // Tests to see if the Speech API works | |
| 135 TEST_F(TextServicesContextMenuTest, SpeechApi) { | |
| 136 menu_.SpeakText(kSpeechText); | |
| 137 EXPECT_TRUE(menu_.IsSpeaking()); | |
| 138 menu_.StopSpeaking(); | |
| 139 } | |
| 140 | |
| 141 // Tests to see if the Speech submenu items are correct. | |
| 142 TEST_F(TextServicesContextMenuTest, SpeechSubmenu) { | |
| 143 const int kStartSpeakingIndex = 0; | |
| 144 const int kStopSpeakingIndex = 1; | |
| 145 | |
| 146 menu_.AppendToContextMenu(&menu_model_); | |
| 147 | |
| 148 // Get and check the Speech submenu. | |
| 149 EXPECT_TRUE(menu_model_.IsEnabledAt(0)); | |
| 150 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, menu_model_.GetTypeAt(0)); | |
| 151 MenuModel* speech_menu = menu_model_.GetSubmenuModelAt(0); | |
| 152 ASSERT_TRUE(speech_menu); | |
| 153 | |
| 154 // Check each item in the Speech submenu. | |
| 155 EXPECT_EQ(speech_menu->GetItemCount(), 2); | |
| 156 EXPECT_EQ(IDS_SPEECH_START_SPEAKING_MAC, | |
| 157 speech_menu->GetCommandIdAt(kStartSpeakingIndex)); | |
| 158 EXPECT_EQ(IDS_SPEECH_STOP_SPEAKING_MAC, | |
| 159 speech_menu->GetCommandIdAt(kStopSpeakingIndex)); | |
| 160 EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); | |
| 161 EXPECT_FALSE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); | |
| 162 | |
| 163 menu_.SpeakText(kSpeechText); | |
| 164 EXPECT_TRUE(menu_.IsSpeaking()); | |
| 165 | |
| 166 EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); | |
| 167 EXPECT_TRUE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); | |
| 168 | |
| 169 menu_.StopSpeaking(); | |
| 170 EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); | |
| 171 EXPECT_FALSE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); | |
| 172 } | |
| 173 | |
| 174 // Tests to see if the BiDi submenu items are correct. | |
| 175 TEST_F(TextServicesContextMenuTest, BiDiSubmenu) { | |
| 176 const int kTextDirectionDefaultIndex = 0; | |
| 177 const int kTextDirectionLTRIndex = 1; | |
| 178 const int kTextDirectionRTLIndex = 2; | |
| 179 | |
| 180 menu_.AppendEditableItems(&menu_model_); | |
| 181 | |
| 182 // Get and check the BiDi Submenu. | |
| 183 EXPECT_TRUE(menu_model_.IsEnabledAt(0)); | |
| 184 EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, menu_model_.GetTypeAt(0)); | |
| 185 MenuModel* bidi_menu = menu_model_.GetSubmenuModelAt(0); | |
| 186 ASSERT_TRUE(bidi_menu); | |
| 187 | |
| 188 // Check each item in the BiDi submenu. | |
| 189 EXPECT_EQ(bidi_menu->GetItemCount(), 3); | |
| 190 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, | |
| 191 bidi_menu->GetCommandIdAt(kTextDirectionDefaultIndex)); | |
| 192 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, | |
| 193 bidi_menu->GetCommandIdAt(kTextDirectionLTRIndex)); | |
| 194 EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, | |
| 195 bidi_menu->GetCommandIdAt(kTextDirectionRTLIndex)); | |
| 196 | |
| 197 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | |
| 198 EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); | |
| 199 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); | |
| 200 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); | |
| 201 | |
| 202 text_direction_ = base::i18n::LEFT_TO_RIGHT; | |
| 203 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); | |
| 204 EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); | |
| 205 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); | |
| 206 | |
| 207 text_direction_ = base::i18n::RIGHT_TO_LEFT; | |
| 208 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); | |
| 209 EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); | |
| 210 EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); | |
| 211 } | |
| 212 | |
| 213 } // namespace | |
| OLD | NEW |