Index: ios/chrome/browser/ui/settings/voicesearch_collection_view_controller_unittest.mm |
diff --git a/ios/chrome/browser/ui/settings/voicesearch_collection_view_controller_unittest.mm b/ios/chrome/browser/ui/settings/voicesearch_collection_view_controller_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..27b93524c1f06cc820ff4a781e511cd6d840bbf5 |
--- /dev/null |
+++ b/ios/chrome/browser/ui/settings/voicesearch_collection_view_controller_unittest.mm |
@@ -0,0 +1,146 @@ |
+// 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. |
+ |
+#import "ios/chrome/browser/ui/settings/voicesearch_collection_view_controller.h" |
+ |
+#include <memory> |
+ |
+#include "base/compiler_specific.h" |
+#include "base/files/file_path.h" |
+#include "base/mac/foundation_util.h" |
+#include "base/mac/scoped_nsobject.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "components/prefs/pref_member.h" |
+#include "components/prefs/pref_registry_simple.h" |
+#include "components/prefs/testing_pref_service.h" |
+#import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h" |
+#import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
+#import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h" |
+#include "ios/chrome/browser/voice/speech_input_locale_config_impl.h" |
+#include "ios/public/provider/chrome/browser/voice/voice_search_prefs.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "testing/gtest_mac.h" |
+#include "testing/platform_test.h" |
+#include "ui/base/l10n/l10n_util_mac.h" |
+ |
+namespace { |
+ |
+class VoicesearchCollectionViewControllerTest |
+ : public CollectionViewControllerTest { |
+ protected: |
+ void SetUp() override { |
+ CollectionViewControllerTest::SetUp(); |
+ pref_service_ = CreateLocalState(); |
+ } |
+ |
+ CollectionViewController* NewController() override NS_RETURNS_RETAINED { |
+ return [[VoicesearchCollectionViewController alloc] |
+ initWithPrefs:pref_service_.get()]; |
+ } |
+ |
+ std::unique_ptr<PrefService> CreateLocalState() { |
+ TestingPrefServiceSimple* prefs = new TestingPrefServiceSimple(); |
+ PrefRegistrySimple* registry = prefs->registry(); |
+ registry->RegisterBooleanPref(prefs::kVoiceSearchTTS, false); |
+ registry->RegisterStringPref(prefs::kVoiceSearchLocale, "en-US"); |
+ return std::unique_ptr<PrefService>(prefs); |
+ } |
+ |
+ CollectionViewSwitchCell* GetSwitchCell() { |
+ return base::mac::ObjCCastStrict<CollectionViewSwitchCell>( |
+ [controller().collectionView |
+ cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 |
+ inSection:0]]); |
+ } |
+ |
+ base::MessageLoopForUI message_loop_; |
+ std::unique_ptr<PrefService> pref_service_; |
+}; |
+ |
+TEST_F(VoicesearchCollectionViewControllerTest, NumberOfSectionsAndItems) { |
+ CreateController(); |
+ CheckController(); |
+ |
+ EXPECT_EQ(2, NumberOfSections()); |
+ EXPECT_EQ(1, NumberOfItemsInSection(0)); |
+ const std::vector<voice::SpeechInputLocale>& locales = |
+ voice::SpeechInputLocaleConfig::GetInstance()->GetAvailableLocales(); |
+ // Add one to the available locale list size to account for the default locale |
+ // preference. |
+ EXPECT_EQ(locales.size() + 1, |
+ static_cast<unsigned int>(NumberOfItemsInSection(1))); |
+} |
+ |
+// Taps the last item in the languages list to ensure that the app does not |
+// crash. This tests for a regression in crbug.com/661358. |
+TEST_F(VoicesearchCollectionViewControllerTest, TapTheLastItemInTheList) { |
+ CreateController(); |
+ |
+ const std::vector<voice::SpeechInputLocale>& locales = |
+ voice::SpeechInputLocaleConfig::GetInstance()->GetAvailableLocales(); |
+ // Add one to the available locale list size to account for the default locale |
+ // preference. |
+ ASSERT_EQ(locales.size() + 1, |
+ static_cast<unsigned int>(NumberOfItemsInSection(1))); |
+ |
+ // Simulate a tap on the last item in the list. |
+ [controller() collectionView:[controller() collectionView] |
+ didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:locales.size() |
+ inSection:1]]; |
+} |
+ |
+TEST_F(VoicesearchCollectionViewControllerTest, |
+ TestModel_TextToSpeechOff_TTSSupported) { |
+ CreateController(); |
+ CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0); |
+ EXPECT_FALSE(switchItem.isOn); |
+ EXPECT_TRUE(switchItem.isEnabled); |
+} |
+ |
+TEST_F(VoicesearchCollectionViewControllerTest, |
+ TestModel_TextToSpeechOn_TTSSupported) { |
+ // Enable the global TTS setting. |
+ BooleanPrefMember textToSpeechEnabled; |
+ textToSpeechEnabled.Init(prefs::kVoiceSearchTTS, pref_service_.get()); |
+ textToSpeechEnabled.SetValue(true); |
+ |
+ CreateController(); |
+ CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0); |
+ EXPECT_TRUE(switchItem.isOn); |
+ EXPECT_TRUE(switchItem.isEnabled); |
+} |
+ |
+TEST_F(VoicesearchCollectionViewControllerTest, |
+ TestModel_TextToSpeechOff_TTSNotSupported) { |
+ // Set current language to a language that doesn't support TTS. |
+ StringPrefMember selectedLanguage; |
+ selectedLanguage.Init(prefs::kVoiceSearchLocale, pref_service_.get()); |
+ selectedLanguage.SetValue("af-ZA"); |
+ |
+ CreateController(); |
+ CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0); |
+ EXPECT_FALSE(switchItem.isOn); |
+ EXPECT_FALSE(switchItem.isEnabled); |
+} |
+ |
+TEST_F(VoicesearchCollectionViewControllerTest, |
+ TestModel_TextToSpeechOn_TTSNotSupported) { |
+ // Set current language to a language that doesn't support TTS. |
+ StringPrefMember selectedLanguage; |
+ selectedLanguage.Init(prefs::kVoiceSearchLocale, pref_service_.get()); |
+ selectedLanguage.SetValue("af-ZA"); |
+ |
+ // Enable the global TTS setting. |
+ BooleanPrefMember textToSpeechEnabled; |
+ textToSpeechEnabled.Init(prefs::kVoiceSearchTTS, pref_service_.get()); |
+ textToSpeechEnabled.SetValue(true); |
+ |
+ CreateController(); |
+ CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0); |
+ EXPECT_TRUE(switchItem.isOn); |
+ EXPECT_FALSE(switchItem.isEnabled); |
+} |
+ |
+} // namespace |