| OLD | NEW |
| (Empty) | |
| 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/chromeos/input_method/input_method_persistence.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" |
| 9 #include "chrome/browser/chromeos/language_preferences.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "chrome/test/base/testing_pref_service.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "chrome/test/base/testing_profile_manager.h" |
| 18 |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace chromeos { |
| 22 namespace input_method { |
| 23 |
| 24 namespace { |
| 25 const char kInputId1[] = "xkb:us:dvorak:eng"; |
| 26 const char kInputId2[] = "xkb:us:colemak:eng"; |
| 27 const char kProfileName[] = "input_method_test"; |
| 28 } |
| 29 |
| 30 class InputMethodPersistenceTest : public testing::Test { |
| 31 protected: |
| 32 InputMethodPersistenceTest() |
| 33 : mock_profile_manager_( |
| 34 static_cast<TestingBrowserProcess*>(g_browser_process)) { |
| 35 } |
| 36 |
| 37 virtual void SetUp() OVERRIDE { |
| 38 // Set up a profile that will be returned by |
| 39 // ProfileManager::GetDefaultProfile(). |
| 40 ASSERT_TRUE(mock_profile_manager_.SetUp()); |
| 41 TestingProfile* mock_profile = |
| 42 mock_profile_manager_.CreateTestingProfile(kProfileName); |
| 43 CommandLine *cl = CommandLine::ForCurrentProcess(); |
| 44 cl->AppendSwitchASCII(switches::kLoginProfile, kProfileName); |
| 45 mock_profile_manager_.SetLoggedIn(true); |
| 46 EXPECT_TRUE(ProfileManager::GetDefaultProfile() != NULL); |
| 47 mock_user_prefs_ = mock_profile->GetTestingPrefService(); |
| 48 } |
| 49 |
| 50 // Verifies that the user and system prefs contain the expected values. |
| 51 void VerifyPrefs(const std::string& current_input_method, |
| 52 const std::string& previous_input_method, |
| 53 const std::string& preferred_keyboard_layout) { |
| 54 EXPECT_EQ(current_input_method, |
| 55 mock_user_prefs_->GetString(prefs::kLanguageCurrentInputMethod)); |
| 56 EXPECT_EQ(previous_input_method, |
| 57 mock_user_prefs_->GetString(prefs::kLanguagePreviousInputMethod)); |
| 58 EXPECT_EQ(preferred_keyboard_layout, |
| 59 g_browser_process->local_state()->GetString( |
| 60 language_prefs::kPreferredKeyboardLayout)); |
| 61 } |
| 62 |
| 63 TestingPrefService* mock_user_prefs_; |
| 64 MockInputMethodManager mock_manager_; |
| 65 TestingProfileManager mock_profile_manager_; |
| 66 }; |
| 67 |
| 68 TEST_F(InputMethodPersistenceTest, TestLifetime) { |
| 69 { |
| 70 InputMethodPersistence persistence(&mock_manager_); |
| 71 EXPECT_EQ(1, mock_manager_.add_observer_count_); |
| 72 } |
| 73 EXPECT_EQ(1, mock_manager_.remove_observer_count_); |
| 74 } |
| 75 |
| 76 TEST_F(InputMethodPersistenceTest, TestPrefPersistenceByState) { |
| 77 InputMethodPersistence persistence(&mock_manager_); |
| 78 |
| 79 persistence.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN); |
| 80 mock_manager_.SetCurrentInputMethodId(kInputId1); |
| 81 persistence.InputMethodChanged(&mock_manager_, false); |
| 82 VerifyPrefs("", "", kInputId1); |
| 83 |
| 84 persistence.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN); |
| 85 mock_manager_.SetCurrentInputMethodId(kInputId2); |
| 86 persistence.InputMethodChanged(&mock_manager_, false); |
| 87 VerifyPrefs(kInputId2, "", kInputId1); |
| 88 |
| 89 persistence.OnSessionStateChange(InputMethodManager::STATE_LOCK_SCREEN); |
| 90 mock_manager_.SetCurrentInputMethodId(kInputId1); |
| 91 persistence.InputMethodChanged(&mock_manager_, false); |
| 92 VerifyPrefs(kInputId2, "", kInputId1); |
| 93 |
| 94 persistence.OnSessionStateChange(InputMethodManager::STATE_TERMINATING); |
| 95 mock_manager_.SetCurrentInputMethodId(kInputId1); |
| 96 persistence.InputMethodChanged(&mock_manager_, false); |
| 97 VerifyPrefs(kInputId2, "", kInputId1); |
| 98 |
| 99 persistence.OnSessionStateChange(InputMethodManager::STATE_LOGIN_SCREEN); |
| 100 mock_manager_.SetCurrentInputMethodId(kInputId2); |
| 101 persistence.InputMethodChanged(&mock_manager_, false); |
| 102 VerifyPrefs(kInputId2, "", kInputId2); |
| 103 |
| 104 persistence.OnSessionStateChange(InputMethodManager::STATE_BROWSER_SCREEN); |
| 105 mock_manager_.SetCurrentInputMethodId(kInputId1); |
| 106 persistence.InputMethodChanged(&mock_manager_, false); |
| 107 VerifyPrefs(kInputId1, kInputId2, kInputId2); |
| 108 } |
| 109 |
| 110 } // namespace input_method |
| 111 } // namespace chromeos |
| OLD | NEW |