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

ash / system / power / backlights_forced_off_setter_unittest.cc [blame]

// Copyright 2017 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/system/power/backlights_forced_off_setter.h"

#include <memory>
#include <utility>
#include <vector>

#include "ash/public/cpp/screen_backlight_observer.h"
#include "ash/shell.h"
#include "ash/system/power/scoped_backlights_forced_off.h"
#include "ash/test/ash_test_base.h"
#include "ash/touch/ash_touch_transform_controller.h"
#include "ash/touch/touch_devices_controller.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/manager/test/touch_transform_controller_test_api.h"
#include "ui/display/manager/touch_transform_setter.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/events/devices/device_data_manager_test_api.h"
#include "ui/events/devices/touch_device_transform.h"
#include "ui/events/devices/touchscreen_device.h"

namespace ash {

namespace {

class TestObserver : public ScreenBacklightObserver {
 public:
  explicit TestObserver(BacklightsForcedOffSetter* backlights_forced_off_setter)
      : backlights_forced_off_setter_(backlights_forced_off_setter) {
    scoped_observation_.Observe(backlights_forced_off_setter);
  }

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

  ~TestObserver() override = default;

  const std::vector<bool>& forced_off_states() const {
    return forced_off_states_;
  }

  void ClearForcedOffStates() { forced_off_states_.clear(); }

  // ScreenBacklightObserver:
  void OnBacklightsForcedOffChanged(bool backlights_forced_off) override {
    ASSERT_EQ(backlights_forced_off,
              backlights_forced_off_setter_->backlights_forced_off());
    forced_off_states_.push_back(backlights_forced_off);
  }

 private:
  const raw_ptr<BacklightsForcedOffSetter> backlights_forced_off_setter_;

  std::vector<bool> forced_off_states_;

  base::ScopedObservation<BacklightsForcedOffSetter, ScreenBacklightObserver>
      scoped_observation_{this};
};

}  // namespace

class BacklightsForcedOffSetterTest : public AshTestBase {
 public:
  BacklightsForcedOffSetterTest() = default;

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

  ~BacklightsForcedOffSetterTest() override = default;

  void SetUp() override {
    AshTestBase::SetUp();

    screen_backlight_resetter_ =
        std::make_unique<ash::ScreenBacklight::ScopedResetterForTest>();
    backlights_forced_off_setter_ =
        std::make_unique<BacklightsForcedOffSetter>();
    backlights_forced_off_observer_ =
        std::make_unique<TestObserver>(backlights_forced_off_setter_.get());
  }

  void TearDown() override {
    backlights_forced_off_observer_.reset();
    backlights_forced_off_setter_.reset();
    screen_backlight_resetter_.reset();
    AshTestBase::TearDown();
  }

  void ResetBacklightsForcedOffSetter() {
    backlights_forced_off_observer_.reset();
    backlights_forced_off_setter_.reset();
  }

 protected:
  std::unique_ptr<ash::ScreenBacklight::ScopedResetterForTest>
      screen_backlight_resetter_;
  std::unique_ptr<BacklightsForcedOffSetter> backlights_forced_off_setter_;
  std::unique_ptr<TestObserver> backlights_forced_off_observer_;
};

TEST_F(BacklightsForcedOffSetterTest, SingleForcedOffRequest) {
  ASSERT_FALSE(power_manager_client()->backlights_forced_off());

  std::unique_ptr<ScopedBacklightsForcedOff> scoped_forced_off =
      backlights_forced_off_setter_->ForceBacklightsOff();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({true}),
            backlights_forced_off_observer_->forced_off_states());
  backlights_forced_off_observer_->ClearForcedOffStates();

  scoped_forced_off.reset();

  EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({false}),
            backlights_forced_off_observer_->forced_off_states());
}

TEST_F(BacklightsForcedOffSetterTest, BacklightsForcedOffSetterDeleted) {
  ASSERT_FALSE(power_manager_client()->backlights_forced_off());

  std::unique_ptr<ScopedBacklightsForcedOff> scoped_forced_off =
      backlights_forced_off_setter_->ForceBacklightsOff();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({true}),
            backlights_forced_off_observer_->forced_off_states());
  backlights_forced_off_observer_->ClearForcedOffStates();

  ResetBacklightsForcedOffSetter();

  EXPECT_FALSE(power_manager_client()->backlights_forced_off());

  // Verify that deleting scoped forced off request does not affect
  // power manager state (nor cause a crash).
  scoped_forced_off.reset();
  EXPECT_FALSE(power_manager_client()->backlights_forced_off());
}

TEST_F(BacklightsForcedOffSetterTest,
       OverlappingRequests_SecondRequestResetFirst) {
  ASSERT_FALSE(power_manager_client()->backlights_forced_off());

  std::unique_ptr<ScopedBacklightsForcedOff> scoped_forced_off_1 =
      backlights_forced_off_setter_->ForceBacklightsOff();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({true}),
            backlights_forced_off_observer_->forced_off_states());
  backlights_forced_off_observer_->ClearForcedOffStates();

  std::unique_ptr<ScopedBacklightsForcedOff> scoped_forced_off_2 =
      backlights_forced_off_setter_->ForceBacklightsOff();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_TRUE(backlights_forced_off_observer_->forced_off_states().empty());

  scoped_forced_off_2.reset();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_TRUE(backlights_forced_off_observer_->forced_off_states().empty());

  scoped_forced_off_1.reset();

  EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({false}),
            backlights_forced_off_observer_->forced_off_states());
}

