File: composebox_fieldtrial_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (236 lines) | stat: -rw-r--r-- 9,186 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2025 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/ui/webui/new_tab_page/composebox/composebox_fieldtrial.h"

#include <string>

#include "base/base64.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/grit/generated_resources.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/omnibox_proto/ntp_composebox_config.pb.h"
#include "ui/base/l10n/l10n_util.h"

namespace ntp_composebox {

namespace {

std::string SerializeAndBase64EncodeProto(
    const google::protobuf::MessageLite& proto) {
  std::string serialized_proto;
  proto.SerializeToString(&serialized_proto);
  return base::Base64Encode(serialized_proto);
}

}  // namespace

class NtpComposeboxFieldTrialTest : public testing::Test {
 public:
  ScopedFeatureConfigForTesting scoped_config_;
};

// Tests the NTP Composebox configuration when the feature is disabled.
TEST_F(NtpComposeboxFieldTrialTest, NTPComposeboxConfig_Disabled) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(kNtpComposebox);

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  EXPECT_FALSE(scoped_config_.Get().enabled);
  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  EXPECT_EQ(config.entry_point().num_page_load_animations(), 3);

  histogram_tester.ExpectTotalCount(kConfigParamParseSuccessHistogram, 0);
}

// Tests the NTP Composebox configuration when the feature is enabled with the
// default parameter.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_DefaultConfiguration) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeature(kNtpComposebox);

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  EXPECT_TRUE(scoped_config_.Get().enabled);
  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  EXPECT_EQ(config.entry_point().num_page_load_animations(), 3);

  auto composebox = config.composebox();
  EXPECT_TRUE(composebox.close_by_escape());
  EXPECT_TRUE(composebox.close_by_click_outside());

  auto image_upload = config.composebox().image_upload();
  EXPECT_EQ(image_upload.enable_webp_encoding(), false);
  EXPECT_EQ(image_upload.downscale_max_image_size(), 1500000);
  EXPECT_EQ(image_upload.downscale_max_image_width(), 1600);
  EXPECT_EQ(image_upload.downscale_max_image_height(), 1600);
  EXPECT_EQ(image_upload.image_compression_quality(), 40);
  EXPECT_THAT(image_upload.mime_types_allowed(), "image/*");

  auto attachment_upload = config.composebox().attachment_upload();
  EXPECT_EQ(attachment_upload.max_size_bytes(), 200000000);
  EXPECT_THAT(attachment_upload.mime_types_allowed(), ".pdf,application/pdf");

  EXPECT_EQ(composebox.max_num_files(), 1);
  EXPECT_EQ(composebox.input_placeholder_text(),
            l10n_util::GetStringUTF8(IDS_NTP_COMPOSE_PLACEHOLDER_TEXT));

  histogram_tester.ExpectTotalCount(kConfigParamParseSuccessHistogram, 0);
}

// Tests the NTP Composebox configuration when the feature is enabled with an
// invalid Base64-encoded proto parameter.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_Invalid_Configuration) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNtpComposebox, {{kConfigParam.name, "hello world"}});

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  EXPECT_EQ(config.entry_point().num_page_load_animations(), 3);

  histogram_tester.ExpectTotalCount(kConfigParamParseSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(kConfigParamParseSuccessHistogram, false,
                                     1);
}

// Tests the NTP Composebox configuration when the feature is enabled with a
// valid Base64-encoded proto parameter that does not set a value.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_Valid_Unset_Configuration) {
  omnibox::NTPComposeboxConfig fieldtrial_config;

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNtpComposebox,
      {{kConfigParam.name, SerializeAndBase64EncodeProto(fieldtrial_config)}});

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  EXPECT_EQ(config.entry_point().num_page_load_animations(), 3);

  histogram_tester.ExpectTotalCount(kConfigParamParseSuccessHistogram, 0);
}

// Tests the NTP Composebox configuration when the feature is enabled with a
// valid Base64-encoded proto parameter that sets a value.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_Valid_Set_Configuration) {
  omnibox::NTPComposeboxConfig fieldtrial_config;
  fieldtrial_config.mutable_entry_point()->set_num_page_load_animations(5);

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNtpComposebox,
      {{kConfigParam.name, SerializeAndBase64EncodeProto(fieldtrial_config)}});

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  EXPECT_TRUE(scoped_config_.Get().enabled);
  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  EXPECT_EQ(config.entry_point().num_page_load_animations(), 5);

  histogram_tester.ExpectTotalCount(kConfigParamParseSuccessHistogram, 1);
  histogram_tester.ExpectBucketCount(kConfigParamParseSuccessHistogram, true,
                                     1);
}

// Tests that setting `mime_types_allowed` for images in the `fieldtrial_config`
// overrides the default image mime types.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_Valid_OverrideImageMimeTypes) {
  omnibox::NTPComposeboxConfig fieldtrial_config;
  fieldtrial_config.mutable_composebox()
      ->mutable_image_upload()
      ->set_mime_types_allowed("image/png");

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNtpComposebox,
      {{kConfigParam.name, SerializeAndBase64EncodeProto(fieldtrial_config)}});

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  // Check that the image mime types were overridden.
  EXPECT_THAT(config.composebox().image_upload().mime_types_allowed(),
              "image/png");

  histogram_tester.ExpectUniqueSample(kConfigParamParseSuccessHistogram, true,
                                      1);
}

// Tests that setting `mime_types_allowed` for attachments in the
// `fieldtrial_config` overrides the default attachment mime types.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_Valid_OverrideAttachmentMimeTypes) {
  omnibox::NTPComposeboxConfig fieldtrial_config;
  fieldtrial_config.mutable_composebox()
      ->mutable_attachment_upload()
      ->set_mime_types_allowed("text/plain");

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNtpComposebox,
      {{kConfigParam.name, SerializeAndBase64EncodeProto(fieldtrial_config)}});

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  // Check that the attachment mime types were overridden.
  EXPECT_THAT(config.composebox().attachment_upload().mime_types_allowed(),
              "text/plain");

  histogram_tester.ExpectUniqueSample(kConfigParamParseSuccessHistogram, true,
                                      1);
}

// Tests that providing an empty `mime_types_allowed` value in the fieldtrial
// config does not clear the default value.
TEST_F(NtpComposeboxFieldTrialTest,
       NTPComposeboxConfig_Enabled_Valid_ClearMimeTypes) {
  omnibox::NTPComposeboxConfig fieldtrial_config;
  // Providing an empty `mime_types_allowed`, will not clear the default
  // config.
  fieldtrial_config.mutable_composebox()
      ->mutable_image_upload()
      ->mime_types_allowed();
  fieldtrial_config.mutable_composebox()
      ->mutable_attachment_upload()
      ->mime_types_allowed();

  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNtpComposebox,
      {{kConfigParam.name, SerializeAndBase64EncodeProto(fieldtrial_config)}});

  base::HistogramTester histogram_tester;
  scoped_config_.Reset();

  omnibox::NTPComposeboxConfig config = scoped_config_.Get().config;
  // Check that both default mime type lists were cleared.
  EXPECT_THAT(config.composebox().image_upload().mime_types_allowed(),
              "image/*");
  EXPECT_THAT(config.composebox().attachment_upload().mime_types_allowed(),
              ".pdf,application/pdf");

  histogram_tester.ExpectUniqueSample(kConfigParamParseSuccessHistogram, true,
                                      1);
}

}  // namespace ntp_composebox