Chromium Code Reviews| Index: chrome/browser/chromeos/net/network_pref_state_observer_unittest.cc |
| diff --git a/chrome/browser/chromeos/net/network_pref_state_observer_unittest.cc b/chrome/browser/chromeos/net/network_pref_state_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3a64c5131b3fde731dbd7e77f683c493b754edf9 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/net/network_pref_state_observer_unittest.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/net/network_pref_state_observer.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/macros.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| +#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/test/base/testing_browser_process.h" |
| +#include "chrome/test/base/testing_profile_manager.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/shill_device_client.h" |
| +#include "chromeos/dbus/shill_service_client.h" |
| +#include "chromeos/network/network_handler.h" |
| +#include "chromeos/network/proxy/ui_proxy_config.h" |
| +#include "chromeos/network/proxy/ui_proxy_config_service.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/proxy_config/proxy_config_pref_names.h" |
| +#include "components/proxy_config/proxy_prefs.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/cros_system_api/dbus/service_constants.h" |
| + |
| +namespace { |
| +const char kUserId[] = "test@example.com"; |
| +const char kNetworkId[] = "test_wifi_guid"; |
| +} |
| + |
| +namespace chromeos { |
| +namespace test { |
|
James Cook
2016/11/01 23:02:04
optional nit: I don't usually put the tests themse
stevenjb
2016/11/01 23:55:11
Done.
|
| + |
| +class NetworkPrefStateObserverTest : public testing::Test { |
| + public: |
| + NetworkPrefStateObserverTest() |
| + : fake_user_manager_(new chromeos::FakeChromeUserManager), |
| + user_manager_enabler_(fake_user_manager_), |
| + profile_manager_(TestingBrowserProcess::GetGlobal()) {} |
| + ~NetworkPrefStateObserverTest() override {} |
| + |
| + void SetUp() override { |
| + testing::Test::SetUp(); |
| + DBusThreadManager::Initialize(); |
| + SetupDefaultShillState(); |
| + NetworkHandler::Initialize(); |
| + base::RunLoop().RunUntilIdle(); |
| + ASSERT_TRUE(profile_manager_.SetUp()); |
| + network_pref_state_observer_.reset(new NetworkPrefStateObserver); |
| + } |
| + |
| + void TearDown() override { |
| + network_pref_state_observer_.reset(); |
| + NetworkHandler::Shutdown(); |
| + DBusThreadManager::Shutdown(); |
| + testing::Test::TearDown(); |
| + } |
| + |
| + protected: |
| + void SetupDefaultShillState() { |
| + base::RunLoop().RunUntilIdle(); |
|
James Cook
2016/11/01 23:02:04
It reads kinda weird to do this RunUntilIdle here.
stevenjb
2016/11/01 23:55:11
Removed.
|
| + ShillDeviceClient::TestInterface* device_test = |
| + DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); |
| + device_test->ClearDevices(); |
| + device_test->AddDevice("/device/stub_wifi_device1", shill::kTypeWifi, |
| + "stub_wifi_device1"); |
| + |
| + ShillServiceClient::TestInterface* service_test = |
| + DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| + service_test->ClearServices(); |
| + // Create a wifi network and set to online. |
| + service_test->AddService("/service/wifi1", kNetworkId, "wifi1", |
| + shill::kTypeWifi, shill::kStateOnline, |
| + true /* add_to_visible */); |
| + base::RunLoop().RunUntilIdle(); |
| + } |
| + |
| + Profile* LoginAndReturnProfile() { |
| + fake_user_manager_->AddUser(AccountId::FromUserEmail(kUserId)); |
| + Profile* profile = profile_manager_.CreateTestingProfile(kUserId); |
| + content::NotificationService::current()->Notify( |
| + chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
| + content::NotificationService::AllSources(), |
| + content::Details<Profile>(profile)); |
| + base::RunLoop().RunUntilIdle(); |
| + return profile; |
| + } |
| + |
| + protected: |
|
James Cook
2016/11/01 23:02:04
not needed. or make the above helper methods publi
stevenjb
2016/11/01 23:55:11
Done.
|
| + content::TestBrowserThreadBundle thread_bundle_; |
| + FakeChromeUserManager* fake_user_manager_; |
| + chromeos::ScopedUserManagerEnabler user_manager_enabler_; |
| + TestingProfileManager profile_manager_; |
| + std::unique_ptr<NetworkPrefStateObserver> network_pref_state_observer_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(NetworkPrefStateObserverTest); |
| +}; |
| + |
| +TEST_F(NetworkPrefStateObserverTest, LoginUser) { |
| + UIProxyConfig ui_proxy_config; |
| + |
| + // UIProxyConfigService should exist with device PrefService. |
| + UIProxyConfigService* device_ui_proxy_config_service = |
| + NetworkHandler::Get()->ui_proxy_config_service(); |
| + ASSERT_TRUE(device_ui_proxy_config_service); |
| + // Default mode for device prefs should be MODE_DIRECT. |
| + device_ui_proxy_config_service->GetProxyConfig(kNetworkId, &ui_proxy_config); |
| + EXPECT_EQ(UIProxyConfig::MODE_DIRECT, ui_proxy_config.mode); |
| + |
| + Profile* profile = LoginAndReturnProfile(); |
| + |
| + // New UIProxyConfigService should be created with a profile PrefService. |
| + UIProxyConfigService* profile_ui_proxy_config_service = |
| + NetworkHandler::Get()->ui_proxy_config_service(); |
| + ASSERT_TRUE(profile_ui_proxy_config_service); |
| + ASSERT_NE(device_ui_proxy_config_service, profile_ui_proxy_config_service); |
| + // Mode should still be MODE_DIRECT. |
| + profile_ui_proxy_config_service->GetProxyConfig(kNetworkId, &ui_proxy_config); |
| + EXPECT_EQ(UIProxyConfig::MODE_DIRECT, ui_proxy_config.mode); |
| + |
| + // Set the profile pref to PAC script mode. |
| + std::unique_ptr<base::DictionaryValue> proxy_config( |
| + base::MakeUnique<base::DictionaryValue>()); |
| + proxy_config->SetString("mode", ProxyPrefs::kPacScriptProxyModeName); |
| + proxy_config->SetString("pac_url", "http://proxy"); |
| + profile->GetPrefs()->Set(proxy_config::prefs::kProxy, *proxy_config.get()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Mode should now be MODE_PAC_SCRIPT. |
| + NetworkHandler::Get()->ui_proxy_config_service()->GetProxyConfig( |
| + kNetworkId, &ui_proxy_config); |
| + EXPECT_EQ(UIProxyConfig::MODE_PAC_SCRIPT, ui_proxy_config.mode); |
| +} |
|
James Cook
2016/11/01 23:02:04
Thanks for writing a nice test.
stevenjb
2016/11/01 23:55:11
Acknowledged.
|
| + |
| +} // namespace test |
| +} // namespace chromeos |