Commit fc5ce7d5 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Remove gmock from network_throttling_observer_unittests.

BUG=234463
TEST=Ran bots.

Change-Id: I17099f919e60e83f8add28b5ce1158e0ac9b98ba
Reviewed-on: https://chromium-review.googlesource.com/819191Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523349}
parent 381f3e8d
......@@ -9,17 +9,12 @@
#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/dbus/fake_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 {
......@@ -27,12 +22,7 @@ namespace test {
class NetworkThrottlingObserverTest : public ::testing::Test {
public:
NetworkThrottlingObserverTest() {
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_));
DBusThreadManager::Initialize();
network_state_handler_ = NetworkStateHandler::InitializeForTest();
NetworkHandler::Initialize();
local_state_ = base::MakeUnique<TestingPrefServiceSimple>();
......@@ -50,13 +40,21 @@ class NetworkThrottlingObserverTest : public ::testing::Test {
DBusThreadManager::Shutdown();
}
TestingPrefServiceSimple* local_state() { return local_state_.get(); }
const ShillManagerClient::NetworkThrottlingStatus&
GetNetworkThrottlingStatus() {
auto* manager_client = static_cast<FakeShillManagerClient*>(
DBusThreadManager::Get()->GetShillManagerClient());
return manager_client->GetNetworkThrottlingStatus();
}
private:
base::MessageLoop message_loop_;
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);
};
......@@ -64,25 +62,31 @@ 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;
constexpr bool enabled = true;
constexpr uint32_t upload_rate = 1200;
constexpr 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_);
// Make sure throttling is disabled just before setting preferece.
EXPECT_FALSE(GetNetworkThrottlingStatus().enabled);
// Setting the preference should update the network throttling.
local_state()->Set(prefs::kNetworkThrottlingEnabled,
updated_throttling_policy);
base::RunLoop().RunUntilIdle();
{
const auto& status = GetNetworkThrottlingStatus();
EXPECT_TRUE(status.enabled);
EXPECT_EQ(upload_rate, status.upload_rate_kbits);
EXPECT_EQ(download_rate, status.download_rate_kbits);
}
// Clearing the preference should disable throttling
EXPECT_CALL(*mock_manager_client_,
SetNetworkThrottlingStatus(false, 0, 0, _, _))
.Times(1);
local_state_->ClearPref(prefs::kNetworkThrottlingEnabled);
local_state()->ClearPref(prefs::kNetworkThrottlingEnabled);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(GetNetworkThrottlingStatus().enabled);
}
} // namespace test
......
......@@ -590,12 +590,18 @@ void FakeShillManagerClient::SetBestServiceToConnect(
best_service_ = service_path;
}
const ShillManagerClient::NetworkThrottlingStatus&
FakeShillManagerClient::GetNetworkThrottlingStatus() {
return network_throttling_status_;
}
void FakeShillManagerClient::SetNetworkThrottlingStatus(
bool enabled,
uint32_t upload_rate_kbits,
uint32_t download_rate_kbits,
const NetworkThrottlingStatus& status,
const base::Closure& callback,
const ErrorCallback& error_callback) {}
const ErrorCallback& error_callback) {
network_throttling_status_ = status;
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
}
void FakeShillManagerClient::SetupDefaultEnvironment() {
// Bail out from setup if there is no message loop. This will be the common
......
......@@ -73,6 +73,9 @@ class CHROMEOS_EXPORT FakeShillManagerClient
const ErrorCallback& error_callback) override;
void ConnectToBestServices(const base::Closure& callback,
const ErrorCallback& error_callback) override;
void SetNetworkThrottlingStatus(const NetworkThrottlingStatus& status,
const base::Closure& callback,
const ErrorCallback& error_callback) override;
ShillManagerClient::TestInterface* GetTestInterface() override;
......@@ -100,11 +103,7 @@ class CHROMEOS_EXPORT FakeShillManagerClient
void SetupDefaultEnvironment() override;
int GetInteractiveDelay() const override;
void SetBestServiceToConnect(const std::string& service_path) override;
void SetNetworkThrottlingStatus(bool enabled,
uint32_t upload_rate_kbits,
uint32_t download_rate_kbits,
const base::Closure& callback,
const ErrorCallback& error_callback) override;
const NetworkThrottlingStatus& GetNetworkThrottlingStatus() override;
// Constants used for testing.
static const char kFakeEthernetNetworkGuid[];
......@@ -154,6 +153,9 @@ class CHROMEOS_EXPORT FakeShillManagerClient
// Roaming state for fake cellular service.
std::string roaming_state_;
// Current network throttling status.
NetworkThrottlingStatus network_throttling_status_ = {false, 0, 0};
typedef std::map<std::string, base::Value*> ShillPropertyMap;
typedef std::map<std::string, ShillPropertyMap> DevicePropertyMap;
DevicePropertyMap shill_device_property_map_;
......
......@@ -69,10 +69,8 @@ class MockShillManagerClient : public ShillManagerClient {
MOCK_METHOD2(ConnectToBestServices,
void(const base::Closure& callback,
const ErrorCallback& error_callback));
MOCK_METHOD5(SetNetworkThrottlingStatus,
void(const bool,
const uint32_t,
const uint32_t,
MOCK_METHOD3(SetNetworkThrottlingStatus,
void(const NetworkThrottlingStatus&,
const base::Closure&,
const ErrorCallback& error_callback));
MOCK_METHOD0(GetTestInterface, TestInterface*());
......
......@@ -93,17 +93,15 @@ class ShillManagerClientImpl : public ShillManagerClient {
}
void SetNetworkThrottlingStatus(
const bool enabled,
const uint32_t upload_rate_kbits,
const uint32_t download_rate_kbits,
const NetworkThrottlingStatus& status,
const base::Closure& callback,
const ErrorCallback& error_callback) override {
dbus::MethodCall method_call(shill::kFlimflamManagerInterface,
shill::kSetNetworkThrottlingFunction);
dbus::MessageWriter writer(&method_call);
writer.AppendBool(enabled);
writer.AppendUint32(upload_rate_kbits);
writer.AppendUint32(download_rate_kbits);
writer.AppendBool(status.enabled);
writer.AppendUint32(status.upload_rate_kbits);
writer.AppendUint32(status.download_rate_kbits);
helper_->CallVoidMethodWithErrorCallback(&method_call, callback,
error_callback);
}
......
......@@ -32,6 +32,18 @@ class CHROMEOS_EXPORT ShillManagerClient : public DBusClient {
typedef ShillClientHelper::StringCallback StringCallback;
typedef ShillClientHelper::BooleanCallback BooleanCallback;
struct NetworkThrottlingStatus {
// Enable or disable network bandwidth throttling.
// Following fields are available only if |enabled| is true.
bool enabled;
// Uploading rate (kbits/s).
uint32_t upload_rate_kbits;
// Downloading rate (kbits/s).
uint32_t download_rate_kbits;
};
// Interface for setting up devices, services, and technologies for testing.
// Accessed through GetTestInterface(), only implemented in the Stub Impl.
class TestInterface {
......@@ -87,6 +99,9 @@ class CHROMEOS_EXPORT ShillManagerClient : public DBusClient {
// Sets the 'best' service to connect to on a ConnectToBestServices call.
virtual void SetBestServiceToConnect(const std::string& service_path) = 0;
// Returns the current network throttling status.
virtual const NetworkThrottlingStatus& GetNetworkThrottlingStatus() = 0;
protected:
virtual ~TestInterface() {}
};
......@@ -221,13 +236,9 @@ class CHROMEOS_EXPORT ShillManagerClient : public DBusClient {
const ErrorCallback& error_callback) = 0;
// Enable or disable network bandwidth throttling, on all interfaces on the
// system. If |enabled| is true, |upload_rate_kbits| and |download_rate_kbits|
// are the desired rates (in kbits/s) to throttle to. If |enabled| is false,
// throttling is off, and the rates are ignored.
// system.
virtual void SetNetworkThrottlingStatus(
bool enabled,
uint32_t upload_rate_kbits,
uint32_t download_rate_kbits,
const NetworkThrottlingStatus& status,
const base::Closure& callback,
const ErrorCallback& error_callback) = 0;
......
......@@ -273,13 +273,13 @@ TEST_F(ShillManagerClientTest, NetworkThrottling) {
// Call method.
base::MockCallback<base::Closure> mock_closure;
base::MockCallback<ShillManagerClient::ErrorCallback> mock_error_callback;
client_->SetNetworkThrottlingStatus(enabled, upload_rate, download_rate,
mock_closure.Get(),
mock_error_callback.Get());
EXPECT_CALL(mock_closure, Run()).Times(1);
EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
// Run the message loop.
client_->SetNetworkThrottlingStatus(
ShillManagerClient::NetworkThrottlingStatus{enabled, upload_rate,
download_rate},
mock_closure.Get(), mock_error_callback.Get());
base::RunLoop().RunUntilIdle();
}
......
......@@ -243,7 +243,9 @@ void ShillPropertyHandler::SetNetworkThrottlingStatus(
uint32_t upload_rate_kbits,
uint32_t download_rate_kbits) {
shill_manager_->SetNetworkThrottlingStatus(
throttling_enabled, upload_rate_kbits, download_rate_kbits,
ShillManagerClient::NetworkThrottlingStatus{
throttling_enabled, upload_rate_kbits, download_rate_kbits,
},
base::Bind(&base::DoNothing),
base::Bind(&network_handler::ShillErrorCallbackFunction,
"SetNetworkThrottlingStatus failed", "Manager",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment