File: language_detection_javascript_unittest.mm

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (267 lines) | stat: -rw-r--r-- 10,121 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#import "base/test/ios/wait_util.h"
#import "components/language/ios/browser/ios_language_detection_tab_helper.h"
#import "ios/web/public/test/javascript_test.h"
#import "ios/web/public/test/js_test_util.h"
#import "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"

using base::test::ios::kWaitForJSCompletionTimeout;
using base::test::ios::WaitUntilConditionOrTimeout;

namespace {

const char kExpectedLanguage[] = "Foo";

// Returns an NSString filled with the char 'a' of length `length`.
NSString* GetLongString(NSUInteger length) {
  NSMutableData* data = [[NSMutableData alloc] initWithLength:length];
  memset([data mutableBytes], 'a', length);
  NSString* long_string = [[NSString alloc] initWithData:data
                                                encoding:NSASCIIStringEncoding];
  return long_string;
}

}  // namespace

// A WKScriptMessageHandler which stores the last received WKScriptMessage;
@interface FakeScriptMessageHandler : NSObject <WKScriptMessageHandler>

@property(nonatomic, strong) WKScriptMessage* lastReceivedMessage;

@end

@implementation FakeScriptMessageHandler

- (void)userContentController:(WKUserContentController*)userContentController
      didReceiveScriptMessage:(WKScriptMessage*)message {
  _lastReceivedMessage = message;
}

@end

namespace language {

class LanguageDetectionJavascriptTest : public web::JavascriptTest {
 protected:
  LanguageDetectionJavascriptTest()
      : handler_([[FakeScriptMessageHandler alloc] init]) {
    [web_view().configuration.userContentController
        addScriptMessageHandler:handler_
                           name:@"LanguageDetectionTextCaptured"];
  }
  ~LanguageDetectionJavascriptTest() override = default;

  void SetUp() override {
    web::JavascriptTest::SetUp();

    AddGCrWebScript();
    AddCommonScript();
    AddMessageScript();
    AddUserScript(@"language_detection");
  }

  // Triggers Javascript language detection and waits for the received
  // detection results in `handler_.lastReceivedMessage`.
  bool TriggerLanguageDetection() {
    // Reset value to ensure wait below stops at correct time.
    handler_.lastReceivedMessage = nil;

    web::test::ExecuteJavaScript(
        web_view(), @"__gCrWeb.languageDetection.detectLanguage()");
    // Wait until `detectLanguage` completes.
    return WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^bool() {
      return handler_.lastReceivedMessage;
    });
  }

  // Retrieves the buffered text content from the language detection script.
  id GetTextContent() {
    return web::test::ExecuteJavaScript(
        web_view(),
        @"__gCrWeb.languageDetection.retrieveBufferedTextContent()");
  }

  FakeScriptMessageHandler* handler() { return handler_; }

 private:
  FakeScriptMessageHandler* handler_;
};

// Tests correctness of the `document.documentElement.lang` attribute.
TEST_F(LanguageDetectionJavascriptTest, HtmlLang) {
  // Non-empty attribute.
  NSString* html = [[NSString alloc]
      initWithFormat:@"<html lang='%s'></html>", kExpectedLanguage];
  ASSERT_TRUE(LoadHtml(html));
  id document_element_lang = web::test::ExecuteJavaScript(
      web_view(), @"document.documentElement.lang;");
  EXPECT_NSEQ(@(kExpectedLanguage), document_element_lang);

  // Empty attribute.
  ASSERT_TRUE(LoadHtml(@"<html></html>"));
  document_element_lang = web::test::ExecuteJavaScript(
      web_view(), @"document.documentElement.lang;");
  EXPECT_NSEQ(@"", document_element_lang);

  // Test with mixed case.
  html = [[NSString alloc]
      initWithFormat:@"<html lAnG='%s'></html>", kExpectedLanguage];
  ASSERT_TRUE(LoadHtml(html));
  document_element_lang = web::test::ExecuteJavaScript(
      web_view(), @"document.documentElement.lang;");
  EXPECT_NSEQ(@(kExpectedLanguage), document_element_lang);
}

// HTML elements introduce a line break, except inline ones.
TEST_F(LanguageDetectionJavascriptTest, ExtractWhitespace) {
  // `b` and `span` do not break lines.
  // `br` and `div` do.
  ASSERT_TRUE(LoadHtml(@"<html><body>"
                        "O<b>n</b>e<br>Two\tT<span>hr</span>ee<div>Four</div>"
                        "</body></html>"));

  ASSERT_TRUE(TriggerLanguageDetection());
  EXPECT_NSEQ(@"One\nTwo\tThree\nFour", GetTextContent());

  // `a` does not break lines.
  // `li`, `p` and `ul` do.
  ASSERT_TRUE(LoadHtml(
      @"<html><body>"
       "<ul><li>One</li><li>T<a href='foo'>wo</a></li></ul><p>Three</p>"
       "</body></html>"));

  ASSERT_TRUE(TriggerLanguageDetection());
  EXPECT_NSEQ(@"\n\nOne\nTwo\nThree", GetTextContent());
}

// Tests that the text content returns only up to `kMaxIndexChars` number of
// characters even if the text content is very large.
TEST_F(LanguageDetectionJavascriptTest, LongTextContent) {
  // Very long string.
  NSUInteger kLongStringLength = kMaxIndexChars - 5;
  NSMutableString* long_string = [GetLongString(kLongStringLength) mutableCopy];
  [long_string appendString:@" b cdefghijklmnopqrstuvwxyz"];

  NSString* html = [[NSString alloc]
      initWithFormat:@"<html><body>%@</html></body>", long_string];
  LoadHtml(html);

  ASSERT_TRUE(TriggerLanguageDetection());
  // The string should be cut at the last whitespace, after the 'b' character.
  EXPECT_EQ(language::kMaxIndexChars, [GetTextContent() length]);
}

// Tests if `__gCrWeb.languageDetection.retrieveBufferedTextContent` correctly
// retrieves the cache and then purges it.
TEST_F(LanguageDetectionJavascriptTest, RetrieveBufferedTextContent) {
  LoadHtml(@"<html>foo</html>");

  // Set cached text content by running language detection.
  ASSERT_TRUE(TriggerLanguageDetection());
  // Retrieve the content which should clear the cache.
  EXPECT_NSEQ(@"foo", GetTextContent());
  // Verify cache is purged.
  EXPECT_NSEQ([NSNull null], GetTextContent());
}

// Tests that the LanguageDetectionTextCaptured message body correctly informs
// the native side of page state.
TEST_F(LanguageDetectionJavascriptTest,
       LanguageDetectionTextCapturedResponseBody) {
  LoadHtml(@"<html></html>");
  ASSERT_TRUE(TriggerLanguageDetection());

  // Verify the response has all required keys.
  NSDictionary* body = handler().lastReceivedMessage.body;
  ASSERT_TRUE(body);
  ASSERT_TRUE([body isKindOfClass:[NSDictionary class]]);
  EXPECT_TRUE(body[@"frameId"]);
  EXPECT_TRUE(body[@"hasNoTranslate"]);
  EXPECT_TRUE(body[@"htmlLang"]);
  EXPECT_TRUE(body[@"httpContentLanguage"]);
}

// Tests if `__gCrWeb.languageDetection.detectLanguage` correctly informs the
// native side when the notranslate meta tag is specified.
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithNoTranslateMeta) {
  // A simple page using the notranslate meta tag.
  NSString* html = @"<html><head>"
                   @"<meta http-equiv='content-language' content='foo'>"
                   @"<meta name='google' content='notranslate'>"
                   @"</head></html>";
  LoadHtml(html);
  ASSERT_TRUE(TriggerLanguageDetection());

  ASSERT_TRUE(handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  EXPECT_NSEQ(@"foo",
              handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  ASSERT_TRUE(handler().lastReceivedMessage.body[@"hasNoTranslate"]);
  EXPECT_TRUE(
      [handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
}

// Tests if `__gCrWeb.languageDetection.detectLanguage` correctly informs the
// native side when the notranslate html attribute is specified
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithHTMLNoTranslate) {
  // A simple page using the notranslate meta tag.
  NSString* html = @"<html translate='no'><head>"
                   @"<meta http-equiv='content-language' content='foo'>"
                   @"</head></html>";
  LoadHtml(html);
  ASSERT_TRUE(TriggerLanguageDetection());

  ASSERT_TRUE(handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  EXPECT_NSEQ(@"foo",
              handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  ASSERT_TRUE(handler().lastReceivedMessage.body[@"hasNoTranslate"]);
  EXPECT_TRUE(
      [handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
}

// Tests if `__gCrWeb.languageDetection.detectLanguage` does not confuse a body
// or div language='no' for a page wide translate disabling.
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithDIVNoTranslate) {
  // A simple page using the notranslate meta tag.
  NSString* html = @"<html><head>"
                   @"<meta http-equiv='content-language' content='foo'>"
                   @"</head><body translate='no'>"
                   @"<div translate='no'>test</div></html>";
  LoadHtml(html);
  ASSERT_TRUE(TriggerLanguageDetection());

  ASSERT_TRUE(handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  EXPECT_NSEQ(@"foo",
              handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  ASSERT_TRUE(handler().lastReceivedMessage.body[@"hasNoTranslate"]);
  EXPECT_FALSE(
      [handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
}

// Tests if `__gCrWeb.languageDetection.detectLanguage` correctly informs the
// native side when no notranslate meta tag is specified.
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithoutNoTranslateMeta) {
  // A simple page using the notranslate meta tag.
  NSString* html = @"<html><head>"
                   @"<meta http-equiv='content-language' content='foo'>"
                   @"</head></html>";
  LoadHtml(html);
  ASSERT_TRUE(TriggerLanguageDetection());

  ASSERT_TRUE(handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  EXPECT_NSEQ(@"foo",
              handler().lastReceivedMessage.body[@"httpContentLanguage"]);
  ASSERT_TRUE(handler().lastReceivedMessage.body[@"hasNoTranslate"]);
  EXPECT_FALSE(
      [handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
}

}  // namespace language