Initial import from upstream Chromium of:

AXActivePopup
AXFragmentRootWin
AXPlatformRelationWin
AXPlatformNodeDelegateUtilsWin
AXPlatformNodeWin
Range
ScopedVariant
ScopedBstr
ScopedSafearray
EnumVariant
UiaRegistrarWin
VariantUtil
VariantVector
atl.h
atl_module.h
base_export.h
This commit is contained in:
George Wright
2021-03-09 13:49:58 -08:00
committed by George Wright
parent e8a33f1be4
commit 7c3d796a83
38 changed files with 23463 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
// Copyright 2019 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 "ui/accessibility/ax_active_popup.h"
namespace ui {
// Represents a global storage for the view accessibility for an
// autofill popup. It is a singleton wrapper around the ax unique id of the
// autofill popup. This singleton is used for communicating the live status of
// the autofill popup between web contents and views.
// The assumption here is that only one autofill popup can exist at a time.
static base::NoDestructor<base::Optional<int32_t>> g_active_popup_ax_unique_id;
base::Optional<int32_t> GetActivePopupAxUniqueId() {
return *g_active_popup_ax_unique_id;
}
void SetActivePopupAxUniqueId(base::Optional<int32_t> ax_unique_id) {
// When an instance of autofill popup hides, the caller of popup hide should
// make sure g_active_popup_ax_unique_id is cleared. The assumption is that
// there can only be one active autofill popup existing at a time. If on
// popup showing, we encounter g_active_popup_ax_unique_id is already set,
// this would indicate two autofill popups are showing at the same time or
// previous on popup hide call did not clear the variable, so we should fail
// DCHECK here.
DCHECK(!GetActivePopupAxUniqueId());
*g_active_popup_ax_unique_id = ax_unique_id;
}
void ClearActivePopupAxUniqueId() {
*g_active_popup_ax_unique_id = base::nullopt;
}
} // namespace ui

View File

@@ -0,0 +1,22 @@
// Copyright 2019 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 UI_ACCESSIBILITY_AX_ACTIVE_POPUP_H_
#define UI_ACCESSIBILITY_AX_ACTIVE_POPUP_H_
#include "base/macros.h"
#include "base/no_destructor.h"
#include "base/optional.h"
#include "ui/accessibility/ax_export.h"
namespace ui {
AX_EXPORT base::Optional<int32_t> GetActivePopupAxUniqueId();
AX_EXPORT void SetActivePopupAxUniqueId(base::Optional<int32_t> ax_unique_id);
AX_EXPORT void ClearActivePopupAxUniqueId();
} // namespace ui
#endif // UI_ACCESSIBILITY_AX_ACTIVE_POPUP_H_

View File

@@ -0,0 +1,36 @@
// Copyright 2019 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 UI_ACCESSIBILITY_PLATFORM_AX_FRAGMENT_ROOT_DELEGATE_WIN_H_
#define UI_ACCESSIBILITY_PLATFORM_AX_FRAGMENT_ROOT_DELEGATE_WIN_H_
#include "ui/gfx/native_widget_types.h"
namespace ui {
// Delegate interface for clients of AXFragmentRootWin. This allows the client
// to relate the fragment root to its neighbors in a loosely coupled way.
class AXFragmentRootDelegateWin {
public:
// In our design, a fragment root can have at most one child.
// See AXFragmentRootWin for more details.
virtual gfx::NativeViewAccessible GetChildOfAXFragmentRoot() = 0;
// Optionally returns a parent node for the fragment root. This is used, for
// example, to place the web content fragment at the correct spot in the
// browser UI's accessibility tree.
// If a fragment root returns no parent, the OS will use HWND parent-child
// relationships to establish the fragment root's location in the tree.
virtual gfx::NativeViewAccessible GetParentOfAXFragmentRoot() = 0;
// Return true if the window should be treated as an accessible control or
// false if the window should be considered a structural detail that should
// not be exposed to assistive technology users. See AXFragmentRootWin for
// more details.
virtual bool IsAXFragmentRootAControlElement() = 0;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_AX_FRAGMENT_ROOT_DELEGATE_WIN_H_

View File

@@ -0,0 +1,444 @@
// Copyright 2019 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 "ui/accessibility/platform/ax_fragment_root_win.h"
#include <unordered_map>
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "ui/accessibility/platform/ax_fragment_root_delegate_win.h"
#include "ui/accessibility/platform/ax_platform_node_win.h"
#include "ui/accessibility/platform/uia_registrar_win.h"
#include "ui/base/win/atl_module.h"
namespace ui {
class AXFragmentRootPlatformNodeWin : public AXPlatformNodeWin,
public IItemContainerProvider,
public IRawElementProviderFragmentRoot,
public IRawElementProviderAdviseEvents {
public:
BEGIN_COM_MAP(AXFragmentRootPlatformNodeWin)
COM_INTERFACE_ENTRY(IItemContainerProvider)
COM_INTERFACE_ENTRY(IRawElementProviderFragmentRoot)
COM_INTERFACE_ENTRY(IRawElementProviderAdviseEvents)
COM_INTERFACE_ENTRY_CHAIN(AXPlatformNodeWin)
END_COM_MAP()
static AXFragmentRootPlatformNodeWin* Create(
AXPlatformNodeDelegate* delegate) {
// Make sure ATL is initialized in this module.
win::CreateATLModuleIfNeeded();
CComObject<AXFragmentRootPlatformNodeWin>* instance = nullptr;
HRESULT hr =
CComObject<AXFragmentRootPlatformNodeWin>::CreateInstance(&instance);
DCHECK(SUCCEEDED(hr));
instance->Init(delegate);
instance->AddRef();
return instance;
}
//
// IItemContainerProvider methods.
//
IFACEMETHODIMP FindItemByProperty(
IRawElementProviderSimple* start_after_element,
PROPERTYID property_id,
VARIANT value,
IRawElementProviderSimple** result) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_ITEMCONTAINER_FINDITEMBYPROPERTY);
UIA_VALIDATE_CALL_1_ARG(result);
*result = nullptr;
// We currently only support the custom UIA property ID for unique id.
if (property_id ==
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId() &&
value.vt == VT_BSTR) {
int32_t ax_unique_id;
if (!base::StringToInt(value.bstrVal, &ax_unique_id))
return S_OK;
// In the Windows accessibility platform implementation, id 0 represents
// self; a positive id represents the immediate descendants; and a
// negative id represents a unique id that can be mapped to any node.
if (AXPlatformNodeWin* result_platform_node =
static_cast<AXPlatformNodeWin*>(GetFromUniqueId(-ax_unique_id))) {
if (start_after_element) {
Microsoft::WRL::ComPtr<AXPlatformNodeWin> start_after_platform_node;
if (!SUCCEEDED(start_after_element->QueryInterface(
IID_PPV_ARGS(&start_after_platform_node))))
return E_INVALIDARG;
// We want |result| to be nullptr if it comes before or is equal to
// |start_after_element|.
if (start_after_platform_node->CompareTo(*result_platform_node) >= 0)
return S_OK;
}
return result_platform_node->QueryInterface(IID_PPV_ARGS(result));
}
}
return E_INVALIDARG;
}
//
// IRawElementProviderSimple methods.
//
IFACEMETHODIMP get_HostRawElementProvider(
IRawElementProviderSimple** host_element_provider) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_GET_HOST_RAW_ELEMENT_PROVIDER);
UIA_VALIDATE_CALL_1_ARG(host_element_provider);
HWND hwnd = GetDelegate()->GetTargetForNativeAccessibilityEvent();
return UiaHostProviderFromHwnd(hwnd, host_element_provider);
}
IFACEMETHODIMP GetPatternProvider(PATTERNID pattern_id,
IUnknown** result) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_GET_PATTERN_PROVIDER);
UIA_VALIDATE_CALL_1_ARG(result);
*result = nullptr;
if (pattern_id == UIA_ItemContainerPatternId) {
AddRef();
*result = static_cast<IItemContainerProvider*>(this);
return S_OK;
}
return AXPlatformNodeWin::GetPatternProviderImpl(pattern_id, result);
}
IFACEMETHODIMP GetPropertyValue(PROPERTYID property_id,
VARIANT* result) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_GET_PROPERTY_VALUE);
UIA_VALIDATE_CALL_1_ARG(result);
switch (property_id) {
default:
// UIA has a built-in provider that will expose values for several
// properties based on the HWND. This information is useful to someone
// examining the accessibility tree using tools such as Inspect. Return
// VT_EMPTY for most properties so that we don't override values from
// the default provider with blank data.
result->vt = VT_EMPTY;
break;
case UIA_IsControlElementPropertyId:
case UIA_IsContentElementPropertyId:
// Override IsControlElement and IsContentElement to fine tune which
// fragment roots appear in the control and content views.
result->vt = VT_BOOL;
result->boolVal =
static_cast<AXFragmentRootWin*>(GetDelegate())->IsControlElement()
? VARIANT_TRUE
: VARIANT_FALSE;
break;
}
return S_OK;
}
//
// IRawElementProviderFragment methods.
//
IFACEMETHODIMP get_FragmentRoot(
IRawElementProviderFragmentRoot** fragment_root) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_GET_FRAGMENTROOT);
UIA_VALIDATE_CALL_1_ARG(fragment_root);
QueryInterface(IID_PPV_ARGS(fragment_root));
return S_OK;
}
//
// IRawElementProviderFragmentRoot methods.
//
IFACEMETHODIMP ElementProviderFromPoint(
double screen_physical_pixel_x,
double screen_physical_pixel_y,
IRawElementProviderFragment** element_provider) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_ELEMENT_PROVIDER_FROM_POINT);
WIN_ACCESSIBILITY_API_PERF_HISTOGRAM(UMA_API_ELEMENT_PROVIDER_FROM_POINT);
UIA_VALIDATE_CALL_1_ARG(element_provider);
*element_provider = nullptr;
gfx::NativeViewAccessible hit_element = nullptr;
// Descend the tree until we get a non-hit or can't go any further.
AXPlatformNode* node_to_test = this;
do {
gfx::NativeViewAccessible test_result =
node_to_test->GetDelegate()->HitTestSync(screen_physical_pixel_x,
screen_physical_pixel_y);
if (test_result != nullptr && test_result != hit_element) {
hit_element = test_result;
node_to_test = AXPlatformNode::FromNativeViewAccessible(test_result);
} else {
node_to_test = nullptr;
}
} while (node_to_test);
if (hit_element)
hit_element->QueryInterface(element_provider);
return S_OK;
}
IFACEMETHODIMP GetFocus(IRawElementProviderFragment** focus) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_GET_FOCUS);
UIA_VALIDATE_CALL_1_ARG(focus);
*focus = nullptr;
gfx::NativeViewAccessible focused_element = nullptr;
// GetFocus() can return a node at the root of a subtree, for example when
// transitioning from Views into web content. In such cases we want to
// continue drilling to retrieve the actual focused element.
AXPlatformNode* node_to_test = this;
do {
gfx::NativeViewAccessible test_result =
node_to_test->GetDelegate()->GetFocus();
if (test_result != nullptr && test_result != focused_element) {
focused_element = test_result;
node_to_test =
AXPlatformNode::FromNativeViewAccessible(focused_element);
} else {
node_to_test = nullptr;
}
} while (node_to_test);
if (focused_element)
focused_element->QueryInterface(IID_PPV_ARGS(focus));
return S_OK;
}
//
// IRawElementProviderAdviseEvents methods.
//
IFACEMETHODIMP AdviseEventAdded(EVENTID event_id,
SAFEARRAY* property_ids) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_ADVISE_EVENT_ADDED);
if (event_id == UIA_LiveRegionChangedEventId) {
live_region_change_listeners_++;
if (live_region_change_listeners_ == 1) {
// Fire a LiveRegionChangedEvent for each live-region to tell the
// newly-attached assistive technology about the regions.
//
// Ideally we'd be able to direct these events to only the
// newly-attached AT, but we don't have that capability, so we only
// fire events when the *first* AT attaches. (A common scenario will
// be an attached screen-reader, then a software-keyboard attaches to
// handle an input field; we don't want the screen-reader to announce
// that every live-region has changed.) There isn't a perfect solution,
// but this heuristic seems to work well in practice.
FireLiveRegionChangeRecursive();
}
}
return S_OK;
}
IFACEMETHODIMP AdviseEventRemoved(EVENTID event_id,
SAFEARRAY* property_ids) override {
WIN_ACCESSIBILITY_API_HISTOGRAM(UMA_API_ADVISE_EVENT_REMOVED);
if (event_id == UIA_LiveRegionChangedEventId) {
DCHECK(live_region_change_listeners_ > 0);
live_region_change_listeners_--;
}
return S_OK;
}
private:
int32_t live_region_change_listeners_ = 0;
};
class AXFragmentRootMapWin {
public:
static AXFragmentRootMapWin& GetInstance() {
static base::NoDestructor<AXFragmentRootMapWin> instance;
return *instance;
}
void AddFragmentRoot(gfx::AcceleratedWidget widget,
AXFragmentRootWin* fragment_root) {
map_[widget] = fragment_root;
}
void RemoveFragmentRoot(gfx::AcceleratedWidget widget) { map_.erase(widget); }
ui::AXFragmentRootWin* GetFragmentRoot(gfx::AcceleratedWidget widget) const {
const auto& entry = map_.find(widget);
if (entry != map_.end())
return entry->second;
return nullptr;
}
ui::AXFragmentRootWin* GetFragmentRootParentOf(
gfx::NativeViewAccessible accessible) const {
for (const auto& entry : map_) {
AXPlatformNodeDelegate* child = entry.second->GetChildNodeDelegate();
if (child && (child->GetNativeViewAccessible() == accessible))
return entry.second;
}
return nullptr;
}
private:
std::unordered_map<gfx::AcceleratedWidget, AXFragmentRootWin*> map_;
};
AXFragmentRootWin::AXFragmentRootWin(gfx::AcceleratedWidget widget,
AXFragmentRootDelegateWin* delegate)
: widget_(widget), delegate_(delegate) {
platform_node_ = ui::AXFragmentRootPlatformNodeWin::Create(this);
AXFragmentRootMapWin::GetInstance().AddFragmentRoot(widget, this);
}
AXFragmentRootWin::~AXFragmentRootWin() {
AXFragmentRootMapWin::GetInstance().RemoveFragmentRoot(widget_);
platform_node_->Destroy();
platform_node_ = nullptr;
}
AXFragmentRootWin* AXFragmentRootWin::GetForAcceleratedWidget(
gfx::AcceleratedWidget widget) {
return AXFragmentRootMapWin::GetInstance().GetFragmentRoot(widget);
}
// static
AXFragmentRootWin* AXFragmentRootWin::GetFragmentRootParentOf(
gfx::NativeViewAccessible accessible) {
return AXFragmentRootMapWin::GetInstance().GetFragmentRootParentOf(
accessible);
}
gfx::NativeViewAccessible AXFragmentRootWin::GetNativeViewAccessible() {
// The fragment root is the entry point from the operating system for UI
// Automation. Signal observers when we're asked for a platform object on it.
for (WinAccessibilityAPIUsageObserver& observer :
GetWinAccessibilityAPIUsageObserverList()) {
observer.OnUIAutomationUsed();
}
return platform_node_.Get();
}
bool AXFragmentRootWin::IsControlElement() {
return delegate_->IsAXFragmentRootAControlElement();
}
gfx::NativeViewAccessible AXFragmentRootWin::GetParent() {
return delegate_->GetParentOfAXFragmentRoot();
}
int AXFragmentRootWin::GetChildCount() const {
return delegate_->GetChildOfAXFragmentRoot() ? 1 : 0;
}
gfx::NativeViewAccessible AXFragmentRootWin::ChildAtIndex(int index) {
if (index == 0) {
return delegate_->GetChildOfAXFragmentRoot();
}
return nullptr;
}
gfx::NativeViewAccessible AXFragmentRootWin::GetNextSibling() {
int child_index = GetIndexInParentOfChild();
if (child_index >= 0) {
AXPlatformNodeDelegate* parent = GetParentNodeDelegate();
if (parent && child_index < (parent->GetChildCount() - 1))
return GetParentNodeDelegate()->ChildAtIndex(child_index + 1);
}
return nullptr;
}
gfx::NativeViewAccessible AXFragmentRootWin::GetPreviousSibling() {
int child_index = GetIndexInParentOfChild();
if (child_index > 0)
return GetParentNodeDelegate()->ChildAtIndex(child_index - 1);
return nullptr;
}
gfx::NativeViewAccessible AXFragmentRootWin::HitTestSync(int x, int y) const {
AXPlatformNodeDelegate* child_delegate = GetChildNodeDelegate();
if (child_delegate)
return child_delegate->HitTestSync(x, y);
return nullptr;
}
gfx::NativeViewAccessible AXFragmentRootWin::GetFocus() {
AXPlatformNodeDelegate* child_delegate = GetChildNodeDelegate();
if (child_delegate)
return child_delegate->GetFocus();
return nullptr;
}
const ui::AXUniqueId& AXFragmentRootWin::GetUniqueId() const {
return unique_id_;
}
gfx::AcceleratedWidget
AXFragmentRootWin::GetTargetForNativeAccessibilityEvent() {
return widget_;
}
AXPlatformNode* AXFragmentRootWin::GetFromTreeIDAndNodeID(
const ui::AXTreeID& ax_tree_id,
int32_t node_id) {
AXPlatformNodeDelegate* child_delegate = GetChildNodeDelegate();
if (child_delegate)
return child_delegate->GetFromTreeIDAndNodeID(ax_tree_id, node_id);
return nullptr;
}
AXPlatformNodeDelegate* AXFragmentRootWin::GetParentNodeDelegate() const {
gfx::NativeViewAccessible parent = delegate_->GetParentOfAXFragmentRoot();
if (parent)
return ui::AXPlatformNode::FromNativeViewAccessible(parent)->GetDelegate();
return nullptr;
}
AXPlatformNodeDelegate* AXFragmentRootWin::GetChildNodeDelegate() const {
gfx::NativeViewAccessible child = delegate_->GetChildOfAXFragmentRoot();
if (child)
return ui::AXPlatformNode::FromNativeViewAccessible(child)->GetDelegate();
return nullptr;
}
int AXFragmentRootWin::GetIndexInParentOfChild() const {
AXPlatformNodeDelegate* parent = GetParentNodeDelegate();
if (!parent)
return 0;
AXPlatformNodeDelegate* child = GetChildNodeDelegate();
if (child) {
int child_count = parent->GetChildCount();
for (int child_index = 0; child_index < child_count; child_index++) {
if (ui::AXPlatformNode::FromNativeViewAccessible(
parent->ChildAtIndex(child_index))
->GetDelegate() == child)
return child_index;
}
}
return 0;
}
} // namespace ui

View File

@@ -0,0 +1,89 @@
// Copyright 2019 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 UI_ACCESSIBILITY_PLATFORM_AX_FRAGMENT_ROOT_WIN_H_
#define UI_ACCESSIBILITY_PLATFORM_AX_FRAGMENT_ROOT_WIN_H_
#include "ui/accessibility/platform/ax_platform_node_delegate_base.h"
#include <wrl/client.h>
namespace ui {
class AXFragmentRootDelegateWin;
class AXFragmentRootPlatformNodeWin;
// UI Automation on Windows requires the root of a multi-element provider to
// implement IRawElementProviderFragmentRoot. Our internal accessibility trees
// may not know their roots for right away; for example, web content may
// deserialize the document for an iframe before the host document. Because of
// this, and because COM rules require that the list of interfaces returned by
// QueryInterface remain static over the lifetime of an object instance, we
// implement IRawElementProviderFragmentRoot on its own node for each HWND, with
// the root of our internal accessibility tree for that HWND as its sole child.
//
// Since UIA derives some information from the underlying HWND hierarchy, we
// expose one fragment root per HWND. The class that owns the HWND is expected
// to own the corresponding AXFragmentRootWin.
class AX_EXPORT AXFragmentRootWin : public ui::AXPlatformNodeDelegateBase {
public:
AXFragmentRootWin(gfx::AcceleratedWidget widget,
AXFragmentRootDelegateWin* delegate);
~AXFragmentRootWin() override;
// Fragment roots register themselves in a map upon creation and unregister
// upon destruction. This method provides a lookup, which allows the internal
// accessibility root to navigate back to the corresponding fragment root.
static AXFragmentRootWin* GetForAcceleratedWidget(
gfx::AcceleratedWidget widget);
// If the given NativeViewAccessible is the direct descendant of a fragment
// root, return the corresponding fragment root.
static AXFragmentRootWin* GetFragmentRootParentOf(
gfx::NativeViewAccessible accessible);
// Returns the NativeViewAccessible for this fragment root.
gfx::NativeViewAccessible GetNativeViewAccessible() override;
// Assistive technologies will typically use UI Automation's control or
// content view rather than the raw view.
// Returns true if the fragment root should be included in the control and
// content views or false if it should be excluded.
bool IsControlElement();
// If a child node is available, return its delegate.
AXPlatformNodeDelegate* GetChildNodeDelegate() const;
private:
// AXPlatformNodeDelegate overrides.
gfx::NativeViewAccessible GetParent() override;
int GetChildCount() const override;
gfx::NativeViewAccessible ChildAtIndex(int index) override;
gfx::NativeViewAccessible GetNextSibling() override;
gfx::NativeViewAccessible GetPreviousSibling() override;
gfx::NativeViewAccessible HitTestSync(int x, int y) const override;
gfx::NativeViewAccessible GetFocus() override;
const ui::AXUniqueId& GetUniqueId() const override;
gfx::AcceleratedWidget GetTargetForNativeAccessibilityEvent() override;
AXPlatformNode* GetFromTreeIDAndNodeID(const ui::AXTreeID& ax_tree_id,
int32_t id) override;
// A fragment root does not correspond to any node in the platform neutral
// accessibility tree. Rather, the fragment root's child is a child of the
// fragment root's parent. This helper computes the child's index in the
// parent's array of children.
int GetIndexInParentOfChild() const;
// If a parent node is available, return its delegate.
AXPlatformNodeDelegate* GetParentNodeDelegate() const;
gfx::AcceleratedWidget widget_;
AXFragmentRootDelegateWin* const delegate_;
Microsoft::WRL::ComPtr<ui::AXFragmentRootPlatformNodeWin> platform_node_;
ui::AXUniqueId unique_id_;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_AX_FRAGMENT_ROOT_WIN_H_

View File

@@ -0,0 +1,697 @@
// Copyright 2019 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 "ui/accessibility/platform/ax_fragment_root_win.h"
#include "ui/accessibility/accessibility_switches.h"
#include "ui/accessibility/platform/ax_platform_node_win.h"
#include "ui/accessibility/platform/ax_platform_node_win_unittest.h"
#include "ui/accessibility/platform/test_ax_node_wrapper.h"
#include <UIAutomationClient.h>
#include <UIAutomationCoreApi.h>
#include "base/auto_reset.h"
#include "base/win/scoped_safearray.h"
#include "base/win/scoped_variant.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/platform/uia_registrar_win.h"
using base::win::ScopedVariant;
using Microsoft::WRL::ComPtr;
namespace ui {
#define EXPECT_UIA_BSTR_EQ(node, property_id, expected) \
{ \
ScopedVariant expectedVariant(expected); \
ASSERT_EQ(VT_BSTR, expectedVariant.type()); \
ASSERT_NE(nullptr, expectedVariant.ptr()->bstrVal); \
ScopedVariant actual; \
ASSERT_HRESULT_SUCCEEDED( \
node->GetPropertyValue(property_id, actual.Receive())); \
ASSERT_EQ(VT_BSTR, actual.type()); \
ASSERT_NE(nullptr, actual.ptr()->bstrVal); \
EXPECT_STREQ(expectedVariant.ptr()->bstrVal, actual.ptr()->bstrVal); \
}
class AXFragmentRootTest : public AXPlatformNodeWinTest {
public:
AXFragmentRootTest() = default;
~AXFragmentRootTest() override = default;
AXFragmentRootTest(const AXFragmentRootTest&) = delete;
AXFragmentRootTest& operator=(const AXFragmentRootTest&) = delete;
};
TEST_F(AXFragmentRootTest, UIAFindItemByPropertyUniqueId) {
AXNodeData root;
root.id = 1;
root.role = ax::mojom::Role::kRootWebArea;
root.SetName("root");
root.child_ids = {2, 3};
AXNodeData text1;
text1.id = 2;
text1.role = ax::mojom::Role::kStaticText;
text1.SetName("text1");
AXNodeData button;
button.id = 3;
button.role = ax::mojom::Role::kButton;
button.SetName("button");
button.child_ids = {4};
AXNodeData text2;
text2.id = 4;
text2.role = ax::mojom::Role::kStaticText;
text2.SetName("text2");
Init(root, text1, button, text2);
InitFragmentRoot();
ComPtr<IRawElementProviderSimple> root_raw_element_provider_simple;
ax_fragment_root_->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&root_raw_element_provider_simple));
ComPtr<IRawElementProviderSimple> text1_raw_element_provider_simple =
GetIRawElementProviderSimpleFromChildIndex(0);
ComPtr<IRawElementProviderSimple> button_raw_element_provider_simple =
GetIRawElementProviderSimpleFromChildIndex(1);
AXNode* text1_node = GetRootAsAXNode()->children()[0];
AXNode* button_node = GetRootAsAXNode()->children()[1];
ComPtr<IItemContainerProvider> item_container_provider;
EXPECT_HRESULT_SUCCEEDED(root_raw_element_provider_simple->GetPatternProvider(
UIA_ItemContainerPatternId, &item_container_provider));
ASSERT_NE(nullptr, item_container_provider.Get());
ScopedVariant unique_id_variant;
int32_t unique_id;
ComPtr<IRawElementProviderSimple> result;
// When |start_after_element| is an invalid element, we should fail at finding
// the item.
{
unique_id = AXPlatformNodeFromNode(GetRootAsAXNode())->GetUniqueId();
unique_id_variant.Set(
SysAllocString(base::NumberToString16(-unique_id).c_str()));
ComPtr<IRawElementProviderSimple> invalid_element_provider_simple;
EXPECT_HRESULT_SUCCEEDED(
MockIRawElementProviderSimple::CreateMockIRawElementProviderSimple(
&invalid_element_provider_simple));
EXPECT_HRESULT_FAILED(item_container_provider->FindItemByProperty(
invalid_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
result.Reset();
unique_id_variant.Release();
}
// Fetch the AxUniqueId of "root", and verify we can retrieve its
// corresponding IRawElementProviderSimple through FindItemByProperty().
{
unique_id = AXPlatformNodeFromNode(GetRootAsAXNode())->GetUniqueId();
unique_id_variant.Set(
SysAllocString(base::NumberToString16(-unique_id).c_str()));
// When |start_after_element| of FindItemByProperty() is nullptr, we should
// be able to find "text1".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
nullptr, UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"root");
result.Reset();
// When |start_after_element| of FindItemByProperty() is "text1", there
// should be no element found, since "text1" comes after the element we are
// looking for.
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
text1_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_EQ(nullptr, result.Get());
result.Reset();
// When |start_after_element| of FindItemByProperty() is "button", there
// should be no element found, since "button" comes after the element we are
// looking for.
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
button_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_EQ(nullptr, result.Get());
result.Reset();
unique_id_variant.Release();
}
// Fetch the AxUniqueId of "text1", and verify if we can retrieve its
// corresponding IRawElementProviderSimple through FindItemByProperty().
{
unique_id = AXPlatformNodeFromNode(text1_node)->GetUniqueId();
unique_id_variant.Set(
SysAllocString(base::NumberToString16(-unique_id).c_str()));
// When |start_after_element| of FindItemByProperty() is nullptr, we should
// be able to find "text1".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
nullptr, UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"text1");
result.Reset();
// When |start_after_element| of FindItemByProperty() is "text1", there
// should be no element found, since "text1" equals the element we are
// looking for.
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
text1_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_EQ(nullptr, result.Get());
result.Reset();
// When |start_after_element| of FindItemByProperty() is "button", there
// should be no element found, since "button" comes after the element we are
// looking for.
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
button_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_EQ(nullptr, result.Get());
result.Reset();
unique_id_variant.Release();
}
// Fetch the AxUniqueId of "button", and verify we can retrieve its
// corresponding IRawElementProviderSimple through FindItemByProperty().
{
unique_id = AXPlatformNodeFromNode(button_node)->GetUniqueId();
unique_id_variant.Set(
SysAllocString(base::NumberToString16(-unique_id).c_str()));
// When |start_after_element| of FindItemByProperty() is nullptr, we should
// be able to find "button".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
nullptr, UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"button");
result.Reset();
// When |start_after_element| of FindItemByProperty() is "text1", we should
// be able to find "button".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
text1_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"button");
result.Reset();
// When |start_after_element| of FindItemByProperty() is "button", there
// should be no element found, since "button" equals the element we are
// looking for.
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
button_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_EQ(nullptr, result.Get());
result.Reset();
unique_id_variant.Release();
}
// Fetch the AxUniqueId of "text2", and verify we can retrieve its
// corresponding IRawElementProviderSimple through FindItemByProperty().
{
unique_id =
AXPlatformNodeFromNode(button_node->children()[0])->GetUniqueId();
unique_id_variant.Set(
SysAllocString(base::NumberToString16(-unique_id).c_str()));
// When |start_after_element| of FindItemByProperty() is nullptr, we should
// be able to find "text2".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
nullptr, UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"text2");
// When |start_after_element| of FindItemByProperty() is root, we should
// be able to find "text2".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
root_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"text2");
// When |start_after_element| of FindItemByProperty() is "text1", we should
// be able to find "text2".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
text1_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"text2");
// When |start_after_element| of FindItemByProperty() is "button", we should
// be able to find "text2".
EXPECT_HRESULT_SUCCEEDED(item_container_provider->FindItemByProperty(
button_raw_element_provider_simple.Get(),
UiaRegistrarWin::GetInstance().GetUiaUniqueIdPropertyId(),
unique_id_variant, &result));
EXPECT_UIA_BSTR_EQ(result, UIA_NamePropertyId, L"text2");
}
}
TEST_F(AXFragmentRootTest, TestUIAGetFragmentRoot) {
AXNodeData root;
Init(root);
InitFragmentRoot();
ComPtr<IRawElementProviderFragmentRoot> expected_fragment_root =
GetFragmentRoot();
ComPtr<IRawElementProviderFragment> fragment_provider;
expected_fragment_root.As(&fragment_provider);
ComPtr<IRawElementProviderFragmentRoot> actual_fragment_root;
EXPECT_HRESULT_SUCCEEDED(
fragment_provider->get_FragmentRoot(&actual_fragment_root));
EXPECT_EQ(expected_fragment_root.Get(), actual_fragment_root.Get());
}
TEST_F(AXFragmentRootTest, TestUIAElementProviderFromPoint) {
AXNodeData root_data;
root_data.id = 1;
root_data.relative_bounds.bounds = gfx::RectF(0, 0, 80, 80);
AXNodeData element1_data;
element1_data.id = 2;
element1_data.relative_bounds.bounds = gfx::RectF(0, 0, 50, 50);
root_data.child_ids.push_back(element1_data.id);
AXNodeData element2_data;
element2_data.id = 3;
element2_data.relative_bounds.bounds = gfx::RectF(0, 50, 30, 30);
root_data.child_ids.push_back(element2_data.id);
Init(root_data, element1_data, element2_data);
InitFragmentRoot();
AXNode* root_node = GetRootAsAXNode();
AXNode* element1_node = root_node->children()[0];
AXNode* element2_node = root_node->children()[1];
ComPtr<IRawElementProviderFragmentRoot> fragment_root_prov(GetFragmentRoot());
ComPtr<IRawElementProviderFragment> root_provider(
GetRootIRawElementProviderFragment());
ComPtr<IRawElementProviderFragment> element1_provider =
QueryInterfaceFromNode<IRawElementProviderFragment>(element1_node);
ComPtr<IRawElementProviderFragment> element2_provider =
QueryInterfaceFromNode<IRawElementProviderFragment>(element2_node);
ComPtr<IRawElementProviderFragment> provider_from_point;
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->ElementProviderFromPoint(
23, 31, &provider_from_point));
EXPECT_EQ(element1_provider.Get(), provider_from_point.Get());
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->ElementProviderFromPoint(
23, 67, &provider_from_point));
EXPECT_EQ(element2_provider.Get(), provider_from_point.Get());
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->ElementProviderFromPoint(
47, 67, &provider_from_point));
EXPECT_EQ(root_provider.Get(), provider_from_point.Get());
// This is on node 1 with scale factor of 1.5.
std::unique_ptr<base::AutoReset<float>> scale_factor_reset =
TestAXNodeWrapper::SetScaleFactor(1.5);
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->ElementProviderFromPoint(
60, 60, &provider_from_point));
EXPECT_EQ(element1_provider.Get(), provider_from_point.Get());
}
TEST_F(AXFragmentRootTest, TestUIAGetFocus) {
AXNodeData root_data;
root_data.id = 1;
AXNodeData element1_data;
element1_data.id = 2;
root_data.child_ids.push_back(element1_data.id);
AXNodeData element2_data;
element2_data.id = 3;
root_data.child_ids.push_back(element2_data.id);
Init(root_data, element1_data, element2_data);
InitFragmentRoot();
AXNode* root_node = GetRootAsAXNode();
AXNode* element1_node = root_node->children()[0];
AXNode* element2_node = root_node->children()[1];
ComPtr<IRawElementProviderFragmentRoot> fragment_root_prov(GetFragmentRoot());
ComPtr<IRawElementProviderFragment> root_provider(
GetRootIRawElementProviderFragment());
ComPtr<IRawElementProviderFragment> element1_provider =
QueryInterfaceFromNode<IRawElementProviderFragment>(element1_node);
ComPtr<IRawElementProviderFragment> element2_provider =
QueryInterfaceFromNode<IRawElementProviderFragment>(element2_node);
ComPtr<IRawElementProviderFragment> focused_fragment;
EXPECT_HRESULT_SUCCEEDED(root_provider->SetFocus());
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->GetFocus(&focused_fragment));
EXPECT_EQ(root_provider.Get(), focused_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(element1_provider->SetFocus());
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->GetFocus(&focused_fragment));
EXPECT_EQ(element1_provider.Get(), focused_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(element2_provider->SetFocus());
EXPECT_HRESULT_SUCCEEDED(fragment_root_prov->GetFocus(&focused_fragment));
EXPECT_EQ(element2_provider.Get(), focused_fragment.Get());
}
TEST_F(AXFragmentRootTest, TestUIAErrorHandling) {
AXNodeData root;
Init(root);
InitFragmentRoot();
ComPtr<IRawElementProviderSimple> simple_provider =
GetRootIRawElementProviderSimple();
ComPtr<IRawElementProviderFragment> fragment_provider =
GetRootIRawElementProviderFragment();
ComPtr<IRawElementProviderFragmentRoot> fragment_root_provider =
GetFragmentRoot();
SetTree(std::make_unique<AXTree>());
ax_fragment_root_.reset(nullptr);
ComPtr<IRawElementProviderSimple> returned_simple_provider;
ComPtr<IRawElementProviderFragment> returned_fragment_provider;
ComPtr<IRawElementProviderFragmentRoot> returned_fragment_root_provider;
base::win::ScopedSafearray returned_runtime_id;
EXPECT_EQ(
static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE),
simple_provider->get_HostRawElementProvider(&returned_simple_provider));
EXPECT_EQ(
static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE),
fragment_provider->get_FragmentRoot(&returned_fragment_root_provider));
EXPECT_EQ(static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE),
fragment_provider->GetRuntimeId(returned_runtime_id.Receive()));
EXPECT_EQ(static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE),
fragment_root_provider->ElementProviderFromPoint(
67, 23, &returned_fragment_provider));
EXPECT_EQ(static_cast<HRESULT>(UIA_E_ELEMENTNOTAVAILABLE),
fragment_root_provider->GetFocus(&returned_fragment_provider));
}
TEST_F(AXFragmentRootTest, TestGetChildCount) {
AXNodeData root;
Init(root);
InitFragmentRoot();
AXPlatformNodeDelegate* fragment_root = ax_fragment_root_.get();
EXPECT_EQ(1, fragment_root->GetChildCount());
test_fragment_root_delegate_->child_ = nullptr;
EXPECT_EQ(0, fragment_root->GetChildCount());
}
TEST_F(AXFragmentRootTest, TestChildAtIndex) {
AXNodeData root;
Init(root);
InitFragmentRoot();
gfx::NativeViewAccessible native_view_accessible =
AXPlatformNodeFromNode(GetRootAsAXNode())->GetNativeViewAccessible();
AXPlatformNodeDelegate* fragment_root = ax_fragment_root_.get();
EXPECT_EQ(native_view_accessible, fragment_root->ChildAtIndex(0));
EXPECT_EQ(nullptr, fragment_root->ChildAtIndex(1));
test_fragment_root_delegate_->child_ = nullptr;
EXPECT_EQ(nullptr, fragment_root->ChildAtIndex(0));
}
TEST_F(AXFragmentRootTest, TestGetParent) {
AXNodeData root;
Init(root);
InitFragmentRoot();
AXPlatformNodeDelegate* fragment_root = ax_fragment_root_.get();
EXPECT_EQ(nullptr, fragment_root->GetParent());
gfx::NativeViewAccessible native_view_accessible =
AXPlatformNodeFromNode(GetRootAsAXNode())->GetNativeViewAccessible();
test_fragment_root_delegate_->parent_ = native_view_accessible;
EXPECT_EQ(native_view_accessible, fragment_root->GetParent());
}
TEST_F(AXFragmentRootTest, TestGetPropertyValue) {
AXNodeData root;
Init(root);
InitFragmentRoot();
ComPtr<IRawElementProviderSimple> root_provider;
ax_fragment_root_->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&root_provider));
// IsControlElement and IsContentElement should follow the setting on the
// fragment root delegate.
test_fragment_root_delegate_->is_control_element_ = true;
ScopedVariant result;
EXPECT_HRESULT_SUCCEEDED(root_provider->GetPropertyValue(
UIA_IsControlElementPropertyId, result.Receive()));
EXPECT_EQ(result.type(), VT_BOOL);
EXPECT_EQ(result.ptr()->boolVal, VARIANT_TRUE);
EXPECT_HRESULT_SUCCEEDED(root_provider->GetPropertyValue(
UIA_IsContentElementPropertyId, result.Receive()));
EXPECT_EQ(result.type(), VT_BOOL);
EXPECT_EQ(result.ptr()->boolVal, VARIANT_TRUE);
test_fragment_root_delegate_->is_control_element_ = false;
EXPECT_HRESULT_SUCCEEDED(root_provider->GetPropertyValue(
UIA_IsControlElementPropertyId, result.Receive()));
EXPECT_EQ(result.type(), VT_BOOL);
EXPECT_EQ(result.ptr()->boolVal, VARIANT_FALSE);
EXPECT_HRESULT_SUCCEEDED(root_provider->GetPropertyValue(
UIA_IsContentElementPropertyId, result.Receive()));
EXPECT_EQ(result.type(), VT_BOOL);
EXPECT_EQ(result.ptr()->boolVal, VARIANT_FALSE);
// Other properties should return VT_EMPTY.
EXPECT_HRESULT_SUCCEEDED(root_provider->GetPropertyValue(
UIA_ControlTypePropertyId, result.Receive()));
EXPECT_EQ(result.type(), VT_EMPTY);
}
TEST_F(AXFragmentRootTest, TestUIAMultipleFragmentRoots) {
// Consider the following platform-neutral tree:
//
// N1
// _____/ \_____
// / \
// N2---N3---N4---N5
// / \ / \
// N6---N7 N8---N9
//
// N3 and N5 are nodes for which we need a fragment root. This will correspond
// to the following tree in UIA:
//
// U1
// _____/ \_____
// / \
// U2---R3---U4---R5
// | |
// U3 U5
// / \ / \
// U6---U7 U8---U9
ui::AXNodeData top_fragment_root_n1;
top_fragment_root_n1.id = 1;
ui::AXNodeData sibling_n2;
sibling_n2.id = 2;
ui::AXNodeData child_fragment_root_n3;
child_fragment_root_n3.id = 3;
ui::AXNodeData sibling_n6;
sibling_n6.id = 6;
ui::AXNodeData sibling_n7;
sibling_n7.id = 7;
child_fragment_root_n3.child_ids = {6, 7};
ui::AXNodeData sibling_n4;
sibling_n4.id = 4;
ui::AXNodeData child_fragment_root_n5;
child_fragment_root_n5.id = 5;
ui::AXNodeData sibling_n8;
sibling_n8.id = 8;
ui::AXNodeData sibling_n9;
sibling_n9.id = 9;
child_fragment_root_n5.child_ids = {8, 9};
top_fragment_root_n1.child_ids = {2, 3, 4, 5};
ui::AXTreeUpdate update;
update.has_tree_data = true;
update.root_id = top_fragment_root_n1.id;
update.nodes = {top_fragment_root_n1,
sibling_n2,
child_fragment_root_n3,
sibling_n6,
sibling_n7,
sibling_n4,
child_fragment_root_n5,
sibling_n8,
sibling_n9};
update.tree_data.tree_id = ui::AXTreeID::CreateNewAXTreeID();
Init(update);
InitFragmentRoot();
AXNode* root_node = GetRootAsAXNode();
// Set up other fragment roots
AXNode* child_fragment_root_n3_node = root_node->children()[1];
std::unique_ptr<TestFragmentRootDelegate> n3_fragment_root_delegate =
std::make_unique<TestFragmentRootDelegate>();
std::unique_ptr<AXFragmentRootWin> n3_fragment_root(InitNodeAsFragmentRoot(
child_fragment_root_n3_node, n3_fragment_root_delegate.get()));
AXNode* child_fragment_root_n5_node = root_node->children()[3];
std::unique_ptr<TestFragmentRootDelegate> n5_fragment_root_delegate =
std::make_unique<TestFragmentRootDelegate>();
std::unique_ptr<AXFragmentRootWin> n5_fragment_root(InitNodeAsFragmentRoot(
child_fragment_root_n5_node, n5_fragment_root_delegate.get()));
// Test navigation from root fragment
ComPtr<IRawElementProviderFragmentRoot> root_fragment_root =
GetFragmentRoot();
ComPtr<IRawElementProviderFragment> root_fragment;
root_fragment_root.As(&root_fragment);
ComPtr<IRawElementProviderFragment> test_fragment;
EXPECT_HRESULT_SUCCEEDED(
root_fragment->Navigate(NavigateDirection_Parent, &test_fragment));
EXPECT_EQ(nullptr, test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
root_fragment->Navigate(NavigateDirection_NextSibling, &test_fragment));
EXPECT_EQ(nullptr, test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(root_fragment->Navigate(
NavigateDirection_PreviousSibling, &test_fragment));
EXPECT_EQ(nullptr, test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
root_fragment->Navigate(NavigateDirection_FirstChild, &test_fragment));
ComPtr<IUnknown> root_child_unknown = test_fragment_root_delegate_->child_;
ComPtr<IRawElementProviderFragment> root_child_fragment;
root_child_unknown.As(&root_child_fragment);
EXPECT_EQ(root_child_fragment.Get(), test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
root_fragment->Navigate(NavigateDirection_LastChild, &test_fragment));
EXPECT_EQ(root_child_fragment.Get(), test_fragment.Get());
// Test navigation from first child root (R3)
ComPtr<IRawElementProviderFragmentRoot> n3_fragment_root_provider;
n3_fragment_root->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&n3_fragment_root_provider));
ComPtr<IRawElementProviderFragment> n3_fragment;
n3_fragment_root_provider.As(&n3_fragment);
EXPECT_HRESULT_SUCCEEDED(
n3_fragment->Navigate(NavigateDirection_Parent, &test_fragment));
EXPECT_EQ(root_child_fragment.Get(), test_fragment.Get());
AXNode* sibling_n2_node = root_node->children()[0];
EXPECT_HRESULT_SUCCEEDED(
n3_fragment->Navigate(NavigateDirection_PreviousSibling, &test_fragment));
EXPECT_EQ(IRawElementProviderFragmentFromNode(sibling_n2_node).Get(),
test_fragment.Get());
AXNode* sibling_n4_node = root_node->children()[2];
EXPECT_HRESULT_SUCCEEDED(
n3_fragment->Navigate(NavigateDirection_NextSibling, &test_fragment));
EXPECT_EQ(IRawElementProviderFragmentFromNode(sibling_n4_node).Get(),
test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
n3_fragment->Navigate(NavigateDirection_FirstChild, &test_fragment));
EXPECT_EQ(
IRawElementProviderFragmentFromNode(child_fragment_root_n3_node).Get(),
test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
n3_fragment->Navigate(NavigateDirection_LastChild, &test_fragment));
EXPECT_EQ(
IRawElementProviderFragmentFromNode(child_fragment_root_n3_node).Get(),
test_fragment.Get());
// Test navigation from second child root (R5)
ComPtr<IRawElementProviderFragmentRoot> n5_fragment_root_provider;
n5_fragment_root->GetNativeViewAccessible()->QueryInterface(
IID_PPV_ARGS(&n5_fragment_root_provider));
ComPtr<IRawElementProviderFragment> n5_fragment;
n5_fragment_root_provider.As(&n5_fragment);
EXPECT_HRESULT_SUCCEEDED(
n5_fragment->Navigate(NavigateDirection_Parent, &test_fragment));
EXPECT_EQ(root_child_fragment.Get(), test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
n5_fragment->Navigate(NavigateDirection_NextSibling, &test_fragment));
EXPECT_EQ(nullptr, test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
n5_fragment->Navigate(NavigateDirection_PreviousSibling, &test_fragment));
EXPECT_EQ(IRawElementProviderFragmentFromNode(sibling_n4_node).Get(),
test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
n5_fragment->Navigate(NavigateDirection_FirstChild, &test_fragment));
EXPECT_EQ(
IRawElementProviderFragmentFromNode(child_fragment_root_n5_node).Get(),
test_fragment.Get());
EXPECT_HRESULT_SUCCEEDED(
n5_fragment->Navigate(NavigateDirection_LastChild, &test_fragment));
EXPECT_EQ(
IRawElementProviderFragmentFromNode(child_fragment_root_n5_node).Get(),
test_fragment.Get());
}
TEST_F(AXFragmentRootTest, TestFragmentRootMap) {
AXNodeData root;
Init(root);
// There should be nothing in the map before we create a fragment root.
// Call GetForAcceleratedWidget() first to ensure that querying for a
// fragment root doesn't inadvertently create an empty entry in the map
// (https://crbug.com/1071185).
EXPECT_EQ(nullptr, AXFragmentRootWin::GetForAcceleratedWidget(
gfx::kMockAcceleratedWidget));
EXPECT_EQ(nullptr, AXFragmentRootWin::GetFragmentRootParentOf(
GetRootIAccessible().Get()));
// After initializing a fragment root, we should be able to retrieve it using
// its accelerated widget, or as the parent of its child.
InitFragmentRoot();
EXPECT_EQ(ax_fragment_root_.get(), AXFragmentRootWin::GetForAcceleratedWidget(
gfx::kMockAcceleratedWidget));
EXPECT_EQ(ax_fragment_root_.get(), AXFragmentRootWin::GetFragmentRootParentOf(
GetRootIAccessible().Get()));
// After deleting a fragment root, it should no longer be reachable from the
// map.
ax_fragment_root_.reset();
EXPECT_EQ(nullptr, AXFragmentRootWin::GetForAcceleratedWidget(
gfx::kMockAcceleratedWidget));
EXPECT_EQ(nullptr, AXFragmentRootWin::GetFragmentRootParentOf(
GetRootIAccessible().Get()));
}
} // namespace ui

