Commit 56e11d79 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Remove gmock from managed_network_configuration_handler_unittest.cc part 5.

Now, all tests are migrated to non-gmock test cases.
Clean up unnecessary code.

BUG=234463
TEST=Ran bots.

Change-Id: I71861d8b70544764c6b149574730f1abda007250
Reviewed-on: https://chromium-review.googlesource.com/819190Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523371}
parent 601e4a05
...@@ -2,28 +2,18 @@ ...@@ -2,28 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <iostream>
#include <memory> #include <memory>
#include <sstream> #include <string>
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/values.h" #include "base/values.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_shill_profile_client.h" #include "chromeos/dbus/fake_shill_profile_client.h"
#include "chromeos/dbus/fake_shill_service_client.h" #include "chromeos/dbus/fake_shill_service_client.h"
#include "chromeos/dbus/mock_shill_manager_client.h"
#include "chromeos/dbus/mock_shill_profile_client.h"
#include "chromeos/dbus/mock_shill_service_client.h"
#include "chromeos/dbus/shill_client_helper.h"
#include "chromeos/network/managed_network_configuration_handler_impl.h" #include "chromeos/network/managed_network_configuration_handler_impl.h"
#include "chromeos/network/network_configuration_handler.h" #include "chromeos/network/network_configuration_handler.h"
#include "chromeos/network/network_policy_observer.h" #include "chromeos/network/network_policy_observer.h"
...@@ -31,165 +21,23 @@ ...@@ -31,165 +21,23 @@
#include "chromeos/network/network_state_handler.h" #include "chromeos/network/network_state_handler.h"
#include "chromeos/network/onc/onc_test_utils.h" #include "chromeos/network/onc/onc_test_utils.h"
#include "chromeos/network/onc/onc_utils.h" #include "chromeos/network/onc/onc_utils.h"
#include "dbus/object_path.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h" #include "third_party/cros_system_api/dbus/service_constants.h"
using ::testing::AnyNumber;
using ::testing::Invoke;
using ::testing::Mock;
using ::testing::Pointee;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::StrEq;
using ::testing::StrictMock;
using ::testing::_;
namespace test_utils = ::chromeos::onc::test_utils; namespace test_utils = ::chromeos::onc::test_utils;
namespace chromeos { namespace chromeos {
namespace { namespace {
void DereferenceAndCall( constexpr char kUser1[] = "user1";
base::Callback<void(const base::DictionaryValue& result)> callback, constexpr char kUser1ProfilePath[] = "/profile/user1/shill";
const base::DictionaryValue* value) {
callback.Run(*value);
}
const char kUser1[] = "user1";
const char kUser1ProfilePath[] = "/profile/user1/shill";
// Matcher to match base::Value.
MATCHER_P(IsEqualTo,
value,
std::string(negation ? "isn't" : "is") + " equal to " +
ValueToString(value)) {
return value->Equals(&arg);
}
// Match properties in |value| to |arg|. |arg| may contain extra properties).
MATCHER_P(MatchesProperties,
value,
std::string(negation ? "does't match " : "matches ") +
ValueToString(value)) {
for (base::DictionaryValue::Iterator iter(*value); !iter.IsAtEnd();
iter.Advance()) {
const base::Value* property;
if (!arg.GetWithoutPathExpansion(iter.key(), &property) ||
!iter.value().Equals(property)) {
return false;
}
}
return true;
}
class ShillProfileTestClient {
public:
typedef ShillClientHelper::DictionaryValueCallbackWithoutStatus
DictionaryValueCallbackWithoutStatus;
typedef ShillClientHelper::ErrorCallback ErrorCallback;
void AddProfile(const std::string& profile_path,
const std::string& userhash) {
if (profile_entries_.HasKey(profile_path))
return;
profile_entries_.SetWithoutPathExpansion(
profile_path, std::make_unique<base::DictionaryValue>());
profile_to_user_[profile_path] = userhash;
}
void AddEntry(const std::string& profile_path,
const std::string& entry_path,
const base::DictionaryValue& entry) {
base::DictionaryValue* entries = NULL;
profile_entries_.GetDictionaryWithoutPathExpansion(profile_path, &entries);
ASSERT_TRUE(entries);
base::Value new_entry = entry.Clone();
new_entry.SetKey(shill::kProfileProperty, base::Value(profile_path));
entries->SetKey(entry_path, std::move(new_entry));
}
void GetProperties(const dbus::ObjectPath& profile_path,
const DictionaryValueCallbackWithoutStatus& callback,
const ErrorCallback& error_callback) {
base::DictionaryValue* entries = NULL;
profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
&entries);
ASSERT_TRUE(entries);
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue);
auto entry_paths = std::make_unique<base::ListValue>();
for (base::DictionaryValue::Iterator it(*entries); !it.IsAtEnd();
it.Advance()) {
entry_paths->AppendString(it.key());
}
result->SetWithoutPathExpansion(shill::kEntriesProperty,
std::move(entry_paths));
ASSERT_TRUE(base::ContainsKey(profile_to_user_, profile_path.value()));
const std::string& userhash = profile_to_user_[profile_path.value()];
result->SetKey(shill::kUserHashProperty, base::Value(userhash));
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&DereferenceAndCall, callback,
base::Owned(result.release())));
}
void GetEntry(const dbus::ObjectPath& profile_path,
const std::string& entry_path,
const DictionaryValueCallbackWithoutStatus& callback,
const ErrorCallback& error_callback) {
base::DictionaryValue* entries = NULL;
profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(),
&entries);
ASSERT_TRUE(entries);
base::DictionaryValue* entry = NULL;
entries->GetDictionaryWithoutPathExpansion(entry_path, &entry);
ASSERT_TRUE(entry);
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&DereferenceAndCall, callback,
base::Owned(entry->DeepCopy())));
}
protected:
base::DictionaryValue profile_entries_;
std::map<std::string, std::string> profile_to_user_;
};
class ShillServiceTestClient {
public:
typedef ShillClientHelper::DictionaryValueCallback DictionaryValueCallback;
void SetFakeProperties(const base::DictionaryValue& service_properties) {
service_properties_.Clear();
service_properties_.MergeDictionary(&service_properties);
}
void GetProperties(const dbus::ObjectPath& service_path,
const DictionaryValueCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS,
base::ConstRef(service_properties_)));
}
protected:
base::DictionaryValue service_properties_;
};
class TestNetworkProfileHandler : public NetworkProfileHandler { class TestNetworkProfileHandler : public NetworkProfileHandler {
public: public:
TestNetworkProfileHandler() { TestNetworkProfileHandler() {
Init(); Init();
} }
~TestNetworkProfileHandler() override = default;
void AddProfileForTest(const NetworkProfile& profile) {
AddProfile(profile);
}
private: private:
DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler); DISALLOW_COPY_AND_ASSIGN(TestNetworkProfileHandler);
...@@ -197,7 +45,7 @@ class TestNetworkProfileHandler : public NetworkProfileHandler { ...@@ -197,7 +45,7 @@ class TestNetworkProfileHandler : public NetworkProfileHandler {
class TestNetworkPolicyObserver : public NetworkPolicyObserver { class TestNetworkPolicyObserver : public NetworkPolicyObserver {
public: public:
TestNetworkPolicyObserver() : policies_applied_count_(0) {} TestNetworkPolicyObserver() = default;
void PoliciesApplied(const std::string& userhash) override { void PoliciesApplied(const std::string& userhash) override {
policies_applied_count_++; policies_applied_count_++;
...@@ -210,7 +58,7 @@ class TestNetworkPolicyObserver : public NetworkPolicyObserver { ...@@ -210,7 +58,7 @@ class TestNetworkPolicyObserver : public NetworkPolicyObserver {
} }
private: private:
int policies_applied_count_; int policies_applied_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver); DISALLOW_COPY_AND_ASSIGN(TestNetworkPolicyObserver);
}; };
...@@ -333,189 +181,6 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test { ...@@ -333,189 +181,6 @@ class ManagedNetworkConfigurationHandlerTest : public testing::Test {
DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest); DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerTest);
}; };
// DEPRECATED. Use ManagedNetworkConfigurationHandlerTest, instead.
// TODO(hidehiko): Remove this, when all TEST_Fs are migrated into
// fake.
class ManagedNetworkConfigurationHandlerMockTest : public testing::Test {
public:
ManagedNetworkConfigurationHandlerMockTest()
: mock_manager_client_(NULL),
mock_profile_client_(NULL),
mock_service_client_(NULL) {}
~ManagedNetworkConfigurationHandlerMockTest() override = default;
void SetUp() override {
std::unique_ptr<DBusThreadManagerSetter> dbus_setter =
DBusThreadManager::GetSetterForTesting();
mock_manager_client_ = new StrictMock<MockShillManagerClient>();
mock_profile_client_ = new StrictMock<MockShillProfileClient>();
mock_service_client_ = new StrictMock<MockShillServiceClient>();
dbus_setter->SetShillManagerClient(
std::unique_ptr<ShillManagerClient>(mock_manager_client_));
dbus_setter->SetShillProfileClient(
std::unique_ptr<ShillProfileClient>(mock_profile_client_));
dbus_setter->SetShillServiceClient(
std::unique_ptr<ShillServiceClient>(mock_service_client_));
SetNetworkConfigurationHandlerExpectations();
ON_CALL(*mock_profile_client_, GetProperties(_, _, _))
.WillByDefault(
Invoke(&profiles_stub_, &ShillProfileTestClient::GetProperties));
ON_CALL(*mock_profile_client_, GetEntry(_, _, _, _))
.WillByDefault(
Invoke(&profiles_stub_, &ShillProfileTestClient::GetEntry));
ON_CALL(*mock_service_client_, GetProperties(_, _))
.WillByDefault(
Invoke(&services_stub_, &ShillServiceTestClient::GetProperties));
network_state_handler_ = NetworkStateHandler::InitializeForTest();
network_profile_handler_.reset(new TestNetworkProfileHandler());
network_configuration_handler_.reset(
NetworkConfigurationHandler::InitializeForTest(
network_state_handler_.get(),
nullptr /* no NetworkDeviceHandler */));
managed_network_configuration_handler_.reset(
new ManagedNetworkConfigurationHandlerImpl());
managed_network_configuration_handler_->Init(
network_state_handler_.get(), network_profile_handler_.get(),
network_configuration_handler_.get(), nullptr /* no DeviceHandler */,
nullptr /* no ProhibitedTechnologiesHandler */);
managed_network_configuration_handler_->AddObserver(&policy_observer_);
base::RunLoop().RunUntilIdle();
}
void TearDown() override {
network_state_handler_->Shutdown();
if (managed_network_configuration_handler_)
managed_network_configuration_handler_->RemoveObserver(&policy_observer_);
managed_network_configuration_handler_.reset();
network_configuration_handler_.reset();
network_profile_handler_.reset();
network_state_handler_.reset();
DBusThreadManager::Shutdown();
}
void VerifyAndClearExpectations() {
Mock::VerifyAndClearExpectations(mock_manager_client_);
Mock::VerifyAndClearExpectations(mock_profile_client_);
SetNetworkConfigurationHandlerExpectations();
}
void InitializeStandardProfiles() {
profiles_stub_.AddProfile(kUser1ProfilePath, kUser1);
network_profile_handler_->
AddProfileForTest(NetworkProfile(kUser1ProfilePath, kUser1));
profiles_stub_.AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
std::string() /* no userhash */);
network_profile_handler_->AddProfileForTest(
NetworkProfile(NetworkProfileHandler::GetSharedProfilePath(),
std::string() /* no userhash */));
}
void SetUpEntry(const std::string& path_to_shill_json,
const std::string& profile_path,
const std::string& entry_path) {
std::unique_ptr<base::DictionaryValue> entry =
test_utils::ReadTestDictionary(path_to_shill_json);
profiles_stub_.AddEntry(profile_path, entry_path, *entry);
}
void SetPolicy(::onc::ONCSource onc_source,
const std::string& userhash,
const std::string& path_to_onc) {
std::unique_ptr<base::DictionaryValue> policy;
if (path_to_onc.empty())
policy = onc::ReadDictionaryFromJson(onc::kEmptyUnencryptedConfiguration);
else
policy = test_utils::ReadTestDictionary(path_to_onc);
base::ListValue empty_network_configs;
base::ListValue* network_configs = &empty_network_configs;
policy->GetListWithoutPathExpansion(
::onc::toplevel_config::kNetworkConfigurations, &network_configs);
base::DictionaryValue empty_global_config;
base::DictionaryValue* global_network_config = &empty_global_config;
policy->GetDictionaryWithoutPathExpansion(
::onc::toplevel_config::kGlobalNetworkConfiguration,
&global_network_config);
managed_handler()->SetPolicy(
onc_source, userhash, *network_configs, *global_network_config);
}
void SetNetworkConfigurationHandlerExpectations() {
// These calls occur in NetworkConfigurationHandler.
EXPECT_CALL(*mock_manager_client_, GetProperties(_)).Times(AnyNumber());
EXPECT_CALL(*mock_manager_client_,
SetProperty("ProhibitedTechnologies", _, _, _))
.Times(AnyNumber());
EXPECT_CALL(*mock_manager_client_, AddPropertyChangedObserver(_))
.Times(AnyNumber());
EXPECT_CALL(*mock_manager_client_,
RemovePropertyChangedObserver(_)).Times(AnyNumber());
}
ManagedNetworkConfigurationHandler* managed_handler() {
return managed_network_configuration_handler_.get();
}
void GetManagedProperties(const std::string& userhash,
const std::string& service_path) {
managed_handler()->GetManagedProperties(
userhash, service_path,
base::Bind(
&ManagedNetworkConfigurationHandlerMockTest::GetPropertiesCallback,
base::Unretained(this)),
base::Bind(
&ManagedNetworkConfigurationHandlerMockTest::UnexpectedError));
}
void GetPropertiesCallback(const std::string& service_path,
const base::DictionaryValue& dictionary) {
get_properties_service_path_ = service_path;
get_properties_result_.Clear();
get_properties_result_.MergeDictionary(&dictionary);
}
static void UnexpectedError(
const std::string& error_name,
std::unique_ptr<base::DictionaryValue> error_data) {
ASSERT_FALSE(true);
}
protected:
MockShillManagerClient* mock_manager_client_;
MockShillProfileClient* mock_profile_client_;
MockShillServiceClient* mock_service_client_;
ShillProfileTestClient profiles_stub_;
ShillServiceTestClient services_stub_;
TestNetworkPolicyObserver policy_observer_;
std::unique_ptr<NetworkStateHandler> network_state_handler_;
std::unique_ptr<TestNetworkProfileHandler> network_profile_handler_;
std::unique_ptr<NetworkConfigurationHandler> network_configuration_handler_;
std::unique_ptr<ManagedNetworkConfigurationHandlerImpl>
managed_network_configuration_handler_;
base::MessageLoop message_loop_;
std::string get_properties_service_path_;
base::DictionaryValue get_properties_result_;
private:
DISALLOW_COPY_AND_ASSIGN(ManagedNetworkConfigurationHandlerMockTest);
};
TEST_F(ManagedNetworkConfigurationHandlerMockTest, ProfileInitialization) {
InitializeStandardProfiles();
base::RunLoop().RunUntilIdle();
}
TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) { TEST_F(ManagedNetworkConfigurationHandlerTest, RemoveIrrelevantFields) {
InitializeStandardProfiles(); InitializeStandardProfiles();
std::unique_ptr<base::DictionaryValue> expected_shill_properties = std::unique_ptr<base::DictionaryValue> expected_shill_properties =
......
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