1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ui_devtools/views/page_agent_views.h"
#include "base/command_line.h"
#include "components/ui_devtools/agent_util.h"
#include "components/ui_devtools/ui_devtools_unittest_utils.h"
#include "components/ui_devtools/ui_element.h"
#include "components/ui_devtools/views/dom_agent_views.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/views_switches.h"
namespace ui_devtools {
class PageAgentViewsTest : public views::ViewsTestBase {
public:
void SetUp() override {
frontend_channel_ = std::make_unique<FakeFrontendChannel>();
uber_dispatcher_ =
std::make_unique<protocol::UberDispatcher>(frontend_channel_.get());
dom_agent_ = DOMAgentViews::Create();
dom_agent_->Init(uber_dispatcher_.get());
page_agent_ = std::make_unique<PageAgentViews>(dom_agent_.get());
page_agent_->Init(uber_dispatcher_.get());
page_agent_->enable();
views::ViewsTestBase::SetUp();
}
void TearDown() override {
page_agent_.reset();
dom_agent_.reset();
uber_dispatcher_.reset();
frontend_channel_.reset();
views::ViewsTestBase::TearDown();
}
bool HasSource(protocol::Array<protocol::Page::FrameResource>* resources,
std::string source) {
for (const auto& resource : *resources) {
if (resource->getUrl() == kChromiumCodeSearchSrcURL + source)
return true;
}
return false;
}
bool VerifyResources(
protocol::Array<protocol::Page::FrameResource>* resources) {
std::stack<UIElement*> elements;
elements.emplace(dom_agent()->element_root());
UIElement* cur_element;
while (!elements.empty()) {
cur_element = elements.top();
elements.pop();
for (const auto& source : cur_element->GetSources()) {
if (!HasSource(resources, source.path_ + "?l=" +
base::NumberToString(source.line_)))
return false;
}
for (auto* child : cur_element->children()) {
elements.emplace(child);
}
}
return true;
}
std::pair<bool, std::string> GetResourceContent(std::string url_input) {
std::string result;
bool out_bool;
auto response =
page_agent()->getResourceContent("1", url_input, &result, &out_bool);
return {response.IsSuccess(), result};
}
protected:
PageAgentViews* page_agent() { return page_agent_.get(); }
DOMAgentViews* dom_agent() { return dom_agent_.get(); }
bool devtools_dismiss_override() const {
return page_agent_->GetDevtoolsDismissOverrideForTesting();
}
private:
std::unique_ptr<PageAgentViews> page_agent_;
std::unique_ptr<DOMAgentViews> dom_agent_;
std::unique_ptr<FakeFrontendChannel> frontend_channel_;
std::unique_ptr<protocol::UberDispatcher> uber_dispatcher_;
};
TEST_F(PageAgentViewsTest, ToggleBubbleLock) {
DCHECK(!devtools_dismiss_override());
// Enable bubble locking.
page_agent()->reload(true);
// Check that |devtools_dismiss_override_| is set to true.
EXPECT_TRUE(devtools_dismiss_override());
// Call again to disable bubble locking.
page_agent()->reload(true);
// Check that |devtools_dismiss_override_| set back to false.
EXPECT_FALSE(devtools_dismiss_override());
}
TEST_F(PageAgentViewsTest, ToggleViewDebugRects) {
std::unique_ptr<protocol::DOM::Node> root;
dom_agent()->getDocument(&root);
DCHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
views::switches::kDrawViewBoundsRects));
// Enable debug rectangles.
page_agent()->reload(false);
// Check that debug rectangles switch is present.
EXPECT_TRUE(base::CommandLine::ForCurrentProcess()->HasSwitch(
views::switches::kDrawViewBoundsRects));
// Disable debug rectangles.
page_agent()->reload(false);
// Check that the debug rectangles switch is removed.
EXPECT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
views::switches::kDrawViewBoundsRects));
}
TEST_F(PageAgentViewsTest, ResetOnDisable) {
std::unique_ptr<protocol::DOM::Node> root;
dom_agent()->getDocument(&root);
// Enable both debug rectangles and bubble locking.
page_agent()->reload(false);
page_agent()->reload(true);
DCHECK(devtools_dismiss_override());
DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
views::switches::kDrawViewBoundsRects));
// Disable PageAgentViews, as is done on UI DevTools closing.
page_agent()->disable();
// Check that both bubble locking and debug rectangles are off.
EXPECT_FALSE(devtools_dismiss_override());
EXPECT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
views::switches::kDrawViewBoundsRects));
}
TEST_F(PageAgentViewsTest, GetResourceTree) {
std::unique_ptr<protocol::Page::FrameResourceTree> resource_tree;
EXPECT_TRUE(page_agent()->getResourceTree(&resource_tree).IsSuccess());
protocol::Page::Frame* frame_object = resource_tree->getFrame();
EXPECT_EQ(frame_object->getId(), "1");
EXPECT_EQ(frame_object->getUrl(), kChromiumCodeSearchURL);
EXPECT_TRUE(VerifyResources(resource_tree->getResources()));
}
TEST_F(PageAgentViewsTest, GetResourceContent) {
auto result = GetResourceContent(
"chromium/src/components/test/data/ui_devtools/test_file.cc?l=0");
EXPECT_TRUE(result.first);
EXPECT_NE(result.second, "");
}
TEST_F(PageAgentViewsTest, GetResourceContentFailsOnBadURL) {
// Test if URL doesn't have src/.
auto result = GetResourceContent(
"incorrect/components/test/data/ui_devtools/test_file.cc?l=0");
EXPECT_FALSE(result.first);
EXPECT_EQ(result.second, "");
// Test if URL doesn't have line number.
result = GetResourceContent(
"chromium/src/components/test/data/ui_devtools/test_file.cc");
EXPECT_FALSE(result.first);
EXPECT_EQ(result.second, "");
// Test if URL isn't a real file.
result = GetResourceContent("chromium/src/not/a/real/file.cc?l=0");
EXPECT_FALSE(result.first);
EXPECT_EQ(result.second, "");
}
} // namespace ui_devtools
|