File: input_method_prefs_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 4,128 bytes parent folder | download | duplicates (5)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/input_method_manager/input_method_prefs.h"

#include <optional>

#include "base/strings/stringprintf.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/crx_file/id_util.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/ash/extension_ime_util.h"

namespace arc {

class InputMethodPrefsTest : public testing::Test {
 public:
  InputMethodPrefsTest() = default;
  ~InputMethodPrefsTest() override = default;

  TestingProfile* profile() { return &profile_; }

 private:
  content::BrowserTaskEnvironment task_environment_;

  TestingProfile profile_;
};

TEST_F(InputMethodPrefsTest, Constructor) {
  InputMethodPrefs prefs(profile());
}

TEST_F(InputMethodPrefsTest, GetEnabledImes) {
  namespace aeiu = ::ash::extension_ime_util;
  using crx_file::id_util::GenerateId;

  const std::string component_extension_ime_id =
      aeiu::GetComponentInputMethodID(
          GenerateId("test.component.extension.ime"), "us");
  const std::string arc_ime_id =
      aeiu::GetArcInputMethodID(GenerateId("test.arc.ime"), "us");

  profile()->GetPrefs()->SetString(
      prefs::kLanguageEnabledImes,
      base::StringPrintf("%s,%s", component_extension_ime_id.c_str(),
                         arc_ime_id.c_str()));

  InputMethodPrefs prefs(profile());
  const std::set<std::string> imes = prefs.GetEnabledImes();
  EXPECT_EQ(2u, imes.size());
  EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
  EXPECT_TRUE(imes.count(arc_ime_id) > 0);
}

TEST_F(InputMethodPrefsTest, UpdateEnabledImes) {
  namespace aeiu = ::ash::extension_ime_util;
  using ::ash::input_method::InputMethodDescriptor;
  using crx_file::id_util::GenerateId;

  const std::string component_extension_ime_id =
      aeiu::GetComponentInputMethodID(
          GenerateId("test.component.extension.ime"), "us");
  const std::string arc_ime_id1 =
      aeiu::GetArcInputMethodID(GenerateId("test.arc.ime"), "us");
  const std::string arc_ime_id2 =
      aeiu::GetArcInputMethodID(GenerateId("test.arc.ime2"), "us");

  PrefService* pref_service = profile()->GetPrefs();

  // Enabled one component extension IME.
  pref_service->SetString(prefs::kLanguageEnabledImes,
                          component_extension_ime_id);

  InputMethodPrefs prefs(profile());
  InputMethodDescriptor arc_ime_descriptor1(
      arc_ime_id1, "", "", {}, {}, false, GURL(), GURL(),
      /*handwriting_language=*/std::nullopt);
  InputMethodDescriptor arc_ime_descriptor2(
      arc_ime_id2, "", "", {}, {}, false, GURL(), GURL(),
      /*handwriting_language=*/std::nullopt);

  {
    prefs.UpdateEnabledImes({arc_ime_descriptor1});
    const std::set<std::string> imes = prefs.GetEnabledImes();
    EXPECT_EQ(2u, imes.size());
    EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
    EXPECT_TRUE(imes.count(arc_ime_id1) > 0);
  }

  // Enable both IMEs and set current/previous IME.
  prefs.UpdateEnabledImes({arc_ime_descriptor1, arc_ime_descriptor2});
  pref_service->SetString(prefs::kLanguageCurrentInputMethod, arc_ime_id1);
  pref_service->SetString(prefs::kLanguagePreviousInputMethod, arc_ime_id2);

  {
    const std::set<std::string> imes = prefs.GetEnabledImes();
    EXPECT_EQ(3u, imes.size());
    EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
    EXPECT_TRUE(imes.count(arc_ime_id1) > 0);
    EXPECT_TRUE(imes.count(arc_ime_id2) > 0);
  }

  // Disable both ARC IMEs. It should reset current/previous IMEs.
  {
    prefs.UpdateEnabledImes({});
    const std::set<std::string> imes = prefs.GetEnabledImes();
    EXPECT_EQ(1u, imes.size());
    EXPECT_TRUE(imes.count(component_extension_ime_id) > 0);
    EXPECT_EQ("", pref_service->GetString(prefs::kLanguageCurrentInputMethod));
    EXPECT_EQ("", pref_service->GetString(prefs::kLanguagePreviousInputMethod));
  }
}

}  // namespace arc