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

ash / system / human_presence / lock_on_leave_controller_unittest.cc [blame]

// Copyright 2021 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/human_presence/lock_on_leave_controller.h"

#include <memory>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/test/ash_test_base.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/components/dbus/human_presence/fake_human_presence_dbus_client.h"
#include "chromeos/ash/components/dbus/human_presence/human_presence_dbus_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

class LockOnLeaveControllerTest : public AshTestBase {
 public:
  void SetUp() override {
    // We need to enable kQuickDim to construct
    // HumanPresenceOrientationController.
    scoped_feature_list_.InitAndEnableFeature(features::kQuickDim);
    base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kHasHps);

    // Initialize FakeHumanPresenceDBusClient.
    HumanPresenceDBusClient::InitializeFake();
    human_presence_client_ = FakeHumanPresenceDBusClient::Get();
    human_presence_client_->Reset();

    AshTestBase::SetUp();
  }

 protected:
  raw_ptr<FakeHumanPresenceDBusClient> human_presence_client_ = nullptr;

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

// EnableLockOnLeave should be skipped if the service is not available.
TEST_F(LockOnLeaveControllerTest,
       EnableLockOnLeaveDoesNothingIfServiceUnavailable) {
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);

  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
}

// DisableLockOnLeave should be called when the human presence service becomes
// available.
TEST_F(LockOnLeaveControllerTest, CallDisableLockOnLeaveOnServiceAvailable) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
}

// EnableLockOnLeave should succeed if the service is available.
TEST_F(LockOnLeaveControllerTest, EnableLockOnLeaveSucceedsIfServiceAvailable) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  human_presence_client_->Reset();

  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 1);
}

// The DBus EnableHpsSense method should only be called once for multiple calls
// of EnableLockOnLeave.
TEST_F(LockOnLeaveControllerTest, EnableHpsSenseOnlyCalledOnceOnTwoCalls) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  human_presence_client_->Reset();

  // Only 1 dbus calls should be sent.
  lock_on_leave_controller->EnableLockOnLeave();
  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 1);
}

TEST_F(LockOnLeaveControllerTest, DisableLockOnLeaveDoesNothingIfNotEnabled) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  human_presence_client_->Reset();

  // Calls DisableLockOnLeave does nothing if LockOnLeave is not enabled.
  lock_on_leave_controller->DisableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
}

// DisableLockOnLeave should succeed if LockOnLeave is enabled.
TEST_F(LockOnLeaveControllerTest, DisableLockOnLeaveSuceedsIfEnabled) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  lock_on_leave_controller->EnableLockOnLeave();
  human_presence_client_->Reset();

  // DisableLockOnLeave succeeds since LockOnLeave is enabled.
  lock_on_leave_controller->DisableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
}

// The DBus method DisableHpsSense should only be called once with two
// consecutive calls to DisableLockOnLeave.
TEST_F(LockOnLeaveControllerTest, DisableHpsSenseOnlyCalledOnceOnTwoCalls) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  human_presence_client_->Reset();

  // Only 1 dbus calls should be sent.
  lock_on_leave_controller->DisableLockOnLeave();
  lock_on_leave_controller->DisableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
}

// No dbus call should be sent on restart if LockOnLeave is currently disabled.
TEST_F(LockOnLeaveControllerTest, NoDbusCallsOnRestartIfLockOnLeaveDisabled) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);

  // LockOnLeave is disabled; Restart will not send dbus calls.
  human_presence_client_->Shutdown();
  human_presence_client_->Restart();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
}

// The DBus method EnableHpsSense should be called on restart if LockOnLeave is
// enabled currently.
TEST_F(LockOnLeaveControllerTest,
       EnableLockOnLeaveOnRestartIfLockOnLeaveWasEnabled) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  base::RunLoop().RunUntilIdle();
  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 1);

  // LockOnLeave is enabled; Restart will call EnableHpsSense.
  human_presence_client_->Shutdown();
  human_presence_client_->Restart();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 2);
}

// The DBus method DisableHpsSense should be called on service available even if
// we need to enable it immediately after that.
TEST_F(LockOnLeaveControllerTest, AlwaysCallDisableOnServiceAvailable) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  lock_on_leave_controller->EnableLockOnLeave();
  // At this point, OnServiceAvailable is not called yet, so no disable/enable
  // functions should be called.
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
  base::RunLoop().RunUntilIdle();
  // Although we only need enabling, the disable function should also be called.
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 1);
}

// Confirm that the DBus method EnableHpsSense is only called when both
// EnableLockOnLeave and OnOrientationChanged(true) is set.
TEST_F(LockOnLeaveControllerTest, OrientationChanging) {
  human_presence_client_->set_hps_service_is_available(true);
  auto lock_on_leave_controller = std::make_unique<LockOnLeaveController>();
  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 1);
  human_presence_client_->Reset();

  // Orientation changed, LockOnLeave should be disabled.
  lock_on_leave_controller->OnOrientationChanged(false);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 1);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);
  human_presence_client_->Reset();

  // Calling enable/disable will not sent any dbus call while OrientationChanged
  // to be false.
  lock_on_leave_controller->DisableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  lock_on_leave_controller->OnOrientationChanged(false);
  base::RunLoop().RunUntilIdle();
  lock_on_leave_controller->EnableLockOnLeave();
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 0);

  // Changing orientation will trigger enabling if EnableLockOnLeave was set.
  lock_on_leave_controller->OnOrientationChanged(true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(human_presence_client_->disable_hps_sense_count(), 0);
  EXPECT_EQ(human_presence_client_->enable_hps_sense_count(), 1);
}

}  // namespace ash