Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: base/i18n/rtl_unittest.cc

Issue 1162603004: Restore locales after tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make restores scoped Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/i18n/rtl.h" 5 #include "base/i18n/rtl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h" 11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h" 14 #include "testing/platform_test.h"
15 #include "third_party/icu/source/i18n/unicode/usearch.h" 15 #include "third_party/icu/source/i18n/unicode/usearch.h"
16 16
17 namespace base { 17 namespace base {
18 namespace i18n { 18 namespace i18n {
19 19
20 namespace { 20 namespace {
21 21
22 class ScopedSetICUDefaultLocale {
23 std::string default_locale;
24
25 public:
26 ScopedSetICUDefaultLocale(std::string locale);
27 ~ScopedSetICUDefaultLocale();
28 };
29
30 ScopedSetICUDefaultLocale::ScopedSetICUDefaultLocale(std::string locale)
31 : default_locale(locale) {}
32
33 ScopedSetICUDefaultLocale::~ScopedSetICUDefaultLocale() {
34 i18n::SetICUDefaultLocale(default_locale.data());
35 }
36
22 // A test utility function to set the application default text direction. 37 // A test utility function to set the application default text direction.
23 void SetRTL(bool rtl) { 38 void SetRTL(bool rtl) {
24 // Override the current locale/direction. 39 // Override the current locale/direction.
25 SetICUDefaultLocale(rtl ? "he" : "en"); 40 SetICUDefaultLocale(rtl ? "he" : "en");
26 EXPECT_EQ(rtl, IsRTL()); 41 EXPECT_EQ(rtl, IsRTL());
27 } 42 }
28 43
29 } // namespace 44 } // namespace
30 45
31 class RTLTest : public PlatformTest { 46 class RTLTest : public PlatformTest {
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 L"abc", 313 L"abc",
299 L"a" L"\x5d0\x5d1", 314 L"a" L"\x5d0\x5d1",
300 L"a" L"\x5d1" L"b", 315 L"a" L"\x5d1" L"b",
301 L"\x5d0\x5d1\x5d2", 316 L"\x5d0\x5d1\x5d2",
302 L"\x5d0\x5d1" L"a", 317 L"\x5d0\x5d1" L"a",
303 L"\x5d0" L"a" L"\x5d1", 318 L"\x5d0" L"a" L"\x5d1",
304 }; 319 };
305 320
306 const bool was_rtl = IsRTL(); 321 const bool was_rtl = IsRTL();
307 322
323 ScopedSetICUDefaultLocale restore_object(uloc_getDefault());
308 for (size_t i = 0; i < 2; ++i) { 324 for (size_t i = 0; i < 2; ++i) {
309 // Toggle the application default text direction (to try each direction). 325 // Toggle the application default text direction (to try each direction).
310 SetRTL(!IsRTL()); 326 SetRTL(!IsRTL());
311 327
312 string16 empty; 328 string16 empty;
313 WrapStringWithLTRFormatting(&empty); 329 WrapStringWithLTRFormatting(&empty);
314 EXPECT_TRUE(empty.empty()); 330 EXPECT_TRUE(empty.empty());
315 WrapStringWithRTLFormatting(&empty); 331 WrapStringWithRTLFormatting(&empty);
316 EXPECT_TRUE(empty.empty()); 332 EXPECT_TRUE(empty.empty());
317 333
(...skipping 27 matching lines...) Expand all
345 { L"\x05d0\x05d1\x05d2", true, true }, 361 { L"\x05d0\x05d1\x05d2", true, true },
346 { L"\x05d0\x05d1\x05d2.txt", true, true }, 362 { L"\x05d0\x05d1\x05d2.txt", true, true },
347 { L"\x05d0" L"abc", true, true }, 363 { L"\x05d0" L"abc", true, true },
348 { L"\x05d0" L"abc.txt", true, true }, 364 { L"\x05d0" L"abc.txt", true, true },
349 { L"abc\x05d0\x05d1", false, true }, 365 { L"abc\x05d0\x05d1", false, true },
350 { L"abc\x05d0\x05d1.jpg", false, true }, 366 { L"abc\x05d0\x05d1.jpg", false, true },
351 }; 367 };
352 368
353 const bool was_rtl = IsRTL(); 369 const bool was_rtl = IsRTL();
354 370
371 ScopedSetICUDefaultLocale restore_object(uloc_getDefault());
355 for (size_t i = 0; i < 2; ++i) { 372 for (size_t i = 0; i < 2; ++i) {
356 // Toggle the application default text direction (to try each direction). 373 // Toggle the application default text direction (to try each direction).
357 SetRTL(!IsRTL()); 374 SetRTL(!IsRTL());
358 for (size_t i = 0; i < arraysize(cases); ++i) { 375 for (size_t i = 0; i < arraysize(cases); ++i) {
359 string16 input = WideToUTF16(cases[i].path); 376 string16 input = WideToUTF16(cases[i].path);
360 string16 output = GetDisplayStringInLTRDirectionality(input); 377 string16 output = GetDisplayStringInLTRDirectionality(input);
361 // Test the expected wrapping behavior for the current UI directionality. 378 // Test the expected wrapping behavior for the current UI directionality.
362 if (IsRTL() ? cases[i].wrap_rtl : cases[i].wrap_ltr) 379 if (IsRTL() ? cases[i].wrap_rtl : cases[i].wrap_ltr)
363 EXPECT_NE(output, input); 380 EXPECT_NE(output, input);
364 else 381 else
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 L",\x05d0 foo bar", 448 L",\x05d0 foo bar",
432 L"\x202a \x05d0 foo bar", 449 L"\x202a \x05d0 foo bar",
433 L"\x202d \x05d0 foo bar", 450 L"\x202d \x05d0 foo bar",
434 L"\x202b foo \x05d0 bar", 451 L"\x202b foo \x05d0 bar",
435 L"\x202e foo \x05d0 bar", 452 L"\x202e foo \x05d0 bar",
436 L"\x0622 foo \x05d0 bar", 453 L"\x0622 foo \x05d0 bar",
437 }; 454 };
438 455
439 const bool was_rtl = IsRTL(); 456 const bool was_rtl = IsRTL();
440 457
458 ScopedSetICUDefaultLocale restore_object(uloc_getDefault());
441 for (size_t i = 0; i < 2; ++i) { 459 for (size_t i = 0; i < 2; ++i) {
442 // Toggle the application default text direction (to try each direction). 460 // Toggle the application default text direction (to try each direction).
443 SetRTL(!IsRTL()); 461 SetRTL(!IsRTL());
444 462
445 for (size_t i = 0; i < arraysize(cases); ++i) { 463 for (size_t i = 0; i < arraysize(cases); ++i) {
446 string16 test_case = WideToUTF16(cases[i]); 464 string16 test_case = WideToUTF16(cases[i]);
447 string16 adjusted_string = test_case; 465 string16 adjusted_string = test_case;
448 466
449 if (!AdjustStringForLocaleDirection(&adjusted_string)) 467 if (!AdjustStringForLocaleDirection(&adjusted_string))
450 continue; 468 continue;
451 469
452 EXPECT_NE(test_case, adjusted_string); 470 EXPECT_NE(test_case, adjusted_string);
453 EXPECT_TRUE(UnadjustStringForLocaleDirection(&adjusted_string)); 471 EXPECT_TRUE(UnadjustStringForLocaleDirection(&adjusted_string));
454 EXPECT_EQ(test_case, adjusted_string) << " for test case [" << test_case 472 EXPECT_EQ(test_case, adjusted_string) << " for test case [" << test_case
455 << "] with IsRTL() == " << IsRTL(); 473 << "] with IsRTL() == " << IsRTL();
456 } 474 }
457 } 475 }
458 476
459 EXPECT_EQ(was_rtl, IsRTL()); 477 EXPECT_EQ(was_rtl, IsRTL());
460 } 478 }
461 479
462 } // namespace i18n 480 } // namespace i18n
463 } // namespace base 481 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698