#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_;
};
base::Time BirchPrivacyNudgeControllerTest::test_time_;
TEST_F(BirchPrivacyNudgeControllerTest, NudgeShows_ByDefault) {
EXPECT_FALSE(IsNudgeShown());
nudge_controller()->MaybeShowNudge(nullptr);
EXPECT_TRUE(IsNudgeShown());
}
TEST_F(BirchPrivacyNudgeControllerTest, NudgeDoesNotShow_WhenMenuWasOpened) {
BirchPrivacyNudgeController::DidShowContextMenu();
EXPECT_FALSE(IsNudgeShown());
nudge_controller()->MaybeShowNudge(nullptr);
EXPECT_FALSE(IsNudgeShown());
}
TEST_F(BirchPrivacyNudgeControllerTest, NudgeDoesNotShow_IfRecentlyShown) {
SetTestTime(base::Time::Now());
base::subtle::ScopedTimeClockOverrides clock_override(
&BirchPrivacyNudgeControllerTest::GetTestTime,
nullptr, nullptr);
EXPECT_FALSE(IsNudgeShown());
nudge_controller()->MaybeShowNudge(nullptr);
EXPECT_TRUE(IsNudgeShown());
nudge_manager()->Cancel(kNudgeId);
nudge_controller()->MaybeShowNudge(nullptr);
EXPECT_FALSE(IsNudgeShown());
SetTestTime(GetTestTime() + base::Hours(23));
nudge_controller()->MaybeShowNudge(nullptr);
EXPECT_FALSE(IsNudgeShown());
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(
&BirchPrivacyNudgeControllerTest::GetTestTime,
nullptr, nullptr);
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));
}
nudge_controller()->MaybeShowNudge(nullptr);
EXPECT_FALSE(IsNudgeShown());
}
}
}