Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 <initializer_list> | |
| 6 #include <map> | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" | |
| 14 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_controller.h" | |
| 17 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_model.h" | |
| 18 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_test_utils.h" | |
| 19 #include "chrome/browser/ui/browser.h" | |
| 20 #include "chrome/browser/ui/test/test_browser_dialog.h" | |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 using safe_browsing::MockProfileResetter; | |
| 28 using safe_browsing::MockSettingsResetPromptConfig; | |
| 29 using testing::_; | |
| 30 using testing::ElementsAre; | |
| 31 using testing::NiceMock; | |
| 32 using testing::Not; | |
| 33 using testing::Return; | |
| 34 using testing::ReturnRef; | |
| 35 | |
| 36 constexpr char kHomepageUrl[] = "http://www.a-homepage.com/some/path"; | |
| 37 constexpr char kSearchUrl[] = "http://www.a-search.com/some/path/?q={%s}"; | |
| 38 constexpr char kStartupUrl1[] = "http://www.a-startup-1.com/some/path"; | |
| 39 constexpr char kStartupUrl2[] = "http://www.a-startup-2.com/some/other/path"; | |
| 40 constexpr char kExtensionId1[] = "abcdefghijklmnopabcdefghijklmnop"; | |
| 41 constexpr char kExtensionId2[] = "bbcdefghijklmnopabcdefghijklmnop"; | |
| 42 constexpr char kExtensionName1[] = "Extensions1"; | |
| 43 constexpr char kExtensionName2[] = "Extensions2"; | |
| 44 | |
| 45 enum SettingType { | |
|
sky
2017/02/23 04:27:31
enum class
alito
2017/02/23 05:37:08
Done.
| |
| 46 DSE, // Default search engine | |
|
sky
2017/02/23 04:27:31
Please spell these out, otherwise it's super crypt
alito
2017/02/23 05:37:08
Done.
| |
| 47 SP, // Startup pages | |
| 48 HP, // Homepage | |
| 49 EXT, // Also disable some extensions | |
| 50 }; | |
| 51 | |
| 52 class MockSettingsResetPromptModel | |
| 53 : public safe_browsing::SettingsResetPromptModel { | |
| 54 public: | |
| 55 // Create a mock model that pretends that the settings passed in via | |
| 56 // |settings_to_reset| require resetting. |settings_to_reset| must not be | |
| 57 // empty or have |EXT| as the only member. | |
| 58 explicit MockSettingsResetPromptModel(Profile* profile, | |
| 59 std::set<SettingType> settings_to_reset) | |
| 60 : SettingsResetPromptModel( | |
| 61 profile, | |
| 62 base::MakeUnique<NiceMock<MockSettingsResetPromptConfig>>(), | |
| 63 base::MakeUnique<ResettableSettingsSnapshot>(profile), | |
| 64 base::MakeUnique<BrandcodedDefaultSettings>(), | |
| 65 base::MakeUnique<NiceMock<MockProfileResetter>>(profile)) { | |
| 66 EXPECT_FALSE(settings_to_reset.empty()); | |
| 67 EXPECT_THAT(settings_to_reset, Not(ElementsAre(EXT))); | |
| 68 | |
| 69 // Set up startup URLs and extensions to be returned by member functions | |
| 70 // based on which settings should require reset. | |
| 71 startup_urls_.push_back(GURL(kStartupUrl1)); | |
| 72 startup_urls_.push_back(GURL(kStartupUrl2)); | |
| 73 if (settings_to_reset.find(SP) != settings_to_reset.end()) | |
| 74 startup_urls_to_reset_ = startup_urls_; | |
| 75 | |
| 76 if (settings_to_reset.find(EXT) != settings_to_reset.end()) { | |
| 77 extensions_ = {{kExtensionId1, safe_browsing::ExtensionInfo( | |
| 78 kExtensionId1, kExtensionName1)}, | |
| 79 {kExtensionId2, safe_browsing::ExtensionInfo( | |
| 80 kExtensionId2, kExtensionName2)}}; | |
| 81 } | |
| 82 | |
| 83 ON_CALL(*this, PerformReset(_)).WillByDefault(Return()); | |
| 84 | |
| 85 ON_CALL(*this, ShouldPromptForReset()) | |
| 86 .WillByDefault(Return(!settings_to_reset.empty())); | |
| 87 | |
| 88 ON_CALL(*this, homepage()).WillByDefault(Return(GURL(kHomepageUrl))); | |
| 89 ON_CALL(*this, homepage_reset_state()) | |
| 90 .WillByDefault(Return(ChooseResetState(settings_to_reset, HP))); | |
| 91 | |
| 92 ON_CALL(*this, default_search()).WillByDefault(Return(GURL(kSearchUrl))); | |
| 93 ON_CALL(*this, default_search_reset_state()) | |
| 94 .WillByDefault(Return(ChooseResetState(settings_to_reset, DSE))); | |
| 95 | |
| 96 ON_CALL(*this, startup_urls()).WillByDefault(ReturnRef(startup_urls_)); | |
| 97 ON_CALL(*this, startup_urls_to_reset()) | |
| 98 .WillByDefault(ReturnRef(startup_urls_to_reset_)); | |
| 99 ON_CALL(*this, startup_urls_reset_state()) | |
| 100 .WillByDefault(Return(ChooseResetState(settings_to_reset, SP))); | |
| 101 | |
| 102 ON_CALL(*this, extensions_to_disable()) | |
| 103 .WillByDefault(ReturnRef(extensions_)); | |
| 104 } | |
| 105 ~MockSettingsResetPromptModel() override {} | |
| 106 | |
| 107 MOCK_CONST_METHOD1(PerformReset, void(const base::Closure&)); | |
| 108 MOCK_CONST_METHOD0(ShouldPromptForReset, bool()); | |
| 109 MOCK_CONST_METHOD0(homepage, GURL()); | |
| 110 MOCK_CONST_METHOD0(homepage_reset_state, ResetState()); | |
| 111 MOCK_CONST_METHOD0(default_search, GURL()); | |
| 112 MOCK_CONST_METHOD0(default_search_reset_state, ResetState()); | |
| 113 MOCK_CONST_METHOD0(startup_urls, const std::vector<GURL>&()); | |
| 114 MOCK_CONST_METHOD0(startup_urls_to_reset, const std::vector<GURL>&()); | |
| 115 MOCK_CONST_METHOD0(startup_urls_reset_state, ResetState()); | |
| 116 MOCK_CONST_METHOD0(extensions_to_disable, const ExtensionMap&()); | |
| 117 | |
| 118 private: | |
| 119 static ResetState ChooseResetState(const std::set<SettingType>& settings, | |
| 120 SettingType setting) { | |
| 121 if (settings.find(setting) != settings.end()) | |
| 122 return RESET_REQUIRED; | |
| 123 return NO_RESET_REQUIRED_DUE_TO_DOMAIN_NOT_MATCHED; | |
| 124 } | |
| 125 | |
| 126 std::vector<GURL> startup_urls_; | |
| 127 std::vector<GURL> startup_urls_to_reset_; | |
| 128 ExtensionMap extensions_; | |
| 129 }; | |
|
sky
2017/02/23 04:27:31
DISALLOW...
alito
2017/02/23 05:37:08
Done.
| |
| 130 | |
| 131 class SettingsResetPromptDialogTest : public DialogBrowserTest { | |
| 132 public: | |
| 133 void ShowDialog(const std::string& name) override { | |
| 134 const std::map<std::string, std::set<SettingType>> name_to_settings = { | |
| 135 {"dse", {DSE}}, | |
| 136 {"sp", {SP}}, | |
| 137 {"hp", {HP}}, | |
| 138 {"dse_sp", {DSE, SP}}, | |
| 139 {"dse_hp", {DSE, HP}}, | |
| 140 {"sp_hp", {SP, HP}}, | |
| 141 {"dse_sp_hp", {DSE, SP, HP}}, | |
| 142 {"dse_ext", {DSE, EXT}}, | |
| 143 {"sp_ext", {SP, EXT}}, | |
| 144 {"hp_ext", {HP, EXT}}, | |
| 145 {"dse_sp_ext", {DSE, SP, EXT}}, | |
| 146 {"dse_hp_ext", {DSE, HP, EXT}}, | |
| 147 {"sp_hp_ext", {SP, HP, EXT}}, | |
| 148 {"dse_sp_hp_ext", {DSE, SP, HP, EXT}}}; | |
| 149 | |
| 150 auto model = base::MakeUnique<NiceMock<MockSettingsResetPromptModel>>( | |
| 151 browser()->profile(), name_to_settings.find(name)->second); | |
| 152 | |
| 153 safe_browsing::SettingsResetPromptController::ShowSettingsResetPrompt( | |
| 154 browser(), | |
| 155 new safe_browsing::SettingsResetPromptController(std::move(model))); | |
| 156 } | |
| 157 }; | |
| 158 | |
| 159 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse) { | |
| 160 RunDialog(); | |
| 161 } | |
| 162 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_sp) { | |
| 163 RunDialog(); | |
| 164 } | |
| 165 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_hp) { | |
| 166 RunDialog(); | |
| 167 } | |
| 168 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse_sp) { | |
| 169 RunDialog(); | |
| 170 } | |
| 171 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse_hp) { | |
| 172 RunDialog(); | |
| 173 } | |
| 174 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_sp_hp) { | |
| 175 RunDialog(); | |
| 176 } | |
| 177 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse_sp_hp) { | |
| 178 RunDialog(); | |
| 179 } | |
| 180 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse_ext) { | |
| 181 RunDialog(); | |
| 182 } | |
| 183 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_sp_ext) { | |
| 184 RunDialog(); | |
| 185 } | |
| 186 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_hp_ext) { | |
| 187 RunDialog(); | |
| 188 } | |
| 189 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse_sp_ext) { | |
| 190 RunDialog(); | |
| 191 } | |
| 192 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_dse_hp_ext) { | |
| 193 RunDialog(); | |
| 194 } | |
| 195 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, InvokeDialog_sp_hp_ext) { | |
| 196 RunDialog(); | |
| 197 } | |
| 198 IN_PROC_BROWSER_TEST_F(SettingsResetPromptDialogTest, | |
| 199 InvokeDialog_dse_sp_hp_ext) { | |
| 200 RunDialog(); | |
| 201 } | |
| 202 | |
| 203 } // namespace | |
| OLD | NEW |