1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "ash/system/system_notifier.h"
7 #include "ash/test/ash_test_base.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
10 #include "chromeos/login/login_state.h"
11 #include "ui/message_center/message_center.h"
12 #include "ui/message_center/notification.h"
14 class LoginStateNotificationBlockerChromeOSTest
15 : public ash::test::AshTestBase
,
16 public message_center::NotificationBlocker::Observer
{
18 LoginStateNotificationBlockerChromeOSTest()
19 : state_changed_count_(0) {}
20 virtual ~LoginStateNotificationBlockerChromeOSTest() {}
22 // ash::tests::AshTestBase overrides:
23 virtual void SetUp() OVERRIDE
{
24 chromeos::LoginState::Initialize();
25 chromeos::LoginState::Get()->set_always_logged_in(false);
26 ash::test::AshTestBase::SetUp();
27 blocker_
.reset(new LoginStateNotificationBlockerChromeOS(
28 message_center::MessageCenter::Get()));
29 blocker_
->AddObserver(this);
32 virtual void TearDown() OVERRIDE
{
33 blocker_
->RemoveObserver(this);
35 ash::test::AshTestBase::TearDown();
36 chromeos::LoginState::Shutdown();
39 // message_center::NotificationBlocker::Observer overrides:
40 virtual void OnBlockingStateChanged(
41 message_center::NotificationBlocker
* blocker
) OVERRIDE
{
42 state_changed_count_
++;
45 int GetStateChangedCountAndReset() {
46 int result
= state_changed_count_
;
47 state_changed_count_
= 0;
51 bool ShouldShowNotificationAsPopup(
52 const message_center::NotifierId
& notifier_id
) {
53 return blocker_
->ShouldShowNotificationAsPopup(notifier_id
);
57 int state_changed_count_
;
58 scoped_ptr
<message_center::NotificationBlocker
> blocker_
;
60 DISALLOW_COPY_AND_ASSIGN(LoginStateNotificationBlockerChromeOSTest
);
63 TEST_F(LoginStateNotificationBlockerChromeOSTest
, BaseTest
) {
64 // Default status: OOBE.
65 message_center::NotifierId
notifier_id(
66 message_center::NotifierId::APPLICATION
, "test-notifier");
67 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id
));
70 chromeos::LoginState::Get()->SetLoggedInState(
71 chromeos::LoginState::LOGGED_IN_NONE
,
72 chromeos::LoginState::LOGGED_IN_USER_NONE
);
73 EXPECT_EQ(0, GetStateChangedCountAndReset());
74 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id
));
76 // Logged in as a normal user.
77 chromeos::LoginState::Get()->SetLoggedInState(
78 chromeos::LoginState::LOGGED_IN_ACTIVE
,
79 chromeos::LoginState::LOGGED_IN_USER_REGULAR
);
80 EXPECT_EQ(1, GetStateChangedCountAndReset());
81 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));
84 ash::Shell::GetInstance()->OnLockStateChanged(true);
85 EXPECT_EQ(1, GetStateChangedCountAndReset());
86 EXPECT_FALSE(ShouldShowNotificationAsPopup(notifier_id
));
89 ash::Shell::GetInstance()->OnLockStateChanged(false);
90 EXPECT_EQ(1, GetStateChangedCountAndReset());
91 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));
94 TEST_F(LoginStateNotificationBlockerChromeOSTest
, AlwaysAllowedNotifier
) {
95 // NOTIFIER_DISPLAY is allowed to shown in the login screen.
96 message_center::NotifierId
notifier_id(
97 message_center::NotifierId::SYSTEM_COMPONENT
,
98 ash::system_notifier::kNotifierDisplay
);
100 // Default status: OOBE.
101 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));
104 chromeos::LoginState::Get()->SetLoggedInState(
105 chromeos::LoginState::LOGGED_IN_NONE
,
106 chromeos::LoginState::LOGGED_IN_USER_NONE
);
107 EXPECT_EQ(0, GetStateChangedCountAndReset());
108 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));
110 // Logged in as a normal user.
111 chromeos::LoginState::Get()->SetLoggedInState(
112 chromeos::LoginState::LOGGED_IN_ACTIVE
,
113 chromeos::LoginState::LOGGED_IN_USER_REGULAR
);
114 EXPECT_EQ(1, GetStateChangedCountAndReset());
115 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));
118 ash::Shell::GetInstance()->OnLockStateChanged(true);
119 EXPECT_EQ(1, GetStateChangedCountAndReset());
120 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));
123 ash::Shell::GetInstance()->OnLockStateChanged(false);
124 EXPECT_EQ(1, GetStateChangedCountAndReset());
125 EXPECT_TRUE(ShouldShowNotificationAsPopup(notifier_id
));