File: title_util_unittest.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 (126 lines) | stat: -rw-r--r-- 5,554 bytes parent folder | download | duplicates (3)
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
// Copyright 2022 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/chooser_controller/title_util.h"

#include "base/command_line.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/test/navigation_simulator.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"  // nogncheck
#include "chrome/browser/extensions/test_extension_system.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

#if !BUILDFLAG(IS_ANDROID)
#include "base/test/gmock_expected_support.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#endif  // !BUILDFLAG(IS_ANDROID)

namespace {

constexpr int kTitleResourceId = IDS_USB_DEVICE_CHOOSER_PROMPT;

using CreateChooserTitleTest = ChromeRenderViewHostTestHarness;

TEST_F(CreateChooserTitleTest, NoFrame) {
  EXPECT_EQ(u"", CreateChooserTitle(nullptr, kTitleResourceId));
}

TEST_F(CreateChooserTitleTest, UrlFrameTree) {
  NavigateAndCommit(GURL("https://main-frame.com"));
  content::RenderFrameHost* subframe =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL("https://sub-frame.com"),
          content::RenderFrameHostTester::For(main_rfh())
              ->AppendChild("subframe"));

  EXPECT_EQ("main-frame.com", main_rfh()->GetLastCommittedOrigin().host());
  EXPECT_EQ(u"main-frame.com wants to connect",
            CreateChooserTitle(main_rfh(), kTitleResourceId));
  EXPECT_EQ("sub-frame.com", subframe->GetLastCommittedOrigin().host());
  EXPECT_EQ(u"main-frame.com wants to connect",
            CreateChooserTitle(subframe, kTitleResourceId));
}

#if BUILDFLAG(ENABLE_EXTENSIONS)
TEST_F(CreateChooserTitleTest, ExtensionsFrameTree) {
  auto manifest = base::Value::Dict()
                      .Set("name", "Chooser Title Subframe Test")
                      .Set("version", "0.1")
                      .Set("manifest_version", 2)
                      .Set("web_accessible_resources",
                           base::Value::List().Append("index.html"));
  scoped_refptr<const extensions::Extension> extension =
      extensions::ExtensionBuilder().SetManifest(std::move(manifest)).Build();
  ASSERT_TRUE(extension);

  extensions::TestExtensionSystem* extension_system =
      static_cast<extensions::TestExtensionSystem*>(
          extensions::ExtensionSystem::Get(profile()));
  extension_system->CreateExtensionService(
      base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
  extensions::ExtensionRegistrar::Get(profile())->AddExtension(extension.get());

  NavigateAndCommit(extension->ResolveExtensionURL("index.html"));
  content::RenderFrameHost* subframe =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL("data:text/html,"),
          content::RenderFrameHostTester::For(main_rfh())
              ->AppendChild("subframe"));

  ASSERT_EQ(extension->id(), main_rfh()->GetLastCommittedOrigin().host());
  EXPECT_EQ(u"Chooser Title Subframe Test wants to connect",
            CreateChooserTitle(main_rfh(), kTitleResourceId));
  ASSERT_NE(extension->id(), subframe->GetLastCommittedOrigin().host());
  EXPECT_EQ(u"Chooser Title Subframe Test wants to connect",
            CreateChooserTitle(subframe, kTitleResourceId));
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

#if !BUILDFLAG(IS_ANDROID)
TEST_F(CreateChooserTitleTest, IsolatedWebAppFrameTree) {
  data_decoder::test::InProcessDataDecoder in_process_data_decoder;
  web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());

  std::unique_ptr<web_app::ScopedBundledIsolatedWebApp> iwa =
      web_app::IsolatedWebAppBuilder(web_app::ManifestBuilder().SetName(
                                         "Chooser Title FrameTree IWA Name"))
          .BuildBundle();
  iwa->TrustSigningKey();
  iwa->FakeInstallPageState(profile());
  ASSERT_OK_AND_ASSIGN(web_app::IsolatedWebAppUrlInfo url_info,
                       iwa->Install(profile()));
  GURL app_url = url_info.origin().GetURL();
  web_app::SimulateIsolatedWebAppNavigation(web_contents(), app_url);

  content::RenderFrameHost* subframe =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL("data:text/html,"),
          content::RenderFrameHostTester::For(main_rfh())
              ->AppendChild("subframe"));

  ASSERT_EQ(app_url, main_rfh()->GetLastCommittedOrigin().GetURL());
  EXPECT_EQ(u"Chooser Title FrameTree IWA Name wants to connect",
            CreateChooserTitle(main_rfh(), kTitleResourceId));
  ASSERT_NE(app_url, subframe->GetLastCommittedOrigin().GetURL());
  EXPECT_EQ(u"Chooser Title FrameTree IWA Name wants to connect",
            CreateChooserTitle(subframe, kTitleResourceId));
}
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace