Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to safely retrieve ElementIdentifiers by name.
This CL contains a bunch of quality-of-life and security improvements: - Can now DCHECK_EQ/NE on ElementIdentifier and ElementContext - ElementIdentifier::FromRawValue() is now much safer - Can get the name for an ElementIdentifier and translate safely back to ElementIdentifier (identifier must be actively used first) - ElementTracker[Views] now allows getting all elements with a given ID from any context Implementation details: The first time the name or raw value of an identifier is used, or when an element with an identifier that is not in use starts being tracked, the identifier is added to a master list of known identifiers. An ElementIdentifier can only be retrieved for a name or raw value if it has been registered as a known identifier. That way, it is not possible to convert an arbitrary value to an ElementIdentifier via a debug API. This approach does mean that if an ElementIdentifier exists but its associated name or raw value has not been accessed and no element has been created with that value, the identifier cannot be retrieved by name. In the future, we may create ways to register known identifiers or identifier names, but for now we don't need it. The "retrieve all elements" methods on ElementTracker and ElementTrackerViews are implemented in the same manner as context-specific retrieval methods. Tests are added for all new functionality. Change-Id: I061d6d87c2456ade0bd8d561056f319373e35c50 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3265908 Reviewed-by: Yuheng Huang <yuhengh@chromium.org> Commit-Queue: Dana Fried <dfried@chromium.org> Cr-Commit-Position: refs/heads/main@{#940026}
- Loading branch information
Dana Fried
authored and
Chromium LUCI CQ
committed
Nov 9, 2021
1 parent
ca78ea0
commit 2c40a11
Showing
11 changed files
with
403 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2021 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/base/interaction/element_identifier.h" | ||
|
||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace ui { | ||
|
||
namespace { | ||
|
||
// Use DECLARE/DEFINE_ELEMENT instead of DEFINE_LOCAL_ELEMENT so that this | ||
// ElementIdentifier's name is predictable. | ||
DECLARE_ELEMENT_IDENTIFIER_VALUE(kTestElementIdentifier); | ||
DEFINE_ELEMENT_IDENTIFIER_VALUE(kTestElementIdentifier); | ||
const char* const kTestElementIdentifierName = "kTestElementIdentifier"; | ||
|
||
} // namespace | ||
|
||
class ElementIdentifierTest : public testing::Test { | ||
public: | ||
void SetUp() override { ElementIdentifier::GetKnownIdentifiers().clear(); } | ||
|
||
protected: | ||
static intptr_t GetRawValue(ElementIdentifier id) { return id.GetRawValue(); } | ||
}; | ||
|
||
TEST_F(ElementIdentifierTest, FromName) { | ||
EXPECT_FALSE(ElementIdentifier::FromName(kTestElementIdentifierName)); | ||
EXPECT_EQ(kTestElementIdentifierName, kTestElementIdentifier.GetName()); | ||
EXPECT_EQ(kTestElementIdentifier, | ||
ElementIdentifier::FromName(kTestElementIdentifierName)); | ||
} | ||
|
||
TEST_F(ElementIdentifierTest, FromRawValue) { | ||
EXPECT_FALSE(ElementIdentifier::FromRawValue(0)); | ||
const intptr_t raw_value = GetRawValue(kTestElementIdentifier); | ||
EXPECT_NE(0, raw_value); | ||
EXPECT_EQ(kTestElementIdentifier, ElementIdentifier::FromRawValue(raw_value)); | ||
} | ||
|
||
} // namespace ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.