File: execution_engine_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (233 lines) | stat: -rw-r--r-- 9,419 bytes parent folder | download | duplicates (3)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/actor/execution_engine.h"

#include <optional>
#include <string_view>

#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/actor/actor_keyed_service.h"
#include "chrome/browser/actor/actor_test_util.h"
#include "chrome/browser/glic/glic_keyed_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/actor.mojom.h"
#include "chrome/common/actor/action_result.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/optimization_guide/content/browser/page_content_proto_provider.h"
#include "components/optimization_guide/proto/features/actions_data.pb.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"

using ::base::test::TestFuture;
using ::optimization_guide::proto::BrowserAction;
using ::optimization_guide::proto::ClickAction;

namespace actor {

namespace {

class ExecutionEngineBrowserTest : public InProcessBrowserTest {
 public:
  ExecutionEngineBrowserTest() {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{features::kGlic, features::kTabstripComboButton,
                              features::kGlicActor},
        /*disabled_features=*/{features::kGlicWarming});
  }
  ExecutionEngineBrowserTest(const ExecutionEngineBrowserTest&) = delete;
  ExecutionEngineBrowserTest& operator=(const ExecutionEngineBrowserTest&) =
      delete;

  ~ExecutionEngineBrowserTest() override = default;

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

 protected:
  content::WebContents* web_contents() {
    return chrome_test_utils::GetActiveWebContents(this);
  }

  content::RenderFrameHost* main_frame() {
    return web_contents()->GetPrimaryMainFrame();
  }

  ExecutionEngine& execution_engine() {
    Profile* profile = chrome_test_utils::GetProfile(this);
    auto* glic_service = glic::GlicKeyedService::Get(profile);
    return glic_service->GetExecutionEngineForTesting(
        browser()->GetActiveTabInterface());
  }

