File: system_display_apitest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (386 lines) | stat: -rw-r--r-- 14,992 bytes parent folder | download | duplicates (6)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <optional>
#include <string>

#include "base/functional/bind.h"
#include "base/test/gtest_tags.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/api/system_display/display_info_provider.h"
#include "extensions/browser/api/system_display/system_display_api.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/mock_display_info_provider.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/api/system_display.h"
#include "extensions/common/extension_builder.h"
#include "extensions/shell/test/shell_apitest.h"
#include "extensions/test/result_catcher.h"

namespace extensions {

class SystemDisplayApiTest : public ShellApiTest {
 public:
  SystemDisplayApiTest() : provider_(new MockDisplayInfoProvider) {}

  SystemDisplayApiTest(const SystemDisplayApiTest&) = delete;
  SystemDisplayApiTest& operator=(const SystemDisplayApiTest&) = delete;

  ~SystemDisplayApiTest() override = default;

  void SetUpOnMainThread() override {
    ShellApiTest::SetUpOnMainThread();
    DisplayInfoProvider::InitializeForTesting(provider_.get());
  }

 protected:
  void SetInfo(const std::string& display_id,
               const api::system_display::DisplayProperties& properties) {
    provider_->SetDisplayProperties(
        display_id, properties,
        base::BindOnce([](std::optional<std::string>) {}));
  }
  std::unique_ptr<MockDisplayInfoProvider> provider_;
};

#if BUILDFLAG(IS_CHROMEOS)

// TODO(stevenjb): Add API tests for {GS}etDisplayLayout. That code currently
// lives in src/chrome but should be getting moved soon.

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayNotKioskEnabled) {
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP).Build();

  scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
      new SystemDisplaySetDisplayPropertiesFunction());

  set_info_function->set_extension(test_extension.get());
  set_info_function->set_has_callback(true);

  EXPECT_EQ(
      SystemDisplayCrOSRestrictedFunction::kKioskOnlyError,
      api_test_utils::RunFunctionAndReturnError(
          set_info_function.get(), "[\"display_id\", {}]", browser_context()));

  std::optional<base::Value::Dict> set_info = provider_->GetSetInfoValue();
  EXPECT_FALSE(set_info);
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetDisplayKioskEnabled) {
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .Build();

  scoped_refptr<SystemDisplaySetDisplayPropertiesFunction> set_info_function(
      new SystemDisplaySetDisplayPropertiesFunction());

  set_info_function->set_has_callback(true);
  set_info_function->set_extension(test_extension.get());

  ASSERT_TRUE(api_test_utils::RunFunction(
      set_info_function.get(),
      "[\"display_id\", {\n"
      "  \"isPrimary\": true,\n"
      "  \"mirroringSourceId\": \"mirroringId\",\n"
      "  \"boundsOriginX\": 100,\n"
      "  \"boundsOriginY\": 200,\n"
      "  \"rotation\": 90,\n"
      "  \"overscan\": {\"left\": 1, \"top\": 2, \"right\": 3, \"bottom\": 4}\n"
      "}]",
      browser_context()));

  std::optional<base::Value::Dict> set_info = provider_->GetSetInfoValue();
  ASSERT_TRUE(set_info);

  EXPECT_TRUE(api_test_utils::GetBoolean(*set_info, "isPrimary"));
  EXPECT_EQ("mirroringId",
            api_test_utils::GetString(*set_info, "mirroringSourceId"));
  EXPECT_EQ(100, api_test_utils::GetInteger(*set_info, "boundsOriginX"));
  EXPECT_EQ(200, api_test_utils::GetInteger(*set_info, "boundsOriginY"));
  EXPECT_EQ(90, api_test_utils::GetInteger(*set_info, "rotation"));
  base::Value::Dict overscan = api_test_utils::GetDict(*set_info, "overscan");
  EXPECT_EQ(1, api_test_utils::GetInteger(overscan, "left"));
  EXPECT_EQ(2, api_test_utils::GetInteger(overscan, "top"));
  EXPECT_EQ(3, api_test_utils::GetInteger(overscan, "right"));
  EXPECT_EQ(4, api_test_utils::GetInteger(overscan, "bottom"));

  EXPECT_EQ("display_id", provider_->GetSetInfoDisplayId());
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, EnableUnifiedDesktop) {
  base::AddFeatureIdTagToTestResult(
      "screenplay-49098611-4862-4326-a90f-3f04b49eb336");

  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .Build();
  {
    scoped_refptr<SystemDisplayEnableUnifiedDesktopFunction>
        enable_unified_function(
            new SystemDisplayEnableUnifiedDesktopFunction());

    enable_unified_function->set_has_callback(true);
    enable_unified_function->set_extension(test_extension.get());

    EXPECT_FALSE(provider_->unified_desktop_enabled());

    ASSERT_TRUE(api_test_utils::RunFunction(enable_unified_function.get(),
                                            "[true]", browser_context()));
    EXPECT_TRUE(provider_->unified_desktop_enabled());
  }
  {
    scoped_refptr<SystemDisplayEnableUnifiedDesktopFunction>
        enable_unified_function(
            new SystemDisplayEnableUnifiedDesktopFunction());

    enable_unified_function->set_has_callback(true);
    enable_unified_function->set_extension(test_extension.get());
    ASSERT_TRUE(api_test_utils::RunFunction(enable_unified_function.get(),
                                            "[false]", browser_context()));
    EXPECT_FALSE(provider_->unified_desktop_enabled());
  }
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, OverscanCalibrationStart) {
  const std::string id = "display0";
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .Build();

  // Setup MockDisplayInfoProvider.
  api::system_display::DisplayProperties params;
  SetInfo(id, params);

  // Call OverscanCalibrationStart.
  scoped_refptr<SystemDisplayOverscanCalibrationStartFunction> start_function(
      new SystemDisplayOverscanCalibrationStartFunction());
  start_function->set_extension(test_extension.get());
  start_function->set_has_callback(true);
  ASSERT_TRUE(api_test_utils::RunFunction(
      start_function.get(), "[\"" + id + "\"]", browser_context()));

  ASSERT_TRUE(provider_->calibration_started(id));

  // Call OverscanCalibrationComplete.
  scoped_refptr<SystemDisplayOverscanCalibrationCompleteFunction>
      complete_function(new SystemDisplayOverscanCalibrationCompleteFunction());
  complete_function->set_extension(test_extension.get());
  complete_function->set_has_callback(true);
  ASSERT_TRUE(api_test_utils::RunFunction(
      complete_function.get(), "[\"" + id + "\"]", browser_context()));

  ASSERT_FALSE(provider_->calibration_started(id));
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, OverscanCalibrationApp) {
  // Setup MockDisplayInfoProvider.
  const std::string id = "display0";
  api::system_display::DisplayProperties params;
  SetInfo(id, params);

  ASSERT_TRUE(RunAppTest("system/display/overscan")) << message_;

  ASSERT_FALSE(provider_->calibration_started(id));
  ASSERT_TRUE(provider_->calibration_changed(id));
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, OverscanCalibrationAppNoComplete) {
  // Setup MockDisplayInfoProvider.
  const std::string id = "display0";
  api::system_display::DisplayProperties params;
  SetInfo(id, params);

  ResultCatcher catcher;
  scoped_refptr<const Extension> extension =
      LoadApp("system/display/overscan_no_complete");
  ASSERT_TRUE(extension);
  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();

  // Calibration was started by the app but not completed.
  ASSERT_TRUE(provider_->calibration_started(id));

  // Unloading the app should complete the calibration (and hide the overlay).
  UnloadApp(extension.get());
  ASSERT_FALSE(provider_->calibration_changed(id));
  ASSERT_FALSE(provider_->calibration_started(id));
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, ShowNativeTouchCalibrationFail) {
  const std::string id = "display0";
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .Build();

  scoped_refptr<SystemDisplayShowNativeTouchCalibrationFunction>
      show_native_calibration(
          new SystemDisplayShowNativeTouchCalibrationFunction());

  show_native_calibration->set_has_callback(true);
  show_native_calibration->set_extension(test_extension.get());

  provider_->SetTouchCalibrationWillSucceed(false);

  std::string result(api_test_utils::RunFunctionAndReturnError(
      show_native_calibration.get(), "[\"" + id + "\"]", browser_context()));

  EXPECT_FALSE(result.empty());
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, ShowNativeTouchCalibration) {
  const std::string id = "display0";
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .Build();

  scoped_refptr<SystemDisplayShowNativeTouchCalibrationFunction>
      show_native_calibration(
          new SystemDisplayShowNativeTouchCalibrationFunction());

  show_native_calibration->set_has_callback(true);
  show_native_calibration->set_extension(test_extension.get());

  provider_->SetTouchCalibrationWillSucceed(true);

  std::optional<base::Value> result(
      api_test_utils::RunFunctionAndReturnSingleResult(
          show_native_calibration.get(), "[\"" + id + "\"]",
          browser_context()));

  ASSERT_TRUE(result->is_bool());
  EXPECT_TRUE(result->GetBool());
}

IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, SetMirrorMode) {
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP)
          .SetManifestKey("kiosk_enabled", true)
          .Build();
  {
    auto set_mirror_mode_function =
        base::MakeRefCounted<SystemDisplaySetMirrorModeFunction>();

    set_mirror_mode_function->set_has_callback(true);
    set_mirror_mode_function->set_extension(test_extension.get());

    ASSERT_TRUE(api_test_utils::RunFunction(set_mirror_mode_function.get(),
                                            "[{\n"
                                            "  \"mode\": \"normal\"\n"
                                            "}]",
                                            browser_context()));
    EXPECT_EQ(api::system_display::MirrorMode::kNormal,
              provider_->mirror_mode());
  }
  {
    auto set_mirror_mode_function =
        base::MakeRefCounted<SystemDisplaySetMirrorModeFunction>();

    set_mirror_mode_function->set_has_callback(true);
    set_mirror_mode_function->set_extension(test_extension.get());
    ASSERT_TRUE(
        api_test_utils::RunFunction(set_mirror_mode_function.get(),
                                    "[{\n"
                                    "  \"mode\": \"mixed\",\n"
                                    "  \"mirroringSourceId\": \"10\",\n"
                                    "  \"mirroringDestinationIds\": [\"11\"]\n"
                                    "}]",
                                    browser_context()));
    EXPECT_EQ(api::system_display::MirrorMode::kMixed,
              provider_->mirror_mode());
  }
  {
    auto set_mirror_mode_function =
        base::MakeRefCounted<SystemDisplaySetMirrorModeFunction>();

    set_mirror_mode_function->set_has_callback(true);
    set_mirror_mode_function->set_extension(test_extension.get());

    ASSERT_TRUE(api_test_utils::RunFunction(set_mirror_mode_function.get(),
                                            "[{\n"
                                            "  \"mode\": \"off\"\n"
                                            "}]",
                                            browser_context()));
    EXPECT_EQ(api::system_display::MirrorMode::kOff, provider_->mirror_mode());
  }
}

