Chromium Code Reviews| Index: chrome/browser/chromeos/net/network_throttling_observer_unittest.cc |
| diff --git a/chrome/browser/chromeos/net/network_throttling_observer_unittest.cc b/chrome/browser/chromeos/net/network_throttling_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bdd6f2272a6dfb474266e3016eec8b17c9e5b489 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/net/network_throttling_observer_unittest.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2016 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 <memory> |
| + |
| +#include "base/message_loop/message_loop.h" |
| +#include "chrome/browser/chromeos/net/network_throttling_observer.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/mock_shill_manager_client.h" |
| +#include "chromeos/network/network_state_handler.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/testing_pref_service.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::_; |
| +using testing::Mock; |
| +using testing::NiceMock; |
| + |
| +namespace chromeos { |
| + |
| +namespace test { |
| + |
| +class NetworkThrottlingObserverTest : public ::testing::Test { |
| + public: |
| + NetworkThrottlingObserverTest() : message_loop_(new base::MessageLoop()) { |
| + std::unique_ptr<DBusThreadManagerSetter> dbus_setter = |
| + DBusThreadManager::GetSetterForTesting(); |
| + // Owned by DBusThreadManager, which takes care of cleaning it |
| + mock_manager_client_ = new NiceMock<MockShillManagerClient>(); |
| + dbus_setter->SetShillManagerClient( |
| + std::unique_ptr<ShillManagerClient>(mock_manager_client_)); |
| + network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); |
| + NetworkHandler::Initialize(); |
| + local_state_.reset(new TestingPrefServiceSimple()); |
|
Lei Zhang
2016/11/04 00:44:19
Prefer: local_state_ = base::MakeUnique<TestingPre
|
| + local_state_.get()->registry()->RegisterDictionaryPref( |
|
Lei Zhang
2016/11/04 00:44:19
You don't need the ".get()" here. std::unique_ptr
|
| + prefs::kNetworkThrottlingEnabled); |
| + observer_ = base::MakeUnique<NetworkThrottlingObserver>(local_state_.get()); |
| + } |
| + |
| + ~NetworkThrottlingObserverTest() override { |
| + observer_.reset(); |
|
Lei Zhang
2016/11/04 00:44:19
These 2 are probably not necessary, but probably c
|
| + local_state_.reset(); |
| + network_state_handler_.reset(); |
| + NetworkHandler::Shutdown(); |
| + DBusThreadManager::Shutdown(); |
| + delete message_loop_; |
| + } |
| + |
| + base::MessageLoop* message_loop_; |
|
Lei Zhang
2016/11/04 00:44:19
This is not what I mean, just declare this as: bas
|
| + std::unique_ptr<NetworkStateHandler> network_state_handler_; |
| + std::unique_ptr<TestingPrefServiceSimple> local_state_; |
| + std::unique_ptr<NetworkThrottlingObserver> observer_; |
| + MockShillManagerClient* mock_manager_client_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(NetworkThrottlingObserverTest); |
| +}; |
| + |
| +TEST_F(NetworkThrottlingObserverTest, ThrottlingChangeCallsShill) { |
| + // Test that a change in the throttling policy value leads to |
| + // shill_manager_client being called. |
| + base::DictionaryValue updated_throttling_policy; |
| + bool enabled = true; |
| + uint32_t upload_rate = 1200; |
| + uint32_t download_rate = 2000; |
| + updated_throttling_policy.SetBoolean("enabled", enabled); |
| + updated_throttling_policy.SetInteger("upload_rate_kbits", upload_rate); |
| + updated_throttling_policy.SetInteger("download_rate_kbits", download_rate); |
| + EXPECT_CALL( |
| + *mock_manager_client_, |
| + SetNetworkThrottlingStatus(enabled, upload_rate, download_rate, _, _)) |
| + .Times(1); |
| + local_state_->Set(prefs::kNetworkThrottlingEnabled, |
| + updated_throttling_policy); |
| + Mock::VerifyAndClearExpectations(mock_manager_client_); |
| + |
| + // Clearing the preference should disable throttling |
| + EXPECT_CALL(*mock_manager_client_, |
| + SetNetworkThrottlingStatus(false, 0, 0, _, _)) |
| + .Times(1); |
| + local_state_->ClearPref(prefs::kNetworkThrottlingEnabled); |
| +} |
| + |
| +} // namespace test |
| +} // namespace chromeos |