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/preferences.h"
6
7#include "base/prefs/pref_member.h"
8#include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
9#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
10#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
11#include "chrome/browser/download/download_prefs.h"
12#include "chrome/common/pref_names.h"
13#include "chrome/test/base/testing_pref_service_syncable.h"
14#include "components/pref_registry/pref_registry_syncable.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace chromeos {
18namespace {
19
20class MyMockInputMethodManager : public input_method::MockInputMethodManager {
21 public:
22  class State : public MockInputMethodManager::State {
23   public:
24    explicit State(MyMockInputMethodManager* manager)
25        : MockInputMethodManager::State(manager), manager_(manager) {};
26
27    virtual void ChangeInputMethod(const std::string& input_method_id,
28                                   bool show_message) OVERRIDE {
29      manager_->last_input_method_id_ = input_method_id;
30      // Do the same thing as BrowserStateMonitor::UpdateUserPreferences.
31      const std::string current_input_method_on_pref =
32          manager_->current_->GetValue();
33      if (current_input_method_on_pref == input_method_id)
34        return;
35      manager_->previous_->SetValue(current_input_method_on_pref);
36      manager_->current_->SetValue(input_method_id);
37    }
38
39   protected:
40    virtual ~State() {};
41
42   private:
43    MyMockInputMethodManager* const manager_;
44  };
45
46  MyMockInputMethodManager(StringPrefMember* previous,
47                           StringPrefMember* current)
48      : previous_(previous),
49        current_(current) {
50    state_ = new State(this);
51  }
52
53  virtual ~MyMockInputMethodManager() {}
54
55  std::string last_input_method_id_;
56
57 private:
58  StringPrefMember* previous_;
59  StringPrefMember* current_;
60};
61
62}  // anonymous namespace
63
64TEST(PreferencesTest, TestUpdatePrefOnBrowserScreenDetails) {
65  chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager();
66  chromeos::ScopedUserManagerEnabler user_manager_enabler(user_manager);
67  const char test_user_email[] = "test_user@example.com";
68  const user_manager::User* test_user = user_manager->AddUser(test_user_email);
69  user_manager->LoginUser(test_user_email);
70
71  TestingPrefServiceSyncable prefs;
72  Preferences::RegisterProfilePrefs(prefs.registry());
73  DownloadPrefs::RegisterProfilePrefs(prefs.registry());
74  // kSelectFileLastDirectory is registered for Profile. Here we register it for
75  // testing.
76  prefs.registry()->RegisterStringPref(
77      prefs::kSelectFileLastDirectory,
78      std::string(),
79      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
80
81  StringPrefMember previous;
82  previous.Init(prefs::kLanguagePreviousInputMethod, &prefs);
83  previous.SetValue("KeyboardA");
84  StringPrefMember current;
85  current.Init(prefs::kLanguageCurrentInputMethod, &prefs);
86  current.SetValue("KeyboardB");
87
88  MyMockInputMethodManager mock_manager(&previous, &current);
89  Preferences testee(&mock_manager);
90  testee.InitUserPrefsForTesting(
91      &prefs, test_user, mock_manager.GetActiveIMEState());
92  testee.SetInputMethodListForTesting();
93
94  // Confirm they're unchanged.
95  EXPECT_EQ("KeyboardA", previous.GetValue());
96  EXPECT_EQ("KeyboardB", current.GetValue());
97  EXPECT_EQ("KeyboardB", mock_manager.last_input_method_id_);
98}
99
100}  // namespace chromeos
101