File: virtual_keyboard_features_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (190 lines) | stat: -rw-r--r-- 7,345 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
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>

#include "ash/public/cpp/keyboard/keyboard_config.h"
#include "ash/public/cpp/keyboard/keyboard_controller.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/ash/app_mode/kiosk_app.h"
#include "chrome/browser/ash/app_mode/kiosk_app_types.h"
#include "chrome/browser/ash/app_mode/test/kiosk_mixin.h"
#include "chrome/browser/ash/app_mode/test/kiosk_test_utils.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "components/policy/core/common/cloud/test/policy_builder.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

using kiosk::test::AutoLaunchKioskApp;
using kiosk::test::CachePolicy;
using kiosk::test::WaitKioskLaunched;

namespace {

bool IsChromeApp(const KioskApp& app) {
  return app.id().type == KioskAppType::kChromeApp;
}

std::string ToJsonString(const keyboard::KeyboardConfig& config) {
  auto dict = base::Value::Dict()
                  .Set("auto_complete_enabled", config.auto_complete)
                  .Set("auto_correct_enabled", config.auto_correct)
                  .Set("handwriting_enabled", config.handwriting)
                  .Set("spell_check_enabled", config.spell_check)
                  .Set("voice_input_enabled", config.voice_input);
  std::string json_string;
  CHECK(base::JSONWriter::Write(dict, &json_string));
  return json_string;
}

// Returns the `KeyboardConfig` used by default when the policy is unset.
keyboard::KeyboardConfig UnsetKeyboardConfig() {
  // Note that `KeyboardConfig.auto_capitalize` is not configurable by policy.
  // Other fields default to `false` when the policy is unset.
  return keyboard::KeyboardConfig{
      .auto_complete = false,
      .auto_correct = false,
      .handwriting = false,
      .spell_check = false,
      .voice_input = false,
  };
}

// The parameter used to define a name and a `KeyboardConfig` for the test.
using TestParam =
    std::tuple<std::string, std::optional<keyboard::KeyboardConfig>>;

// Returns a name like "ChromeAppWithMixedValuesPolicy" for the test parameter.
std::string ParamName(
    const testing::TestParamInfo<std::tuple<KioskMixin::Config, TestParam>>&
        info) {
  auto [kiosk_mixin_config, test_param] = info.param;
  auto [test_param_name, _] = test_param;
  auto index = base::StringPrintf("App%zu", info.index);
  return kiosk_mixin_config.name.value_or(index) + test_param_name;
}

// Caches the given `keyboard_config` in fake session manager.
void CacheKeyboardPolicy(
    const std::string& account_id,
    const std::optional<keyboard::KeyboardConfig>& keyboard_config) {
  if (!keyboard_config.has_value()) {
    return;
  }

  CachePolicy(account_id,
              [keyboard_config](policy::UserPolicyBuilder& builder) {
                builder.payload().mutable_virtualkeyboardfeatures()->set_value(
                    ToJsonString(keyboard_config.value()));
              });
}

}  // namespace

// Verifies the `VirtualKeyboardFeatures` applies correctly in Kiosk.
class VirtualKeyboardFeaturesTest
    : public MixinBasedInProcessBrowserTest,
      public testing::WithParamInterface<
          std::tuple<KioskMixin::Config, TestParam>> {
 public:
  VirtualKeyboardFeaturesTest() = default;
  VirtualKeyboardFeaturesTest(const VirtualKeyboardFeaturesTest&) = delete;
  VirtualKeyboardFeaturesTest& operator=(const VirtualKeyboardFeaturesTest&) =
      delete;

  ~VirtualKeyboardFeaturesTest() override = default;

  void SetUpInProcessBrowserTestFixture() override {
    MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
    const auto& auto_launch_account_id =
        config().auto_launch_account_id.value();
    CacheKeyboardPolicy(auto_launch_account_id.value(),
                        policy_keyboard_config());
  }

  void SetUpOnMainThread() override {
    MixinBasedInProcessBrowserTest::SetUpOnMainThread();
    ASSERT_TRUE(WaitKioskLaunched());
  }

  const KioskMixin::Config& config() {
    auto& [config, _] = GetParam();
    return config;
  }

  const std::optional<keyboard::KeyboardConfig>& policy_keyboard_config() {
    auto& [_, keyboard_config] = std::get<TestParam>(GetParam());
    return keyboard_config;
  }

  KioskMixin kiosk_{&mixin_host_,
                    /*cached_configuration=*/config()};
};

IN_PROC_BROWSER_TEST_P(VirtualKeyboardFeaturesTest,
                       KeyboardConfigRespectsPolicy) {
  const auto config = KeyboardController::Get()->GetKeyboardConfig();

  // `auto_capitalize` is not controlled by policy and remain the default.
  EXPECT_EQ(config.auto_capitalize, keyboard::KeyboardConfig().auto_capitalize);

  // Chrome apps are not affected by this policy and remain the default. Web
  // apps get the policy value if set, or the unset config otherwise.
  auto expected_config =
      IsChromeApp(AutoLaunchKioskApp())
          ? keyboard::KeyboardConfig()
          : policy_keyboard_config().value_or(UnsetKeyboardConfig());

  EXPECT_EQ(config.auto_complete, expected_config.auto_complete);
  EXPECT_EQ(config.auto_correct, expected_config.auto_correct);
  EXPECT_EQ(config.handwriting, expected_config.handwriting);
  EXPECT_EQ(config.spell_check, expected_config.spell_check);
  EXPECT_EQ(config.voice_input, expected_config.voice_input);
}

INSTANTIATE_TEST_SUITE_P(
    All,
    VirtualKeyboardFeaturesTest,
    testing::Combine(
        testing::ValuesIn(KioskMixin::ConfigsToAutoLaunchEachAppType()),
        testing::Values(TestParam{"WithoutPolicy", {}},
                        TestParam{"WithAllFalsePolicy",
                                  keyboard::KeyboardConfig{
                                      .auto_complete = false,
                                      .auto_correct = false,
                                      .handwriting = false,
                                      .spell_check = false,
                                      .voice_input = false,
                                  }},
                        TestParam{"WithAllTruePolicy",
                                  keyboard::KeyboardConfig{
                                      .auto_complete = true,
                                      .auto_correct = true,
                                      .handwriting = true,
                                      .spell_check = true,
                                      .voice_input = true,
                                  }},
                        TestParam{"WithMixedValuesPolicy",
                                  keyboard::KeyboardConfig{
                                      .auto_complete = true,
                                      .auto_correct = false,
                                      .handwriting = true,
                                      .spell_check = false,
                                      .voice_input = true,
                                  }})),
    ParamName);

}  // namespace ash