TEST_F(BacklightsForcedOffSetterTest,
       OverlappingRequests_FirstRequestResetFirst) {
  ASSERT_FALSE(power_manager_client()->backlights_forced_off());

  std::unique_ptr<ScopedBacklightsForcedOff> scoped_forced_off_1 =
      backlights_forced_off_setter_->ForceBacklightsOff();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({true}),
            backlights_forced_off_observer_->forced_off_states());
  backlights_forced_off_observer_->ClearForcedOffStates();

  std::unique_ptr<ScopedBacklightsForcedOff> scoped_forced_off_2 =
      backlights_forced_off_setter_->ForceBacklightsOff();

  EXPECT_TRUE(power_manager_client()->backlights_forced_off());
  EXPECT_TRUE(backlights_forced_off_observer_->forced_off_states().empty());

  scoped_forced_off_1.reset();

  EXPECT_TRUE(backlights_forced_off_observer_->forced_off_states().empty());

  scoped_forced_off_2.reset();

  EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  EXPECT_EQ(std::vector<bool>({false}),
            backlights_forced_off_observer_->forced_off_states());
}

TEST_F(BacklightsForcedOffSetterTest,
       ExternalTouchDevicePreventsTouchscreenDisable) {
  UpdateDisplay("1200x600,600x1000*2");
  const int64_t kInternalDisplayId =
      display::test::DisplayManagerTestApi(display_manager())
          .SetFirstDisplayAsInternalDisplay();

  display::DisplayIdList display_id_list =
      display_manager()->GetConnectedDisplayIdList();

  // Pick the non internal display Id.
  const int64_t kExternalDisplayId = display_id_list[0] == kInternalDisplayId
                                         ? display_id_list[1]
                                         : display_id_list[0];

  // Initialize an external touch device.
  ui::TouchscreenDevice external_touchdevice(
      123, ui::InputDeviceType::INPUT_DEVICE_USB,
      std::string("test external touch device"), gfx::Size(1000, 1000), 1);

  ui::TouchscreenDevice internal_touchdevice(
      234, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
      std::string("test internal touch device"), gfx::Size(1000, 1000), 1);
  ui::DeviceDataManagerTestApi().SetTouchscreenDevices(
      {external_touchdevice, internal_touchdevice});

  std::vector<ui::TouchDeviceTransform> transforms;
  ui::TouchDeviceTransform touch_device_transform;

  // Add external touch device to the list.
  touch_device_transform.display_id = kExternalDisplayId;
  touch_device_transform.device_id = external_touchdevice.id;
  transforms.push_back(touch_device_transform);

  // Add internal touch device to the list.
  touch_device_transform.display_id = kInternalDisplayId;
  touch_device_transform.device_id = internal_touchdevice.id;
  transforms.push_back(touch_device_transform);

  // Initialize the transforms and the DeviceDataManager.
  display::test::TouchTransformControllerTestApi(
      Shell::Get()->touch_transformer_controller())
      .touch_transform_setter()
      ->ConfigureTouchDevices(transforms);

  // The brightness change is due to user inactivity or lid close.
  power_manager::BacklightBrightnessChange change;
  change.set_cause(power_manager::BacklightBrightnessChange_Cause_OTHER);
  change.set_percent(0.0);

  power_manager_client()->SendScreenBrightnessChanged(change);

  // The touchscreens should be enabled.
  EXPECT_TRUE(Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
      TouchDeviceEnabledSource::GLOBAL));
}

TEST_F(BacklightsForcedOffSetterTest, TouchscreensDisableOnBrightnessChange) {
  UpdateDisplay("1200x600,600x1000*2");
  const int64_t kInternalDisplayId =
      display::test::DisplayManagerTestApi(display_manager())
          .SetFirstDisplayAsInternalDisplay();

  display::DisplayIdList display_id_list =
      display_manager()->GetConnectedDisplayIdList();

  ui::TouchscreenDevice internal_touchdevice(
      234, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
      std::string("test internal touch device"), gfx::Size(1000, 1000), 1);
  ui::DeviceDataManagerTestApi().SetTouchscreenDevices({internal_touchdevice});

  // Add internal touch device to the list.
  std::vector<ui::TouchDeviceTransform> transforms;
  ui::TouchDeviceTransform touch_device_transform;
  touch_device_transform.display_id = kInternalDisplayId;
  touch_device_transform.device_id = internal_touchdevice.id;
  transforms.push_back(touch_device_transform);

  // Initialize the transforms and the DeviceDataManager.
  display::test::TouchTransformControllerTestApi(
      Shell::Get()->touch_transformer_controller())
      .touch_transform_setter()
      ->ConfigureTouchDevices(transforms);

  // The brightness change is due to user inactivity or lid close.
  power_manager::BacklightBrightnessChange change;
  change.set_cause(power_manager::BacklightBrightnessChange_Cause_OTHER);
  change.set_percent(0.0);

  power_manager_client()->SendScreenBrightnessChanged(change);

  // The touchscreens should be disabled.
  EXPECT_FALSE(Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
      TouchDeviceEnabledSource::GLOBAL));
}

}  // namespace ash