Commit 30a4bee3 authored by Antonio Sartori's avatar Antonio Sartori Committed by Commit Bot

Add unit tests for PolicyContainer

This change:
* adds some basic unit tests for content::PolicyContainer,
* creates a mock for the interface blink::mojom::PolicyContainerHost,
* uses that mock to unit test blink::PolicyContainer.

Bug: 1130587
Change-Id: If35e0b0c7099560f6a2de73fff6669274e00e9bc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2440666
Commit-Queue: Antonio Sartori <antoniosartori@chromium.org>
Reviewed-by: default avatarCamille Lamy <clamy@chromium.org>
Reviewed-by: default avatarArthur Sonzogni <arthursonzogni@chromium.org>
Reviewed-by: default avatarPâris Meuleman <pmeuleman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822601}
parent 05f22741
// Copyright 2020 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 "content/browser/renderer_host/policy_container.h"
#include "services/network/public/mojom/referrer_policy.mojom-shared.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
TEST(PolicyContainerTest, ReferrerPolicy) {
PolicyContainer policy_container;
EXPECT_EQ(network::mojom::ReferrerPolicy::kDefault,
policy_container.referrer_policy());
static_cast<blink::mojom::PolicyContainerHost*>(&policy_container)
->SetReferrerPolicy(network::mojom::ReferrerPolicy::kAlways);
EXPECT_EQ(network::mojom::ReferrerPolicy::kAlways,
policy_container.referrer_policy());
}
} // namespace content
......@@ -1939,6 +1939,7 @@ test("content_unittests") {
"../browser/renderer_host/navigator_unittest.cc",
"../browser/renderer_host/origin_policy_throttle_unittest.cc",
"../browser/renderer_host/overscroll_controller_unittest.cc",
"../browser/renderer_host/policy_container_unittest.cc",
"../browser/renderer_host/render_frame_host_feature_policy_unittest.cc",
"../browser/renderer_host/render_frame_host_impl_unittest.cc",
"../browser/renderer_host/render_frame_host_manager_unittest.cc",
......
......@@ -402,6 +402,8 @@ source_set("testing") {
"testing/mock_clipboard_host.h",
"testing/mock_hyphenation.cc",
"testing/mock_hyphenation.h",
"testing/mock_policy_container_host.cc",
"testing/mock_policy_container_host.h",
"testing/null_execution_context.cc",
"testing/null_execution_context.h",
"testing/origin_trials_test.cc",
......@@ -426,6 +428,8 @@ source_set("testing") {
"timing/internals_profiler.cc",
"timing/internals_profiler.h",
]
public_deps = [ "//testing/gmock:gmock" ]
}
# core_bindings_generated ------------------------------------------------------
......@@ -1278,6 +1282,7 @@ source_set("unit_tests") {
"frame/mhtml_archive_test.cc",
"frame/mhtml_loading_test.cc",
"frame/performance_monitor_test.cc",
"frame/policy_container_test.cc",
"frame/report_test.cc",
"frame/reporting_context_test.cc",
"frame/root_frame_viewport_test.cc",
......
// Copyright 2020 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 "third_party/blink/renderer/core/frame/policy_container.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/testing/mock_policy_container_host.h"
namespace blink {
TEST(PolicyContainerTest, UpdateReferrerPolicyIsPropagated) {
MockPolicyContainerHost host;
auto policies = mojom::blink::PolicyContainerData::New(
network::mojom::blink::ReferrerPolicy::kAlways);
PolicyContainer policy_container(host.BindNewEndpointAndPassDedicatedRemote(),
std::move(policies));
EXPECT_CALL(host,
SetReferrerPolicy(network::mojom::blink::ReferrerPolicy::kNever));
policy_container.UpdateReferrerPolicy(
network::mojom::blink::ReferrerPolicy::kNever);
EXPECT_EQ(network::mojom::blink::ReferrerPolicy::kNever,
policy_container.GetReferrerPolicy());
// Wait for mojo messages to be received.
host.FlushForTesting();
}
} // namespace blink
// Copyright 2020 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 "third_party/blink/renderer/core/testing/mock_policy_container_host.h"
namespace blink {
mojo::PendingAssociatedRemote<mojom::blink::PolicyContainerHost>
MockPolicyContainerHost::BindNewEndpointAndPassDedicatedRemote() {
return receiver_.BindNewEndpointAndPassDedicatedRemote();
}
void MockPolicyContainerHost::FlushForTesting() {
receiver_.FlushForTesting();
}
} // namespace blink
// Copyright 2020 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.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_MOCK_POLICY_CONTAINER_HOST_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_MOCK_POLICY_CONTAINER_HOST_H_
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "services/network/public/mojom/referrer_policy.mojom-shared.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/mojom/frame/policy_container.mojom-blink.h"
namespace blink {
class MockPolicyContainerHost : public mojom::blink::PolicyContainerHost {
public:
MOCK_METHOD(void,
SetReferrerPolicy,
(network::mojom::ReferrerPolicy),
(override));
MockPolicyContainerHost() = default;
mojo::PendingAssociatedRemote<mojom::blink::PolicyContainerHost>
BindNewEndpointAndPassDedicatedRemote();
void FlushForTesting();
private:
mojo::AssociatedReceiver<mojom::blink::PolicyContainerHost> receiver_{this};
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_FAKE_POLICY_CONTAINER_HOST_H_
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