15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind_helpers.h"
99ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/settings/cros_settings.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/chromeos/settings/device_settings_service.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chromeos/settings/cros_settings_names.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_browser_thread.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kFudgeInt = 100;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class KioskModeSettingsTest : public testing::Test {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  KioskModeSettingsTest()
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : ui_thread_(content::BrowserThread::UI, &message_loop_),
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        file_thread_(content::BrowserThread::FILE, &message_loop_) {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CrosSettings* cros_settings = CrosSettings::Get();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Remove the real DeviceSettingsProvider and replace it with a stub.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    device_settings_provider_ =
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cros_settings->GetProvider(chromeos::kReportDeviceVersionInfo);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(device_settings_provider_ != NULL);
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cros_settings->RemoveSettingsProvider(device_settings_provider_));
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cros_settings->AddSettingsProvider(&stub_settings_provider_);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~KioskModeSettingsTest() {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Restore the real DeviceSettingsProvider.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CrosSettings* cros_settings = CrosSettings::Get();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      cros_settings->RemoveSettingsProvider(&stub_settings_provider_));
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cros_settings->AddSettingsProvider(device_settings_provider_);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!KioskModeSettings::Get()->is_initialized()) {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::Get()->Initialize(
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::Bind(&KioskModeSettingsTest::SetUp,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     base::Unretained(this)));
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    KioskModeSettings::Get()->set_initialized(false);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ReInitialize() {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    KioskModeSettings::Get()->set_initialized(false);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    KioskModeSettings::Get()->Initialize(base::Bind(&base::DoNothing));
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DisableKioskModeSettings() {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    KioskModeSettings::Get()->set_initialized(false);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::MessageLoopForUI message_loop_;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TestBrowserThread ui_thread_;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TestBrowserThread file_thread_;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ScopedTestDeviceSettingsService test_device_settings_service_;
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ScopedTestCrosSettings test_cros_settings_;
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CrosSettingsProvider* device_settings_provider_;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StubCrosSettingsProvider stub_settings_provider_;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(KioskModeSettingsTest, DisabledByDefault) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(KioskModeSettings::Get()->IsKioskModeEnabled());
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(KioskModeSettingsTest, InstanceAvailable) {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(KioskModeSettings::Get() != NULL);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(KioskModeSettings::Get()->is_initialized());
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(KioskModeSettingsTest, CheckLogoutTimeoutBounds) {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if we go over max.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cros_settings->SetInteger(
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      kIdleLogoutTimeout,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::kMaxIdleLogoutTimeout + kFudgeInt);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReInitialize();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(KioskModeSettings::Get()->GetIdleLogoutTimeout(),
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta::FromMilliseconds(
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                KioskModeSettings::kMaxIdleLogoutTimeout));
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if we go under min.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cros_settings->SetInteger(
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      kIdleLogoutTimeout,
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::kMinIdleLogoutTimeout - kFudgeInt);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReInitialize();
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(KioskModeSettings::Get()->GetIdleLogoutTimeout(),
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta::FromMilliseconds(
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                KioskModeSettings::kMinIdleLogoutTimeout));
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if we are between max and min.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cros_settings->SetInteger(
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      kIdleLogoutTimeout,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::kMaxIdleLogoutTimeout - kFudgeInt);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReInitialize();
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(KioskModeSettings::Get()->GetIdleLogoutTimeout(),
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta::FromMilliseconds(
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                KioskModeSettings::kMaxIdleLogoutTimeout - kFudgeInt));
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(KioskModeSettingsTest, CheckLogoutWarningDurationBounds) {
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if we go over max.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cros_settings->SetInteger(
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      kIdleLogoutWarningDuration,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::kMaxIdleLogoutWarningDuration + kFudgeInt);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReInitialize();
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(KioskModeSettings::Get()->GetIdleLogoutWarningDuration(),
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta::FromMilliseconds(
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                KioskModeSettings::kMaxIdleLogoutWarningDuration));
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if we go under min.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cros_settings->SetInteger(
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      kIdleLogoutWarningDuration,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::kMinIdleLogoutWarningDuration - kFudgeInt);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReInitialize();
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(KioskModeSettings::Get()->GetIdleLogoutWarningDuration(),
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta::FromMilliseconds(
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                KioskModeSettings::kMinIdleLogoutWarningDuration));
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if we are between max and min.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  cros_settings->SetInteger(
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      kIdleLogoutWarningDuration,
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      KioskModeSettings::kMaxIdleLogoutWarningDuration - kFudgeInt);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ReInitialize();
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(KioskModeSettings::Get()->GetIdleLogoutWarningDuration(),
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta::FromMilliseconds(
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                KioskModeSettings::kMaxIdleLogoutWarningDuration - kFudgeInt));
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(KioskModeSettingsTest, UnitializedValues) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DisableKioskModeSettings();
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Time delta initializes to '0' microseconds.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(KioskModeSettings::Get()->GetScreensaverTimeout(),
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta());
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(KioskModeSettings::Get()->GetIdleLogoutTimeout(),
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta());
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_LT(KioskModeSettings::Get()->GetIdleLogoutWarningDuration(),
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            base::TimeDelta());
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace chromeos
167