View File

@@ -0,0 +1,33 @@
// Copyright 2019 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 "ui/accessibility/platform/ax_platform_node_delegate_utils_win.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/platform/ax_platform_node.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
namespace ui {
bool IsValuePatternSupported(AXPlatformNodeDelegate* delegate) {
// https://docs.microsoft.com/en-us/windows/desktop/winauto/uiauto-implementingvalue
// The Value control pattern is used to support controls that have an
// intrinsic value not spanning a range and that can be represented as
// a string.
//
// IValueProvider must be implemented by controls such as the color picker
// selection control [...]
// https://www.w3.org/TR/html-aam-1.0/
// The HTML AAM maps "href [a; area]" to UIA Value.Value
return delegate->GetData().IsRangeValueSupported() ||
IsReadOnlySupported(delegate->GetData().role) ||
IsLink(delegate->GetData().role) ||
delegate->GetData().role == ax::mojom::Role::kColorWell ||
delegate->IsCellOrHeaderOfARIAGrid();
}
} // namespace ui

View File

@@ -0,0 +1,19 @@
// Copyright 2019 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 UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_DELEGATE_UTILS_WIN_H_
#define UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_DELEGATE_UTILS_WIN_H_
#include "ui/accessibility/ax_export.h"
namespace ui {
class AXPlatformNodeDelegate;
// Returns true if the value pattern is supported
AX_EXPORT bool IsValuePatternSupported(AXPlatformNodeDelegate*);
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_DELEGATE_UTILS_WIN_H_

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,138 @@
// Copyright 2019 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 UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_WIN_UNITTEST_H_
#define UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_WIN_UNITTEST_H_
#include "ui/accessibility/platform/ax_platform_node_unittest.h"
#include <memory>
#include <unordered_set>
#include "base/test/scoped_feature_list.h"
#include "ui/accessibility/platform/ax_fragment_root_delegate_win.h"
#include "ui/base/win/accessibility_misc_utils.h"
struct IAccessible;
struct IAccessible2;
struct IAccessible2_2;
struct IAccessibleTableCell;
struct IRawElementProviderFragment;
struct IRawElementProviderFragmentRoot;
struct IRawElementProviderSimple;
struct IUnknown;
namespace base {
namespace win {
class ScopedVariant;
} // namespace win
} // namespace base
namespace ui {
class AXFragmentRootWin;
class AXPlatformNode;
class TestFragmentRootDelegate : public AXFragmentRootDelegateWin {
public:
TestFragmentRootDelegate();
virtual ~TestFragmentRootDelegate();
gfx::NativeViewAccessible GetChildOfAXFragmentRoot() override;
gfx::NativeViewAccessible GetParentOfAXFragmentRoot() override;
bool IsAXFragmentRootAControlElement() override;
gfx::NativeViewAccessible child_ = nullptr;
gfx::NativeViewAccessible parent_ = nullptr;
bool is_control_element_ = true;
};
class MockIRawElementProviderSimple
: public CComObjectRootEx<CComMultiThreadModel>,
public IRawElementProviderSimple {
public:
BEGIN_COM_MAP(MockIRawElementProviderSimple)
COM_INTERFACE_ENTRY(IRawElementProviderSimple)
END_COM_MAP()
MockIRawElementProviderSimple();
~MockIRawElementProviderSimple();
static HRESULT CreateMockIRawElementProviderSimple(
IRawElementProviderSimple** provider);
//
// IRawElementProviderSimple methods.
//
IFACEMETHODIMP GetPatternProvider(PATTERNID pattern_id,
IUnknown** result) override;
IFACEMETHODIMP GetPropertyValue(PROPERTYID property_id,
VARIANT* result) override;
IFACEMETHODIMP
get_ProviderOptions(enum ProviderOptions* ret) override;
IFACEMETHODIMP
get_HostRawElementProvider(IRawElementProviderSimple** provider) override;
};
class AXPlatformNodeWinTest : public AXPlatformNodeTest {
public:
AXPlatformNodeWinTest();
~AXPlatformNodeWinTest() override;
void SetUp() override;
void TearDown() override;
protected:
static const base::string16 kEmbeddedCharacterAsString;
AXPlatformNode* AXPlatformNodeFromNode(AXNode* node);
template <typename T>
Microsoft::WRL::ComPtr<T> QueryInterfaceFromNodeId(AXNode::AXID id);
template <typename T>
Microsoft::WRL::ComPtr<T> QueryInterfaceFromNode(AXNode* node);
Microsoft::WRL::ComPtr<IRawElementProviderSimple>
GetRootIRawElementProviderSimple();
Microsoft::WRL::ComPtr<IRawElementProviderSimple>
GetIRawElementProviderSimpleFromChildIndex(int child_index);
Microsoft::WRL::ComPtr<IRawElementProviderSimple>
GetIRawElementProviderSimpleFromTree(const ui::AXTreeID tree_id,
const AXNode::AXID node_id);
Microsoft::WRL::ComPtr<IRawElementProviderFragment>
GetRootIRawElementProviderFragment();
Microsoft::WRL::ComPtr<IRawElementProviderFragment>
IRawElementProviderFragmentFromNode(AXNode* node);
Microsoft::WRL::ComPtr<IAccessible> IAccessibleFromNode(AXNode* node);
Microsoft::WRL::ComPtr<IAccessible> GetRootIAccessible();
Microsoft::WRL::ComPtr<IAccessible2> ToIAccessible2(
Microsoft::WRL::ComPtr<IUnknown> unknown);
Microsoft::WRL::ComPtr<IAccessible2> ToIAccessible2(
Microsoft::WRL::ComPtr<IAccessible> accessible);
Microsoft::WRL::ComPtr<IAccessible2_2> ToIAccessible2_2(
Microsoft::WRL::ComPtr<IAccessible> accessible);
void CheckVariantHasName(const base::win::ScopedVariant& variant,
const wchar_t* expected_name);
void CheckIUnknownHasName(Microsoft::WRL::ComPtr<IUnknown> unknown,
const wchar_t* expected_name);
Microsoft::WRL::ComPtr<IAccessibleTableCell> GetCellInTable();
void InitFragmentRoot();
AXFragmentRootWin* InitNodeAsFragmentRoot(AXNode* node,
TestFragmentRootDelegate* delegate);
Microsoft::WRL::ComPtr<IRawElementProviderFragmentRoot> GetFragmentRoot();
using PatternSet = std::unordered_set<LONG>;
PatternSet GetSupportedPatternsFromNodeId(AXNode::AXID id);
std::unique_ptr<AXFragmentRootWin> ax_fragment_root_;
std::unique_ptr<TestFragmentRootDelegate> test_fragment_root_delegate_;
base::test::ScopedFeatureList scoped_feature_list_;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_NODE_WIN_UNITTEST_H_

View File

@@ -0,0 +1,319 @@
// Copyright 2017 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 "ui/accessibility/platform/ax_platform_relation_win.h"
#include <wrl/client.h>
#include <algorithm>
#include <vector>
#include "base/lazy_instance.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/enum_variant.h"
#include "base/win/scoped_variant.h"
#include "third_party/iaccessible2/ia2_api_all.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_mode_observer.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_text_utils.h"
#include "ui/accessibility/ax_tree_data.h"
#include "ui/accessibility/platform/ax_platform_node_base.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
#include "ui/accessibility/platform/ax_unique_id.h"
#include "ui/base/win/atl_module.h"
#include "ui/gfx/geometry/rect_conversions.h"
namespace ui {
AXPlatformRelationWin::AXPlatformRelationWin() {
win::CreateATLModuleIfNeeded();
}
AXPlatformRelationWin::~AXPlatformRelationWin() {}
base::string16 GetIA2RelationFromIntAttr(ax::mojom::IntAttribute attribute) {
switch (attribute) {
case ax::mojom::IntAttribute::kMemberOfId:
return IA2_RELATION_MEMBER_OF;
case ax::mojom::IntAttribute::kErrormessageId:
return IA2_RELATION_ERROR;
case ax::mojom::IntAttribute::kPopupForId:
// Map "popup for" to "controlled by".
// Unlike ATK there is no special IA2 popup-for relationship, but it can
// be exposed via the controlled by relation, which is also computed for
// content as the reverse of the controls relationship.
return IA2_RELATION_CONTROLLED_BY;
default:
break;
}
return base::string16();
}
base::string16 GetIA2RelationFromIntListAttr(
ax::mojom::IntListAttribute attribute) {
switch (attribute) {
case ax::mojom::IntListAttribute::kControlsIds:
return IA2_RELATION_CONTROLLER_FOR;
case ax::mojom::IntListAttribute::kDescribedbyIds:
return IA2_RELATION_DESCRIBED_BY;
case ax::mojom::IntListAttribute::kDetailsIds:
return IA2_RELATION_DETAILS;
case ax::mojom::IntListAttribute::kFlowtoIds:
return IA2_RELATION_FLOWS_TO;
case ax::mojom::IntListAttribute::kLabelledbyIds:
return IA2_RELATION_LABELLED_BY;
default:
break;
}
return base::string16();
}
base::string16 GetIA2ReverseRelationFromIntAttr(
ax::mojom::IntAttribute attribute) {
switch (attribute) {
case ax::mojom::IntAttribute::kErrormessageId:
return IA2_RELATION_ERROR_FOR;
default:
break;
}
return base::string16();
}
base::string16 GetIA2ReverseRelationFromIntListAttr(
ax::mojom::IntListAttribute attribute) {
switch (attribute) {
case ax::mojom::IntListAttribute::kControlsIds:
return IA2_RELATION_CONTROLLED_BY;
case ax::mojom::IntListAttribute::kDescribedbyIds:
return IA2_RELATION_DESCRIPTION_FOR;
case ax::mojom::IntListAttribute::kDetailsIds:
return IA2_RELATION_DETAILS_FOR;
case ax::mojom::IntListAttribute::kFlowtoIds:
return IA2_RELATION_FLOWS_FROM;
case ax::mojom::IntListAttribute::kLabelledbyIds:
return IA2_RELATION_LABEL_FOR;
default:
break;
}
return base::string16();
}
// static
int AXPlatformRelationWin::EnumerateRelationships(
AXPlatformNodeBase* node,
int desired_index,
const base::string16& desired_ia2_relation,
base::string16* out_ia2_relation,
std::set<AXPlatformNode*>* out_targets) {
const AXNodeData& node_data = node->GetData();
AXPlatformNodeDelegate* delegate = node->GetDelegate();
// The first time this is called, populate vectors with all of the
// int attributes and intlist attributes that have reverse relations
// we care about on Windows. Computing these by calling
// GetIA2ReverseRelationFrom{Int|IntList}Attr on every possible attribute
// simplifies the work needed to support an additional relation
// attribute in the future.
static std::vector<ax::mojom::IntAttribute>
int_attributes_with_reverse_relations;
static std::vector<ax::mojom::IntListAttribute>
intlist_attributes_with_reverse_relations;
static bool first_time = true;
if (first_time) {
for (int32_t attr_index =
static_cast<int32_t>(ax::mojom::IntAttribute::kNone);
attr_index <= static_cast<int32_t>(ax::mojom::IntAttribute::kMaxValue);
++attr_index) {
auto attr = static_cast<ax::mojom::IntAttribute>(attr_index);
if (!GetIA2ReverseRelationFromIntAttr(attr).empty())
int_attributes_with_reverse_relations.push_back(attr);
}
for (int32_t attr_index =
static_cast<int32_t>(ax::mojom::IntListAttribute::kNone);
attr_index <=
static_cast<int32_t>(ax::mojom::IntListAttribute::kMaxValue);
++attr_index) {
auto attr = static_cast<ax::mojom::IntListAttribute>(attr_index);
if (!GetIA2ReverseRelationFromIntListAttr(attr).empty())
intlist_attributes_with_reverse_relations.push_back(attr);
}
first_time = false;
}
// Enumerate all of the relations and reverse relations that
// are exposed via IAccessible2 on Windows. We do this with a series
// of loops. Every time we encounter one, we check if the caller
// requested that particular relation by index, and return it.
// Otherwise we build up and return the total number of relations found.
int total_count = 0;
// Iterate over all int attributes on this node to check the ones
// that correspond to IAccessible2 relations.
for (size_t i = 0; i < node_data.int_attributes.size(); ++i) {
ax::mojom::IntAttribute int_attribute = node_data.int_attributes[i].first;
base::string16 relation = GetIA2RelationFromIntAttr(int_attribute);
if (!relation.empty() &&
(desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
// Skip reflexive relations
if (node_data.int_attributes[i].second == node_data.id)
continue;
if (desired_index == total_count) {
*out_ia2_relation = relation;
out_targets->insert(
delegate->GetFromNodeID(node_data.int_attributes[i].second));
return 1;
}
total_count++;
}
}
// Iterate over all of the int attributes that have reverse relations
// in IAccessible2, and query AXTree to see if the reverse relation exists.
for (ax::mojom::IntAttribute int_attribute :
int_attributes_with_reverse_relations) {
base::string16 relation = GetIA2ReverseRelationFromIntAttr(int_attribute);
std::set<AXPlatformNode*> targets =
delegate->GetReverseRelations(int_attribute);
// Erase reflexive relations.
targets.erase(node);
if (targets.size()) {
if (!relation.empty() &&
(desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
if (desired_index == total_count) {
*out_ia2_relation = relation;
*out_targets = targets;
return 1;
}
total_count++;
}
}
}
// Iterate over all intlist attributes on this node to check the ones
// that correspond to IAccessible2 relations.
for (size_t i = 0; i < node_data.intlist_attributes.size(); ++i) {
ax::mojom::IntListAttribute intlist_attribute =
node_data.intlist_attributes[i].first;
base::string16 relation = GetIA2RelationFromIntListAttr(intlist_attribute);
if (!relation.empty() &&
(desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
if (desired_index == total_count) {
*out_ia2_relation = relation;
for (int32_t target_id : node_data.intlist_attributes[i].second) {
// Skip reflexive relations
if (target_id == node_data.id)
continue;
out_targets->insert(delegate->GetFromNodeID(target_id));
}
if (out_targets->size() == 0)
continue;
return 1;
}
total_count++;
}
}
// Iterate over all of the intlist attributes that have reverse relations
// in IAccessible2, and query AXTree to see if the reverse relation exists.
for (ax::mojom::IntListAttribute intlist_attribute :
intlist_attributes_with_reverse_relations) {
base::string16 relation =
GetIA2ReverseRelationFromIntListAttr(intlist_attribute);
std::set<AXPlatformNode*> targets =
delegate->GetReverseRelations(intlist_attribute);
// Erase reflexive relations.
targets.erase(node);
if (targets.size()) {
if (!relation.empty() &&
(desired_ia2_relation.empty() || desired_ia2_relation == relation)) {
if (desired_index == total_count) {
*out_ia2_relation = relation;
*out_targets = targets;
return 1;
}
total_count++;
}
}
}
return total_count;
}
void AXPlatformRelationWin::Initialize(const base::string16& type) {
type_ = type;
}
void AXPlatformRelationWin::Invalidate() {
targets_.clear();
}
void AXPlatformRelationWin::AddTarget(AXPlatformNodeWin* target) {
targets_.push_back(target);
}
IFACEMETHODIMP AXPlatformRelationWin::get_relationType(BSTR* relation_type) {
if (!relation_type)
return E_INVALIDARG;
*relation_type = SysAllocString(type_.c_str());
DCHECK(*relation_type);
return S_OK;
}
IFACEMETHODIMP AXPlatformRelationWin::get_nTargets(LONG* n_targets) {
if (!n_targets)
return E_INVALIDARG;
*n_targets = static_cast<LONG>(targets_.size());
return S_OK;
}
IFACEMETHODIMP AXPlatformRelationWin::get_target(LONG target_index,
IUnknown** target) {
if (!target)
return E_INVALIDARG;
if (target_index < 0 || target_index >= static_cast<LONG>(targets_.size())) {
return E_INVALIDARG;
}
*target = static_cast<IAccessible*>(targets_[target_index].Get());
(*target)->AddRef();
return S_OK;
}
IFACEMETHODIMP AXPlatformRelationWin::get_targets(LONG max_targets,
IUnknown** targets,
LONG* n_targets) {
if (!targets || !n_targets)
return E_INVALIDARG;
LONG count = static_cast<LONG>(targets_.size());
if (count > max_targets)
count = max_targets;
*n_targets = count;
if (count == 0)
return S_FALSE;
for (LONG i = 0; i < count; ++i) {
HRESULT result = get_target(i, &targets[i]);
if (result != S_OK)
return result;
}
return S_OK;
}
IFACEMETHODIMP
AXPlatformRelationWin::get_localizedRelationType(BSTR* relation_type) {
return E_NOTIMPL;
}
} // namespace ui

View File

@@ -0,0 +1,82 @@
// Copyright 2017 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 UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_RELATION_WIN_H_
#define UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_RELATION_WIN_H_
#include <oleacc.h>
#include <wrl/client.h>
#include <set>
#include <vector>
#include "base/compiler_specific.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/win/atl.h"
#include "third_party/iaccessible2/ia2_api_all.h"
#include "ui/accessibility/ax_export.h"
#include "ui/accessibility/ax_text_utils.h"
#include "ui/accessibility/platform/ax_platform_node_win.h"
namespace ui {
//
// AXPlatformRelationWin
//
// A simple implementation of IAccessibleRelation, used to represent a
// relationship between one accessible node in the tree and
// potentially multiple target nodes. Also contains a utility function
// to compute all of the possible IAccessible2 relations and reverse
// relations given the internal relation id attributes.
class AXPlatformRelationWin : public CComObjectRootEx<CComMultiThreadModel>,
public IAccessibleRelation {
public:
BEGIN_COM_MAP(AXPlatformRelationWin)
COM_INTERFACE_ENTRY(IAccessibleRelation)
END_COM_MAP()
AXPlatformRelationWin();
virtual ~AXPlatformRelationWin();
// This is the main utility function that enumerates all of the possible
// IAccessible2 relations between one node and any other node in the tree.
// Forward relations come from the int_attributes and intlist_attributes
// in node_data. Reverse relations come from querying |delegate| for the
// reverse relations given |node_data.id|.
//
// If you pass -1 for |desired_index| and "" for |desired_ia2_relation|,
// it will return a count of all relations.
//
// If you pass either an index in |desired_index| or a specific relation
// in |desired_ia2_relation|, the first matching relation will be returned in
// |out_ia2_relation| and |out_targets| (both of which must not be null),
// and it will return 1 on success, and 0 if none were found matching that
// criteria.
static int EnumerateRelationships(AXPlatformNodeBase* node,
int desired_index,
const base::string16& desired_ia2_relation,
base::string16* out_ia2_relation,
std::set<AXPlatformNode*>* out_targets);
void Initialize(const base::string16& type);
void Invalidate();
void AddTarget(AXPlatformNodeWin* target);
// IAccessibleRelation methods.
IFACEMETHODIMP get_relationType(BSTR* relation_type) override;
IFACEMETHODIMP get_nTargets(LONG* n_targets) override;
IFACEMETHODIMP get_target(LONG target_index, IUnknown** target) override;
IFACEMETHODIMP get_targets(LONG max_targets,
IUnknown** targets,
LONG* n_targets) override;
IFACEMETHODIMP get_localizedRelationType(BSTR* relation_type) override;
private:
base::string16 type_;
std::vector<Microsoft::WRL::ComPtr<AXPlatformNodeWin>> targets_;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_AX_PLATFORM_RELATION_WIN_H_

View File

@@ -0,0 +1,50 @@
// 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 "uia_registrar_win.h"
#include <wrl/implements.h>
#include "base/stl_util.h"
namespace ui {
UiaRegistrarWin::UiaRegistrarWin() {
// Create the registrar object and get the IUIAutomationRegistrar
// interface pointer.
Microsoft::WRL::ComPtr<IUIAutomationRegistrar> registrar;
if (FAILED(CoCreateInstance(CLSID_CUIAutomationRegistrar, nullptr,
CLSCTX_INPROC_SERVER, IID_IUIAutomationRegistrar,
&registrar)))
return;
// Register the custom UIA property that represents the unique id of an UIA
// element which also matches its corresponding IA2 element's unique id.
UIAutomationPropertyInfo unique_id_property_info = {
kUiaPropertyUniqueIdGuid, L"UniqueId", UIAutomationType_String};
registrar->RegisterProperty(&unique_id_property_info,
&uia_unique_id_property_id_);
// Register the custom UIA event that represents the test end event for the
// UIA test suite.
UIAutomationEventInfo test_complete_event_info = {
kUiaEventTestCompleteSentinelGuid, L"kUiaTestCompleteSentinel"};
registrar->RegisterEvent(&test_complete_event_info,
&uia_test_complete_event_id_);
}
UiaRegistrarWin::~UiaRegistrarWin() = default;
PROPERTYID UiaRegistrarWin::GetUiaUniqueIdPropertyId() const {
return uia_unique_id_property_id_;
}
EVENTID UiaRegistrarWin::GetUiaTestCompleteEventId() const {
return uia_test_complete_event_id_;
}
const UiaRegistrarWin& UiaRegistrarWin::GetInstance() {
static base::NoDestructor<UiaRegistrarWin> instance;
return *instance;
}
} // namespace ui

View File

@@ -0,0 +1,45 @@
// 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 UI_ACCESSIBILITY_PLATFORM_UIA_REGISTRAR_WIN_H_
#define UI_ACCESSIBILITY_PLATFORM_UIA_REGISTRAR_WIN_H_
#include <objbase.h>
#include <uiautomation.h>
#include "base/macros.h"
#include "base/no_destructor.h"
#include "ax/ax_export.h"
namespace ui {
// {3761326A-34B2-465A-835D-7A3D8F4EFB92}
static const GUID kUiaEventTestCompleteSentinelGuid = {
0x3761326a,
0x34b2,
0x465a,
{0x83, 0x5d, 0x7a, 0x3d, 0x8f, 0x4e, 0xfb, 0x92}};
// {cc7eeb32-4b62-4f4c-aff6-1c2e5752ad8e}
static const GUID kUiaPropertyUniqueIdGuid = {
0xcc7eeb32,
0x4b62,
0x4f4c,
{0xaf, 0xf6, 0x1c, 0x2e, 0x57, 0x52, 0xad, 0x8e}};
class AX_EXPORT UiaRegistrarWin {
public:
UiaRegistrarWin();
~UiaRegistrarWin();
PROPERTYID GetUiaUniqueIdPropertyId() const;
EVENTID GetUiaTestCompleteEventId() const;
static const UiaRegistrarWin& GetInstance();
private:
PROPERTYID uia_unique_id_property_id_ = 0;
EVENTID uia_test_complete_event_id_ = 0;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_UIA_REGISTRAR_WIN_H_

View File

@@ -0,0 +1,29 @@
// Copyright (c) 2012 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 BASE_BASE_EXPORT_H_
#define BASE_BASE_EXPORT_H_
#if defined(COMPONENT_BUILD)
#if defined(WIN32)
#if defined(BASE_IMPLEMENTATION)
#define BASE_EXPORT __declspec(dllexport)
#else
#define BASE_EXPORT __declspec(dllimport)
#endif // defined(BASE_IMPLEMENTATION)
#else // defined(WIN32)
#if defined(BASE_IMPLEMENTATION)
#define BASE_EXPORT __attribute__((visibility("default")))
#else
#define BASE_EXPORT
#endif // defined(BASE_IMPLEMENTATION)
#endif
#else // defined(COMPONENT_BUILD)
#define BASE_EXPORT
#endif
#endif // BASE_BASE_EXPORT_H_

View File

@@ -0,0 +1,38 @@
// Copyright 2018 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 BASE_WIN_ATL_H_
#define BASE_WIN_ATL_H_
// Check no prior poisonous defines were made.
#include "base/win/windows_defines.inc"
// Undefine before windows header will make the poisonous defines
#include "base/win/windows_undefines.inc"
// atlwin.h relies on std::void_t, but libc++ doesn't define it unless
// _LIBCPP_STD_VER > 14. Workaround this by manually defining it.
#include <type_traits>
#if defined(_LIBCPP_STD_VER) && _LIBCPP_STD_VER <= 14
namespace std {
template <class...>
using void_t = void;
}
#endif
// Declare our own exception thrower (atl_throw.h includes atldef.h).
#include "base/win/atl_throw.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
#include <atlhost.h>
#include <atlsecurity.h>
#include <atlwin.h>
// Undefine the poisonous defines
#include "base/win/windows_undefines.inc"
// Check no poisonous defines follow this include
#include "base/win/windows_defines.inc"
#endif // BASE_WIN_ATL_H_

View File

@@ -0,0 +1,32 @@
// Copyright (c) 2011 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 UI_BASE_WIN_ATL_MODULE_H_
#define UI_BASE_WIN_ATL_MODULE_H_
namespace ui {
namespace win {
// Ensure that we have exactly one ATL module registered. It's safe to
// call this more than once. ATL functions will crash if there's no
// ATL module registered, or if you try to register two of them, so
// dynamically registering one if needed makes it much easier for us
// to support different build configurations like multi-dll without
// worrying about which side of a module boundary each ATL module object
// belongs on.
//
// This function must be implemented in this header file rather than a
// source file so that it's inlined into the module where it's included,
// rather than in the "ui" module.
inline void CreateATLModuleIfNeeded() {
if (_pAtlModule == NULL) {
// This creates the module and automatically updates _pAtlModule.
new CComModule;
}
}
} // namespace win
} // namespace ui
#endif // UI_BASE_WIN_ATL_MODULE_H_

View File

@@ -0,0 +1,81 @@
// Copyright (c) 2011 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 "base/win/enum_variant.h"
#include <wrl/client.h>
#include <algorithm>
#include "base/check_op.h"
namespace base {
namespace win {
EnumVariant::EnumVariant(ULONG count) : current_index_(0) {
for (ULONG i = 0; i < count; ++i)
items_.emplace_back(ScopedVariant::kEmptyVariant);
}
EnumVariant::~EnumVariant() = default;
VARIANT* EnumVariant::ItemAt(ULONG index) {
DCHECK_LT(index, items_.size());
// This is a hack to return a mutable pointer to the ScopedVariant, even
// though the original intent of the AsInput method was to allow only readonly
// access to the wrapped variant.
return items_[index].AsInput();
}
HRESULT EnumVariant::Next(ULONG requested_count,
VARIANT* out_elements,
ULONG* out_elements_received) {
if (!out_elements)
return E_INVALIDARG;
DCHECK_LE(current_index_, items_.size());
ULONG available_count = ULONG{items_.size()} - current_index_;
ULONG count = std::min(requested_count, available_count);
for (ULONG i = 0; i < count; ++i)
out_elements[i] = items_[current_index_ + i].Copy();
current_index_ += count;
// The caller can choose not to get the number of received elements by setting
// |out_elements_received| to nullptr.
if (out_elements_received)
*out_elements_received = count;
return (count == requested_count ? S_OK : S_FALSE);
}
HRESULT EnumVariant::Skip(ULONG skip_count) {
ULONG count = skip_count;
if (current_index_ + count > ULONG{items_.size()})
count = ULONG{items_.size()} - current_index_;
current_index_ += count;
return (count == skip_count ? S_OK : S_FALSE);
}
HRESULT EnumVariant::Reset() {
current_index_ = 0;
return S_OK;
}
HRESULT EnumVariant::Clone(IEnumVARIANT** out_cloned_object) {
if (!out_cloned_object)
return E_INVALIDARG;
size_t count = items_.size();
Microsoft::WRL::ComPtr<EnumVariant> other =
Microsoft::WRL::Make<EnumVariant>(ULONG{count});
for (size_t i = 0; i < count; ++i)
other->items_[i] = static_cast<const VARIANT&>(items_[i]);
other->Skip(current_index_);
return other.CopyTo(IID_PPV_ARGS(out_cloned_object));
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,49 @@
// Copyright (c) 2011 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 BASE_WIN_ENUM_VARIANT_H_
#define BASE_WIN_ENUM_VARIANT_H_
#include <wrl/implements.h>
#include <memory>
#include <vector>
#include "base/win/scoped_variant.h"
namespace base {
namespace win {
// A simple implementation of IEnumVARIANT.
class BASE_EXPORT EnumVariant
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
IEnumVARIANT> {
public:
// The constructor allocates a vector of empty ScopedVariants of size |count|.
// Use ItemAt to set the value of each item in the array.
explicit EnumVariant(ULONG count);
// IEnumVARIANT:
IFACEMETHODIMP Next(ULONG requested_count,
VARIANT* out_elements,
ULONG* out_elements_received) override;
IFACEMETHODIMP Skip(ULONG skip_count) override;
IFACEMETHODIMP Reset() override;
IFACEMETHODIMP Clone(IEnumVARIANT** out_cloned_object) override;
// Returns a mutable pointer to the item at position |index|.
VARIANT* ItemAt(ULONG index);
private:
~EnumVariant() override;
std::vector<ScopedVariant> items_;
ULONG current_index_;
};
} // namespace win
} // namespace base
#endif // BASE_WIN_ENUM_VARIANT_H_

View File

@@ -0,0 +1,130 @@
// Copyright (c) 2011 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 "base/win/enum_variant.h"
#include <wrl/client.h>
#include <wrl/implements.h>
#include "base/win/scoped_com_initializer.h"
#include "base/win/scoped_variant.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
TEST(EnumVariantTest, EmptyEnumVariant) {
ScopedCOMInitializer com_initializer;
Microsoft::WRL::ComPtr<EnumVariant> ev = Microsoft::WRL::Make<EnumVariant>(0);
Microsoft::WRL::ComPtr<IEnumVARIANT> ienumvariant;
ASSERT_TRUE(SUCCEEDED(ev->QueryInterface(IID_PPV_ARGS(&ienumvariant))));
{
base::win::ScopedVariant out_element;
ULONG out_received = 0;
EXPECT_EQ(S_FALSE, ev->Next(1, out_element.Receive(), &out_received));
EXPECT_EQ(0u, out_received);
}
EXPECT_EQ(S_FALSE, ev->Skip(1));
EXPECT_EQ(S_OK, ev->Reset());
Microsoft::WRL::ComPtr<IEnumVARIANT> ev2;
EXPECT_EQ(S_OK, ev->Clone(&ev2));
EXPECT_NE(nullptr, ev2);
EXPECT_NE(ev, ev2);
EXPECT_EQ(S_FALSE, ev2->Skip(1));
EXPECT_EQ(S_OK, ev2->Reset());
}
TEST(EnumVariantTest, SimpleEnumVariant) {
ScopedCOMInitializer com_initializer;
Microsoft::WRL::ComPtr<EnumVariant> ev = Microsoft::WRL::Make<EnumVariant>(3);
ev->ItemAt(0)->vt = VT_I4;
ev->ItemAt(0)->lVal = 10;
ev->ItemAt(1)->vt = VT_I4;
ev->ItemAt(1)->lVal = 20;
ev->ItemAt(2)->vt = VT_I4;
ev->ItemAt(2)->lVal = 30;
// Get elements one at a time from index 0 and 2.
base::win::ScopedVariant out_element_0;
ULONG out_received_0 = 0;
EXPECT_EQ(S_OK, ev->Next(1, out_element_0.Receive(), &out_received_0));
EXPECT_EQ(1u, out_received_0);
EXPECT_EQ(VT_I4, out_element_0.ptr()->vt);
EXPECT_EQ(10, out_element_0.ptr()->lVal);
EXPECT_EQ(S_OK, ev->Skip(1));
base::win::ScopedVariant out_element_2;
ULONG out_received_2 = 0;
EXPECT_EQ(S_OK, ev->Next(1, out_element_2.Receive(), &out_received_2));
EXPECT_EQ(1u, out_received_2);
EXPECT_EQ(VT_I4, out_element_2.ptr()->vt);
EXPECT_EQ(30, out_element_2.ptr()->lVal);
base::win::ScopedVariant placeholder_variant;
EXPECT_EQ(S_FALSE, ev->Next(1, placeholder_variant.Receive(), nullptr));
// Verify the reset works for the next step.
ASSERT_EQ(S_OK, ev->Reset());
// Get all elements at once.
VARIANT out_elements[3];
ULONG out_received_multiple;
for (int i = 0; i < 3; ++i)
::VariantInit(&out_elements[i]);
EXPECT_EQ(S_OK, ev->Next(3, out_elements, &out_received_multiple));
EXPECT_EQ(3u, out_received_multiple);
EXPECT_EQ(VT_I4, out_elements[0].vt);
EXPECT_EQ(10, out_elements[0].lVal);
EXPECT_EQ(VT_I4, out_elements[1].vt);
EXPECT_EQ(20, out_elements[1].lVal);
EXPECT_EQ(VT_I4, out_elements[2].vt);
EXPECT_EQ(30, out_elements[2].lVal);
for (int i = 0; i < 3; ++i)
::VariantClear(&out_elements[i]);
base::win::ScopedVariant placeholder_variant_multiple;
EXPECT_EQ(S_FALSE,
ev->Next(1, placeholder_variant_multiple.Receive(), nullptr));
}
TEST(EnumVariantTest, Clone) {
ScopedCOMInitializer com_initializer;
Microsoft::WRL::ComPtr<EnumVariant> ev = Microsoft::WRL::Make<EnumVariant>(3);
ev->ItemAt(0)->vt = VT_I4;
ev->ItemAt(0)->lVal = 10;
ev->ItemAt(1)->vt = VT_I4;
ev->ItemAt(1)->lVal = 20;
ev->ItemAt(2)->vt = VT_I4;
ev->ItemAt(2)->lVal = 30;
// Clone it.
Microsoft::WRL::ComPtr<IEnumVARIANT> ev2;
EXPECT_EQ(S_OK, ev->Clone(&ev2));
EXPECT_TRUE(ev2 != nullptr);
VARIANT out_elements[3];
for (int i = 0; i < 3; ++i)
::VariantInit(&out_elements[i]);
EXPECT_EQ(S_OK, ev2->Next(3, out_elements, nullptr));
EXPECT_EQ(VT_I4, out_elements[0].vt);
EXPECT_EQ(10, out_elements[0].lVal);
EXPECT_EQ(VT_I4, out_elements[1].vt);
EXPECT_EQ(20, out_elements[1].lVal);
EXPECT_EQ(VT_I4, out_elements[2].vt);
EXPECT_EQ(30, out_elements[2].lVal);
for (int i = 0; i < 3; ++i)
::VariantClear(&out_elements[i]);
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,96 @@
// Copyright (c) 2010 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 "base/win/scoped_bstr.h"
#include <stdint.h>
#include "base/check.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/memory.h"
#include "base/strings/string_util.h"
namespace base {
namespace win {
namespace {
BSTR AllocBstrOrDie(WStringPiece non_bstr) {
BSTR result = ::SysAllocStringLen(non_bstr.data(),
checked_cast<UINT>(non_bstr.length()));
if (!result) {
base::TerminateBecauseOutOfMemory((non_bstr.length() + 1) *
sizeof(wchar_t));
}
return result;
}
BSTR AllocBstrBytesOrDie(size_t bytes) {
BSTR result = ::SysAllocStringByteLen(nullptr, checked_cast<UINT>(bytes));
if (!result)
base::TerminateBecauseOutOfMemory(bytes + sizeof(wchar_t));
return result;
}
} // namespace
ScopedBstr::ScopedBstr(WStringPiece non_bstr)
: bstr_(AllocBstrOrDie(non_bstr)) {}
ScopedBstr::~ScopedBstr() {
static_assert(sizeof(ScopedBstr) == sizeof(BSTR), "ScopedBstrSize");
::SysFreeString(bstr_);
}
void ScopedBstr::Reset(BSTR bstr) {
if (bstr != bstr_) {
// SysFreeString handles null properly.
::SysFreeString(bstr_);
bstr_ = bstr;
}
}
BSTR ScopedBstr::Release() {
BSTR bstr = bstr_;
bstr_ = nullptr;
return bstr;
}
void ScopedBstr::Swap(ScopedBstr& bstr2) {
BSTR tmp = bstr_;
bstr_ = bstr2.bstr_;
bstr2.bstr_ = tmp;
}
BSTR* ScopedBstr::Receive() {
DCHECK(!bstr_) << "BSTR leak.";
return &bstr_;
}
BSTR ScopedBstr::Allocate(WStringPiece str) {
Reset(AllocBstrOrDie(str));
return bstr_;
}
BSTR ScopedBstr::AllocateBytes(size_t bytes) {
Reset(AllocBstrBytesOrDie(bytes));
return bstr_;
}
void ScopedBstr::SetByteLen(size_t bytes) {
DCHECK(bstr_);
uint32_t* data = reinterpret_cast<uint32_t*>(bstr_);
data[-1] = checked_cast<uint32_t>(bytes);
}
size_t ScopedBstr::Length() const {
return ::SysStringLen(bstr_);
}
size_t ScopedBstr::ByteLength() const {
return ::SysStringByteLen(bstr_);
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,98 @@
// Copyright (c) 2011 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 BASE_WIN_SCOPED_BSTR_H_
#define BASE_WIN_SCOPED_BSTR_H_
#include <windows.h>
#include <oleauto.h>
#include <stddef.h>
#include "base/base_export.h"
#include "base/check.h"
#include "base/macros.h"
#include "base/strings/string_piece.h"
namespace base {
namespace win {
// Manages a BSTR string pointer.
// The class interface is based on unique_ptr.
class BASE_EXPORT ScopedBstr {
public:
ScopedBstr() = default;
// Constructor to create a new BSTR.
//
// NOTE: Do not pass a BSTR to this constructor expecting ownership to
// be transferred - even though it compiles! ;-)
explicit ScopedBstr(WStringPiece non_bstr);
~ScopedBstr();
BSTR Get() const { return bstr_; }
// Give ScopedBstr ownership over an already allocated BSTR or null.
// If you need to allocate a new BSTR instance, use |allocate| instead.
void Reset(BSTR bstr = nullptr);
// Releases ownership of the BSTR to the caller.
BSTR Release();
// Creates a new BSTR from a 16-bit C-style string.
//
// If you already have a BSTR and want to transfer ownership to the
// ScopedBstr instance, call |reset| instead.
//
// Returns a pointer to the new BSTR.
BSTR Allocate(WStringPiece str);
// Allocates a new BSTR with the specified number of bytes.
// Returns a pointer to the new BSTR.
BSTR AllocateBytes(size_t bytes);
// Sets the allocated length field of the already-allocated BSTR to be
// |bytes|. This is useful when the BSTR was preallocated with e.g.
// SysAllocStringLen or SysAllocStringByteLen (call |AllocateBytes|) and then
// not all the bytes are being used.
//
// Note that if you want to set the length to a specific number of
// characters, you need to multiply by sizeof(wchar_t). Oddly, there's no
// public API to set the length, so we do this ourselves by hand.
//
// NOTE: The actual allocated size of the BSTR MUST be >= bytes. That
// responsibility is with the caller.
void SetByteLen(size_t bytes);
// Swap values of two ScopedBstr's.
void Swap(ScopedBstr& bstr2);
// Retrieves the pointer address.
// Used to receive BSTRs as out arguments (and take ownership).
// The function DCHECKs on the current value being null.
// Usage: GetBstr(bstr.Receive());
BSTR* Receive();
// Returns number of chars in the BSTR.
size_t Length() const;
// Returns the number of bytes allocated for the BSTR.
size_t ByteLength() const;
// Forbid comparison of ScopedBstr types. You should never have the same
// BSTR owned by two different scoped_ptrs.
bool operator==(const ScopedBstr& bstr2) const = delete;
bool operator!=(const ScopedBstr& bstr2) const = delete;
protected:
BSTR bstr_ = nullptr;
private:
DISALLOW_COPY_AND_ASSIGN(ScopedBstr);
};
} // namespace win
} // namespace base
#endif // BASE_WIN_SCOPED_BSTR_H_

View File

@@ -0,0 +1,97 @@
// Copyright (c) 2010 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 "base/win/scoped_bstr.h"
#include <stddef.h>
#include "base/stl_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
constexpr wchar_t kTestString1[] = L"123";
constexpr wchar_t kTestString2[] = L"456789";
constexpr size_t test1_len = size(kTestString1) - 1;
constexpr size_t test2_len = size(kTestString2) - 1;
} // namespace
TEST(ScopedBstrTest, Empty) {
ScopedBstr b;
EXPECT_EQ(nullptr, b.Get());
EXPECT_EQ(0u, b.Length());
EXPECT_EQ(0u, b.ByteLength());
b.Reset(nullptr);
EXPECT_EQ(nullptr, b.Get());
EXPECT_EQ(nullptr, b.Release());
ScopedBstr b2;
b.Swap(b2);
EXPECT_EQ(nullptr, b.Get());
}
TEST(ScopedBstrTest, Basic) {
ScopedBstr b(kTestString1);
EXPECT_EQ(test1_len, b.Length());
EXPECT_EQ(test1_len * sizeof(kTestString1[0]), b.ByteLength());
}
namespace {
void CreateTestString1(BSTR* ret) {
*ret = SysAllocString(kTestString1);
}
} // namespace
TEST(ScopedBstrTest, Swap) {
ScopedBstr b1(kTestString1);
ScopedBstr b2;
b1.Swap(b2);
EXPECT_EQ(test1_len, b2.Length());
EXPECT_EQ(0u, b1.Length());
EXPECT_STREQ(kTestString1, b2.Get());
BSTR tmp = b2.Release();
EXPECT_NE(nullptr, tmp);
EXPECT_STREQ(kTestString1, tmp);
EXPECT_EQ(nullptr, b2.Get());
SysFreeString(tmp);
}
TEST(ScopedBstrTest, OutParam) {
ScopedBstr b;
CreateTestString1(b.Receive());
EXPECT_STREQ(kTestString1, b.Get());
}
TEST(ScopedBstrTest, AllocateBytesAndSetByteLen) {
constexpr size_t num_bytes = 100;
ScopedBstr b;
EXPECT_NE(nullptr, b.AllocateBytes(num_bytes));
EXPECT_EQ(num_bytes, b.ByteLength());
EXPECT_EQ(num_bytes / sizeof(kTestString1[0]), b.Length());
lstrcpy(b.Get(), kTestString1);
EXPECT_EQ(test1_len, static_cast<size_t>(lstrlen(b.Get())));
EXPECT_EQ(num_bytes / sizeof(kTestString1[0]), b.Length());
b.SetByteLen(lstrlen(b.Get()) * sizeof(kTestString2[0]));
EXPECT_EQ(b.Length(), static_cast<size_t>(lstrlen(b.Get())));
}
TEST(ScopedBstrTest, AllocateAndSetByteLen) {
ScopedBstr b;
EXPECT_NE(nullptr, b.Allocate(kTestString2));
EXPECT_EQ(test2_len, b.Length());
b.SetByteLen((test2_len - 1) * sizeof(kTestString2[0]));
EXPECT_EQ(test2_len - 1, b.Length());
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,224 @@
// Copyright 2019 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 BASE_WIN_SCOPED_SAFEARRAY_H_
#define BASE_WIN_SCOPED_SAFEARRAY_H_
#include <objbase.h>
#include "base/base_export.h"
#include "base/check_op.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/win/variant_util.h"
namespace base {
namespace win {
// Manages a Windows SAFEARRAY. This is a minimal wrapper that simply provides
// RAII semantics and does not duplicate the extensive functionality that
// CComSafeArray offers.
class BASE_EXPORT ScopedSafearray {
public:
// LockScope<VARTYPE> class for automatically managing the lifetime of a
// SAFEARRAY lock, and granting easy access to the underlying data either
// through random access or as an iterator.
// It is undefined behavior if the underlying SAFEARRAY is destroyed
// before the LockScope.
// LockScope implements std::iterator_traits as a random access iterator, so
// that LockScope is compatible with STL methods that require these traits.
template <VARTYPE ElementVartype>
class BASE_EXPORT LockScope final {
public:
// Type declarations to support std::iterator_traits
using iterator_category = std::random_access_iterator_tag;
using value_type = typename internal::VariantUtil<ElementVartype>::Type;
using difference_type = ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
LockScope()
: safearray_(nullptr),
vartype_(VT_EMPTY),
array_(nullptr),
array_size_(0U) {}
LockScope(LockScope<ElementVartype>&& other)
: safearray_(std::exchange(other.safearray_, nullptr)),
vartype_(std::exchange(other.vartype_, VT_EMPTY)),
array_(std::exchange(other.array_, nullptr)),
array_size_(std::exchange(other.array_size_, 0U)) {}
LockScope<ElementVartype>& operator=(LockScope<ElementVartype>&& other) {
DCHECK_NE(this, &other);
Reset();
safearray_ = std::exchange(other.safearray_, nullptr);
vartype_ = std::exchange(other.vartype_, VT_EMPTY);
array_ = std::exchange(other.array_, nullptr);
array_size_ = std::exchange(other.array_size_, 0U);
return *this;
}
~LockScope() { Reset(); }
VARTYPE Type() const { return vartype_; }
size_t size() const { return array_size_; }
pointer begin() { return array_; }
pointer end() { return array_ + array_size_; }
const_pointer begin() const { return array_; }
const_pointer end() const { return array_ + array_size_; }
pointer data() { return array_; }
const_pointer data() const { return array_; }
reference operator[](int index) { return at(index); }
const_reference operator[](int index) const { return at(index); }
reference at(size_t index) {
DCHECK_NE(array_, nullptr);
DCHECK_LT(index, array_size_);
return array_[index];
}
const_reference at(size_t index) const {
return const_cast<LockScope<ElementVartype>*>(this)->at(index);
}
private:
LockScope(SAFEARRAY* safearray,
VARTYPE vartype,
pointer array,
size_t array_size)
: safearray_(safearray),
vartype_(vartype),
array_(array),
array_size_(array_size) {}
void Reset() {
if (safearray_)
SafeArrayUnaccessData(safearray_);
safearray_ = nullptr;
vartype_ = VT_EMPTY;
array_ = nullptr;
array_size_ = 0U;
}
SAFEARRAY* safearray_;
VARTYPE vartype_;
pointer array_;
size_t array_size_;
friend class ScopedSafearray;
DISALLOW_COPY_AND_ASSIGN(LockScope);
};
explicit ScopedSafearray(SAFEARRAY* safearray = nullptr)
: safearray_(safearray) {}
// Move constructor
ScopedSafearray(ScopedSafearray&& r) noexcept : safearray_(r.safearray_) {
r.safearray_ = nullptr;
}
// Move operator=. Allows assignment from a ScopedSafearray rvalue.
ScopedSafearray& operator=(ScopedSafearray&& rvalue) {
Reset(rvalue.Release());
return *this;
}
~ScopedSafearray() { Destroy(); }
// Creates a LockScope for accessing the contents of a
// single-dimensional SAFEARRAYs.
template <VARTYPE ElementVartype>
base::Optional<LockScope<ElementVartype>> CreateLockScope() const {
if (!safearray_ || SafeArrayGetDim(safearray_) != 1)
return base::nullopt;
VARTYPE vartype;
HRESULT hr = SafeArrayGetVartype(safearray_, &vartype);
if (FAILED(hr) ||
!internal::VariantUtil<ElementVartype>::IsConvertibleTo(vartype)) {
return base::nullopt;
}
typename LockScope<ElementVartype>::pointer array = nullptr;
hr = SafeArrayAccessData(safearray_, reinterpret_cast<void**>(&array));
if (FAILED(hr))
return base::nullopt;
const size_t array_size = GetCount();
return LockScope<ElementVartype>(safearray_, vartype, array, array_size);
}
void Destroy() {
if (safearray_) {
HRESULT hr = SafeArrayDestroy(safearray_);
DCHECK_EQ(S_OK, hr);
safearray_ = nullptr;
}
}
// Give ScopedSafearray ownership over an already allocated SAFEARRAY or
// nullptr.
void Reset(SAFEARRAY* safearray = nullptr) {
if (safearray != safearray_) {
Destroy();
safearray_ = safearray;
}
}
// Releases ownership of the SAFEARRAY to the caller.
SAFEARRAY* Release() {
SAFEARRAY* safearray = safearray_;
safearray_ = nullptr;
return safearray;
}
// Retrieves the pointer address.
// Used to receive SAFEARRAYs as out arguments (and take ownership).
// This function releases any existing references because it will leak
// the existing ref otherwise.
// Usage: GetSafearray(safearray.Receive());
SAFEARRAY** Receive() {
Destroy();
return &safearray_;
}
// Returns the number of elements in a dimension of the array.
size_t GetCount(UINT dimension = 0) const {
DCHECK(safearray_);
// Initialize |lower| and |upper| so this method will return zero if either
// SafeArrayGetLBound or SafeArrayGetUBound returns failure because they
// only write to the output parameter when successful.
LONG lower = 0;
LONG upper = -1;
DCHECK_LT(dimension, SafeArrayGetDim(safearray_));
HRESULT hr = SafeArrayGetLBound(safearray_, dimension + 1, &lower);
DCHECK(SUCCEEDED(hr));
hr = SafeArrayGetUBound(safearray_, dimension + 1, &upper);
DCHECK(SUCCEEDED(hr));
return (upper - lower + 1);
}
// Returns the internal pointer.
SAFEARRAY* Get() const { return safearray_; }
// Forbid comparison of ScopedSafearray types. You should never have the same
// SAFEARRAY owned by two different scoped_ptrs.
bool operator==(const ScopedSafearray& safearray2) const = delete;
bool operator!=(const ScopedSafearray& safearray2) const = delete;
private:
SAFEARRAY* safearray_;
DISALLOW_COPY_AND_ASSIGN(ScopedSafearray);
};
} // namespace win
} // namespace base
#endif // BASE_WIN_SCOPED_SAFEARRAY_H_

View File

@@ -0,0 +1,262 @@
// Copyright 2019 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 "base/win/scoped_safearray.h"
#include <stddef.h>
#include <array>
#include <vector>
#include "base/stl_util.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
static constexpr std::array<int, 5> kInputValues = {0, 1, 2, 1, 0};
static void PopulateScopedSafearrayOfInts(ScopedSafearray& scoped_safe_array) {
// TODO(crbug.com/1082005): Create a safer alternative to SAFEARRAY methods.
scoped_safe_array.Reset(SafeArrayCreateVector(
/*vartype=*/VT_I4, /*lower_bound=*/2,
/*element_count=*/kInputValues.size()));
ASSERT_NE(scoped_safe_array.Get(), nullptr);
ASSERT_EQ(SafeArrayGetDim(scoped_safe_array.Get()), 1U);
ASSERT_EQ(scoped_safe_array.GetCount(), kInputValues.size());
int* int_array;
ASSERT_HRESULT_SUCCEEDED(SafeArrayAccessData(
scoped_safe_array.Get(), reinterpret_cast<void**>(&int_array)));
for (size_t i = 0; i < kInputValues.size(); ++i)
int_array[i] = kInputValues[i];
ASSERT_HRESULT_SUCCEEDED(SafeArrayUnaccessData(scoped_safe_array.Get()));
}
} // namespace
TEST(ScopedSafearrayTest, ScopedSafearrayMethods) {
ScopedSafearray empty_safe_array;
EXPECT_EQ(empty_safe_array.Get(), nullptr);
EXPECT_EQ(empty_safe_array.Release(), nullptr);
EXPECT_NE(empty_safe_array.Receive(), nullptr);
SAFEARRAY* safe_array = SafeArrayCreateVector(
VT_R8 /* element type */, 0 /* lower bound */, 4 /* elements */);
ScopedSafearray scoped_safe_array(safe_array);
EXPECT_EQ(scoped_safe_array.Get(), safe_array);
EXPECT_EQ(scoped_safe_array.Release(), safe_array);
EXPECT_NE(scoped_safe_array.Receive(), nullptr);
// The Release() call should have set the internal pointer to nullptr
EXPECT_EQ(scoped_safe_array.Get(), nullptr);
scoped_safe_array.Reset(safe_array);
EXPECT_EQ(scoped_safe_array.Get(), safe_array);
ScopedSafearray moved_safe_array(std::move(scoped_safe_array));
EXPECT_EQ(moved_safe_array.Get(), safe_array);
EXPECT_EQ(moved_safe_array.Release(), safe_array);
EXPECT_NE(moved_safe_array.Receive(), nullptr);
// std::move should have cleared the values of scoped_safe_array
EXPECT_EQ(scoped_safe_array.Get(), nullptr);
EXPECT_EQ(scoped_safe_array.Release(), nullptr);
EXPECT_NE(scoped_safe_array.Receive(), nullptr);
scoped_safe_array.Reset(safe_array);
EXPECT_EQ(scoped_safe_array.Get(), safe_array);
ScopedSafearray assigment_moved_safe_array = std::move(scoped_safe_array);
EXPECT_EQ(assigment_moved_safe_array.Get(), safe_array);
EXPECT_EQ(assigment_moved_safe_array.Release(), safe_array);
EXPECT_NE(assigment_moved_safe_array.Receive(), nullptr);
// The move-assign operator= should have cleared the values of
// scoped_safe_array
EXPECT_EQ(scoped_safe_array.Get(), nullptr);
EXPECT_EQ(scoped_safe_array.Release(), nullptr);
EXPECT_NE(scoped_safe_array.Receive(), nullptr);
// Calling Receive() will free the existing reference
ScopedSafearray safe_array_received(SafeArrayCreateVector(
VT_R8 /* element type */, 0 /* lower bound */, 4 /* elements */));
EXPECT_NE(safe_array_received.Receive(), nullptr);
EXPECT_EQ(safe_array_received.Get(), nullptr);
}
TEST(ScopedSafearrayTest, ScopedSafearrayMoveConstructor) {
ScopedSafearray first;
PopulateScopedSafearrayOfInts(first);
EXPECT_NE(first.Get(), nullptr);
EXPECT_EQ(first.GetCount(), kInputValues.size());
SAFEARRAY* safearray = first.Get();
ScopedSafearray second(std::move(first));
EXPECT_EQ(first.Get(), nullptr);
EXPECT_EQ(second.Get(), safearray);
}
TEST(ScopedSafearrayTest, ScopedSafearrayMoveAssignOperator) {
ScopedSafearray first, second;
PopulateScopedSafearrayOfInts(first);
EXPECT_NE(first.Get(), nullptr);
EXPECT_EQ(first.GetCount(), kInputValues.size());
SAFEARRAY* safearray = first.Get();
second = std::move(first);
EXPECT_EQ(first.Get(), nullptr);
EXPECT_EQ(second.Get(), safearray);
// Indirectly move |second| into itself.
ScopedSafearray& reference_to_second = second;
second = std::move(reference_to_second);
EXPECT_EQ(second.GetCount(), kInputValues.size());
EXPECT_EQ(second.Get(), safearray);
}
TEST(ScopedSafearrayTest, ScopedSafearrayCast) {
SAFEARRAY* safe_array = SafeArrayCreateVector(
VT_R8 /* element type */, 1 /* lower bound */, 5 /* elements */);
ScopedSafearray scoped_safe_array(safe_array);
EXPECT_EQ(SafeArrayGetDim(scoped_safe_array.Get()), 1U);
LONG lower_bound;
EXPECT_HRESULT_SUCCEEDED(
SafeArrayGetLBound(scoped_safe_array.Get(), 1, &lower_bound));
EXPECT_EQ(lower_bound, 1);
LONG upper_bound;
EXPECT_HRESULT_SUCCEEDED(
SafeArrayGetUBound(scoped_safe_array.Get(), 1, &upper_bound));
EXPECT_EQ(upper_bound, 5);
VARTYPE variable_type;
EXPECT_HRESULT_SUCCEEDED(
SafeArrayGetVartype(scoped_safe_array.Get(), &variable_type));
EXPECT_EQ(variable_type, VT_R8);
}
TEST(ScopedSafearrayTest, InitiallyEmpty) {
ScopedSafearray empty_safe_array;
EXPECT_EQ(empty_safe_array.Get(), nullptr);
EXPECT_DCHECK_DEATH(empty_safe_array.GetCount());
}
TEST(ScopedSafearrayTest, ScopedSafearrayGetCount) {
// TODO(crbug.com/1082005): Create a safer alternative to SAFEARRAY methods.
ScopedSafearray scoped_safe_array(SafeArrayCreateVector(
/*vartype=*/VT_I4, /*lower_bound=*/2, /*element_count=*/5));
ASSERT_NE(scoped_safe_array.Get(), nullptr);
EXPECT_EQ(SafeArrayGetDim(scoped_safe_array.Get()), 1U);
LONG lower_bound;
EXPECT_HRESULT_SUCCEEDED(
SafeArrayGetLBound(scoped_safe_array.Get(), 1, &lower_bound));
EXPECT_EQ(lower_bound, 2);
LONG upper_bound;
EXPECT_HRESULT_SUCCEEDED(
SafeArrayGetUBound(scoped_safe_array.Get(), 1, &upper_bound));
EXPECT_EQ(upper_bound, 6);
EXPECT_EQ(scoped_safe_array.GetCount(), 5U);
}
TEST(ScopedSafearrayTest, ScopedSafearrayInitialLockScope) {
ScopedSafearray scoped_safe_array;
base::Optional<ScopedSafearray::LockScope<VT_I4>> lock_scope =
scoped_safe_array.CreateLockScope<VT_I4>();
EXPECT_FALSE(lock_scope.has_value());
}
TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeMoveConstructor) {
ScopedSafearray scoped_safe_array;
PopulateScopedSafearrayOfInts(scoped_safe_array);
base::Optional<ScopedSafearray::LockScope<VT_I4>> first =
scoped_safe_array.CreateLockScope<VT_I4>();
ASSERT_TRUE(first.has_value());
EXPECT_EQ(first->Type(), VT_I4);
EXPECT_EQ(first->size(), kInputValues.size());
ScopedSafearray::LockScope<VT_I4> second(std::move(*first));
EXPECT_EQ(first->Type(), VT_EMPTY);
EXPECT_EQ(first->size(), 0U);
EXPECT_EQ(second.Type(), VT_I4);
EXPECT_EQ(second.size(), kInputValues.size());
}
TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeMoveAssignOperator) {
ScopedSafearray scoped_safe_array;
PopulateScopedSafearrayOfInts(scoped_safe_array);
base::Optional<ScopedSafearray::LockScope<VT_I4>> first =
scoped_safe_array.CreateLockScope<VT_I4>();
ASSERT_TRUE(first.has_value());
EXPECT_EQ(first->Type(), VT_I4);
EXPECT_EQ(first->size(), kInputValues.size());
ScopedSafearray::LockScope<VT_I4> second;
second = std::move(*first);
EXPECT_EQ(first->Type(), VT_EMPTY);
EXPECT_EQ(first->size(), 0U);
EXPECT_EQ(second.Type(), VT_I4);
EXPECT_EQ(second.size(), kInputValues.size());
// Indirectly move |second| into itself.
ScopedSafearray::LockScope<VT_I4>& reference_to_second = second;
EXPECT_DCHECK_DEATH(second = std::move(reference_to_second));
}
TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeTypeMismatch) {
ScopedSafearray scoped_safe_array;
PopulateScopedSafearrayOfInts(scoped_safe_array);
{
base::Optional<ScopedSafearray::LockScope<VT_BSTR>> invalid_lock_scope =
scoped_safe_array.CreateLockScope<VT_BSTR>();
EXPECT_FALSE(invalid_lock_scope.has_value());
}
{
base::Optional<ScopedSafearray::LockScope<VT_UI4>> invalid_lock_scope =
scoped_safe_array.CreateLockScope<VT_UI4>();
EXPECT_FALSE(invalid_lock_scope.has_value());
}
}
TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeRandomAccess) {
ScopedSafearray scoped_safe_array;
PopulateScopedSafearrayOfInts(scoped_safe_array);
base::Optional<ScopedSafearray::LockScope<VT_I4>> lock_scope =
scoped_safe_array.CreateLockScope<VT_I4>();
ASSERT_TRUE(lock_scope.has_value());
EXPECT_EQ(lock_scope->Type(), VT_I4);
EXPECT_EQ(lock_scope->size(), kInputValues.size());
for (size_t i = 0; i < kInputValues.size(); ++i) {
EXPECT_EQ(lock_scope->at(i), kInputValues[i]);
EXPECT_EQ((*lock_scope)[i], kInputValues[i]);
}
}
TEST(ScopedSafearrayTest, ScopedSafearrayLockScopeIterator) {
ScopedSafearray scoped_safe_array;
PopulateScopedSafearrayOfInts(scoped_safe_array);
base::Optional<ScopedSafearray::LockScope<VT_I4>> lock_scope =
scoped_safe_array.CreateLockScope<VT_I4>();
std::vector<int> unpacked_vector(lock_scope->begin(), lock_scope->end());
ASSERT_EQ(unpacked_vector.size(), kInputValues.size());
for (size_t i = 0; i < kInputValues.size(); ++i)
EXPECT_EQ(unpacked_vector[i], kInputValues[i]);
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,350 @@
// Copyright (c) 2010 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 "base/win/scoped_variant.h"
#include <wrl/client.h>
#include <algorithm>
#include <functional>
#include "base/check.h"
#include "base/logging.h"
#include "base/numerics/ranges.h"
#include "base/win/propvarutil.h"
#include "base/win/variant_util.h"
namespace base {
namespace win {
// Global, const instance of an empty variant.
const VARIANT ScopedVariant::kEmptyVariant = {{{VT_EMPTY}}};
ScopedVariant::ScopedVariant(ScopedVariant&& var) {
var_.vt = VT_EMPTY;
Reset(var.Release());
}
ScopedVariant::~ScopedVariant() {
static_assert(sizeof(ScopedVariant) == sizeof(VARIANT), "ScopedVariantSize");
::VariantClear(&var_);
}
ScopedVariant::ScopedVariant(const wchar_t* str) {
var_.vt = VT_EMPTY;
Set(str);
}
ScopedVariant::ScopedVariant(const wchar_t* str, UINT length) {
var_.vt = VT_BSTR;
var_.bstrVal = ::SysAllocStringLen(str, length);
}
ScopedVariant::ScopedVariant(long value, VARTYPE vt) {
var_.vt = vt;
var_.lVal = value;
}
ScopedVariant::ScopedVariant(int value) {
var_.vt = VT_I4;
var_.lVal = value;
}
ScopedVariant::ScopedVariant(bool value) {
var_.vt = VT_BOOL;
var_.boolVal = value ? VARIANT_TRUE : VARIANT_FALSE;
}
ScopedVariant::ScopedVariant(double value, VARTYPE vt) {
DCHECK(vt == VT_R8 || vt == VT_DATE);
var_.vt = vt;
var_.dblVal = value;
}
ScopedVariant::ScopedVariant(IDispatch* dispatch) {
var_.vt = VT_EMPTY;
Set(dispatch);
}
ScopedVariant::ScopedVariant(IUnknown* unknown) {
var_.vt = VT_EMPTY;
Set(unknown);
}
ScopedVariant::ScopedVariant(SAFEARRAY* safearray) {
var_.vt = VT_EMPTY;
Set(safearray);
}
ScopedVariant::ScopedVariant(const VARIANT& var) {
var_.vt = VT_EMPTY;
Set(var);
}
void ScopedVariant::Reset(const VARIANT& var) {
if (&var != &var_) {
::VariantClear(&var_);
var_ = var;
}
}
VARIANT ScopedVariant::Release() {
VARIANT var = var_;
var_.vt = VT_EMPTY;
return var;
}
void ScopedVariant::Swap(ScopedVariant& var) {
VARIANT tmp = var_;
var_ = var.var_;
var.var_ = tmp;
}
VARIANT* ScopedVariant::Receive() {
DCHECK(!IsLeakableVarType(var_.vt)) << "variant leak. type: " << var_.vt;
return &var_;
}
VARIANT ScopedVariant::Copy() const {
VARIANT ret = {{{VT_EMPTY}}};
::VariantCopy(&ret, &var_);
return ret;
}
int ScopedVariant::Compare(const VARIANT& other, bool ignore_case) const {
DCHECK(!V_ISARRAY(&var_))
<< "Comparison is not supported when |this| owns a SAFEARRAY";
DCHECK(!V_ISARRAY(&other))
<< "Comparison is not supported when |other| owns a SAFEARRAY";
const bool this_is_empty = var_.vt == VT_EMPTY || var_.vt == VT_NULL;
const bool other_is_empty = other.vt == VT_EMPTY || other.vt == VT_NULL;
// 1. VT_NULL and VT_EMPTY is always considered less-than any other VARTYPE.
if (this_is_empty)
return other_is_empty ? 0 : -1;
if (other_is_empty)
return 1;
// 2. If both VARIANTS have either VT_UNKNOWN or VT_DISPATCH even if the
// VARTYPEs do not match, the address of its IID_IUnknown is compared to
// guarantee a logical ordering even though it is not a meaningful order.
// e.g. (a.Compare(b) != b.Compare(a)) unless (a == b).
const bool this_is_unknown = var_.vt == VT_UNKNOWN || var_.vt == VT_DISPATCH;
const bool other_is_unknown =
other.vt == VT_UNKNOWN || other.vt == VT_DISPATCH;
if (this_is_unknown && other_is_unknown) {
// https://docs.microsoft.com/en-us/windows/win32/com/rules-for-implementing-queryinterface
// Query IID_IUnknown to determine whether the two variants point
// to the same instance of an object
Microsoft::WRL::ComPtr<IUnknown> this_unknown;
Microsoft::WRL::ComPtr<IUnknown> other_unknown;
V_UNKNOWN(&var_)->QueryInterface(IID_PPV_ARGS(&this_unknown));
V_UNKNOWN(&other)->QueryInterface(IID_PPV_ARGS(&other_unknown));
if (this_unknown.Get() == other_unknown.Get())
return 0;
// std::less for any pointer type yields a strict total order even if the
// built-in operator< does not.
return std::less<>{}(this_unknown.Get(), other_unknown.Get()) ? -1 : 1;
}
// 3. If the VARTYPEs do not match, then the value of the VARTYPE is compared.
if (V_VT(&var_) != V_VT(&other))
return (V_VT(&var_) < V_VT(&other)) ? -1 : 1;
const VARTYPE shared_vartype = V_VT(&var_);
// 4. Comparing VT_BSTR values is a lexicographical comparison of the contents
// of the BSTR, taking into account |ignore_case|.
if (shared_vartype == VT_BSTR) {
ULONG flags = ignore_case ? NORM_IGNORECASE : 0;
HRESULT hr =
::VarBstrCmp(V_BSTR(&var_), V_BSTR(&other), LOCALE_USER_DEFAULT, flags);
DCHECK(SUCCEEDED(hr) && hr != VARCMP_NULL)
<< "unsupported variant comparison: " << var_.vt << " and " << other.vt;
switch (hr) {
case VARCMP_LT:
return -1;
case VARCMP_GT:
case VARCMP_NULL:
return 1;
default:
return 0;
}
}
// 5. Otherwise returns the lexicographical comparison of the values held by
// the two VARIANTS that share the same VARTYPE.
return ::VariantCompare(var_, other);
}
void ScopedVariant::Set(const wchar_t* str) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_BSTR;
var_.bstrVal = ::SysAllocString(str);
}
void ScopedVariant::Set(int8_t i8) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_I1;
var_.cVal = i8;
}
void ScopedVariant::Set(uint8_t ui8) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_UI1;
var_.bVal = ui8;
}
void ScopedVariant::Set(int16_t i16) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_I2;
var_.iVal = i16;
}
void ScopedVariant::Set(uint16_t ui16) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_UI2;
var_.uiVal = ui16;
}
void ScopedVariant::Set(int32_t i32) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_I4;
var_.lVal = i32;
}
void ScopedVariant::Set(uint32_t ui32) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_UI4;
var_.ulVal = ui32;
}
void ScopedVariant::Set(int64_t i64) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_I8;
var_.llVal = i64;
}
void ScopedVariant::Set(uint64_t ui64) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_UI8;
var_.ullVal = ui64;
}
void ScopedVariant::Set(float r32) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_R4;
var_.fltVal = r32;
}
void ScopedVariant::Set(double r64) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_R8;
var_.dblVal = r64;
}
void ScopedVariant::SetDate(DATE date) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_DATE;
var_.date = date;
}
void ScopedVariant::Set(IDispatch* disp) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_DISPATCH;
var_.pdispVal = disp;
if (disp)
disp->AddRef();
}
void ScopedVariant::Set(bool b) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_BOOL;
var_.boolVal = b ? VARIANT_TRUE : VARIANT_FALSE;
}
void ScopedVariant::Set(IUnknown* unk) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
var_.vt = VT_UNKNOWN;
var_.punkVal = unk;
if (unk)
unk->AddRef();
}
void ScopedVariant::Set(SAFEARRAY* array) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
if (SUCCEEDED(::SafeArrayGetVartype(array, &var_.vt))) {
var_.vt |= VT_ARRAY;
var_.parray = array;
} else {
DCHECK(!array) << "Unable to determine safearray vartype";
var_.vt = VT_EMPTY;
}
}
void ScopedVariant::Set(const VARIANT& var) {
DCHECK(!IsLeakableVarType(var_.vt)) << "leaking variant: " << var_.vt;
if (FAILED(::VariantCopy(&var_, &var))) {
DLOG(ERROR) << "VariantCopy failed";
var_.vt = VT_EMPTY;
}
}
ScopedVariant& ScopedVariant::operator=(ScopedVariant&& var) {
if (var.ptr() != &var_)
Reset(var.Release());
return *this;
}
ScopedVariant& ScopedVariant::operator=(const VARIANT& var) {
if (&var != &var_) {
VariantClear(&var_);
Set(var);
}
return *this;
}
bool ScopedVariant::IsLeakableVarType(VARTYPE vt) {
bool leakable = false;
switch (vt & VT_TYPEMASK) {
case VT_BSTR:
case VT_DISPATCH:
// we treat VT_VARIANT as leakable to err on the safe side.
case VT_VARIANT:
case VT_UNKNOWN:
case VT_SAFEARRAY:
// very rarely used stuff (if ever):
case VT_VOID:
case VT_PTR:
case VT_CARRAY:
case VT_USERDEFINED:
case VT_LPSTR:
case VT_LPWSTR:
case VT_RECORD:
case VT_INT_PTR:
case VT_UINT_PTR:
case VT_FILETIME:
case VT_BLOB:
case VT_STREAM:
case VT_STORAGE:
case VT_STREAMED_OBJECT:
case VT_STORED_OBJECT:
case VT_BLOB_OBJECT:
case VT_VERSIONED_STREAM:
case VT_BSTR_BLOB:
leakable = true;
break;
}
if (!leakable && (vt & VT_ARRAY) != 0) {
leakable = true;
}
return leakable;
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,188 @@
// Copyright (c) 2011 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 BASE_WIN_SCOPED_VARIANT_H_
#define BASE_WIN_SCOPED_VARIANT_H_
#include <windows.h>
#include <oleauto.h>
#include <stdint.h>
#include "base/base_export.h"
#include "base/macros.h"
namespace base {
namespace win {
// Scoped VARIANT class for automatically freeing a COM VARIANT at the
// end of a scope. Additionally provides a few functions to make the
// encapsulated VARIANT easier to use.
// Instead of inheriting from VARIANT, we take the containment approach
// in order to have more control over the usage of the variant and guard
// against memory leaks.
class BASE_EXPORT ScopedVariant {
public:
// Declaration of a global variant variable that's always VT_EMPTY
static const VARIANT kEmptyVariant;
// Default constructor.
ScopedVariant() {
// This is equivalent to what VariantInit does, but less code.
var_.vt = VT_EMPTY;
}
// Constructor to create a new VT_BSTR VARIANT.
// NOTE: Do not pass a BSTR to this constructor expecting ownership to
// be transferred
explicit ScopedVariant(const wchar_t* str);
// Creates a new VT_BSTR variant of a specified length.
ScopedVariant(const wchar_t* str, UINT length);
// Creates a new integral type variant and assigns the value to
// VARIANT.lVal (32 bit sized field).
explicit ScopedVariant(long value, VARTYPE vt = VT_I4);
// Creates a new integral type variant for the int type and assigns the value
// to VARIANT.lVal (32 bit sized field).
explicit ScopedVariant(int value);
// Creates a new boolean (VT_BOOL) variant and assigns the value to
// VARIANT.boolVal.
explicit ScopedVariant(bool value);
// Creates a new double-precision type variant. |vt| must be either VT_R8
// or VT_DATE.
explicit ScopedVariant(double value, VARTYPE vt = VT_R8);
// VT_DISPATCH
explicit ScopedVariant(IDispatch* dispatch);
// VT_UNKNOWN
explicit ScopedVariant(IUnknown* unknown);
// SAFEARRAY
explicit ScopedVariant(SAFEARRAY* safearray);
// Copies the variant.
explicit ScopedVariant(const VARIANT& var);
// Moves the wrapped variant into another ScopedVariant.
ScopedVariant(ScopedVariant&& var);
~ScopedVariant();
inline VARTYPE type() const { return var_.vt; }
// Give ScopedVariant ownership over an already allocated VARIANT.
void Reset(const VARIANT& var = kEmptyVariant);
// Releases ownership of the VARIANT to the caller.
VARIANT Release();
// Swap two ScopedVariant's.
void Swap(ScopedVariant& var);
// Returns a copy of the variant.
VARIANT Copy() const;
// The return value is 0 if the variants are equal, 1 if this object is
// greater than |other|, -1 if it is smaller.
// Comparison with an array VARIANT is not supported.
// 1. VT_NULL and VT_EMPTY is always considered less-than any other VARTYPE.
// 2. If both VARIANTS have either VT_UNKNOWN or VT_DISPATCH even if the
// VARTYPEs do not match, the address of its IID_IUnknown is compared to
// guarantee a logical ordering even though it is not a meaningful order.
// e.g. (a.Compare(b) != b.Compare(a)) unless (a == b).
// 3. If the VARTYPEs do not match, then the value of the VARTYPE is compared.
// 4. Comparing VT_BSTR values is a lexicographical comparison of the contents
// of the BSTR, taking into account |ignore_case|.
// 5. Otherwise returns the lexicographical comparison of the values held by
// the two VARIANTS that share the same VARTYPE.
int Compare(const VARIANT& other, bool ignore_case = false) const;
// Retrieves the pointer address.
// Used to receive a VARIANT as an out argument (and take ownership).
// The function DCHECKs on the current value being empty/null.
// Usage: GetVariant(var.receive());
VARIANT* Receive();
void Set(const wchar_t* str);
// Setters for simple types.
void Set(int8_t i8);
void Set(uint8_t ui8);
void Set(int16_t i16);
void Set(uint16_t ui16);
void Set(int32_t i32);
void Set(uint32_t ui32);
void Set(int64_t i64);
void Set(uint64_t ui64);
void Set(float r32);
void Set(double r64);
void Set(bool b);
// Creates a copy of |var| and assigns as this instance's value.
// Note that this is different from the Reset() method that's used to
// free the current value and assume ownership.
void Set(const VARIANT& var);
// COM object setters
void Set(IDispatch* disp);
void Set(IUnknown* unk);
// SAFEARRAY support
void Set(SAFEARRAY* array);
// Special setter for DATE since DATE is a double and we already have
// a setter for double.
void SetDate(DATE date);
// Allows const access to the contained variant without DCHECKs etc.
// This support is necessary for the V_XYZ (e.g. V_BSTR) set of macros to
// work properly but still doesn't allow modifications since we want control
// over that.
const VARIANT* ptr() const { return &var_; }
// Moves the ScopedVariant to another instance.
ScopedVariant& operator=(ScopedVariant&& var);
// Like other scoped classes (e.g. scoped_refptr, ScopedBstr,
// Microsoft::WRL::ComPtr) we support the assignment operator for the type we
// wrap.
ScopedVariant& operator=(const VARIANT& var);
// A hack to pass a pointer to the variant where the accepting
// function treats the variant as an input-only, read-only value
// but the function prototype requires a non const variant pointer.
// There's no DCHECK or anything here. Callers must know what they're doing.
VARIANT* AsInput() const {
// The nature of this function is const, so we declare
// it as such and cast away the constness here.
return const_cast<VARIANT*>(&var_);
}
// Allows the ScopedVariant instance to be passed to functions either by value
// or by const reference.
operator const VARIANT&() const { return var_; }
// Used as a debug check to see if we're leaking anything.
static bool IsLeakableVarType(VARTYPE vt);
protected:
VARIANT var_;
private:
// Comparison operators for ScopedVariant are not supported at this point.
// Use the Compare method instead.
bool operator==(const ScopedVariant& var) const;
bool operator!=(const ScopedVariant& var) const;
DISALLOW_COPY_AND_ASSIGN(ScopedVariant);
};
} // namespace win
} // namespace base
#endif // BASE_WIN_SCOPED_VARIANT_H_

View File

@@ -0,0 +1,353 @@
// Copyright (c) 2011 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 <stdint.h>
#include <wrl/client.h>
#include <wrl/implements.h>
#include <utility>
#include "base/win/dispatch_stub.h"
#include "base/win/scoped_variant.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::win::test::DispatchStub;
namespace base {
namespace win {
namespace {
constexpr wchar_t kTestString[] = L"Test string for BSTRs.";
void InitializeVariantWithBstr(VARIANT* var) {
if (!var) {
ADD_FAILURE() << "|var| cannot be null.";
return;
}
var->vt = VT_BSTR;
V_BSTR(var) = ::SysAllocString(kTestString);
}
void ExpectRefCount(ULONG expected_refcount, IUnknown* object) {
// In general, code should not check the values of AddRef() and Release().
// However, tests need to validate that ScopedVariant safely owns a COM object
// so they are checked for this unit test.
EXPECT_EQ(expected_refcount + 1, object->AddRef());
EXPECT_EQ(expected_refcount, object->Release());
}
void ExpectVariantType(VARENUM var_type, const ScopedVariant& var) {
EXPECT_EQ(var_type, var.type());
EXPECT_EQ(var_type, V_VT(var.ptr()));
}
} // namespace
TEST(ScopedVariantTest, Empty) {
ScopedVariant var;
ExpectVariantType(VT_EMPTY, var);
}
TEST(ScopedVariantTest, ConstructBstr) {
ScopedVariant var(kTestString);
ExpectVariantType(VT_BSTR, var);
EXPECT_STREQ(kTestString, V_BSTR(var.ptr()));
}
TEST(ScopedVariantTest, SetBstr) {
ScopedVariant var;
var.Set(kTestString);
ExpectVariantType(VT_BSTR, var);
EXPECT_STREQ(kTestString, V_BSTR(var.ptr()));
}
TEST(ScopedVariantTest, ReleaseBstr) {
ScopedVariant var;
var.Set(kTestString);
VARIANT released_variant = var.Release();
ExpectVariantType(VT_EMPTY, var);
EXPECT_EQ(VT_BSTR, V_VT(&released_variant));
EXPECT_STREQ(kTestString, V_BSTR(&released_variant));
::VariantClear(&released_variant);
}
TEST(ScopedVariantTest, ResetToEmptyBstr) {
ScopedVariant var(kTestString);
ExpectVariantType(VT_BSTR, var);
var.Reset();
ExpectVariantType(VT_EMPTY, var);
}
TEST(ScopedVariantTest, TakeOwnershipBstr) {
VARIANT bstr_variant;
bstr_variant.vt = VT_BSTR;
bstr_variant.bstrVal = ::SysAllocString(kTestString);
ScopedVariant var;
var.Reset(bstr_variant);
ExpectVariantType(VT_BSTR, var);
EXPECT_EQ(bstr_variant.bstrVal, V_BSTR(var.ptr()));
}
TEST(ScopedVariantTest, SwapBstr) {
ScopedVariant from(kTestString);
ScopedVariant to;
to.Swap(from);
ExpectVariantType(VT_EMPTY, from);
ExpectVariantType(VT_BSTR, to);
EXPECT_STREQ(kTestString, V_BSTR(to.ptr()));
}
TEST(ScopedVariantTest, CompareBstr) {
ScopedVariant var_bstr1;
InitializeVariantWithBstr(var_bstr1.Receive());
ScopedVariant var_bstr2(V_BSTR(var_bstr1.ptr()));
EXPECT_EQ(0, var_bstr1.Compare(var_bstr2));
var_bstr2.Reset();
EXPECT_NE(0, var_bstr1.Compare(var_bstr2));
}
TEST(ScopedVariantTest, ReceiveAndCopyBstr) {
ScopedVariant var_bstr1;
InitializeVariantWithBstr(var_bstr1.Receive());
ScopedVariant var_bstr2;
var_bstr2.Reset(var_bstr1.Copy());
EXPECT_EQ(0, var_bstr1.Compare(var_bstr2));
}
TEST(ScopedVariantTest, SetBstrFromBstrVariant) {
ScopedVariant var_bstr1;
InitializeVariantWithBstr(var_bstr1.Receive());
ScopedVariant var_bstr2;
var_bstr2.Set(V_BSTR(var_bstr1.ptr()));
EXPECT_EQ(0, var_bstr1.Compare(var_bstr2));
}
TEST(ScopedVariantTest, SetDate) {
ScopedVariant var;
SYSTEMTIME sys_time;
::GetSystemTime(&sys_time);
DATE date;
::SystemTimeToVariantTime(&sys_time, &date);
var.SetDate(date);
ExpectVariantType(VT_DATE, var);
EXPECT_EQ(date, V_DATE(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned1Byte) {
ScopedVariant var;
var.Set(static_cast<int8_t>('v'));
ExpectVariantType(VT_I1, var);
EXPECT_EQ('v', V_I1(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned2Byte) {
ScopedVariant var;
var.Set(static_cast<short>(123));
ExpectVariantType(VT_I2, var);
EXPECT_EQ(123, V_I2(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned4Byte) {
ScopedVariant var;
var.Set(123);
ExpectVariantType(VT_I4, var);
EXPECT_EQ(123, V_I4(var.ptr()));
}
TEST(ScopedVariantTest, SetSigned8Byte) {
ScopedVariant var;
var.Set(static_cast<int64_t>(123));
ExpectVariantType(VT_I8, var);
EXPECT_EQ(123, V_I8(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned1Byte) {
ScopedVariant var;
var.Set(static_cast<uint8_t>(123));
ExpectVariantType(VT_UI1, var);
EXPECT_EQ(123u, V_UI1(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned2Byte) {
ScopedVariant var;
var.Set(static_cast<unsigned short>(123));
ExpectVariantType(VT_UI2, var);
EXPECT_EQ(123u, V_UI2(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned4Byte) {
ScopedVariant var;
var.Set(static_cast<uint32_t>(123));
ExpectVariantType(VT_UI4, var);
EXPECT_EQ(123u, V_UI4(var.ptr()));
}
TEST(ScopedVariantTest, SetUnsigned8Byte) {
ScopedVariant var;
var.Set(static_cast<uint64_t>(123));
ExpectVariantType(VT_UI8, var);
EXPECT_EQ(123u, V_UI8(var.ptr()));
}
TEST(ScopedVariantTest, SetReal4Byte) {
ScopedVariant var;
var.Set(123.123f);
ExpectVariantType(VT_R4, var);
EXPECT_EQ(123.123f, V_R4(var.ptr()));
}
TEST(ScopedVariantTest, SetReal8Byte) {
ScopedVariant var;
var.Set(static_cast<double>(123.123));
ExpectVariantType(VT_R8, var);
EXPECT_EQ(123.123, V_R8(var.ptr()));
}
TEST(ScopedVariantTest, SetBooleanTrue) {
ScopedVariant var;
var.Set(true);
ExpectVariantType(VT_BOOL, var);
EXPECT_EQ(VARIANT_TRUE, V_BOOL(var.ptr()));
}
TEST(ScopedVariantTest, SetBooleanFalse) {
ScopedVariant var;
var.Set(false);
ExpectVariantType(VT_BOOL, var);
EXPECT_EQ(VARIANT_FALSE, V_BOOL(var.ptr()));
}
TEST(ScopedVariantTest, SetComIDispatch) {
ScopedVariant var;
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
ExpectRefCount(1U, dispatch_stub.Get());
var.Set(dispatch_stub.Get());
ExpectVariantType(VT_DISPATCH, var);
EXPECT_EQ(dispatch_stub.Get(), V_DISPATCH(var.ptr()));
ExpectRefCount(2U, dispatch_stub.Get());
var.Reset();
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariantTest, SetComNullIDispatch) {
ScopedVariant var;
var.Set(static_cast<IDispatch*>(nullptr));
ExpectVariantType(VT_DISPATCH, var);
EXPECT_EQ(nullptr, V_DISPATCH(var.ptr()));
}
TEST(ScopedVariantTest, SetComIUnknown) {
ScopedVariant var;
Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
Microsoft::WRL::Make<DispatchStub>();
ExpectRefCount(1U, unknown_stub.Get());
var.Set(unknown_stub.Get());
ExpectVariantType(VT_UNKNOWN, var);
EXPECT_EQ(unknown_stub.Get(), V_UNKNOWN(var.ptr()));
ExpectRefCount(2U, unknown_stub.Get());
var.Reset();
ExpectRefCount(1U, unknown_stub.Get());
}
TEST(ScopedVariantTest, SetComNullIUnknown) {
ScopedVariant var;
var.Set(static_cast<IUnknown*>(nullptr));
ExpectVariantType(VT_UNKNOWN, var);
EXPECT_EQ(nullptr, V_UNKNOWN(var.ptr()));
}
TEST(ScopedVariant, ScopedComIDispatchConstructor) {
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant var(dispatch_stub.Get());
ExpectVariantType(VT_DISPATCH, var);
EXPECT_EQ(dispatch_stub.Get(), V_DISPATCH(var.ptr()));
ExpectRefCount(2U, dispatch_stub.Get());
}
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariant, ScopedComIDispatchMove) {
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant var1(dispatch_stub.Get());
ExpectRefCount(2U, dispatch_stub.Get());
ScopedVariant var2(std::move(var1));
ExpectRefCount(2U, dispatch_stub.Get());
ScopedVariant var3;
var3 = std::move(var2);
ExpectRefCount(2U, dispatch_stub.Get());
}
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariant, ScopedComIDispatchCopy) {
Microsoft::WRL::ComPtr<IDispatch> dispatch_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant var1(dispatch_stub.Get());
ExpectRefCount(2U, dispatch_stub.Get());
ScopedVariant var2(static_cast<const VARIANT&>(var1));
ExpectRefCount(3U, dispatch_stub.Get());
ScopedVariant var3;
var3 = static_cast<const VARIANT&>(var2);
ExpectRefCount(4U, dispatch_stub.Get());
}
ExpectRefCount(1U, dispatch_stub.Get());
}
TEST(ScopedVariant, ScopedComIUnknownConstructor) {
Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
Microsoft::WRL::Make<DispatchStub>();
{
ScopedVariant unk_var(unknown_stub.Get());
ExpectVariantType(VT_UNKNOWN, unk_var);
EXPECT_EQ(unknown_stub.Get(), V_UNKNOWN(unk_var.ptr()));
ExpectRefCount(2U, unknown_stub.Get());
}
ExpectRefCount(1U, unknown_stub.Get());
}
TEST(ScopedVariant, ScopedComIUnknownWithRawVariant) {
ScopedVariant var;
Microsoft::WRL::ComPtr<IUnknown> unknown_stub =
Microsoft::WRL::Make<DispatchStub>();
VARIANT raw;
raw.vt = VT_UNKNOWN;
raw.punkVal = unknown_stub.Get();
ExpectRefCount(1U, unknown_stub.Get());
var.Set(raw);
ExpectRefCount(2U, unknown_stub.Get());
var.Reset();
ExpectRefCount(1U, unknown_stub.Get());
}
TEST(ScopedVariant, SetSafeArray) {
SAFEARRAY* sa = ::SafeArrayCreateVector(VT_UI1, 0, 100);
ASSERT_TRUE(sa);
ScopedVariant var;
var.Set(sa);
EXPECT_TRUE(ScopedVariant::IsLeakableVarType(var.type()));
ExpectVariantType(static_cast<VARENUM>(VT_ARRAY | VT_UI1), var);
EXPECT_EQ(sa, V_ARRAY(var.ptr()));
// The array is destroyed in the destructor of var.
sa = nullptr;
}
TEST(ScopedVariant, SetNullSafeArray) {
ScopedVariant var;
var.Set(static_cast<SAFEARRAY*>(nullptr));
ExpectVariantType(VT_EMPTY, var);
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,151 @@
// 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 BASE_WIN_VARIANT_UTIL_H_
#define BASE_WIN_VARIANT_UTIL_H_
#include "base/logging.h"
namespace base {
namespace win {
namespace internal {
// Returns true if a VARIANT of type |self| can be assigned to a
// variant of type |other|.
// Does not allow converting unsigned <-> signed or converting between
// different sized types, but does allow converting IDispatch* -> IUnknown*.
constexpr bool VarTypeIsConvertibleTo(VARTYPE self, VARTYPE other) {
// IDispatch inherits from IUnknown, so it's safe to
// upcast a VT_DISPATCH into an IUnknown*.
return (self == other) || (self == VT_DISPATCH && other == VT_UNKNOWN);
}
// VartypeToNativeType contains the underlying |Type| and offset to the
// VARIANT union member related to the |ElementVartype| for simple types.
template <VARTYPE ElementVartype>
struct VartypeToNativeType final {};
template <>
struct VartypeToNativeType<VT_BOOL> final {
using Type = VARIANT_BOOL;
static constexpr VARIANT_BOOL VARIANT::*kMemberOffset = &VARIANT::boolVal;
};
template <>
struct VartypeToNativeType<VT_I1> final {
using Type = int8_t;
static constexpr CHAR VARIANT::*kMemberOffset = &VARIANT::cVal;
};
template <>
struct VartypeToNativeType<VT_UI1> final {
using Type = uint8_t;
static constexpr BYTE VARIANT::*kMemberOffset = &VARIANT::bVal;
};
template <>
struct VartypeToNativeType<VT_I2> final {
using Type = int16_t;
static constexpr SHORT VARIANT::*kMemberOffset = &VARIANT::iVal;
};
template <>
struct VartypeToNativeType<VT_UI2> final {
using Type = uint16_t;
static constexpr USHORT VARIANT::*kMemberOffset = &VARIANT::uiVal;
};
template <>
struct VartypeToNativeType<VT_I4> final {
using Type = int32_t;
static constexpr LONG VARIANT::*kMemberOffset = &VARIANT::lVal;
};
template <>
struct VartypeToNativeType<VT_UI4> final {
using Type = uint32_t;
static constexpr ULONG VARIANT::*kMemberOffset = &VARIANT::ulVal;
};
template <>
struct VartypeToNativeType<VT_I8> final {
using Type = int64_t;
static constexpr LONGLONG VARIANT::*kMemberOffset = &VARIANT::llVal;
};
template <>
struct VartypeToNativeType<VT_UI8> final {
using Type = uint64_t;
static constexpr ULONGLONG VARIANT::*kMemberOffset = &VARIANT::ullVal;
};
template <>
struct VartypeToNativeType<VT_R4> final {
using Type = float;
static constexpr FLOAT VARIANT::*kMemberOffset = &VARIANT::fltVal;
};
template <>
struct VartypeToNativeType<VT_R8> final {
using Type = double;
static constexpr DOUBLE VARIANT::*kMemberOffset = &VARIANT::dblVal;
};
template <>
struct VartypeToNativeType<VT_DATE> final {
using Type = DATE;
static constexpr DATE VARIANT::*kMemberOffset = &VARIANT::date;
};
template <>
struct VartypeToNativeType<VT_BSTR> final {
using Type = BSTR;
static constexpr BSTR VARIANT::*kMemberOffset = &VARIANT::bstrVal;
};
template <>
struct VartypeToNativeType<VT_UNKNOWN> final {
using Type = IUnknown*;
static constexpr IUnknown* VARIANT::*kMemberOffset = &VARIANT::punkVal;
};
template <>
struct VartypeToNativeType<VT_DISPATCH> final {
using Type = IDispatch*;
static constexpr IDispatch* VARIANT::*kMemberOffset = &VARIANT::pdispVal;
};
// VariantUtil contains the underlying |Type| and helper methods
// related to the |ElementVartype| for simple types.
template <VARTYPE ElementVartype>
struct VariantUtil final {
using Type = typename VartypeToNativeType<ElementVartype>::Type;
static constexpr bool IsConvertibleTo(VARTYPE vartype) {
return VarTypeIsConvertibleTo(ElementVartype, vartype);
}
static constexpr bool IsConvertibleFrom(VARTYPE vartype) {
return VarTypeIsConvertibleTo(vartype, ElementVartype);
}
// Get the associated VARIANT union member value.
// Returns the value owned by the VARIANT without affecting the lifetime
// of managed contents.
// e.g. Does not affect IUnknown* reference counts or allocate a BSTR.
static Type RawGet(const VARIANT& var) {
DCHECK(IsConvertibleFrom(V_VT(&var)));
return var.*VartypeToNativeType<ElementVartype>::kMemberOffset;
}
// Set the associated VARIANT union member value.
// The caller is responsible for handling the lifetime of managed contents.
// e.g. Incrementing IUnknown* reference counts or allocating a BSTR.
static void RawSet(VARIANT* var, Type value) {
DCHECK(IsConvertibleTo(V_VT(var)));
var->*VartypeToNativeType<ElementVartype>::kMemberOffset = value;
}
};
} // namespace internal
} // namespace win
} // namespace base
#endif // BASE_WIN_VARIANT_UTIL_H_

View File

@@ -0,0 +1,345 @@
// 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 "base/win/variant_vector.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/optional.h"
#include "base/process/memory.h"
#include "base/win/scoped_safearray.h"
#include "base/win/scoped_variant.h"
namespace base {
namespace win {
namespace {
// Lexicographical comparison between the contents of |vector| and |safearray|.
template <VARTYPE ElementVartype>
int CompareAgainstSafearray(const std::vector<ScopedVariant>& vector,
const ScopedSafearray& safearray,
bool ignore_case) {
base::Optional<ScopedSafearray::LockScope<ElementVartype>> lock_scope =
safearray.CreateLockScope<ElementVartype>();
// If we fail to create a lock scope, then arbitrarily treat |this| as
// greater. This should only happen when the SAFEARRAY fails to be locked,
// so we cannot compare the contents of the SAFEARRAY.
if (!lock_scope)
return 1;
// Create a temporary VARIANT which does not own its contents, and is
// populated with values from the |lock_scope| so it can be compared against.
VARIANT non_owning_temp;
V_VT(&non_owning_temp) = ElementVartype;
auto vector_iter = vector.begin();
auto scope_iter = lock_scope->begin();
for (; vector_iter != vector.end() && scope_iter != lock_scope->end();
++vector_iter, ++scope_iter) {
internal::VariantUtil<ElementVartype>::RawSet(&non_owning_temp,
*scope_iter);
int compare_result = vector_iter->Compare(non_owning_temp, ignore_case);
// If there is a difference in values, return the difference.
if (compare_result)
return compare_result;
}
// There are more elements in |vector|, so |vector| is
// greater than |safearray|.
if (vector_iter != vector.end())
return 1;
// There are more elements in |safearray|, so |vector| is
// less than |safearray|.
if (scope_iter != lock_scope->end())
return -1;
return 0;
}
} // namespace
VariantVector::VariantVector() = default;
VariantVector::VariantVector(VariantVector&& other)
: vartype_(std::exchange(other.vartype_, VT_EMPTY)),
vector_(std::move(other.vector_)) {}
VariantVector& VariantVector::operator=(VariantVector&& other) {
DCHECK_NE(this, &other);
vartype_ = std::exchange(other.vartype_, VT_EMPTY);
vector_ = std::move(other.vector_);
return *this;
}
VariantVector::~VariantVector() {
Reset();
}
bool VariantVector::operator==(const VariantVector& other) const {
return !Compare(other);
}
bool VariantVector::operator!=(const VariantVector& other) const {
return !VariantVector::operator==(other);
}
void VariantVector::Reset() {
vector_.clear();
vartype_ = VT_EMPTY;
}
VARIANT VariantVector::ReleaseAsScalarVariant() {
ScopedVariant scoped_variant;
if (!Empty()) {
DCHECK_EQ(Size(), 1U);
scoped_variant = std::move(vector_[0]);
Reset();
}
return scoped_variant.Release();
}
VARIANT VariantVector::ReleaseAsSafearrayVariant() {
ScopedVariant scoped_variant;
switch (Type()) {
case VT_EMPTY:
break;
case VT_BOOL:
scoped_variant.Set(CreateAndPopulateSafearray<VT_BOOL>());
break;
case VT_I1:
scoped_variant.Set(CreateAndPopulateSafearray<VT_I1>());
break;
case VT_UI1:
scoped_variant.Set(CreateAndPopulateSafearray<VT_UI1>());
break;
case VT_I2:
scoped_variant.Set(CreateAndPopulateSafearray<VT_I2>());
break;
case VT_UI2:
scoped_variant.Set(CreateAndPopulateSafearray<VT_UI2>());
break;
case VT_I4:
scoped_variant.Set(CreateAndPopulateSafearray<VT_I4>());
break;
case VT_UI4:
scoped_variant.Set(CreateAndPopulateSafearray<VT_UI4>());
break;
case VT_I8:
scoped_variant.Set(CreateAndPopulateSafearray<VT_I8>());
break;
case VT_UI8:
scoped_variant.Set(CreateAndPopulateSafearray<VT_UI8>());
break;
case VT_R4:
scoped_variant.Set(CreateAndPopulateSafearray<VT_R4>());
break;
case VT_R8:
scoped_variant.Set(CreateAndPopulateSafearray<VT_R8>());
break;
case VT_DATE:
scoped_variant.Set(CreateAndPopulateSafearray<VT_DATE>());
break;
case VT_BSTR:
scoped_variant.Set(CreateAndPopulateSafearray<VT_BSTR>());
break;
case VT_DISPATCH:
scoped_variant.Set(CreateAndPopulateSafearray<VT_DISPATCH>());
break;
case VT_UNKNOWN:
scoped_variant.Set(CreateAndPopulateSafearray<VT_UNKNOWN>());
break;
// The default case shouldn't be reachable, but if we added support for more
// VARTYPEs to base::win::internal::VariantUtil<> and they were inserted
// into a VariantVector then it would be possible to reach the default case
// for those new types until implemented.
//
// Because the switch is against VARTYPE (unsigned short) and not VARENUM,
// removing the default case will not result in build warnings/errors if
// there are missing cases. It is important that this uses VARTYPE rather
// than VARENUM, because in the future we may want to support complex
// VARTYPES. For example a value within VT_TYPEMASK that's joined something
// outside the typemask like VT_ARRAY or VT_BYREF.
default:
NOTREACHED();
break;
}
// CreateAndPopulateSafearray handles resetting |this| to VT_EMPTY because it
// transfers ownership of each element to the SAFEARRAY.
return scoped_variant.Release();
}
int VariantVector::Compare(const VARIANT& other, bool ignore_case) const {
// If the element variant types are different, compare against the types.
if (Type() != (V_VT(&other) & VT_TYPEMASK))
return (Type() < (V_VT(&other) & VT_TYPEMASK)) ? (-1) : 1;
// Both have an empty variant type so they are the same.
if (Type() == VT_EMPTY)
return 0;
int compare_result = 0;
if (V_ISARRAY(&other)) {
compare_result = Compare(V_ARRAY(&other), ignore_case);
} else {
compare_result = vector_[0].Compare(other, ignore_case);
// If the first element is equal to |other|, and |vector_|
// has more than one element, then |vector_| is greater.
if (!compare_result && Size() > 1)
compare_result = 1;
}
return compare_result;
}
int VariantVector::Compare(const VariantVector& other, bool ignore_case) const {
// If the element variant types are different, compare against the types.
if (Type() != other.Type())
return (Type() < other.Type()) ? (-1) : 1;
// Both have an empty variant type so they are the same.
if (Type() == VT_EMPTY)
return 0;
auto iter1 = vector_.begin();
auto iter2 = other.vector_.begin();
for (; (iter1 != vector_.end()) && (iter2 != other.vector_.end());
++iter1, ++iter2) {
int compare_result = iter1->Compare(*iter2, ignore_case);
if (compare_result)
return compare_result;
}
// There are more elements in |this|, so |this| is greater than |other|.
if (iter1 != vector_.end())
return 1;
// There are more elements in |other|, so |this| is less than |other|.
if (iter2 != other.vector_.end())
return -1;
return 0;
}
int VariantVector::Compare(SAFEARRAY* safearray, bool ignore_case) const {
VARTYPE safearray_vartype;
// If we fail to get the element variant type for the SAFEARRAY, then
// arbitrarily treat |this| as greater.
if (FAILED(SafeArrayGetVartype(safearray, &safearray_vartype)))
return 1;
// If the element variant types are different, compare against the types.
if (Type() != safearray_vartype)
return (Type() < safearray_vartype) ? (-1) : 1;
ScopedSafearray scoped_safearray(safearray);
int compare_result = 0;
switch (Type()) {
case VT_BOOL:
compare_result = CompareAgainstSafearray<VT_BOOL>(
vector_, scoped_safearray, ignore_case);
break;
case VT_I1:
compare_result = CompareAgainstSafearray<VT_I1>(vector_, scoped_safearray,
ignore_case);
break;
case VT_UI1:
compare_result = CompareAgainstSafearray<VT_UI1>(
vector_, scoped_safearray, ignore_case);
break;
case VT_I2:
compare_result = CompareAgainstSafearray<VT_I2>(vector_, scoped_safearray,
ignore_case);
break;
case VT_UI2:
compare_result = CompareAgainstSafearray<VT_UI2>(
vector_, scoped_safearray, ignore_case);
break;
case VT_I4:
compare_result = CompareAgainstSafearray<VT_I4>(vector_, scoped_safearray,
ignore_case);
break;
case VT_UI4:
compare_result = CompareAgainstSafearray<VT_UI4>(
vector_, scoped_safearray, ignore_case);
break;
case VT_I8:
compare_result = CompareAgainstSafearray<VT_I8>(vector_, scoped_safearray,
ignore_case);
break;
case VT_UI8:
compare_result = CompareAgainstSafearray<VT_UI8>(
vector_, scoped_safearray, ignore_case);
break;
case VT_R4:
compare_result = CompareAgainstSafearray<VT_R4>(vector_, scoped_safearray,
ignore_case);
break;
case VT_R8:
compare_result = CompareAgainstSafearray<VT_R8>(vector_, scoped_safearray,
ignore_case);
break;
case VT_DATE:
compare_result = CompareAgainstSafearray<VT_DATE>(
vector_, scoped_safearray, ignore_case);
break;
case VT_BSTR:
compare_result = CompareAgainstSafearray<VT_BSTR>(
vector_, scoped_safearray, ignore_case);
break;
case VT_DISPATCH:
compare_result = CompareAgainstSafearray<VT_DISPATCH>(
vector_, scoped_safearray, ignore_case);
break;
case VT_UNKNOWN:
compare_result = CompareAgainstSafearray<VT_UNKNOWN>(
vector_, scoped_safearray, ignore_case);
break;
// The default case shouldn't be reachable, but if we added support for more
// VARTYPEs to base::win::internal::VariantUtil<> and they were inserted
// into a VariantVector then it would be possible to reach the default case
// for those new types until implemented.
//
// Because the switch is against VARTYPE (unsigned short) and not VARENUM,
// removing the default case will not result in build warnings/errors if
// there are missing cases. It is important that this uses VARTYPE rather
// than VARENUM, because in the future we may want to support complex
// VARTYPES. For example a value within VT_TYPEMASK that's joined something
// outside the typemask like VT_ARRAY or VT_BYREF.
default:
NOTREACHED();
compare_result = 1;
break;
}
scoped_safearray.Release();
return compare_result;
}
template <VARTYPE ElementVartype>
SAFEARRAY* VariantVector::CreateAndPopulateSafearray() {
DCHECK(!Empty());
ScopedSafearray scoped_safearray(
SafeArrayCreateVector(ElementVartype, 0, Size()));
if (!scoped_safearray.Get()) {
constexpr size_t kElementSize =
sizeof(typename internal::VariantUtil<ElementVartype>::Type);
base::TerminateBecauseOutOfMemory(sizeof(SAFEARRAY) +
(Size() * kElementSize));
}
base::Optional<ScopedSafearray::LockScope<ElementVartype>> lock_scope =
scoped_safearray.CreateLockScope<ElementVartype>();
DCHECK(lock_scope);
for (size_t i = 0; i < Size(); ++i) {
VARIANT element = vector_[i].Release();
(*lock_scope)[i] = internal::VariantUtil<ElementVartype>::RawGet(element);
}
Reset();
return scoped_safearray.Release();
}
} // namespace win
} // namespace base

View File

@@ -0,0 +1,140 @@
// 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 BASE_WIN_VARIANT_VECTOR_H_
#define BASE_WIN_VARIANT_VECTOR_H_
#include <objbase.h>
#include <oleauto.h>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/base_export.h"
#include "base/check.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/win/scoped_variant.h"
#include "base/win/variant_util.h"
namespace base {
namespace win {
// This class has RAII semantics and is used to build a vector for a specific
// OLE VARTYPE, and handles converting the data to a VARIANT or VARIANT
// SAFEARRAY. It can be populated similarly to a STL vector<T>, but without the
// compile-time requirement of knowing what element type the VariantVector will
// store. The VariantVector only allows one variant type to be stored at a time.
//
// This class can release ownership of its contents to a VARIANT, and will
// automatically allocate + populate a SAFEARRAY as needed or when explicitly
// requesting that the results be released as a SAFEARRAY.
class BASE_EXPORT VariantVector final {
public:
VariantVector();
VariantVector(VariantVector&& other);
VariantVector& operator=(VariantVector&& other);
VariantVector(const VariantVector&) = delete;
VariantVector& operator=(const VariantVector&) = delete;
~VariantVector();
bool operator==(const VariantVector& other) const;
bool operator!=(const VariantVector& other) const;
// Returns the variant type for data stored in the VariantVector.
VARTYPE Type() const { return vartype_; }
// Returns the number of elements in the VariantVector.
size_t Size() const { return vector_.size(); }
// Returns whether or not there are any elements.
bool Empty() const { return vector_.empty(); }
// Resets VariantVector to its default state, releasing any managed content.
void Reset();
// Helper template method for selecting the correct |Insert| call based
// on the underlying type that is expected for a VARTYPE.
template <VARTYPE ExpectedVartype,
std::enable_if_t<ExpectedVartype != VT_BOOL, int> = 0>
void Insert(typename internal::VariantUtil<ExpectedVartype>::Type value) {
if (vartype_ == VT_EMPTY)
vartype_ = ExpectedVartype;
AssertVartype<ExpectedVartype>();
ScopedVariant scoped_variant;
scoped_variant.Set(value);
vector_.push_back(std::move(scoped_variant));
}
// Specialize VT_BOOL to accept a bool type instead of VARIANT_BOOL,
// this is to make calling insert with VT_BOOL safer.
template <VARTYPE ExpectedVartype,
std::enable_if_t<ExpectedVartype == VT_BOOL, int> = 0>
void Insert(bool value) {
if (vartype_ == VT_EMPTY)
vartype_ = ExpectedVartype;
AssertVartype<ExpectedVartype>();
ScopedVariant scoped_variant;
scoped_variant.Set(value);
vector_.push_back(std::move(scoped_variant));
}
// Specialize VT_DATE because ScopedVariant has a separate SetDate method,
// this is because VT_R8 and VT_DATE share the same underlying type.
template <>
void Insert<VT_DATE>(typename internal::VariantUtil<VT_DATE>::Type value) {
if (vartype_ == VT_EMPTY)
vartype_ = VT_DATE;
AssertVartype<VT_DATE>();
ScopedVariant scoped_variant;
scoped_variant.SetDate(value);
vector_.push_back(std::move(scoped_variant));
}
// Populates a VARIANT based on what is stored, transferring ownership
// of managed contents.
// This is only valid when the VariantVector is empty or has a single element.
// The VariantVector is then reset.
VARIANT ReleaseAsScalarVariant();
// Populates a VARIANT as a SAFEARRAY, even if there is only one element.
// The VariantVector is then reset.
VARIANT ReleaseAsSafearrayVariant();
// Lexicographical comparison between a VariantVector and a VARIANT.
// The return value is 0 if the variants are equal, 1 if this object is
// greater than |other|, -1 if it is smaller.
int Compare(const VARIANT& other, bool ignore_case = false) const;
// Lexicographical comparison between a VariantVector and a SAFEARRAY.
int Compare(SAFEARRAY* safearray, bool ignore_case = false) const;
// Lexicographical comparison between two VariantVectors.
int Compare(const VariantVector& other, bool ignore_case = false) const;
private:
// Returns true if the current |vartype_| is compatible with |ExpectedVartype|
// for inserting into |vector_|.
template <VARTYPE ExpectedVartype>
void AssertVartype() const {
DCHECK(internal::VariantUtil<ExpectedVartype>::IsConvertibleTo(vartype_))
<< "Type mismatch, " << ExpectedVartype << " is not convertible to "
<< Type();
}
// Creates a SAFEARRAY and populates it with teh values held by each VARIANT
// in |vector_|, transferring ownership to the new SAFEARRAY.
// The VariantVector is reset when successful.
template <VARTYPE ElementVartype>
SAFEARRAY* CreateAndPopulateSafearray();
VARTYPE vartype_ = VT_EMPTY;
std::vector<ScopedVariant> vector_;
};
} // namespace win
} // namespace base
#endif // BASE_WIN_VARIANT_VECTOR_H_

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,29 @@
// 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.
#ifndef GFX_RANGE_EXPORT_H_
#define GFX_RANGE_EXPORT_H_
#if defined(COMPONENT_BUILD)
#if defined(WIN32)
#if defined(GFX_RANGE_IMPLEMENTATION)
#define GFX_RANGE_EXPORT __declspec(dllexport)
#else
#define GFX_RANGE_EXPORT __declspec(dllimport)
#endif // defined(GFX_RANGE_IMPLEMENTATION)
#else // defined(WIN32)
#if defined(GFX_RANGE_IMPLEMENTATION)
#define GFX_RANGE_EXPORT __attribute__((visibility("default")))
#else
#define GFX_RANGE_EXPORT
#endif
#endif
#else // defined(COMPONENT_BUILD)
#define GFX_RANGE_EXPORT
#endif
#endif // GFX_RANGE_EXPORT_H_

View File

@@ -0,0 +1,23 @@
// Copyright (c) 2012 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 "ui/gfx/range/range.h"
#include <inttypes.h>
#include <algorithm>
#include "base/strings/stringprintf.h"
namespace gfx {
std::string Range::ToString() const {
return base::StringPrintf("{%" PRIu32 ",%" PRIu32 "}", start(), end());
}
std::ostream& operator<<(std::ostream& os, const Range& range) {
return os << range.ToString();
}
} // namespace gfx

View File

@@ -0,0 +1,141 @@
// Copyright (c) 2012 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 UI_GFX_RANGE_RANGE_H_
#define UI_GFX_RANGE_RANGE_H_
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include <ostream>
#include <string>
#include "build/build_config.h"
#include "ui/gfx/range/gfx_range_export.h"
#if defined(OS_APPLE)
#if __OBJC__
#import <Foundation/Foundation.h>
#else
typedef struct _NSRange NSRange;
#endif
#endif // defined(OS_APPLE)
namespace gfx {
// This class represents either a forward range [min, max) or a reverse range
// (max, min]. |start_| is always the first of these and |end_| the second; as a
// result, the range is forward if (start_ <= end_). The zero-width range
// [val, val) is legal, contains and intersects itself, and is contained by and
// intersects any nonempty range [min, max) where min <= val < max.
class GFX_RANGE_EXPORT Range {
public:
// Creates an empty range {0,0}.
constexpr Range() : Range(0) {}
// Initializes the range with a start and end.
constexpr Range(uint32_t start, uint32_t end) : start_(start), end_(end) {}
// Initializes the range with the same start and end positions.
constexpr explicit Range(uint32_t position) : Range(position, position) {}
// Platform constructors.
#if defined(OS_APPLE)
explicit Range(const NSRange& range);
#endif
// Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}.
static constexpr Range InvalidRange() {
return Range(std::numeric_limits<uint32_t>::max());
}
// Checks if the range is valid through comparison to InvalidRange().
constexpr bool IsValid() const { return *this != InvalidRange(); }
// Getters and setters.
constexpr uint32_t start() const { return start_; }
void set_start(uint32_t start) { start_ = start; }
constexpr uint32_t end() const { return end_; }
void set_end(uint32_t end) { end_ = end; }
// Returns the absolute value of the length.
constexpr uint32_t length() const { return GetMax() - GetMin(); }
constexpr bool is_reversed() const { return start() > end(); }
constexpr bool is_empty() const { return start() == end(); }
// Returns the minimum and maximum values.
constexpr uint32_t GetMin() const {
return start() < end() ? start() : end();
}
constexpr uint32_t GetMax() const {
return start() > end() ? start() : end();
}
constexpr bool operator==(const Range& other) const {
return start() == other.start() && end() == other.end();
}
constexpr bool operator!=(const Range& other) const {
return !(*this == other);
}
constexpr bool EqualsIgnoringDirection(const Range& other) const {
return GetMin() == other.GetMin() && GetMax() == other.GetMax();
}
// Returns true if this range intersects the specified |range|.
constexpr bool Intersects(const Range& range) const {
return Intersect(range).IsValid();
}
// Returns true if this range contains the specified |range|.
constexpr bool Contains(const Range& range) const {
return range.IsBoundedBy(*this) &&
// A non-empty range doesn't contain the range [max, max).
(range.GetMax() != GetMax() || range.is_empty() == is_empty());
}
// Returns true if this range is contained by the specified |range| or it is
// an empty range and ending the range |range|.
constexpr bool IsBoundedBy(const Range& range) const {
return IsValid() && range.IsValid() && GetMin() >= range.GetMin() &&
GetMax() <= range.GetMax();
}
// Computes the intersection of this range with the given |range|.
// If they don't intersect, it returns an InvalidRange().
// The returned range is always empty or forward (never reversed).
constexpr Range Intersect(const Range& range) const {
const uint32_t min = std::max(GetMin(), range.GetMin());
const uint32_t max = std::min(GetMax(), range.GetMax());
return (min < max || Contains(range) || range.Contains(*this))
? Range(min, max)
: InvalidRange();
}
#if defined(OS_APPLE)
Range& operator=(const NSRange& range);
// NSRange does not store the directionality of a range, so if this
// is_reversed(), the range will get flipped when converted to an NSRange.
NSRange ToNSRange() const;
#endif
// GTK+ has no concept of a range.
std::string ToString() const;
private:
// Note: we use uint32_t instead of size_t because this struct is sent over
// IPC which could span 32 & 64 bit processes. This is fine since text spans
// shouldn't exceed UINT32_MAX even on 64 bit builds.
uint32_t start_;
uint32_t end_;
};
GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range);
} // namespace gfx
#endif // UI_GFX_RANGE_RANGE_H_

View File

@@ -0,0 +1,526 @@
// Copyright (c) 2011 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 <sstream>
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/range/range.h"
#include "ui/gfx/range/range_f.h"
namespace {
template <typename T>
class RangeTest : public testing::Test {
};
typedef testing::Types<gfx::Range, gfx::RangeF> RangeTypes;
TYPED_TEST_SUITE(RangeTest, RangeTypes);
template <typename T>
void TestContainsAndIntersects(const T& r1,
const T& r2,
const T& r3) {
EXPECT_TRUE(r1.Intersects(r1));
EXPECT_TRUE(r1.Contains(r1));
EXPECT_EQ(T(10, 12), r1.Intersect(r1));
EXPECT_FALSE(r1.Intersects(r2));
EXPECT_FALSE(r1.Contains(r2));
EXPECT_TRUE(r1.Intersect(r2).is_empty());
EXPECT_FALSE(r2.Intersects(r1));
EXPECT_FALSE(r2.Contains(r1));
EXPECT_TRUE(r2.Intersect(r1).is_empty());
EXPECT_TRUE(r1.Intersects(r3));
EXPECT_TRUE(r3.Intersects(r1));
EXPECT_TRUE(r3.Contains(r1));
EXPECT_FALSE(r1.Contains(r3));
EXPECT_EQ(T(10, 12), r1.Intersect(r3));
EXPECT_EQ(T(10, 12), r3.Intersect(r1));
EXPECT_TRUE(r2.Intersects(r3));
EXPECT_TRUE(r3.Intersects(r2));
EXPECT_FALSE(r3.Contains(r2));
EXPECT_FALSE(r2.Contains(r3));
EXPECT_EQ(T(5, 8), r2.Intersect(r3));
EXPECT_EQ(T(5, 8), r3.Intersect(r2));
}
} // namespace
TYPED_TEST(RangeTest, EmptyInit) {
TypeParam r;
EXPECT_EQ(0U, r.start());
EXPECT_EQ(0U, r.end());
EXPECT_EQ(0U, r.length());
EXPECT_FALSE(r.is_reversed());
EXPECT_TRUE(r.is_empty());
EXPECT_TRUE(r.IsValid());
EXPECT_EQ(0U, r.GetMin());
EXPECT_EQ(0U, r.GetMax());
}
TYPED_TEST(RangeTest, StartEndInit) {
TypeParam r(10, 15);
EXPECT_EQ(10U, r.start());
EXPECT_EQ(15U, r.end());
EXPECT_EQ(5U, r.length());
EXPECT_FALSE(r.is_reversed());
EXPECT_FALSE(r.is_empty());
EXPECT_TRUE(r.IsValid());
EXPECT_EQ(10U, r.GetMin());
EXPECT_EQ(15U, r.GetMax());
}
TYPED_TEST(RangeTest, StartEndReversedInit) {
TypeParam r(10, 5);
EXPECT_EQ(10U, r.start());
EXPECT_EQ(5U, r.end());
EXPECT_EQ(5U, r.length());
EXPECT_TRUE(r.is_reversed());
EXPECT_FALSE(r.is_empty());
EXPECT_TRUE(r.IsValid());
EXPECT_EQ(5U, r.GetMin());
EXPECT_EQ(10U, r.GetMax());
}
TYPED_TEST(RangeTest, PositionInit) {
TypeParam r(12);
EXPECT_EQ(12U, r.start());
EXPECT_EQ(12U, r.end());
EXPECT_EQ(0U, r.length());
EXPECT_FALSE(r.is_reversed());
EXPECT_TRUE(r.is_empty());
EXPECT_TRUE(r.IsValid());
EXPECT_EQ(12U, r.GetMin());
EXPECT_EQ(12U, r.GetMax());
}
TYPED_TEST(RangeTest, InvalidRange) {
TypeParam r(TypeParam::InvalidRange());
EXPECT_EQ(0U, r.length());
EXPECT_EQ(r.start(), r.end());
EXPECT_EQ(r.GetMax(), r.GetMin());
EXPECT_FALSE(r.is_reversed());
EXPECT_TRUE(r.is_empty());
EXPECT_FALSE(r.IsValid());
EXPECT_EQ(r, TypeParam::InvalidRange());
EXPECT_TRUE(r.EqualsIgnoringDirection(TypeParam::InvalidRange()));
}
TYPED_TEST(RangeTest, Equality) {
TypeParam r1(10, 4);
TypeParam r2(10, 4);
TypeParam r3(10, 2);
EXPECT_EQ(r1, r2);
EXPECT_NE(r1, r3);
EXPECT_NE(r2, r3);
TypeParam r4(11, 4);
EXPECT_NE(r1, r4);
EXPECT_NE(r2, r4);
EXPECT_NE(r3, r4);
TypeParam r5(12, 5);
EXPECT_NE(r1, r5);
EXPECT_NE(r2, r5);
EXPECT_NE(r3, r5);
}
TYPED_TEST(RangeTest, EqualsIgnoringDirection) {
TypeParam r1(10, 5);
TypeParam r2(5, 10);
EXPECT_TRUE(r1.EqualsIgnoringDirection(r2));
}
TYPED_TEST(RangeTest, SetStart) {
TypeParam r(10, 20);
EXPECT_EQ(10U, r.start());
EXPECT_EQ(10U, r.length());
r.set_start(42);
EXPECT_EQ(42U, r.start());
EXPECT_EQ(20U, r.end());
EXPECT_EQ(22U, r.length());
EXPECT_TRUE(r.is_reversed());
}
TYPED_TEST(RangeTest, SetEnd) {
TypeParam r(10, 13);
EXPECT_EQ(10U, r.start());
EXPECT_EQ(3U, r.length());
r.set_end(20);
EXPECT_EQ(10U, r.start());
EXPECT_EQ(20U, r.end());
EXPECT_EQ(10U, r.length());
}
TYPED_TEST(RangeTest, SetStartAndEnd) {
TypeParam r;
r.set_end(5);
r.set_start(1);
EXPECT_EQ(1U, r.start());
EXPECT_EQ(5U, r.end());
EXPECT_EQ(4U, r.length());
EXPECT_EQ(1U, r.GetMin());
EXPECT_EQ(5U, r.GetMax());
}
TYPED_TEST(RangeTest, ReversedRange) {
TypeParam r(10, 5);
EXPECT_EQ(10U, r.start());
EXPECT_EQ(5U, r.end());
EXPECT_EQ(5U, r.length());
EXPECT_TRUE(r.is_reversed());
EXPECT_TRUE(r.IsValid());
EXPECT_EQ(5U, r.GetMin());
EXPECT_EQ(10U, r.GetMax());
}
TYPED_TEST(RangeTest, SetReversedRange) {
TypeParam r(10, 20);
r.set_start(25);
EXPECT_EQ(25U, r.start());
EXPECT_EQ(20U, r.end());
EXPECT_EQ(5U, r.length());
EXPECT_TRUE(r.is_reversed());
EXPECT_TRUE(r.IsValid());
r.set_end(21);
EXPECT_EQ(25U, r.start());
EXPECT_EQ(21U, r.end());
EXPECT_EQ(4U, r.length());
EXPECT_TRUE(r.IsValid());
EXPECT_EQ(21U, r.GetMin());
EXPECT_EQ(25U, r.GetMax());
}
TYPED_TEST(RangeTest, ContainAndIntersect) {
{
SCOPED_TRACE("contain and intersect");
TypeParam r1(10, 12);
TypeParam r2(1, 8);
TypeParam r3(5, 12);
TestContainsAndIntersects(r1, r2, r3);
}
{
SCOPED_TRACE("contain and intersect: reversed");
TypeParam r1(12, 10);
TypeParam r2(8, 1);
TypeParam r3(12, 5);
TestContainsAndIntersects(r1, r2, r3);
}
// Invalid rect tests
TypeParam r1(10, 12);
TypeParam r2(8, 1);
TypeParam invalid = r1.Intersect(r2);
EXPECT_FALSE(invalid.IsValid());
EXPECT_FALSE(invalid.Contains(invalid));
EXPECT_FALSE(invalid.Contains(r1));
EXPECT_FALSE(invalid.Intersects(invalid));
EXPECT_FALSE(invalid.Intersects(r1));
EXPECT_FALSE(r1.Contains(invalid));
EXPECT_FALSE(r1.Intersects(invalid));
}
TEST(RangeTest, RangeOperations) {
constexpr gfx::Range invalid_range = gfx::Range::InvalidRange();
constexpr gfx::Range ranges[] = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1},
{1, 2}, {2, 0}, {2, 1}, {2, 2}};
// Ensures valid behavior over same range.
for (const auto& range : ranges) {
SCOPED_TRACE(range.ToString());
// A range should contain itself.
EXPECT_TRUE(range.Contains(range));
// A ranges should intersect with itself.
EXPECT_TRUE(range.Intersects(range));
}
// Ensures valid behavior with an invalid range.
for (const auto& range : ranges) {
SCOPED_TRACE(range.ToString());
EXPECT_FALSE(invalid_range.Contains(range));
EXPECT_FALSE(invalid_range.Intersects(range));
EXPECT_FALSE(range.Contains(invalid_range));
EXPECT_FALSE(range.Intersects(invalid_range));
}
EXPECT_FALSE(invalid_range.Contains(invalid_range));
EXPECT_FALSE(invalid_range.Intersects(invalid_range));
// Ensures consistent operations between Contains(...) and Intersects(...).
for (const auto& range1 : ranges) {
for (const auto& range2 : ranges) {
SCOPED_TRACE(testing::Message()
<< "range1=" << range1 << " range2=" << range2);
if (range1.Contains(range2)) {
EXPECT_TRUE(range1.Intersects(range2));
EXPECT_EQ(range2.Contains(range1),
range1.EqualsIgnoringDirection(range2));
}
EXPECT_EQ(range2.Intersects(range1), range1.Intersects(range2));
EXPECT_EQ(range1.Intersect(range2) != invalid_range,
range1.Intersects(range2));
}
}
// Ranges should behave the same way no matter the direction.
for (const auto& range1 : ranges) {
for (const auto& range2 : ranges) {
SCOPED_TRACE(testing::Message()
<< "range1=" << range1 << " range2=" << range2);
EXPECT_EQ(range1.Contains(range2),
range1.Contains(gfx::Range(range2.GetMax(), range2.GetMin())));
EXPECT_EQ(
range1.Intersects(range2),
range1.Intersects(gfx::Range(range2.GetMax(), range2.GetMin())));
}
}
}
TEST(RangeTest, RangeFOperations) {
constexpr gfx::RangeF invalid_range = gfx::RangeF::InvalidRange();
constexpr gfx::RangeF ranges[] = {{0.f, 0.f}, {0.f, 1.f}, {0.f, 2.f},
{1.f, 0.f}, {1.f, 1.f}, {1.f, 2.f},
{2.f, 0.f}, {2.f, 1.f}, {2.f, 2.f}};
// Ensures valid behavior over same range.
for (const auto& range : ranges) {
SCOPED_TRACE(range.ToString());
// A range should contain itself.
EXPECT_TRUE(range.Contains(range));
// A ranges should intersect with itself.
EXPECT_TRUE(range.Intersects(range));
}
// Ensures valid behavior with an invalid range.
for (const auto& range : ranges) {
SCOPED_TRACE(range.ToString());
EXPECT_FALSE(invalid_range.Contains(range));
EXPECT_FALSE(invalid_range.Intersects(range));
EXPECT_FALSE(range.Contains(invalid_range));
EXPECT_FALSE(range.Intersects(invalid_range));
}
EXPECT_FALSE(invalid_range.Contains(invalid_range));
EXPECT_FALSE(invalid_range.Intersects(invalid_range));
// Ensures consistent operations between Contains(...) and Intersects(...).
for (const auto& range1 : ranges) {
for (const auto& range2 : ranges) {
SCOPED_TRACE(testing::Message()
<< "range1=" << range1 << " range2=" << range2);
if (range1.Contains(range2)) {
EXPECT_TRUE(range1.Intersects(range2));
EXPECT_EQ(range2.Contains(range1),
range1.EqualsIgnoringDirection(range2));
}
EXPECT_EQ(range2.Intersects(range1), range1.Intersects(range2));
EXPECT_EQ(range1.Intersect(range2) != invalid_range,
range1.Intersects(range2));
}
}
// Ranges should behave the same way no matter the direction.
for (const auto& range1 : ranges) {
for (const auto& range2 : ranges) {
SCOPED_TRACE(testing::Message()
<< "range1=" << range1 << " range2=" << range2);
EXPECT_EQ(range1.Contains(range2),
range1.Contains(gfx::RangeF(range2.GetMax(), range2.GetMin())));
EXPECT_EQ(
range1.Intersects(range2),
range1.Intersects(gfx::RangeF(range2.GetMax(), range2.GetMin())));
}
}
}
TEST(RangeTest, ContainsAndIntersects) {
constexpr gfx::Range r1(0, 0);
constexpr gfx::Range r2(0, 1);
constexpr gfx::Range r3(1, 2);
constexpr gfx::Range r4(1, 0);
constexpr gfx::Range r5(2, 1);
constexpr gfx::Range r6(0, 2);
constexpr gfx::Range r7(2, 0);
constexpr gfx::Range r8(1, 1);
// Ensures Contains(...) handle the open range.
EXPECT_TRUE(r2.Contains(r1));
EXPECT_TRUE(r4.Contains(r1));
EXPECT_TRUE(r3.Contains(r5));
EXPECT_TRUE(r5.Contains(r3));
// Ensures larger ranges contains smaller ranges.
EXPECT_TRUE(r6.Contains(r1));
EXPECT_TRUE(r6.Contains(r2));
EXPECT_TRUE(r6.Contains(r3));
EXPECT_TRUE(r6.Contains(r4));
EXPECT_TRUE(r6.Contains(r5));
EXPECT_TRUE(r7.Contains(r1));
EXPECT_TRUE(r7.Contains(r2));
EXPECT_TRUE(r7.Contains(r3));
EXPECT_TRUE(r7.Contains(r4));
EXPECT_TRUE(r7.Contains(r5));
// Ensures Intersects(...) handle the open range.
EXPECT_TRUE(r2.Intersects(r1));
EXPECT_TRUE(r4.Intersects(r1));
// Ensures larger ranges intersects smaller ranges.
EXPECT_TRUE(r6.Intersects(r1));
EXPECT_TRUE(r6.Intersects(r2));
EXPECT_TRUE(r6.Intersects(r3));
EXPECT_TRUE(r6.Intersects(r4));
EXPECT_TRUE(r6.Intersects(r5));
EXPECT_TRUE(r7.Intersects(r1));
EXPECT_TRUE(r7.Intersects(r2));
EXPECT_TRUE(r7.Intersects(r3));
EXPECT_TRUE(r7.Intersects(r4));
EXPECT_TRUE(r7.Intersects(r5));
// Ensures adjacent ranges don't overlap.
EXPECT_FALSE(r2.Intersects(r3));
EXPECT_FALSE(r5.Intersects(r4));
// Ensures empty ranges are handled correctly.
EXPECT_FALSE(r1.Contains(r8));
EXPECT_FALSE(r2.Contains(r8));
EXPECT_TRUE(r3.Contains(r8));
EXPECT_TRUE(r8.Contains(r8));
EXPECT_FALSE(r1.Intersects(r8));
EXPECT_FALSE(r2.Intersects(r8));
EXPECT_TRUE(r3.Intersects(r8));
EXPECT_TRUE(r8.Intersects(r8));
}
TEST(RangeTest, ContainsAndIntersectsRangeF) {
constexpr gfx::RangeF r1(0.f, 0.f);
constexpr gfx::RangeF r2(0.f, 1.f);
constexpr gfx::RangeF r3(1.f, 2.f);
constexpr gfx::RangeF r4(1.f, 0.f);
constexpr gfx::RangeF r5(2.f, 1.f);
constexpr gfx::RangeF r6(0.f, 2.f);
constexpr gfx::RangeF r7(2.f, 0.f);
constexpr gfx::RangeF r8(1.f, 1.f);
// Ensures Contains(...) handle the open range.
EXPECT_TRUE(r2.Contains(r1));
EXPECT_TRUE(r4.Contains(r1));
EXPECT_TRUE(r3.Contains(r5));
EXPECT_TRUE(r5.Contains(r3));
// Ensures larger ranges contains smaller ranges.
EXPECT_TRUE(r6.Contains(r1));
EXPECT_TRUE(r6.Contains(r2));
EXPECT_TRUE(r6.Contains(r3));
EXPECT_TRUE(r6.Contains(r4));
EXPECT_TRUE(r6.Contains(r5));
EXPECT_TRUE(r7.Contains(r1));
EXPECT_TRUE(r7.Contains(r2));
EXPECT_TRUE(r7.Contains(r3));
EXPECT_TRUE(r7.Contains(r4));
EXPECT_TRUE(r7.Contains(r5));
// Ensures Intersects(...) handle the open range.
EXPECT_TRUE(r2.Intersects(r1));
EXPECT_TRUE(r4.Intersects(r1));
// Ensures larger ranges intersects smaller ranges.
EXPECT_TRUE(r6.Intersects(r1));
EXPECT_TRUE(r6.Intersects(r2));
EXPECT_TRUE(r6.Intersects(r3));
EXPECT_TRUE(r6.Intersects(r4));
EXPECT_TRUE(r6.Intersects(r5));
EXPECT_TRUE(r7.Intersects(r1));
EXPECT_TRUE(r7.Intersects(r2));
EXPECT_TRUE(r7.Intersects(r3));
EXPECT_TRUE(r7.Intersects(r4));
EXPECT_TRUE(r7.Intersects(r5));
// Ensures adjacent ranges don't overlap.
EXPECT_FALSE(r2.Intersects(r3));
EXPECT_FALSE(r5.Intersects(r4));
// Ensures empty ranges are handled correctly.
EXPECT_FALSE(r1.Contains(r8));
EXPECT_FALSE(r2.Contains(r8));
EXPECT_TRUE(r3.Contains(r8));
EXPECT_TRUE(r8.Contains(r8));
EXPECT_FALSE(r1.Intersects(r8));
EXPECT_FALSE(r2.Intersects(r8));
EXPECT_TRUE(r3.Intersects(r8));
EXPECT_TRUE(r8.Intersects(r8));
}
TEST(RangeTest, Intersect) {
EXPECT_EQ(gfx::Range(0, 1).Intersect({0, 1}), gfx::Range(0, 1));
EXPECT_EQ(gfx::Range(0, 1).Intersect({0, 0}), gfx::Range(0, 0));
EXPECT_EQ(gfx::Range(0, 0).Intersect({1, 0}), gfx::Range(0, 0));
EXPECT_EQ(gfx::Range(0, 4).Intersect({2, 3}), gfx::Range(2, 3));
EXPECT_EQ(gfx::Range(0, 4).Intersect({2, 7}), gfx::Range(2, 4));
EXPECT_EQ(gfx::Range(0, 4).Intersect({3, 4}), gfx::Range(3, 4));
EXPECT_EQ(gfx::Range(0, 1).Intersect({1, 1}), gfx::Range::InvalidRange());
EXPECT_EQ(gfx::Range(1, 1).Intersect({1, 0}), gfx::Range::InvalidRange());
EXPECT_EQ(gfx::Range(0, 1).Intersect({1, 2}), gfx::Range::InvalidRange());
EXPECT_EQ(gfx::Range(0, 1).Intersect({2, 1}), gfx::Range::InvalidRange());
EXPECT_EQ(gfx::Range(2, 1).Intersect({1, 0}), gfx::Range::InvalidRange());
}
TEST(RangeTest, IntersectRangeF) {
EXPECT_EQ(gfx::RangeF(0.f, 1.f).Intersect(gfx::RangeF(0.f, 1.f)),
gfx::RangeF(0.f, 1.f));
EXPECT_EQ(gfx::RangeF(0.f, 1.f).Intersect(gfx::RangeF(0.f, 0.f)),
gfx::RangeF(0.f, 0.f));
EXPECT_EQ(gfx::RangeF(0.f, 0.f).Intersect(gfx::RangeF(1.f, 0.f)),
gfx::RangeF(0.f, 0.f));
EXPECT_EQ(gfx::RangeF(0.f, 4.f).Intersect(gfx::RangeF(2.f, 3.f)),
gfx::RangeF(2.f, 3.f));
EXPECT_EQ(gfx::RangeF(0.f, 4.f).Intersect(gfx::RangeF(2.f, 7.f)),
gfx::RangeF(2.f, 4.f));
EXPECT_EQ(gfx::RangeF(0.f, 4.f).Intersect(gfx::RangeF(3.f, 4.f)),
gfx::RangeF(3.f, 4.f));
EXPECT_EQ(gfx::RangeF(0.f, 1.f).Intersect(gfx::RangeF(1.f, 1.f)),
gfx::RangeF::InvalidRange());
EXPECT_EQ(gfx::RangeF(1.f, 1.f).Intersect(gfx::RangeF(1.f, 0.f)),
gfx::RangeF::InvalidRange());
EXPECT_EQ(gfx::RangeF(0.f, 1.f).Intersect(gfx::RangeF(1.f, 2.f)),
gfx::RangeF::InvalidRange());
EXPECT_EQ(gfx::RangeF(0.f, 1.f).Intersect(gfx::RangeF(2.f, 1.f)),
gfx::RangeF::InvalidRange());
EXPECT_EQ(gfx::RangeF(2.f, 1.f).Intersect(gfx::RangeF(1.f, 0.f)),
gfx::RangeF::InvalidRange());
}
TEST(RangeTest, IsBoundedBy) {
constexpr gfx::Range r1(0, 0);
constexpr gfx::Range r2(0, 1);
EXPECT_TRUE(r1.IsBoundedBy(r1));
EXPECT_FALSE(r2.IsBoundedBy(r1));
constexpr gfx::Range r3(0, 2);
constexpr gfx::Range r4(2, 2);
EXPECT_TRUE(r4.IsBoundedBy(r3));
EXPECT_FALSE(r3.IsBoundedBy(r4));
}
TEST(RangeTest, ToString) {
gfx::Range range(4, 7);
EXPECT_EQ("{4,7}", range.ToString());
range = gfx::Range::InvalidRange();
std::ostringstream expected;
expected << "{" << range.start() << "," << range.end() << "}";
EXPECT_EQ(expected.str(), range.ToString());
}