summaryrefslogtreecommitdiff
path: root/chromium/content/browser/linux_ipc_browsertest.cc
blob: 517a3e454bae6b784a2ed474a58f2c13258c3316 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <set>
#include <string>

#include "base/command_line.h"
#include "base/macros.h"
#include "base/synchronization/lock.h"
#include "content/browser/renderer_host/sandbox_ipc_linux.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/test/fontconfig_util_linux.h"

namespace content {

class LinuxIPCBrowserTest : public ContentBrowserTest,
                            public SandboxIPCHandler::TestObserver,
                            public testing::WithParamInterface<std::string> {
 public:
  LinuxIPCBrowserTest() {
    SetUpFontConfigForTest();
    SandboxIPCHandler::SetObserverForTests(this);
  }
  ~LinuxIPCBrowserTest() override {}

 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    ContentBrowserTest::SetUpCommandLine(command_line);
    if (GetParam() == "no-zygote") {
      command_line->AppendSwitch(switches::kNoZygote);
      command_line->AppendSwitch(switches::kNoSandbox);
    }
  }

  // Override the system fontconfig configuration with a test configuration so
  // that the tested font fallback will work the same across systems.
  void SetUpFontConfigForTest() {
    gfx::SetUpFontconfig();
    for (size_t i = 0; i < gfx::kNumSystemFontsForFontconfig; ++i) {
      gfx::LoadFontIntoFontconfig(
          base::FilePath(gfx::kSystemFontsForFontconfig[i]));
    }
  }

  void OnFontOpen(int id) override {
    base::AutoLock lock(lock_);
    opened_fonts_.insert(font_names_[id]);
  }

  void OnGetFallbackFontForChar(UChar32 c, std::string name, int id) override {
    base::AutoLock lock(lock_);
    fallback_fonts_[c] = name;
    font_names_[id] = name;
  }

  std::string FallbackFontName(UChar32 c) {
    base::AutoLock lock(lock_);
    return fallback_fonts_[c];
  }

  std::set<std::string> OpenedFonts() {
    base::AutoLock lock(lock_);
    return opened_fonts_;
  }

  // These variables are accessed on the IPC thread and the test thread.
  // All accesses on the IPC thread should be before the renderer process
  // completes navigation, and all accesses on the test thread should be after.
  // However we still need a mutex for the accesses to be sequenced according to
  // the C++ memory model.
  base::Lock lock_;
  std::map<UChar32, std::string> fallback_fonts_;
  std::map<int, std::string> font_names_;
  std::set<std::string> opened_fonts_;

  DISALLOW_COPY_AND_ASSIGN(LinuxIPCBrowserTest);
};

// Tests that Linux IPC font fallback code runs round-trip when Zygote is
// disabled. It doesn't care what font is chosen, just that the IPC messages are
// flowing. This test assumes that U+65E5 (CJK "Sun" character) will trigger the
// font fallback codepath.
IN_PROC_BROWSER_TEST_P(LinuxIPCBrowserTest, FontFallbackIPCWorks) {
  GURL test_url = GetTestUrl("font", "font_fallback.html");
  EXPECT_TRUE(NavigateToURL(shell(), test_url));
  EXPECT_THAT(FallbackFontName(U'\U000065E5'), testing::Ne(""));
  EXPECT_THAT(OpenedFonts(),
              testing::Contains(FallbackFontName(U'\U000065E5')));
}

INSTANTIATE_TEST_CASE_P(LinuxIPCBrowserTest,
                        LinuxIPCBrowserTest,
                        ::testing::Values("zygote", "no-zygote"));

}  // namespace