Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: ios/chrome/browser/ui/settings/voicesearch_collection_view_controller_unittest.mm

Issue 2587023002: Upstream Chrome on iOS source code [8/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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 "ios/chrome/browser/ui/settings/voicesearch_collection_view_controller.h "
6
7 #include <memory>
8
9 #include "base/compiler_specific.h"
10 #include "base/files/file_path.h"
11 #include "base/mac/foundation_util.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "components/prefs/pref_member.h"
16 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/testing_pref_service.h"
18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item .h"
19 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
20 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h "
21 #include "ios/chrome/browser/voice/speech_input_locale_config_impl.h"
22 #include "ios/public/provider/chrome/browser/voice/voice_search_prefs.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/gtest_mac.h"
25 #include "testing/platform_test.h"
26 #include "ui/base/l10n/l10n_util_mac.h"
27
28 namespace {
29
30 class VoicesearchCollectionViewControllerTest
31 : public CollectionViewControllerTest {
32 protected:
33 void SetUp() override {
34 CollectionViewControllerTest::SetUp();
35 pref_service_ = CreateLocalState();
36 }
37
38 CollectionViewController* NewController() override NS_RETURNS_RETAINED {
39 return [[VoicesearchCollectionViewController alloc]
40 initWithPrefs:pref_service_.get()];
41 }
42
43 std::unique_ptr<PrefService> CreateLocalState() {
44 TestingPrefServiceSimple* prefs = new TestingPrefServiceSimple();
45 PrefRegistrySimple* registry = prefs->registry();
46 registry->RegisterBooleanPref(prefs::kVoiceSearchTTS, false);
47 registry->RegisterStringPref(prefs::kVoiceSearchLocale, "en-US");
48 return std::unique_ptr<PrefService>(prefs);
49 }
50
51 CollectionViewSwitchCell* GetSwitchCell() {
52 return base::mac::ObjCCastStrict<CollectionViewSwitchCell>(
53 [controller().collectionView
54 cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0
55 inSection:0]]);
56 }
57
58 base::MessageLoopForUI message_loop_;
59 std::unique_ptr<PrefService> pref_service_;
60 };
61
62 TEST_F(VoicesearchCollectionViewControllerTest, NumberOfSectionsAndItems) {
63 CreateController();
64 CheckController();
65
66 EXPECT_EQ(2, NumberOfSections());
67 EXPECT_EQ(1, NumberOfItemsInSection(0));
68 const std::vector<voice::SpeechInputLocale>& locales =
69 voice::SpeechInputLocaleConfig::GetInstance()->GetAvailableLocales();
70 // Add one to the available locale list size to account for the default locale
71 // preference.
72 EXPECT_EQ(locales.size() + 1,
73 static_cast<unsigned int>(NumberOfItemsInSection(1)));
74 }
75
76 // Taps the last item in the languages list to ensure that the app does not
77 // crash. This tests for a regression in crbug.com/661358.
78 TEST_F(VoicesearchCollectionViewControllerTest, TapTheLastItemInTheList) {
79 CreateController();
80
81 const std::vector<voice::SpeechInputLocale>& locales =
82 voice::SpeechInputLocaleConfig::GetInstance()->GetAvailableLocales();
83 // Add one to the available locale list size to account for the default locale
84 // preference.
85 ASSERT_EQ(locales.size() + 1,
86 static_cast<unsigned int>(NumberOfItemsInSection(1)));
87
88 // Simulate a tap on the last item in the list.
89 [controller() collectionView:[controller() collectionView]
90 didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:locales.size()
91 inSection:1]];
92 }
93
94 TEST_F(VoicesearchCollectionViewControllerTest,
95 TestModel_TextToSpeechOff_TTSSupported) {
96 CreateController();
97 CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0);
98 EXPECT_FALSE(switchItem.isOn);
99 EXPECT_TRUE(switchItem.isEnabled);
100 }
101
102 TEST_F(VoicesearchCollectionViewControllerTest,
103 TestModel_TextToSpeechOn_TTSSupported) {
104 // Enable the global TTS setting.
105 BooleanPrefMember textToSpeechEnabled;
106 textToSpeechEnabled.Init(prefs::kVoiceSearchTTS, pref_service_.get());
107 textToSpeechEnabled.SetValue(true);
108
109 CreateController();
110 CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0);
111 EXPECT_TRUE(switchItem.isOn);
112 EXPECT_TRUE(switchItem.isEnabled);
113 }
114
115 TEST_F(VoicesearchCollectionViewControllerTest,
116 TestModel_TextToSpeechOff_TTSNotSupported) {
117 // Set current language to a language that doesn't support TTS.
118 StringPrefMember selectedLanguage;
119 selectedLanguage.Init(prefs::kVoiceSearchLocale, pref_service_.get());
120 selectedLanguage.SetValue("af-ZA");
121
122 CreateController();
123 CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0);
124 EXPECT_FALSE(switchItem.isOn);
125 EXPECT_FALSE(switchItem.isEnabled);
126 }
127
128 TEST_F(VoicesearchCollectionViewControllerTest,
129 TestModel_TextToSpeechOn_TTSNotSupported) {
130 // Set current language to a language that doesn't support TTS.
131 StringPrefMember selectedLanguage;
132 selectedLanguage.Init(prefs::kVoiceSearchLocale, pref_service_.get());
133 selectedLanguage.SetValue("af-ZA");
134
135 // Enable the global TTS setting.
136 BooleanPrefMember textToSpeechEnabled;
137 textToSpeechEnabled.Init(prefs::kVoiceSearchTTS, pref_service_.get());
138 textToSpeechEnabled.SetValue(true);
139
140 CreateController();
141 CollectionViewSwitchItem* switchItem = GetCollectionViewItem(0, 0);
142 EXPECT_TRUE(switchItem.isOn);
143 EXPECT_FALSE(switchItem.isEnabled);
144 }
145
146 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698