Chromium Code Reviews| Index: ui/base/cocoa/text_services_context_menu_unittest.mm |
| diff --git a/ui/base/cocoa/text_services_context_menu_unittest.mm b/ui/base/cocoa/text_services_context_menu_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..015885fa33cffc4f3330557eb4dd22bc092e0c88 |
| --- /dev/null |
| +++ b/ui/base/cocoa/text_services_context_menu_unittest.mm |
| @@ -0,0 +1,180 @@ |
| +// Copyright 2017 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. |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include <memory> |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "testing/platform_test.h" |
| +#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.
|
| +#import "ui/gfx/test/ui_cocoa_test_helper.h" |
| +#include "ui/strings/grit/ui_strings.h" |
| + |
| +namespace ui { |
| + |
| +class TextServicesContextMenuTest : public CocoaTest, |
| + public TextServicesContextMenu::Delegate, |
| + public SimpleMenuModel::Delegate { |
| + public: |
| + TextServicesContextMenuTest() |
| + : menu_(this), |
| + menu_model_(this), |
| + text_direction_(base::i18n::UNKNOWN_DIRECTION) {} |
| + |
| + // TextServicesContextMenu::Delegate: |
| + base::string16 GetSelectedText() const override { return base::string16(); } |
| + |
| + bool IsTextDirectionEnabled( |
| + base::i18n::TextDirection direction) const override { |
| + return true; |
| + } |
| + |
| + bool IsTextDirectionChecked( |
| + base::i18n::TextDirection direction) const override { |
| + return text_direction_ == direction; |
| + } |
| + |
| + void UpdateTextDirection(base::i18n::TextDirection direction) override {} |
| + |
| + // SimpleMenuModel::Delegate: |
| + bool IsCommandIdChecked(int command_id) const override { return false; } |
| + |
| + bool IsCommandIdEnabled(int command_id) const override { return true; } |
| + |
| + void ExecuteCommand(int command_id, int event_flags) override {} |
| + |
| + ui::TextServicesContextMenu menu_; |
|
tapted
2017/02/03 10:14:51
protected: before this
spqchan
2017/02/03 23:32:11
Done.
|
| + |
| + ui::SimpleMenuModel menu_model_; |
| + |
| + base::i18n::TextDirection text_direction_; |
| +}; |
|
tapted
2017/02/03 10:14:51
nit:
private:
DISALLOW_COPY_AND_ASSIGN(..)
spqchan
2017/02/03 23:32:11
Done.
|
| + |
| +// Tests to see if the BiDi and Speech menu gets appended. |
| +TEST_F(TextServicesContextMenuTest, AppendMenu) { |
| + menu_model_.AddItem(0, base::UTF8ToUTF16("test item")); |
| + |
| + menu_.AppendToContextMenu(&menu_model_); |
| + menu_.AppendEditableItems(&menu_model_); |
| + |
| + int menu_index = 1; |
| + |
| + // Separator item. |
| + EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); |
| + EXPECT_EQ(MenuModel::ItemType::TYPE_SEPARATOR, |
| + menu_model_.GetTypeAt(menu_index)); |
| + menu_index++; |
| + |
| + // Speech Submenu item. |
| + EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); |
| + EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, |
| + menu_model_.GetTypeAt(menu_index)); |
| + MenuModel* speech_menu = menu_model_.GetSubmenuModelAt(menu_index); |
| + ASSERT_TRUE(speech_menu); |
| + menu_index++; |
| + |
| + // Check each item in the Speech submenu. |
| + EXPECT_EQ(speech_menu->GetItemCount(), 2); |
| + EXPECT_EQ(IDS_SPEECH_START_SPEAKING_MAC, speech_menu->GetCommandIdAt(0)); |
| + EXPECT_EQ(IDS_SPEECH_STOP_SPEAKING_MAC, speech_menu->GetCommandIdAt(1)); |
| + |
| + // BiDi Submenu item. |
| + EXPECT_TRUE(menu_model_.IsEnabledAt(menu_index)); |
| + EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, |
| + menu_model_.GetTypeAt(menu_index)); |
| + MenuModel* bidi_menu = menu_model_.GetSubmenuModelAt(menu_index); |
| + ASSERT_TRUE(bidi_menu); |
| + |
| + // Check each item in the BiDi submenu. |
| + EXPECT_EQ(bidi_menu->GetItemCount(), 3); |
| + EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, |
| + bidi_menu->GetCommandIdAt(0)); |
| + EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, |
| + bidi_menu->GetCommandIdAt(1)); |
| + EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, |
| + bidi_menu->GetCommandIdAt(2)); |
| +} |
| + |
| +// Tests to see if the Speech API works |
| +TEST_F(TextServicesContextMenuTest, SpeechApi) { |
| + menu_.SpeakText(base::UTF8ToUTF16("boxfish")); |
|
tapted
2017/02/03 10:14:51
nit: ASCIIToUTF16
spqchan
2017/02/03 23:32:11
Done.
|
| + 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.
|
| + menu_.StopSpeaking(); |
| +} |
| + |
| +// Tests to see if the Speech submenu items are correct. |
| +TEST_F(TextServicesContextMenuTest, SpeechSubmenu) { |
| + const int kStartSpeakingIndex = 0; |
| + const int kStopSpeakingIndex = 1; |
| + |
| + menu_.AppendToContextMenu(&menu_model_); |
| + |
| + // Get and check the speech submenu item. |
| + EXPECT_TRUE(menu_model_.IsEnabledAt(0)); |
| + EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, menu_model_.GetTypeAt(0)); |
| + MenuModel* speech_menu = menu_model_.GetSubmenuModelAt(0); |
| + ASSERT_TRUE(speech_menu); |
| + |
| + // Check each item in the Speech submenu. |
| + EXPECT_EQ(speech_menu->GetItemCount(), 2); |
| + EXPECT_EQ(IDS_SPEECH_START_SPEAKING_MAC, |
| + speech_menu->GetCommandIdAt(kStartSpeakingIndex)); |
| + EXPECT_EQ(IDS_SPEECH_STOP_SPEAKING_MAC, |
| + speech_menu->GetCommandIdAt(kStopSpeakingIndex)); |
| + EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); |
| + EXPECT_FALSE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); |
| + |
| + 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
|
| + EXPECT_TRUE(menu_.IsSpeaking()); |
| + |
| + EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); |
| + EXPECT_TRUE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); |
| + |
| + menu_.StopSpeaking(); |
| + EXPECT_TRUE(speech_menu->IsEnabledAt(kStartSpeakingIndex)); |
| + EXPECT_FALSE(speech_menu->IsEnabledAt(kStopSpeakingIndex)); |
| +} |
| + |
| +// Tests to see if the BiDi submenu items are correct. |
| +TEST_F(TextServicesContextMenuTest, BiDiSubmenu) { |
| + const int kTextDirectionDefaultIndex = 0; |
| + const int kTextDirectionLTRIndex = 1; |
| + const int kTextDirectionRTLIndex = 2; |
| + |
| + menu_.AppendEditableItems(&menu_model_); |
| + |
| + // BiDi Submenu item. |
| + EXPECT_TRUE(menu_model_.IsEnabledAt(0)); |
| + EXPECT_EQ(MenuModel::ItemType::TYPE_SUBMENU, menu_model_.GetTypeAt(0)); |
| + MenuModel* bidi_menu = menu_model_.GetSubmenuModelAt(0); |
| + ASSERT_TRUE(bidi_menu); |
| + |
| + // Check each item in the BiDi submenu. |
| + EXPECT_EQ(bidi_menu->GetItemCount(), 3); |
| + EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, |
| + bidi_menu->GetCommandIdAt(kTextDirectionDefaultIndex)); |
| + EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, |
| + bidi_menu->GetCommandIdAt(kTextDirectionLTRIndex)); |
| + EXPECT_EQ(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, |
| + bidi_menu->GetCommandIdAt(kTextDirectionRTLIndex)); |
| + |
| + text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| + EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); |
| + EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); |
| + EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); |
| + |
| + text_direction_ = base::i18n::LEFT_TO_RIGHT; |
| + EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); |
| + EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); |
| + EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); |
| + |
| + text_direction_ = base::i18n::RIGHT_TO_LEFT; |
| + EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionDefaultIndex)); |
| + EXPECT_FALSE(bidi_menu->IsItemCheckedAt(kTextDirectionLTRIndex)); |
| + EXPECT_TRUE(bidi_menu->IsItemCheckedAt(kTextDirectionRTLIndex)); |
| +} |
| + |
| +} // namespace |
|
tapted
2017/02/03 10:14:51
namespace ui
|