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"
13 class LoginStateNotificationBlockerChromeOSTest
14 : public ash::test::AshTestBase,
15 public message_center::NotificationBlocker::Observer {
17 LoginStateNotificationBlockerChromeOSTest()
18 : state_changed_count_(0) {}
19 virtual ~LoginStateNotificationBlockerChromeOSTest() {}
21 // ash::tests::AshTestBase overrides:
22 virtual void SetUp() OVERRIDE {
23 chromeos::LoginState::Initialize();
24 chromeos::LoginState::Get()->set_always_logged_in(false);
25 ash::test::AshTestBase::SetUp();
26 blocker_.reset(new LoginStateNotificationBlockerChromeOS(
27 message_center::MessageCenter::Get()));
28 blocker_->AddObserver(this);
31 virtual void TearDown() OVERRIDE {
32 blocker_->RemoveObserver(this);
34 ash::test::AshTestBase::TearDown();
35 chromeos::LoginState::Shutdown();
38 message_center::NotificationBlocker* blocker() { return blocker_.get(); }
40 // message_center::NotificationBlocker::Observer ovverrides:
41 virtual void OnBlockingStateChanged() OVERRIDE {
42 state_changed_count_++;
45 int GetStateChangedCountAndReset() {
46 int result = state_changed_count_;
47 state_changed_count_ = 0;
52 int state_changed_count_;
53 scoped_ptr<LoginStateNotificationBlockerChromeOS> blocker_;
55 DISALLOW_COPY_AND_ASSIGN(LoginStateNotificationBlockerChromeOSTest);
58 TEST_F(LoginStateNotificationBlockerChromeOSTest, BaseTest) {
59 // Default status: OOBE.
60 message_center::NotifierId notifier_id;
61 EXPECT_FALSE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
64 chromeos::LoginState::Get()->SetLoggedInState(
65 chromeos::LoginState::LOGGED_IN_NONE,
66 chromeos::LoginState::LOGGED_IN_USER_NONE);
67 EXPECT_EQ(1, GetStateChangedCountAndReset());
68 EXPECT_FALSE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
70 // Logged in as a normal user.
71 chromeos::LoginState::Get()->SetLoggedInState(
72 chromeos::LoginState::LOGGED_IN_ACTIVE,
73 chromeos::LoginState::LOGGED_IN_USER_REGULAR);
74 EXPECT_EQ(1, GetStateChangedCountAndReset());
75 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
78 ash::Shell::GetInstance()->OnLockStateChanged(true);
79 EXPECT_EQ(1, GetStateChangedCountAndReset());
80 EXPECT_FALSE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
83 ash::Shell::GetInstance()->OnLockStateChanged(false);
84 EXPECT_EQ(1, GetStateChangedCountAndReset());
85 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
88 TEST_F(LoginStateNotificationBlockerChromeOSTest, AlwaysAllowedNotifier) {
89 // NOTIFIER_DISPLAY is allowed to shown in the login screen.
90 message_center::NotifierId notifier_id(
91 ash::system_notifier::NOTIFIER_DISPLAY);
93 // Default status: OOBE.
94 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
97 chromeos::LoginState::Get()->SetLoggedInState(
98 chromeos::LoginState::LOGGED_IN_NONE,
99 chromeos::LoginState::LOGGED_IN_USER_NONE);
100 EXPECT_EQ(1, GetStateChangedCountAndReset());
101 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
103 // Logged in as a normal user.
104 chromeos::LoginState::Get()->SetLoggedInState(
105 chromeos::LoginState::LOGGED_IN_ACTIVE,
106 chromeos::LoginState::LOGGED_IN_USER_REGULAR);
107 EXPECT_EQ(1, GetStateChangedCountAndReset());
108 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
111 ash::Shell::GetInstance()->OnLockStateChanged(true);
112 EXPECT_EQ(1, GetStateChangedCountAndReset());
113 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
116 ash::Shell::GetInstance()->OnLockStateChanged(false);
117 EXPECT_EQ(1, GetStateChangedCountAndReset());
118 EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));