Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/translate/core/browser/translate_ui_delegate.h" | |
| 6 | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "components/infobars/core/infobar.h" | |
| 14 #include "components/pref_registry/testing_pref_service_syncable.h" | |
| 15 #include "components/translate/core/browser/translate_client.h" | |
| 16 #include "components/translate/core/browser/translate_driver.h" | |
| 17 #include "components/translate/core/browser/translate_infobar_delegate.h" | |
| 18 #include "components/translate/core/browser/translate_manager.h" | |
| 19 #include "components/translate/core/browser/translate_prefs.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 #include "url/gurl.h" | |
| 23 | |
| 24 using testing::Return; | |
| 25 | |
| 26 namespace translate { | |
| 27 | |
| 28 class MockTranslateDriver : public TranslateDriver { | |
|
groby-ooo-7-16
2016/01/28 21:17:28
There's already a MockTranslateDriver in component
ftang
2016/01/28 22:37:21
Those are not using gmock. Do we have a directory
groby-ooo-7-16
2016/01/29 20:09:16
We usually place them right next to the header. So
| |
| 29 public: | |
| 30 MOCK_METHOD0(IsLinkNavigation, bool()); | |
| 31 MOCK_METHOD0(OnTranslateEnabledChanged, void()); | |
| 32 MOCK_METHOD0(OnIsPageTranslatedChanged, void()); | |
| 33 MOCK_METHOD4(TranslatePage, void(int, const std::string&, | |
| 34 const std::string&, const std::string&)); | |
| 35 MOCK_METHOD1(RevertTranslation, void(int)); | |
| 36 MOCK_METHOD0(IsOffTheRecord, bool()); | |
| 37 MOCK_METHOD0(GetContentsMimeType, const std::string&()); | |
| 38 MOCK_METHOD0(GetLastCommittedURL, const GURL&()); | |
| 39 MOCK_METHOD0(GetVisibleURL, const GURL&()); | |
| 40 MOCK_METHOD0(HasCurrentPage, bool()); | |
| 41 MOCK_METHOD1(OpenUrlInNewTab, void(const GURL&)); | |
| 42 }; | |
| 43 | |
| 44 class MockTranslateClient : public TranslateClient { | |
| 45 public: | |
| 46 MOCK_METHOD0(GetTranslateDriver, TranslateDriver*()); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
I'd have the mock take a driver in its ctor - and
ftang
2016/01/28 22:37:22
Acknowledged.
| |
| 47 MOCK_METHOD0(GetPrefs, PrefService*()); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
If you rely on prefs in any way, shouldn't this re
ftang
2016/01/28 22:37:21
Acknowledged.
| |
| 48 // MOCK_METHOD cannot mock a scoped_ptr argument. | |
| 49 MOCK_METHOD0(GetTranslatePrefsMock, TranslatePrefs*()); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
I'm not sure why you're using a MOCK_METHOD here -
ftang
2016/01/28 22:37:21
gmock cannot mock scoped_ptr<class> so I follow so
groby-ooo-7-16
2016/01/29 20:09:16
Hm - is that suggested in Chrome, or by gmock? We
ftang
2016/02/02 00:37:50
Done.
| |
| 50 scoped_ptr<TranslatePrefs> GetTranslatePrefs() { | |
| 51 return scoped_ptr<TranslatePrefs>(GetTranslatePrefsMock()); | |
| 52 } | |
| 53 MOCK_METHOD0(GetTranslateAcceptLanguages, TranslateAcceptLanguages*()); | |
| 54 MOCK_CONST_METHOD0(GetInfobarIconID, int()); | |
| 55 | |
| 56 MOCK_CONST_METHOD1(CreateInfoBarMock, | |
| 57 infobars::InfoBar*(TranslateInfoBarDelegate*)); | |
| 58 scoped_ptr<infobars::InfoBar> CreateInfoBar( | |
| 59 scoped_ptr<TranslateInfoBarDelegate> delegate) const { | |
| 60 return scoped_ptr<infobars::InfoBar>( | |
| 61 CreateInfoBarMock(delegate.get())); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
You want to do new TranslateInfoBar(std::move(dele
ftang
2016/01/28 22:37:21
Acknowledged.
| |
| 62 } | |
| 63 | |
| 64 MOCK_METHOD5(ShowTranslateUI, void(translate::TranslateStep, | |
| 65 const std::string&, | |
| 66 const std::string&, | |
| 67 TranslateErrors::Type, | |
| 68 bool)); | |
| 69 MOCK_METHOD1(IsTranslatableURL, bool(const GURL&)); | |
| 70 MOCK_METHOD1(ShowReportLanguageDetectionErrorUI, void(const GURL&)); | |
| 71 }; | |
| 72 | |
| 73 class TranslateUIDelegateTest : public testing::Test { | |
| 74 public: | |
| 75 TranslateUIDelegateTest() : testing::Test() {} | |
| 76 | |
| 77 void SetUp() override { | |
| 78 pref_service_.reset(new user_prefs::TestingPrefServiceSyncable()); | |
| 79 #if defined(OS_CHROMEOS) | |
| 80 const char* preferred_languages_prefs = | |
| 81 "settings.language.preferred_languages"; | |
| 82 #else | |
| 83 const char* preferred_languages_prefs = NULL; | |
|
groby-ooo-7-16
2016/01/28 21:17:28
I think you can always just assume NULL for this.
ftang
2016/01/28 22:37:21
but if I always assume NULL here. Won't the test b
groby-ooo-7-16
2016/01/29 20:09:16
You're right, it will. I forgot TranslatePrefs DCH
| |
| 84 #endif | |
| 85 | |
| 86 prefs_ = new TranslatePrefs(pref_service_.get(), | |
| 87 "intl.accept_languages", preferred_languages_prefs); | |
| 88 TranslatePrefs::RegisterProfilePrefs(pref_service_->registry()); | |
| 89 | |
| 90 EXPECT_CALL(client_, GetTranslateDriver()) | |
| 91 .WillRepeatedly(Return(&driver_)); | |
| 92 EXPECT_CALL(client_, GetTranslatePrefsMock()) | |
| 93 .WillRepeatedly(Return(prefs_)); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
That won't work - when the first return value is o
ftang
2016/01/28 22:37:21
Acknowledged.
| |
| 94 | |
| 95 manager_.reset(new TranslateManager(&client_, "hi")); | |
| 96 manager_->GetLanguageState().set_translation_declined(false); | |
| 97 | |
| 98 delegate_.reset(new TranslateUIDelegate( | |
| 99 manager_->GetWeakPtr(), "ar", "fr")); | |
| 100 | |
| 101 for (int i = 0; i < 10; i++) { | |
| 102 prefs_->IncrementTranslationAcceptedCount("ar"); | |
| 103 } | |
| 104 prefs_->ResetTranslationDeniedCount("ar"); | |
| 105 prefs_->IncrementTranslationDeniedCount("ar"); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
Are you sure this should be in SetUp? It seems set
ftang
2016/01/28 22:37:22
Acknowledged.
| |
| 106 | |
| 107 ASSERT_FALSE(prefs_->IsTooOftenDenied("ar")); | |
| 108 } | |
| 109 | |
| 110 void TearDown() override { | |
|
groby-ooo-7-16
2016/01/28 21:17:28
No need to override if it does nothing.
ftang
2016/01/28 22:37:22
Acknowledged.
| |
| 111 } | |
| 112 | |
| 113 MockTranslateDriver driver_; | |
| 114 MockTranslateClient client_; | |
| 115 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; | |
| 116 TranslatePrefs* prefs_; | |
|
groby-ooo-7-16
2016/01/28 21:17:28
You want this a scoped_ptr, I think. Otherwise, yo
ftang
2016/01/28 22:37:21
Acknowledged.
| |
| 117 scoped_ptr<TranslateManager> manager_; | |
| 118 scoped_ptr<TranslateUIDelegate> delegate_; | |
| 119 | |
| 120 private: | |
| 121 DISALLOW_COPY_AND_ASSIGN(TranslateUIDelegateTest); | |
| 122 }; | |
| 123 | |
| 124 | |
| 125 TEST_F(TranslateUIDelegateTest, CheckDeclinedFalse) { | |
| 126 int accepted_count = prefs_->GetTranslationAcceptedCount("ar"); | |
| 127 int denied_count = prefs_->GetTranslationDeniedCount("ar"); | |
| 128 | |
| 129 delegate_->TranslationDeclined(false); | |
| 130 | |
| 131 ASSERT_EQ(accepted_count, prefs_->GetTranslationAcceptedCount("ar")); | |
|
groby-ooo-7-16
2016/01/28 21:17:28
You probably want EXPECT_EQ/EXPECT_FALSE. No need
ftang
2016/01/28 22:37:21
Acknowledged.
| |
| 132 ASSERT_EQ(denied_count, prefs_->GetTranslationDeniedCount("ar")); | |
| 133 ASSERT_FALSE(prefs_->IsTooOftenDenied("ar")); | |
| 134 ASSERT_FALSE(manager_->GetLanguageState().translation_declined()); | |
| 135 } | |
| 136 | |
| 137 TEST_F(TranslateUIDelegateTest, CheckDeclinedTrue) { | |
| 138 int denied_count = prefs_->GetTranslationDeniedCount("ar"); | |
| 139 | |
| 140 EXPECT_CALL(driver_, IsOffTheRecord()) | |
| 141 .WillOnce(Return(false)); | |
| 142 | |
| 143 delegate_->TranslationDeclined(true); | |
| 144 | |
| 145 ASSERT_EQ(0, prefs_->GetTranslationAcceptedCount("ar")); | |
| 146 ASSERT_EQ(denied_count + 1, prefs_->GetTranslationDeniedCount("ar")); | |
| 147 ASSERT_TRUE(manager_->GetLanguageState().translation_declined()); | |
| 148 } | |
| 149 | |
| 150 // TODO(ftang) Currently this file only test TranslationDeclined(), we | |
|
groby-ooo-7-16
2016/01/28 21:17:28
You sure you want to take this on as your own TODO
ftang
2016/01/28 22:37:22
yes
groby-ooo-7-16
2016/01/29 20:09:16
Thank you for volunteering, then!
ftang
2016/02/02 00:37:50
Acknowledged.
| |
| 151 // need to add the test for other functions soon to increase the test | |
| 152 // coverage. | |
| 153 | |
| 154 } // namespace translate | |
| OLD | NEW |