File: enterprise_kiosk_input_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 (199 lines) | stat: -rw-r--r-- 6,903 bytes parent folder | download | duplicates (4)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2023 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/common/extensions/api/enterprise_kiosk_input.h"

#include <string>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/crosapi/test_crosapi_environment.h"
#include "chrome/browser/extensions/api/enterprise_kiosk_input/enterprise_kiosk_input_api.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/crx_file/id_util.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/api_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/ash/extension_ime_util.h"
#include "ui/base/ime/ash/fake_input_method_delegate.h"
#include "ui/base/ime/ash/input_method_descriptor.h"
#include "ui/base/ime/ash/input_method_manager.h"
#include "ui/base/ime/ash/input_method_util.h"
#include "ui/base/ime/ash/mock_input_method_manager.h"

namespace extensions {

namespace {

constexpr char kUsLayout[] = "xkb:us::eng";
constexpr char kLatamLayout[] = "xkb:latam::spa";
constexpr char kFrLayout[] = "xkb:fr::fra";
constexpr char kDeLayout[] = "xkb:de::ger";
constexpr char kInvalidLayout[] = "asdf_invalid_pinyin";
constexpr char kNoError[] = "";
constexpr char kErrorMessageTemplate[] =
    "Could not change current input method. Invalid input method id: %s.";

namespace im = ::ash::input_method;
namespace ime_util = ::ash::extension_ime_util;

std::string GetComponentExtensionImeId(const std::string& layout) {
  return ime_util::GetComponentInputMethodID(ime_util::kXkbExtensionId, layout);
}

class TestInputMethodManager : public im::MockInputMethodManager {
 public:
  class TestState : public im::MockInputMethodManager::State {
   public:
    TestState() {
      enabled_input_method_ids_.reserve(3);

      enabled_input_method_ids_.emplace_back(
          GetComponentExtensionImeId(kUsLayout));
      enabled_input_method_ids_.emplace_back(
          GetComponentExtensionImeId(kLatamLayout));
      enabled_input_method_ids_.emplace_back(
          GetComponentExtensionImeId(kFrLayout));

      current_ime_id_ = enabled_input_method_ids_[0];
    }

    TestState(const TestState&) = delete;
    TestState& operator=(const TestState&) = delete;

    void ChangeInputMethod(const std::string& input_method_id,
                           bool show_message) override {
      for (const auto& enabled_id : enabled_input_method_ids_) {
        if (input_method_id == enabled_id) {
          current_ime_id_ = input_method_id;
        }
      }
    }

    im::InputMethodDescriptor GetCurrentInputMethod() const override {
      return im::InputMethodDescriptor(
          /*id=*/current_ime_id_,
          /*name=*/"",
          /*indicator=*/"",
          /*keyboard_layout=*/"",
          /*language_codes=*/std::vector<std::string>(),
          /*is_login_keyboard=*/false,
          /*options_page_url=*/GURL(),
          /*input_view_url=*/GURL(),
          /*handwriting_language=*/std::nullopt);
    }

    const std::vector<std::string>& GetEnabledInputMethodIds() const override {
      return enabled_input_method_ids_;
    }

   protected:
    friend base::RefCounted<im::InputMethodManager::State>;
    ~TestState() override = default;

   private:
    std::vector<std::string> enabled_input_method_ids_;
    std::string current_ime_id_;
  };

  TestInputMethodManager() : state_(new TestState) {}

  TestInputMethodManager(const TestInputMethodManager&) = delete;
  TestInputMethodManager& operator=(const TestInputMethodManager&) = delete;

  scoped_refptr<im::InputMethodManager::State> GetActiveIMEState() override {
    return state_;
  }

  std::string GetMigratedInputMethodID(
      const std::string& input_method_id) override {
    if (input_method_id == kUsLayout || input_method_id == kLatamLayout ||
        input_method_id == kFrLayout) {
      return GetComponentExtensionImeId(input_method_id);
    }
    return "";
  }

 private:
  scoped_refptr<TestState> state_;
};

}  // namespace

class EnterpriseKioskInputTest : public extensions::ApiUnitTest {
 public:
  void SetUp() override {
    ApiUnitTest::SetUp();
    crosapi_environment_.SetUp();
  }

  void TearDown() override {
    crosapi_environment_.TearDown();
    ApiUnitTest::TearDown();
  }

 protected:
  base::Value::List CreateArgs(const std::string& input_method_id) {
    api::enterprise_kiosk_input::SetCurrentInputMethodOptions options;
    options.input_method_id = input_method_id;
    base::Value::List args;
    args.Append(options.ToValue());
    return args;
  }
  TestingProfileManager testing_profile_manager_{
      TestingBrowserProcess::GetGlobal()};
  crosapi::TestCrosapiEnvironment crosapi_environment_{
      &testing_profile_manager_};
};

// Test for API enterprise.kioskInput.setCurrentInputMethod
TEST_F(EnterpriseKioskInputTest, SetCurrentInputMethodFunctionTest) {
  TestInputMethodManager::Initialize(new TestInputMethodManager);

  scoped_refptr<im::InputMethodManager::State> ime_state =
      im::InputMethodManager::Get()->GetActiveIMEState();
  ASSERT_TRUE(ime_state);

  EXPECT_EQ(GetComponentExtensionImeId(kUsLayout),
            ime_state->GetCurrentInputMethod().id());

  {
    auto function = base::MakeRefCounted<
        EnterpriseKioskInputSetCurrentInputMethodFunction>();
    api_test_utils::RunFunction(function.get(), CreateArgs(kLatamLayout),
                                browser_context(),
                                api_test_utils::FunctionMode::kNone);
    EXPECT_EQ(function->GetError(), kNoError);
    EXPECT_EQ(GetComponentExtensionImeId(kLatamLayout),
              ime_state->GetCurrentInputMethod().id());
  }

  {
    auto function = base::MakeRefCounted<
        EnterpriseKioskInputSetCurrentInputMethodFunction>();
    api_test_utils::RunFunction(function.get(), CreateArgs(kDeLayout),
                                browser_context(),
                                api_test_utils::FunctionMode::kNone);
    EXPECT_EQ(function->GetError(),
              base::StringPrintf(kErrorMessageTemplate, kDeLayout));
    EXPECT_EQ(GetComponentExtensionImeId(kLatamLayout),
              ime_state->GetCurrentInputMethod().id());
  }

  {
    auto function = base::MakeRefCounted<
        EnterpriseKioskInputSetCurrentInputMethodFunction>();
    api_test_utils::RunFunction(function.get(), CreateArgs(kInvalidLayout),
                                browser_context(),
                                api_test_utils::FunctionMode::kNone);
    EXPECT_EQ(function->GetError(),
              base::StringPrintf(kErrorMessageTemplate, kInvalidLayout));
    EXPECT_EQ(GetComponentExtensionImeId(kLatamLayout),
              ime_state->GetCurrentInputMethod().id());
  }
}

}  // namespace extensions