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

ash / wm / overview / birch / birch_privacy_nudge_controller_unittest.cc [blame]

// Copyright 2024 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/wm/overview/birch/birch_privacy_nudge_controller.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "ash/test/ash_test_base.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "components/prefs/pref_service.h"

namespace ash {

namespace {

constexpr char kNudgeId[] = "BirchPrivacyId";

bool IsNudgeShown() {
  return Shell::Get()->anchored_nudge_manager()->IsNudgeShown(kNudgeId);
}

class BirchPrivacyNudgeControllerTest : public AshTestBase {
 public:
  BirchPrivacyNudgeControllerTest()
      : nudge_controller_(std::make_unique<BirchPrivacyNudgeController>()) {}

  BirchPrivacyNudgeController* nudge_controller() {
    return nudge_controller_.get();
  }

  AnchoredNudgeManagerImpl* nudge_manager() {
    return Shell::Get()->anchored_nudge_manager();
  }

  static base::Time GetTestTime() { return test_time_; }
  static void SetTestTime(base::Time test_time) { test_time_ = test_time; }

 private:
  std::unique_ptr<BirchPrivacyNudgeController> nudge_controller_;
  static base::Time test_time_;
};

// static
base::Time BirchPrivacyNudgeControllerTest::test_time_;

// Tests that the nudge shows by default.
TEST_F(BirchPrivacyNudgeControllerTest, NudgeShows_ByDefault) {
  EXPECT_FALSE(IsNudgeShown());
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_TRUE(IsNudgeShown());
}

// Tests that the nudge does not show if the birch context menu has been
// opened.
TEST_F(BirchPrivacyNudgeControllerTest, NudgeDoesNotShow_WhenMenuWasOpened) {
  BirchPrivacyNudgeController::DidShowContextMenu();

  EXPECT_FALSE(IsNudgeShown());
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_FALSE(IsNudgeShown());
}

// Tests that the nudge won't show if the time between shown threshold hasn't
// passed since it was last shown.
TEST_F(BirchPrivacyNudgeControllerTest, NudgeDoesNotShow_IfRecentlyShown) {
  SetTestTime(base::Time::Now());
  base::subtle::ScopedTimeClockOverrides clock_override(
      /*time_override=*/&BirchPrivacyNudgeControllerTest::GetTestTime,
      /*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);

  // Show the nudge once and close it.
  EXPECT_FALSE(IsNudgeShown());
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_TRUE(IsNudgeShown());
  nudge_manager()->Cancel(kNudgeId);

  // Attempt showing the nudge again immediately. It should not show.
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_FALSE(IsNudgeShown());

  // Attempt showing the nudge after some time but before its threshold time has
  // fully passed. It should not show.
  SetTestTime(GetTestTime() + base::Hours(23));
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_FALSE(IsNudgeShown());

  // Attempt showing the nudge after its "time between shown" threshold has
  // passed. It should show.
  SetTestTime(GetTestTime() + base::Hours(24));
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_TRUE(IsNudgeShown());
}

TEST_F(BirchPrivacyNudgeControllerTest, NudgeDoesNotShow_IfMaxTimesShown) {
  SetTestTime(base::Time::Now());
  base::subtle::ScopedTimeClockOverrides clock_override(
      /*time_override=*/&BirchPrivacyNudgeControllerTest::GetTestTime,
      /*time_ticks_override=*/nullptr, /*thread_ticks_override=*/nullptr);

  // Show the nudge its max number of times.
  for (int i = 0; i < 3; i++) {
    nudge_controller()->MaybeShowNudge(nullptr);
    EXPECT_TRUE(IsNudgeShown());
    nudge_manager()->Cancel(kNudgeId);
    SetTestTime(GetTestTime() + base::Hours(24) + base::Minutes(5));
  }

  // Attempt showing the nudge once more. It should not show.
  nudge_controller()->MaybeShowNudge(nullptr);
  EXPECT_FALSE(IsNudgeShown());
}

}  // namespace
}  // namespace ash