Commit 195c6184 authored by Hoch Hochkeppel's avatar Hoch Hochkeppel Committed by Commit Bot

WebShare: Add fake UriRuntimeClassFactory for testing

Adding a class to represent the Windows UriRuntimeClassFactory class
within tests, as well as basic unit tests to validate the test class
behaviors. The class behaves similarly to the Windows equivalent, except
it includes additional validations for expected usage.

Bug: 1035527
Change-Id: I8296dc31596e2331f63250c303fbfe29f6e35713
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2498950
Commit-Queue: Hoch Hochkeppel <mhochk@microsoft.com>
Reviewed-by: default avatarEric Willigers <ericwilligers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820966}
parent 24f7cf85
// 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 "chrome/browser/webshare/win/fake_uri_runtime_class_factory.h"
#include <string>
#include "base/macros.h"
#include "base/notreached.h"
#include "base/win/scoped_hstring.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using ABI::Windows::Foundation::IUriRuntimeClass;
using ABI::Windows::Foundation::IWwwFormUrlDecoderRuntimeClass;
using Microsoft::WRL::Make;
using Microsoft::WRL::RuntimeClass;
using Microsoft::WRL::RuntimeClassFlags;
using Microsoft::WRL::WinRtClassicComMix;
namespace webshare {
namespace {
// Provides an implementation of IUriRuntimeClass for use in GTests.
//
// Note that implementations for all the functions except get_RawUri are
// intentionally omitted. Though they are safe APIs, they have many subtle
// differences from the behaviors of a GURL. To prevent inconsistencies and
// unexpected edge cases get_RawUri should be used to construct a GURL and its
// similar functionality leveraged, rather than relying on these functions.
class FakeUriRuntimeClass final
: public RuntimeClass<RuntimeClassFlags<WinRtClassicComMix>,
IUriRuntimeClass> {
public:
explicit FakeUriRuntimeClass(std::string raw_uri) : raw_uri_(raw_uri) {}
FakeUriRuntimeClass(const FakeUriRuntimeClass&) = delete;
FakeUriRuntimeClass& operator=(const FakeUriRuntimeClass&) = delete;
~FakeUriRuntimeClass() final = default;
// ABI::Windows::Foundation::IUriRuntimeClass:
IFACEMETHODIMP get_AbsoluteUri(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_DisplayUri(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Domain(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Extension(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Fragment(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Host(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Password(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Path(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Query(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_QueryParsed(
IWwwFormUrlDecoderRuntimeClass** www_form_url_decoder) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_RawUri(HSTRING* value) final {
auto copy = base::win::ScopedHString::Create(raw_uri_);
*value = copy.release();
return S_OK;
}
IFACEMETHODIMP get_SchemeName(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_UserName(HSTRING* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Port(INT32* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP get_Suspicious(boolean* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP
Equals(IUriRuntimeClass* uri, boolean* value) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
IFACEMETHODIMP
CombineUri(HSTRING relative_uri, IUriRuntimeClass** instance) final {
NOTREACHED() << "get_RawUri should be the only function called on an "
"IUriRuntimeClass - see FakeUriRuntimeClass.";
return E_NOTIMPL;
}
private:
std::string raw_uri_;
};
} // namespace
FakeUriRuntimeClassFactory::FakeUriRuntimeClassFactory() = default;
FakeUriRuntimeClassFactory::~FakeUriRuntimeClassFactory() = default;
IFACEMETHODIMP
FakeUriRuntimeClassFactory::CreateUri(HSTRING uri,
IUriRuntimeClass** instance) {
if (!uri) {
ADD_FAILURE() << "CreateUri called with null uri.";
return E_POINTER;
}
// ScopedHString takes ownership of the HSTRING provided to it, but taking
// ownership is not an expected behavior when passing an HSTRING to a system
// API, so we use a temporary ScopedHString to make a copy we can safely own
// and release ownership of the original 'back' to the caller.
base::win::ScopedHString holder(uri);
auto uri_string = holder.GetAsUTF8();
ignore_result(holder.release());
if (uri_string.empty()) {
ADD_FAILURE() << "CreateUri called with empty uri.";
return E_POINTER;
}
auto url = GURL(uri_string);
if (!url.is_valid()) {
ADD_FAILURE() << "CreateUri called with invalid uri.";
return E_INVALIDARG;
}
auto fake_uri_runtime_class = Make<FakeUriRuntimeClass>(uri_string);
HRESULT hr = fake_uri_runtime_class->QueryInterface(IID_PPV_ARGS(instance));
if (FAILED(hr)) {
EXPECT_HRESULT_SUCCEEDED(hr);
return hr;
}
return S_OK;
}
IFACEMETHODIMP FakeUriRuntimeClassFactory::CreateWithRelativeUri(
HSTRING base_uri,
HSTRING relative_uri,
IUriRuntimeClass** instance) {
NOTREACHED();
return E_NOTIMPL;
}
} // namespace webshare
// 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 CHROME_BROWSER_WEBSHARE_WIN_FAKE_URI_RUNTIME_CLASS_FACTORY_H_
#define CHROME_BROWSER_WEBSHARE_WIN_FAKE_URI_RUNTIME_CLASS_FACTORY_H_
#include <windows.foundation.h>
#include <wrl/implements.h>
namespace webshare {
// Provides an implementation of IUriRuntimeClassFactory for use in GTests.
class __declspec(uuid("93741C10-A511-410F-B2CA-3F0A2B674ECE"))
FakeUriRuntimeClassFactory
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>,
ABI::Windows::Foundation::IUriRuntimeClassFactory> {
public:
FakeUriRuntimeClassFactory();
FakeUriRuntimeClassFactory(const FakeUriRuntimeClassFactory&) = delete;
FakeUriRuntimeClassFactory& operator=(const FakeUriRuntimeClassFactory&) =
delete;
~FakeUriRuntimeClassFactory() final;
// ABI::Windows::Foundation::IUriRuntimeClassFactory:
IFACEMETHODIMP
CreateUri(HSTRING uri,
ABI::Windows::Foundation::IUriRuntimeClass** instance) final;
IFACEMETHODIMP CreateWithRelativeUri(
HSTRING base_uri,
HSTRING relative_uri,
ABI::Windows::Foundation::IUriRuntimeClass** instance) final;
};
} // namespace webshare
#endif // CHROME_BROWSER_WEBSHARE_WIN_FAKE_URI_RUNTIME_CLASS_FACTORY_H_
// 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 "chrome/browser/webshare/win/fake_uri_runtime_class_factory.h"
#include <wrl/implements.h>
#include "base/strings/string_piece.h"
#include "base/win/scoped_hstring.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"
using ABI::Windows::Foundation::IUriRuntimeClass;
using Microsoft::WRL::ComPtr;
using Microsoft::WRL::Make;
namespace webshare {
TEST(FakeUriRuntimeClassFactoryTest, CreateUri) {
if (!base::win::ScopedHString::ResolveCoreWinRTStringDelayload())
return;
auto factory = Make<FakeUriRuntimeClassFactory>();
auto uri = base::win::ScopedHString::Create("https://www.site.come");
ComPtr<IUriRuntimeClass> uri_runtime_class;
ASSERT_HRESULT_SUCCEEDED(factory->CreateUri(uri.get(), &uri_runtime_class));
HSTRING result;
ASSERT_HRESULT_SUCCEEDED(uri_runtime_class->get_RawUri(&result));
auto wrapped_result = base::win::ScopedHString(result);
ASSERT_EQ(wrapped_result.GetAsUTF8(), "https://www.site.come");
}
TEST(FakeUriRuntimeClassFactoryTest, CreateUri_Invalid) {
if (!base::win::ScopedHString::ResolveCoreWinRTStringDelayload())
return;
auto factory = Make<FakeUriRuntimeClassFactory>();
auto uri = base::win::ScopedHString::Create("");
ComPtr<IUriRuntimeClass> uri_runtime_class;
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(factory->CreateUri(uri.get(), &uri_runtime_class)),
"CreateUri");
uri = base::win::ScopedHString::Create(" ");
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(factory->CreateUri(uri.get(), &uri_runtime_class)),
"CreateUri");
uri = base::win::ScopedHString::Create("abc");
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(factory->CreateUri(uri.get(), &uri_runtime_class)),
"CreateUri");
uri = base::win::ScopedHString::Create("http://?k=v");
EXPECT_NONFATAL_FAILURE(
ASSERT_HRESULT_FAILED(factory->CreateUri(uri.get(), &uri_runtime_class)),
"CreateUri");
}
} // namespace webshare
......@@ -334,6 +334,8 @@ static_library("test_support") {
"../browser/webshare/win/fake_random_access_stream.h",
"../browser/webshare/win/fake_storage_file_statics.cc",
"../browser/webshare/win/fake_storage_file_statics.h",
"../browser/webshare/win/fake_uri_runtime_class_factory.cc",
"../browser/webshare/win/fake_uri_runtime_class_factory.h",
"../browser/webshare/win/scoped_fake_data_transfer_manager_interop.cc",
"../browser/webshare/win/scoped_fake_data_transfer_manager_interop.h",
"//chrome/app/chrome_crash_reporter_client_win.cc",
......@@ -5828,6 +5830,7 @@ test("unit_tests") {
"../browser/webshare/win/fake_data_writer_factory_unittest.cc",
"../browser/webshare/win/fake_random_access_stream_unittest.cc",
"../browser/webshare/win/fake_storage_file_statics_unittest.cc",
"../browser/webshare/win/fake_uri_runtime_class_factory_unittest.cc",
"../browser/webshare/win/show_share_ui_for_window_operation_unittest.cc",
]
deps += [
......
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