OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/translate_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/message_loop/message_loop.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "components/pref_registry/pref_registry_syncable.h" |
| 14 #include "components/prefs/pref_member.h" |
| 15 #include "components/prefs/pref_service.h" |
| 16 #include "components/strings/grit/components_locale_settings.h" |
| 17 #include "components/sync_preferences/pref_service_mock_factory.h" |
| 18 #include "components/translate/core/browser/translate_prefs.h" |
| 19 #include "components/translate/core/common/translate_pref_names.h" |
| 20 #include "ios/chrome/browser/pref_names.h" |
| 21 #import "ios/chrome/browser/translate/chrome_ios_translate_client.h" |
| 22 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 23 #include "ios/chrome/grit/ios_strings.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "testing/gtest_mac.h" |
| 26 #include "testing/platform_test.h" |
| 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/l10n/l10n_util_mac.h" |
| 29 |
| 30 using user_prefs::PrefRegistrySyncable; |
| 31 |
| 32 namespace { |
| 33 |
| 34 const char kBlacklistedSite[] = "http://blacklistedsite.com"; |
| 35 const char kLanguage1[] = "klingon"; |
| 36 const char kLanguage2[] = "pirate"; |
| 37 |
| 38 class TranslateCollectionViewControllerTest |
| 39 : public CollectionViewControllerTest { |
| 40 protected: |
| 41 void SetUp() override { |
| 42 CollectionViewControllerTest::SetUp(); |
| 43 pref_service_ = CreateLocalState(); |
| 44 } |
| 45 |
| 46 CollectionViewController* NewController() override NS_RETURNS_RETAINED { |
| 47 return [[TranslateCollectionViewController alloc] |
| 48 initWithPrefs:pref_service_.get()]; |
| 49 } |
| 50 |
| 51 std::unique_ptr<PrefService> CreateLocalState() { |
| 52 scoped_refptr<PrefRegistrySyncable> registry = new PrefRegistrySyncable(); |
| 53 registry->RegisterBooleanPref(prefs::kEnableTranslate, false, |
| 54 PrefRegistrySyncable::SYNCABLE_PREF); |
| 55 translate::TranslatePrefs::RegisterProfilePrefs(registry.get()); |
| 56 registry->RegisterStringPref( |
| 57 prefs::kAcceptLanguages, |
| 58 l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES)); |
| 59 base::FilePath path("TranslateCollectionViewControllerTest.pref"); |
| 60 sync_preferences::PrefServiceMockFactory factory; |
| 61 factory.SetUserPrefsFile(path, base::ThreadTaskRunnerHandle::Get().get()); |
| 62 return factory.Create(registry.get()); |
| 63 } |
| 64 |
| 65 base::MessageLoopForUI message_loop_; |
| 66 std::unique_ptr<PrefService> pref_service_; |
| 67 }; |
| 68 |
| 69 TEST_F(TranslateCollectionViewControllerTest, TestModelTranslateOff) { |
| 70 CreateController(); |
| 71 CheckController(); |
| 72 EXPECT_EQ(2, NumberOfSections()); |
| 73 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 74 CheckSwitchCellStateAndTitleWithId(NO, IDS_IOS_TRANSLATE_SETTING, 0, 0); |
| 75 CheckTextCellTitleWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1); |
| 76 } |
| 77 |
| 78 TEST_F(TranslateCollectionViewControllerTest, TestModelTranslateOn) { |
| 79 BooleanPrefMember translateEnabled; |
| 80 translateEnabled.Init(prefs::kEnableTranslate, pref_service_.get()); |
| 81 translateEnabled.SetValue(true); |
| 82 CreateController(); |
| 83 EXPECT_EQ(2, NumberOfSections()); |
| 84 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 85 CheckSwitchCellStateAndTitleWithId(YES, IDS_IOS_TRANSLATE_SETTING, 0, 0); |
| 86 CheckTextCellTitleWithId(IDS_IOS_TRANSLATE_SETTING_RESET, 0, 1); |
| 87 } |
| 88 |
| 89 TEST_F(TranslateCollectionViewControllerTest, TestClearPreferences) { |
| 90 // Set some preferences. |
| 91 std::unique_ptr<translate::TranslatePrefs> translate_prefs( |
| 92 ChromeIOSTranslateClient::CreateTranslatePrefs(pref_service_.get())); |
| 93 translate_prefs->BlacklistSite(kBlacklistedSite); |
| 94 ASSERT_TRUE(translate_prefs->IsSiteBlacklisted(kBlacklistedSite)); |
| 95 translate_prefs->BlockLanguage(kLanguage1); |
| 96 ASSERT_TRUE(translate_prefs->IsBlockedLanguage(kLanguage1)); |
| 97 translate_prefs->WhitelistLanguagePair(kLanguage1, kLanguage2); |
| 98 ASSERT_TRUE( |
| 99 translate_prefs->IsLanguagePairWhitelisted(kLanguage1, kLanguage2)); |
| 100 // Reset the preferences through the UI. |
| 101 CreateController(); |
| 102 TranslateCollectionViewController* controller = |
| 103 static_cast<TranslateCollectionViewController*>(this->controller()); |
| 104 // Simulate a tap on the "reset" item. |
| 105 [controller collectionView:[controller collectionView] |
| 106 didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0]]; |
| 107 // Check that preferences are gone. |
| 108 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(kBlacklistedSite)); |
| 109 EXPECT_FALSE(translate_prefs->IsBlockedLanguage(kLanguage1)); |
| 110 EXPECT_FALSE( |
| 111 translate_prefs->IsLanguagePairWhitelisted(kLanguage1, kLanguage2)); |
| 112 } |
| 113 |
| 114 } // namespace |
OLD | NEW |