1 // Copyright (c) 2012 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 "chrome/browser/prefs/incognito_mode_prefs.h" 6 7 #include "chrome/common/pref_names.h" 8 #include "chrome/test/base/testing_pref_service_syncable.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 class IncognitoModePrefsTest : public testing::Test { 12 protected: 13 virtual void SetUp() { 14 IncognitoModePrefs::RegisterProfilePrefs(prefs_.registry()); 15 } 16 17 TestingPrefServiceSyncable prefs_; 18 }; 19 20 TEST_F(IncognitoModePrefsTest, IntToAvailability) { 21 ASSERT_EQ(0, IncognitoModePrefs::ENABLED); 22 ASSERT_EQ(1, IncognitoModePrefs::DISABLED); 23 ASSERT_EQ(2, IncognitoModePrefs::FORCED); 24 25 IncognitoModePrefs::Availability incognito; 26 EXPECT_TRUE(IncognitoModePrefs::IntToAvailability(0, &incognito)); 27 EXPECT_EQ(IncognitoModePrefs::ENABLED, incognito); 28 EXPECT_TRUE(IncognitoModePrefs::IntToAvailability(1, &incognito)); 29 EXPECT_EQ(IncognitoModePrefs::DISABLED, incognito); 30 EXPECT_TRUE(IncognitoModePrefs::IntToAvailability(2, &incognito)); 31 EXPECT_EQ(IncognitoModePrefs::FORCED, incognito); 32 33 EXPECT_FALSE(IncognitoModePrefs::IntToAvailability(10, &incognito)); 34 EXPECT_EQ(IncognitoModePrefs::ENABLED, incognito); 35 EXPECT_FALSE(IncognitoModePrefs::IntToAvailability(-1, &incognito)); 36 EXPECT_EQ(IncognitoModePrefs::ENABLED, incognito); 37 } 38 39 TEST_F(IncognitoModePrefsTest, GetAvailability) { 40 prefs_.SetUserPref(prefs::kIncognitoModeAvailability, 41 Value::CreateIntegerValue(IncognitoModePrefs::ENABLED)); 42 EXPECT_EQ(IncognitoModePrefs::ENABLED, 43 IncognitoModePrefs::GetAvailability(&prefs_)); 44 45 prefs_.SetUserPref(prefs::kIncognitoModeAvailability, 46 Value::CreateIntegerValue(IncognitoModePrefs::DISABLED)); 47 EXPECT_EQ(IncognitoModePrefs::DISABLED, 48 IncognitoModePrefs::GetAvailability(&prefs_)); 49 50 prefs_.SetUserPref(prefs::kIncognitoModeAvailability, 51 Value::CreateIntegerValue(IncognitoModePrefs::FORCED)); 52 EXPECT_EQ(IncognitoModePrefs::FORCED, 53 IncognitoModePrefs::GetAvailability(&prefs_)); 54 } 55 56 typedef IncognitoModePrefsTest IncognitoModePrefsDeathTest; 57 58 // Takes too long to execute on Mac. http://crbug.com/101109 59 #if defined(OS_MACOSX) 60 #define MAYBE_GetAvailabilityBadValue DISABLED_GetAvailabilityBadValue 61 #else 62 #define MAYBE_GetAvailabilityBadValue GetAvailabilityBadValue 63 #endif 64 65 #if GTEST_HAS_DEATH_TEST 66 TEST_F(IncognitoModePrefsDeathTest, MAYBE_GetAvailabilityBadValue) { 67 prefs_.SetUserPref(prefs::kIncognitoModeAvailability, 68 Value::CreateIntegerValue(-1)); 69 #if defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) 70 EXPECT_DEATH({ 71 IncognitoModePrefs::Availability availability = 72 IncognitoModePrefs::GetAvailability(&prefs_); 73 EXPECT_EQ(IncognitoModePrefs::ENABLED, availability); 74 }, ""); 75 #else 76 EXPECT_DEBUG_DEATH({ 77 IncognitoModePrefs::Availability availability = 78 IncognitoModePrefs::GetAvailability(&prefs_); 79 EXPECT_EQ(IncognitoModePrefs::ENABLED, availability); 80 }, ""); 81 #endif 82 } 83 #endif // GTEST_HAS_DEATH_TEST 84