summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/preferences_unittest.cc
blob: b7866bd666d2f83ece28371adb4e160f5529d0e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/preferences.h"

#include "chrome/browser/api/prefs/pref_member.h"
#include "chrome/browser/chromeos/input_method/mock_input_method_manager.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {
namespace {

class MyMockInputMethodManager : public input_method::MockInputMethodManager {
 public:
  MyMockInputMethodManager(StringPrefMember* previous,
                           StringPrefMember* current)
      : previous_(previous),
        current_(current) {
  }
  virtual ~MyMockInputMethodManager() {
  }

  virtual bool SetInputMethodConfig(
      const std::string& section,
      const std::string& config_name,
      const input_method::InputMethodConfigValue& value) OVERRIDE {
    // Assume the preload engines list is "KeyboardC,KeyboardA,KeyboardB".
    // Switch to the first one, C.
    ChangeInputMethod("KeyboardC");
    return true;
  }

  virtual void ChangeInputMethod(const std::string& input_method_id) OVERRIDE {
    last_input_method_id_ = input_method_id;
    // Do the same thing as BrowserStateMonitor::UpdateUserPreferences.
    const std::string current_input_method_on_pref = current_->GetValue();
    if (current_input_method_on_pref == input_method_id)
      return;
    previous_->SetValue(current_input_method_on_pref);
    current_->SetValue(input_method_id);
  }

  std::string last_input_method_id_;

 private:
  StringPrefMember* previous_;
  StringPrefMember* current_;
};

}  // anonymous namespace

TEST(PreferencesTest, TestUpdatePrefOnBrowserScreenDetails) {
  TestingPrefService prefs;
  Preferences::RegisterUserPrefs(&prefs);
  DownloadPrefs::RegisterUserPrefs(&prefs);

  StringPrefMember previous;
  previous.Init(prefs::kLanguagePreviousInputMethod, &prefs, NULL);
  previous.SetValue("KeyboardA");
  StringPrefMember current;
  current.Init(prefs::kLanguageCurrentInputMethod, &prefs, NULL);
  current.SetValue("KeyboardB");

  MyMockInputMethodManager mock_manager(&previous, &current);
  Preferences testee(&mock_manager);
  testee.InitUserPrefsForTesting(&prefs);
  testee.SetInputMethodListForTesting();

  // Confirm they're unchanged.
  EXPECT_EQ("KeyboardA", previous.GetValue());
  EXPECT_EQ("KeyboardB", current.GetValue());
  EXPECT_EQ("KeyboardB", mock_manager.last_input_method_id_);
}

}  // namespace chromeos