- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_persistence_unittest.cc
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 "base/prefs/pref_service.h"
9 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
10 #include "chrome/browser/chromeos/language_preferences.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "chromeos/chromeos_switches.h"
20
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace chromeos {
24 namespace input_method {
25
26 namespace {
27 const char kInputId1[] = "xkb:us:dvorak:eng";
28 const char kInputId2[] = "xkb:us:colemak:eng";
29 }
30
31 class InputMethodPersistenceTest : public testing::Test {
32  protected:
33   InputMethodPersistenceTest()
34       : mock_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
35
36   virtual void SetUp() OVERRIDE {
37     // Set up a profile that will be returned by
38     // ProfileManager::GetDefaultProfile().
39     ASSERT_TRUE(mock_profile_manager_.SetUp());
40     TestingProfile* mock_profile =
41         mock_profile_manager_.CreateTestingProfile(chrome::kTestUserProfileDir);
42     CommandLine *cl = CommandLine::ForCurrentProcess();
43     cl->AppendSwitchASCII(switches::kLoginProfile, chrome::kTestUserProfileDir);
44     mock_profile_manager_.SetLoggedIn(true);
45     ProfileManager::AllowGetDefaultProfile();
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   TestingPrefServiceSyncable* 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