File: manifest_manager_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (159 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download | duplicates (11)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/manifest/manifest_manager.h"

#include <string>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/html/html_head_element.h"
#include "third_party/blink/renderer/core/html/html_link_element.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/modules/manifest/manifest_change_notifier.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

namespace {

void RegisterMockedURL(const std::string& base_url,
                       const std::string& file_name) {
  url_test_helpers::RegisterMockedURLLoadFromBase(
      WebString::FromUTF8(base_url), test::CoreTestDataPath(),
      WebString::FromUTF8(file_name));
}

}  // namespace

class MockManifestChangeNotifier : public ManifestChangeNotifier {
 public:
  MockManifestChangeNotifier(LocalDOMWindow& window)
      : ManifestChangeNotifier(window), manifest_change_count_(0) {}
  ~MockManifestChangeNotifier() override = default;

  // ManifestChangeNotifier:
  void DidChangeManifest() override { ++manifest_change_count_; }

  int ManifestChangeCount() { return manifest_change_count_; }

 private:
  int manifest_change_count_;
};

class ManifestManagerTest : public PageTestBase {
 protected:
  ManifestManagerTest() : base_url_("http://internal.test/") {}
  void SetUp() override { PageTestBase::SetUp(gfx::Size()); }

  void TearDown() override {
    ThreadState::Current()->CollectAllGarbageForTesting();
  }

  ManifestManager* GetManifestManager() {
    return ManifestManager::From(*GetFrame().DomWindow());
  }

  std::string base_url_;
};

TEST_F(ManifestManagerTest, ManifestURL) {
  // Test the default result.
  EXPECT_EQ(nullptr, GetDocument().LinkManifest());

  // Check that we use the first manifest with <link rel=manifest>
  auto* link_manifest = MakeGarbageCollected<HTMLLinkElement>(
      GetDocument(), CreateElementFlags());
  link_manifest->setAttribute(blink::html_names::kRelAttr,
                              AtomicString("manifest"));
  GetDocument().head()->AppendChild(link_manifest);
  EXPECT_EQ(link_manifest, GetDocument().LinkManifest());

  // No href attribute was set.
  EXPECT_EQ(KURL(), GetManifestManager()->ManifestURL());

  // Set to some absolute url.
  link_manifest->setAttribute(html_names::kHrefAttr,
                              AtomicString("http://example.com/manifest.json"));
  ASSERT_EQ(link_manifest->Href(), GetManifestManager()->ManifestURL());

  // Set to some relative url.
  link_manifest->setAttribute(html_names::kHrefAttr,
                              AtomicString("static/manifest.json"));
  ASSERT_EQ(link_manifest->Href(), GetManifestManager()->ManifestURL());
}

TEST_F(ManifestManagerTest, ManifestUseCredentials) {
  // Test the default result.
  EXPECT_EQ(nullptr, GetDocument().LinkManifest());

  // Check that we use the first manifest with <link rel=manifest>
  auto* link_manifest = MakeGarbageCollected<HTMLLinkElement>(
      GetDocument(), CreateElementFlags());
  link_manifest->setAttribute(blink::html_names::kRelAttr,
                              AtomicString("manifest"));
  GetDocument().head()->AppendChild(link_manifest);

  // No crossorigin attribute was set so credentials shouldn't be used.
  ASSERT_FALSE(link_manifest->FastHasAttribute(html_names::kCrossoriginAttr));
  ASSERT_FALSE(GetManifestManager()->ManifestUseCredentials());

  // Crossorigin set to a random string shouldn't trigger using credentials.
  link_manifest->setAttribute(html_names::kCrossoriginAttr,
                              AtomicString("foobar"));
  ASSERT_FALSE(GetManifestManager()->ManifestUseCredentials());

  // Crossorigin set to 'anonymous' shouldn't trigger using credentials.
  link_manifest->setAttribute(html_names::kCrossoriginAttr,
                              AtomicString("anonymous"));
  ASSERT_FALSE(GetManifestManager()->ManifestUseCredentials());

  // Crossorigin set to 'use-credentials' should trigger using credentials.
  link_manifest->setAttribute(html_names::kCrossoriginAttr,
                              AtomicString("use-credentials"));
  ASSERT_TRUE(GetManifestManager()->ManifestUseCredentials());
}

class OverrideManifestChangeNotifierClient
    : public frame_test_helpers::TestWebFrameClient {
 public:
  void DidCreateDocumentElement() override {
    if (!frame_)
      return;
    notifier_ =
        MakeGarbageCollected<MockManifestChangeNotifier>(*frame_->DomWindow());
    ManifestManager::From(*frame_->DomWindow())
        ->SetManifestChangeNotifierForTest(notifier_);
  }

  void SetFrame(LocalFrame* frame) { frame_ = frame; }
  MockManifestChangeNotifier* GetNotifier() { return notifier_.Get(); }

 private:
  Persistent<LocalFrame> frame_;
  Persistent<MockManifestChangeNotifier> notifier_;
};

TEST_F(ManifestManagerTest, NotifyManifestChange) {
  RegisterMockedURL(base_url_, "link-manifest-change.html");

  OverrideManifestChangeNotifierClient client;
  frame_test_helpers::WebViewHelper web_view_helper;
  web_view_helper.Initialize(&client);

  auto* frame = web_view_helper.GetWebView()->MainFrameImpl();
  client.SetFrame(frame->GetFrame());
  frame_test_helpers::LoadFrame(frame, base_url_ + "link-manifest-change.html");

  EXPECT_EQ(14, client.GetNotifier()->ManifestChangeCount());
}

}  // namespace blink