File: display_prefs_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 (113 lines) | stat: -rw-r--r-- 4,271 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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/shell.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "ui/display/display.h"
#include "ui/display/manager/display_manager.h"

class DisplayPrefsBrowserTest : public InProcessBrowserTest {
 public:
  DisplayPrefsBrowserTest() = default;

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

  ~DisplayPrefsBrowserTest() override = default;

  // InProcessBrowserTest:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    std::string test_name =
        ::testing::UnitTest::GetInstance()->current_test_info()->name();
    // Make sure that display prefs are created in PRE_ test.
    if (test_name.find("PRE_") != std::string::npos) {
      command_line->AppendSwitch(ash::switches::kFirstExecAfterBoot);
    }
  }
  void SetUpOnMainThread() override {
    local_state_ = g_browser_process->local_state();
  }

 protected:
  const base::Value::Dict* GetDisplayProperties(int index) {
    int64_t display_id =
        ash::Shell::Get()->display_manager()->GetDisplayAt(index).id();

    const base::Value::Dict& display_properties =
        local_state_->GetDict(ash::prefs::kDisplayProperties);
    return display_properties.FindDict(base::NumberToString(display_id));
  }

  display::Display::Rotation GetRotation(int index) {
    const base::Value::Dict* properties = GetDisplayProperties(index);
    EXPECT_TRUE(properties);
    display::Display::Rotation result = display::Display::ROTATE_0;
    std::optional<int> rot_value = properties->FindInt("rotation");
    EXPECT_TRUE(rot_value);
    if (rot_value)
      result = static_cast<display::Display::Rotation>(rot_value.value());
    return result;
  }

  raw_ptr<PrefService, DanglingUntriaged> local_state_;
};

// Test that display prefs are registered in the browser local_state
// and persisted across sessions.
IN_PROC_BROWSER_TEST_F(DisplayPrefsBrowserTest, PRE_PowerState) {
  std::string power_state =
      local_state_->GetString(ash::prefs::kDisplayPowerState);
  EXPECT_EQ("all_on", power_state);
  local_state_->Set(ash::prefs::kDisplayPowerState,
                    base::Value("internal_off_external_on"));
}

IN_PROC_BROWSER_TEST_F(DisplayPrefsBrowserTest, PowerState) {
  std::string power_state =
      local_state_->GetString(ash::prefs::kDisplayPowerState);
  EXPECT_EQ("internal_off_external_on", power_state);
}

// Test that changing display rotation changes the pref state and
// persists across sessions.
IN_PROC_BROWSER_TEST_F(DisplayPrefsBrowserTest, PRE_DisplayRotation) {
  display::DisplayManager* display_manager =
      ash::Shell::Get()->display_manager();
  // Verify initial rotation and pref value.
  display::Display::Rotation rotation = GetRotation(0);
  EXPECT_EQ(display::Display::ROTATE_0, rotation);

  const display::Display& display = display_manager->GetDisplayAt(0);
  EXPECT_EQ(display::Display::ROTATE_0, display.rotation());

  // Change rotation.
  display_manager->SetDisplayRotation(display.id(),
                                      display::Display::ROTATE_180,
                                      display::Display::RotationSource::USER);
  base::RunLoop().RunUntilIdle();

  // Verify new rotation and pref value.
  rotation = GetRotation(0);
  EXPECT_EQ(display::Display::ROTATE_180, rotation);

  EXPECT_EQ(display::Display::ROTATE_180,
            display_manager->GetDisplayAt(0).rotation());
}

IN_PROC_BROWSER_TEST_F(DisplayPrefsBrowserTest, DisplayRotation) {
  display::Display::Rotation rotation = GetRotation(0);
  EXPECT_EQ(display::Display::ROTATE_180, rotation);

  const display::Display& display =
      ash::Shell::Get()->display_manager()->GetDisplayAt(0);
  EXPECT_EQ(display::Display::ROTATE_180, display.rotation());
}