  void ClickTarget(std::string_view query_selector) {
    std::optional<int> dom_node_id =
        content::GetDOMNodeId(*main_frame(), query_selector);
    ASSERT_TRUE(dom_node_id);
    BrowserAction action = MakeClick(*main_frame(), dom_node_id.value());
    TestFuture<mojom::ActionResultPtr> result;
    execution_engine().Act(action, result.GetCallback());
    ExpectOkResult(result);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// The coordinator does not yet handle multi-tab cases. For now,
// while acting on a tab, we override attempts by the page to create new
// tabs, and instead navigate the existing tab.
IN_PROC_BROWSER_TEST_F(ExecutionEngineBrowserTest, ForceSameTabNavigation) {
  const GURL url =
      embedded_test_server()->GetURL("/actor/target_blank_links.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents(), url));

  // Check specifically that it's the existing frame that navigates.
  content::TestFrameNavigationObserver frame_nav_observer(main_frame());
  ClickTarget("#anchorTarget");
  frame_nav_observer.Wait();
}

IN_PROC_BROWSER_TEST_F(ExecutionEngineBrowserTest,
                       ForceSameTabNavigationByScript) {
  const GURL url =
      embedded_test_server()->GetURL("/actor/target_blank_links.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents(), url));

  // Check specifically that it's the existing frame that navigates.
  content::TestFrameNavigationObserver frame_nav_observer(main_frame());
  ClickTarget("#scriptOpen");
  frame_nav_observer.Wait();
}

IN_PROC_BROWSER_TEST_F(ExecutionEngineBrowserTest, TwoClicks) {
  const GURL url = embedded_test_server()->GetURL("/actor/two_clicks.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents(), url));

  // Check initial background color is red
  EXPECT_EQ("red", EvalJs(web_contents(), "document.body.bgColor"));

  // Create a single BrowserAction with two click actions
  std::optional<int> button1_id =
      content::GetDOMNodeId(*main_frame(), "#button1");
  std::optional<int> button2_id =
      content::GetDOMNodeId(*main_frame(), "#button2");
  ASSERT_TRUE(button1_id);
  ASSERT_TRUE(button2_id);

  BrowserAction action;
  ClickAction* click1 = action.add_actions()->mutable_click();
  click1->mutable_target()->set_content_node_id(button1_id.value());
  click1->mutable_target()->mutable_document_identifier()->set_serialized_token(
      *optimization_guide::DocumentIdentifierUserData::GetDocumentIdentifier(
          main_frame()->GetGlobalFrameToken()));
  click1->set_click_type(ClickAction::LEFT);
  click1->set_click_count(ClickAction::SINGLE);

  ClickAction* click2 = action.add_actions()->mutable_click();
  click2->mutable_target()->set_content_node_id(button2_id.value());
  click2->mutable_target()->mutable_document_identifier()->set_serialized_token(
      *optimization_guide::DocumentIdentifierUserData::GetDocumentIdentifier(
          main_frame()->GetGlobalFrameToken()));
  click2->set_click_type(ClickAction::LEFT);
  click2->set_click_count(ClickAction::SINGLE);

  // Execute the action
  TestFuture<mojom::ActionResultPtr> result;
  execution_engine().Act(action, result.GetCallback());
  ExpectOkResult(result);

  // Check background color changed to green
  EXPECT_EQ("green", EvalJs(web_contents(), "document.body.bgColor"));
}

IN_PROC_BROWSER_TEST_F(ExecutionEngineBrowserTest, TwoClicksInBackgroundTab) {
  const GURL url = embedded_test_server()->GetURL("/actor/two_clicks.html");
  ASSERT_TRUE(content::NavigateToURL(web_contents(), url));

  // Check initial background color is red
  EXPECT_EQ("red", EvalJs(web_contents(), "document.body.bgColor"));

  // Store a pointer to the first tab.
  content::WebContents* first_tab_contents = web_contents();
  auto* tab = browser()->GetActiveTabInterface();
  auto tab_handle = tab->GetHandle();

  // Create a second tab, which will be in the foreground.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL("about:blank"), WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

  // The first tab should now be in the background.
  ASSERT_TRUE(!tab->IsVisible());

  // Get the ActorKeyedService.
  Profile* profile = chrome_test_utils::GetProfile(this);
  auto* actor_service = actor::ActorKeyedService::Get(profile);
  ASSERT_TRUE(actor_service);

  // Create an ActorTask and register it.
  auto execution_engine = std::make_unique<ExecutionEngine>(profile);
  auto actor_task_owned =
      std::make_unique<actor::ActorTask>(std::move(execution_engine));
  auto* actor_task = actor_task_owned.get();
  const auto task_id = actor_task->id();
  actor_service->AddTask(std::move(actor_task_owned));

  // Create a single Actions proto with two click actions on the background tab.
  std::optional<int> button1_id = content::GetDOMNodeId(
      *first_tab_contents->GetPrimaryMainFrame(), "#button1");
  std::optional<int> button2_id = content::GetDOMNodeId(
      *first_tab_contents->GetPrimaryMainFrame(), "#button2");
  ASSERT_TRUE(button1_id);
  ASSERT_TRUE(button2_id);

  optimization_guide::proto::Actions actions;
  actions.set_task_id(task_id.GetUnsafeValue());
  ClickAction* click1 = actions.add_actions()->mutable_click();
  click1->mutable_target()->set_content_node_id(button1_id.value());
  click1->mutable_target()->mutable_document_identifier()->set_serialized_token(
      *optimization_guide::DocumentIdentifierUserData::GetDocumentIdentifier(
          first_tab_contents->GetPrimaryMainFrame()->GetGlobalFrameToken()));
  click1->set_click_type(ClickAction::LEFT);
  click1->set_click_count(ClickAction::SINGLE);
  click1->set_tab_id(tab_handle.raw_value());

  ClickAction* click2 = actions.add_actions()->mutable_click();
  click2->set_tab_id(tab_handle.raw_value());
  click2->mutable_target()->set_content_node_id(button2_id.value());
  click2->mutable_target()->mutable_document_identifier()->set_serialized_token(
      *optimization_guide::DocumentIdentifierUserData::GetDocumentIdentifier(
          first_tab_contents->GetPrimaryMainFrame()->GetGlobalFrameToken()));
  click2->set_click_type(ClickAction::LEFT);
  click2->set_click_count(ClickAction::SINGLE);
  click2->set_tab_id(tab_handle.raw_value());

  // Execute the actions.
  TestFuture<optimization_guide::proto::ActionsResult> result;
  actor_task->GetExecutionEngine()->Act(actions, result.GetCallback());

  // Check that the action succeeded.
  EXPECT_EQ(result.Get().action_result(),
            static_cast<int>(mojom::ActionResultCode::kOk));

  // Check background color changed to green in the background tab.
  EXPECT_EQ("green", EvalJs(tab->GetContents(), "document.body.bgColor"));
}

}  // namespace

}  // namespace actor