// Tests that Overscan is reset only when the primary main frame is removed.
IN_PROC_BROWSER_TEST_F(SystemDisplayApiTest, ResetDisplayIds) {
  const std::string id = "display0";
  api::system_display::DisplayProperties params;
  SetInfo(id, params);

  ResultCatcher catcher;
  const Extension* extension = LoadApp("system/display/overscan_no_complete");
  ASSERT_TRUE(extension);
  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();

  ExtensionHost* host = ProcessManager::Get(browser_context())
                            ->GetBackgroundHostForExtension(extension->id());
  ASSERT_TRUE(host);
  ASSERT_TRUE(host->host_contents());

  // Add a sub frame.
  ASSERT_TRUE(content::ExecJs(host->host_contents(), R"(
          var bar_frame = document.createElement('iframe');
          document.body.appendChild(bar_frame);
      )"));
  content::RenderFrameHostWrapper sub_frame_render_frame_host_wrapper(
      ChildFrameAt(host->host_contents()->GetPrimaryMainFrame(), 0));

  // By loading the app, calibration is started.
  ASSERT_TRUE(provider_->calibration_started(id));

  EXPECT_TRUE(ExecJs(host->host_contents()->GetPrimaryMainFrame(),
                     "const iframe = document.querySelector('iframe');\
                     iframe.remove();"));
  ASSERT_TRUE(
      sub_frame_render_frame_host_wrapper.WaitUntilRenderFrameDeleted());

  // Removing the sub frame doesn't affect calibration.
  ASSERT_TRUE(provider_->calibration_started(id));

  content::RenderFrameHostWrapper main_frame_render_frame_host_wrapper(
      host->host_contents()->GetPrimaryMainFrame());
  host->host_contents()->ClosePage();
  ASSERT_TRUE(
      main_frame_render_frame_host_wrapper.WaitUntilRenderFrameDeleted());

  ASSERT_FALSE(provider_->calibration_started(id));
}

#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_WIN)
using SystemDisplayGetInfoTest = ShellApiTest;

IN_PROC_BROWSER_TEST_F(SystemDisplayGetInfoTest, GetInfo) {
  DisplayInfoProvider::ResetForTesting();
  scoped_refptr<const Extension> test_extension =
      ExtensionBuilder("Test", ExtensionBuilder::Type::PLATFORM_APP).Build();

  scoped_refptr<SystemDisplayGetInfoFunction> get_info_function(
      new SystemDisplayGetInfoFunction());

  get_info_function->set_has_callback(true);
  get_info_function->set_extension(test_extension.get());

  // Verify that the GetInfo function runs successfully. This uses the real
  // DisplayInfoProvider to verify that DisplayInfoProvider::GetAllDisplaysInfo
  // calls its callback.
  ASSERT_TRUE(api_test_utils::RunFunction(get_info_function.get(), "[]",
                                          browser_context()));
}
#endif  // BUILDFLAG(IS_WIN)

}  // namespace extensions