- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / login_state_notification_blocker_chromeos_unittest.cc
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.
4
5 #include "ash/shell.h"
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
13 class LoginStateNotificationBlockerChromeOSTest
14     : public ash::test::AshTestBase,
15       public message_center::NotificationBlocker::Observer {
16  public:
17   LoginStateNotificationBlockerChromeOSTest()
18       : state_changed_count_(0) {}
19   virtual ~LoginStateNotificationBlockerChromeOSTest() {}
20
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);
29   }
30
31   virtual void TearDown() OVERRIDE {
32     blocker_->RemoveObserver(this);
33     blocker_.reset();
34     ash::test::AshTestBase::TearDown();
35     chromeos::LoginState::Shutdown();
36   }
37
38   message_center::NotificationBlocker* blocker() { return blocker_.get(); }
39
40   // message_center::NotificationBlocker::Observer ovverrides:
41   virtual void OnBlockingStateChanged() OVERRIDE {
42     state_changed_count_++;
43   }
44
45   int GetStateChangedCountAndReset() {
46     int result = state_changed_count_;
47     state_changed_count_ = 0;
48     return result;
49   }
50
51  private:
52   int state_changed_count_;
53   scoped_ptr<LoginStateNotificationBlockerChromeOS> blocker_;
54
55   DISALLOW_COPY_AND_ASSIGN(LoginStateNotificationBlockerChromeOSTest);
56 };
57
58 TEST_F(LoginStateNotificationBlockerChromeOSTest, BaseTest) {
59   // Default status: OOBE.
60   message_center::NotifierId notifier_id;
61   EXPECT_FALSE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
62
63   // Login screen.
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));
69
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));
76
77   // Lock.
78   ash::Shell::GetInstance()->OnLockStateChanged(true);
79   EXPECT_EQ(1, GetStateChangedCountAndReset());
80   EXPECT_FALSE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
81
82   // Unlock.
83   ash::Shell::GetInstance()->OnLockStateChanged(false);
84   EXPECT_EQ(1, GetStateChangedCountAndReset());
85   EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
86 }
87
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);
92
93   // Default status: OOBE.
94   EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
95
96   // Login screen.
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));
102
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));
109
110   // Lock.
111   ash::Shell::GetInstance()->OnLockStateChanged(true);
112   EXPECT_EQ(1, GetStateChangedCountAndReset());
113   EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
114
115   // Unlock.
116   ash::Shell::GetInstance()->OnLockStateChanged(false);
117   EXPECT_EQ(1, GetStateChangedCountAndReset());
118   EXPECT_TRUE(blocker()->ShouldShowNotificationAsPopup(notifier_id));
119 }