1// Copyright 2014 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/date/date_default_view.h"
7#include "ash/system/date/date_view.h"
8#include "ash/system/user/login_status.h"
9#include "base/command_line.h"
10#include "base/message_loop/message_loop.h"
11#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
12#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
13#include "chrome/browser/chromeos/settings/cros_settings.h"
14#include "chrome/browser/ui/ash/system_tray_delegate_chromeos.h"
15#include "chromeos/chromeos_switches.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace em = enterprise_management;
19
20namespace chromeos {
21
22class SystemUse24HourClockPolicyTest
23    : public policy::DevicePolicyCrosBrowserTest {
24 public:
25  SystemUse24HourClockPolicyTest() {
26  }
27
28  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
29    command_line->AppendSwitch(switches::kLoginManager);
30    command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
31  }
32
33  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
34    InstallOwnerKey();
35    MarkAsEnterpriseOwned();
36    DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
37  }
38
39 protected:
40  void RefreshPolicyAndWaitDeviceSettingsUpdated() {
41    scoped_ptr<CrosSettings::ObserverSubscription> observer =
42        CrosSettings::Get()->AddSettingsObserver(
43            kSystemUse24HourClock,
44            base::MessageLoop::current()->QuitWhenIdleClosure());
45
46    RefreshDevicePolicy();
47    base::MessageLoop::current()->Run();
48  }
49
50  static bool GetSystemTrayDelegateShouldUse24HourClock() {
51    chromeos::SystemTrayDelegateChromeOS* tray_delegate =
52        static_cast<chromeos::SystemTrayDelegateChromeOS*>(
53            ash::Shell::GetInstance()->system_tray_delegate());
54    return tray_delegate->GetShouldUse24HourClockForTesting();
55  }
56
57  static base::HourClockType TestGetPrimarySystemTrayTimeHourType() {
58    const ash::TrayDate* tray_date = ash::Shell::GetInstance()
59                                         ->GetPrimarySystemTray()
60                                         ->GetTrayDateForTesting();
61    const ash::tray::TimeView* time_tray = tray_date->GetTimeTrayForTesting();
62
63    return time_tray->GetHourTypeForTesting();
64  }
65
66  static bool TestPrimarySystemTrayHasDateDefaultView() {
67    const ash::TrayDate* tray_date = ash::Shell::GetInstance()
68                                         ->GetPrimarySystemTray()
69                                         ->GetTrayDateForTesting();
70    const ash::DateDefaultView* date_default_view =
71        tray_date->GetDefaultViewForTesting();
72    return (date_default_view != NULL);
73  }
74
75  static void TestPrimarySystemTrayCreateDefaultView() {
76    ash::TrayDate* tray_date = ash::Shell::GetInstance()
77                                   ->GetPrimarySystemTray()
78                                   ->GetTrayDateForTesting();
79    tray_date->CreateDefaultViewForTesting(ash::user::LOGGED_IN_NONE);
80  }
81
82  static base::HourClockType TestGetPrimarySystemTrayDateHourType() {
83    const ash::TrayDate* tray_date = ash::Shell::GetInstance()
84                                         ->GetPrimarySystemTray()
85                                         ->GetTrayDateForTesting();
86    const ash::DateDefaultView* date_default_view =
87        tray_date->GetDefaultViewForTesting();
88
89    return date_default_view->GetDateView()->GetHourTypeForTesting();
90  }
91
92 private:
93  DISALLOW_COPY_AND_ASSIGN(SystemUse24HourClockPolicyTest);
94};
95
96IN_PROC_BROWSER_TEST_F(SystemUse24HourClockPolicyTest, CheckUnset) {
97  bool system_use_24hour_clock;
98  EXPECT_FALSE(CrosSettings::Get()->GetBoolean(kSystemUse24HourClock,
99                                               &system_use_24hour_clock));
100
101  EXPECT_FALSE(GetSystemTrayDelegateShouldUse24HourClock());
102  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayTimeHourType());
103  EXPECT_FALSE(TestPrimarySystemTrayHasDateDefaultView());
104
105  TestPrimarySystemTrayCreateDefaultView();
106  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayDateHourType());
107}
108
109IN_PROC_BROWSER_TEST_F(SystemUse24HourClockPolicyTest, CheckTrue) {
110  bool system_use_24hour_clock = true;
111  EXPECT_FALSE(CrosSettings::Get()->GetBoolean(kSystemUse24HourClock,
112                                               &system_use_24hour_clock));
113  EXPECT_FALSE(TestPrimarySystemTrayHasDateDefaultView());
114
115  EXPECT_FALSE(GetSystemTrayDelegateShouldUse24HourClock());
116  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayTimeHourType());
117  TestPrimarySystemTrayCreateDefaultView();
118  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayDateHourType());
119
120  em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
121  proto.mutable_use_24hour_clock()->set_use_24hour_clock(true);
122  RefreshPolicyAndWaitDeviceSettingsUpdated();
123
124  system_use_24hour_clock = false;
125  EXPECT_TRUE(CrosSettings::Get()->GetBoolean(kSystemUse24HourClock,
126                                              &system_use_24hour_clock));
127  EXPECT_TRUE(system_use_24hour_clock);
128  EXPECT_TRUE(GetSystemTrayDelegateShouldUse24HourClock());
129  EXPECT_EQ(base::k24HourClock, TestGetPrimarySystemTrayTimeHourType());
130
131  EXPECT_TRUE(TestPrimarySystemTrayHasDateDefaultView());
132  EXPECT_EQ(base::k24HourClock, TestGetPrimarySystemTrayDateHourType());
133}
134
135IN_PROC_BROWSER_TEST_F(SystemUse24HourClockPolicyTest, CheckFalse) {
136  bool system_use_24hour_clock = true;
137  EXPECT_FALSE(CrosSettings::Get()->GetBoolean(kSystemUse24HourClock,
138                                               &system_use_24hour_clock));
139  EXPECT_FALSE(TestPrimarySystemTrayHasDateDefaultView());
140
141  EXPECT_FALSE(GetSystemTrayDelegateShouldUse24HourClock());
142  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayTimeHourType());
143  TestPrimarySystemTrayCreateDefaultView();
144  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayDateHourType());
145
146  em::ChromeDeviceSettingsProto& proto(device_policy()->payload());
147  proto.mutable_use_24hour_clock()->set_use_24hour_clock(false);
148  RefreshPolicyAndWaitDeviceSettingsUpdated();
149
150  system_use_24hour_clock = true;
151  EXPECT_TRUE(CrosSettings::Get()->GetBoolean(kSystemUse24HourClock,
152                                              &system_use_24hour_clock));
153  EXPECT_FALSE(system_use_24hour_clock);
154  EXPECT_FALSE(GetSystemTrayDelegateShouldUse24HourClock());
155  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayTimeHourType());
156  EXPECT_TRUE(TestPrimarySystemTrayHasDateDefaultView());
157  EXPECT_EQ(base::k12HourClock, TestGetPrimarySystemTrayDateHourType());
158}
159
160}  // namespace chromeos
161