| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/net/network_pref_state_observer.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 13 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/test/base/testing_browser_process.h" |
| 16 #include "chrome/test/base/testing_profile_manager.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/network/network_handler.h" |
| 19 #include "chromeos/network/proxy/ui_proxy_config.h" |
| 20 #include "chromeos/network/proxy/ui_proxy_config_service.h" |
| 21 #include "components/prefs/pref_service.h" |
| 22 #include "components/proxy_config/proxy_config_pref_names.h" |
| 23 #include "components/proxy_config/proxy_prefs.h" |
| 24 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_source.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 30 |
| 31 namespace { |
| 32 const char kUserId[] = "test@example.com"; |
| 33 const char kNetworkId[] = "wifi1_guid"; // Matches FakeShillManagerClient |
| 34 } |
| 35 |
| 36 namespace chromeos { |
| 37 |
| 38 class NetworkPrefStateObserverTest : public testing::Test { |
| 39 public: |
| 40 NetworkPrefStateObserverTest() |
| 41 : fake_user_manager_(new chromeos::FakeChromeUserManager), |
| 42 user_manager_enabler_(fake_user_manager_), |
| 43 profile_manager_(TestingBrowserProcess::GetGlobal()) {} |
| 44 ~NetworkPrefStateObserverTest() override {} |
| 45 |
| 46 void SetUp() override { |
| 47 testing::Test::SetUp(); |
| 48 DBusThreadManager::Initialize(); |
| 49 NetworkHandler::Initialize(); |
| 50 base::RunLoop().RunUntilIdle(); |
| 51 ASSERT_TRUE(profile_manager_.SetUp()); |
| 52 network_pref_state_observer_.reset(new NetworkPrefStateObserver); |
| 53 } |
| 54 |
| 55 void TearDown() override { |
| 56 network_pref_state_observer_.reset(); |
| 57 NetworkHandler::Shutdown(); |
| 58 DBusThreadManager::Shutdown(); |
| 59 testing::Test::TearDown(); |
| 60 } |
| 61 |
| 62 protected: |
| 63 Profile* LoginAndReturnProfile() { |
| 64 fake_user_manager_->AddUser(AccountId::FromUserEmail(kUserId)); |
| 65 Profile* profile = profile_manager_.CreateTestingProfile(kUserId); |
| 66 content::NotificationService::current()->Notify( |
| 67 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
| 68 content::NotificationService::AllSources(), |
| 69 content::Details<Profile>(profile)); |
| 70 base::RunLoop().RunUntilIdle(); |
| 71 return profile; |
| 72 } |
| 73 |
| 74 content::TestBrowserThreadBundle thread_bundle_; |
| 75 FakeChromeUserManager* fake_user_manager_; |
| 76 chromeos::ScopedUserManagerEnabler user_manager_enabler_; |
| 77 TestingProfileManager profile_manager_; |
| 78 std::unique_ptr<NetworkPrefStateObserver> network_pref_state_observer_; |
| 79 |
| 80 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(NetworkPrefStateObserverTest); |
| 82 }; |
| 83 |
| 84 TEST_F(NetworkPrefStateObserverTest, LoginUser) { |
| 85 UIProxyConfig ui_proxy_config; |
| 86 |
| 87 // UIProxyConfigService should exist with device PrefService. |
| 88 UIProxyConfigService* device_ui_proxy_config_service = |
| 89 NetworkHandler::Get()->ui_proxy_config_service(); |
| 90 ASSERT_TRUE(device_ui_proxy_config_service); |
| 91 // Default mode for device prefs should be MODE_DIRECT. |
| 92 device_ui_proxy_config_service->GetProxyConfig(kNetworkId, &ui_proxy_config); |
| 93 EXPECT_EQ(UIProxyConfig::MODE_DIRECT, ui_proxy_config.mode); |
| 94 |
| 95 Profile* profile = LoginAndReturnProfile(); |
| 96 |
| 97 // New UIProxyConfigService should be created with a profile PrefService. |
| 98 UIProxyConfigService* profile_ui_proxy_config_service = |
| 99 NetworkHandler::Get()->ui_proxy_config_service(); |
| 100 ASSERT_TRUE(profile_ui_proxy_config_service); |
| 101 ASSERT_NE(device_ui_proxy_config_service, profile_ui_proxy_config_service); |
| 102 // Mode should still be MODE_DIRECT. |
| 103 profile_ui_proxy_config_service->GetProxyConfig(kNetworkId, &ui_proxy_config); |
| 104 EXPECT_EQ(UIProxyConfig::MODE_DIRECT, ui_proxy_config.mode); |
| 105 |
| 106 // Set the profile pref to PAC script mode. |
| 107 std::unique_ptr<base::DictionaryValue> proxy_config( |
| 108 base::MakeUnique<base::DictionaryValue>()); |
| 109 proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName); |
| 110 proxy_config->SetString("pac_url", "http://proxy"); |
| 111 profile->GetPrefs()->Set(proxy_config::prefs::kProxy, *proxy_config.get()); |
| 112 base::RunLoop().RunUntilIdle(); |
| 113 |
| 114 // Mode should now be MODE_PAC_SCRIPT. |
| 115 NetworkHandler::Get()->ui_proxy_config_service()->GetProxyConfig( |
| 116 kNetworkId, &ui_proxy_config); |
| 117 EXPECT_EQ(UIProxyConfig::MODE_PAC_SCRIPT, ui_proxy_config.mode); |
| 118 } |
| 119 |
| 120 } // namespace chromeos |
| OLD | NEW |