File: extension_manifests_options_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 (150 lines) | stat: -rw-r--r-- 6,644 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/stringprintf.h"
#include "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/options_page_info.h"
#include "testing/gtest/include/gtest/gtest.h"

using extensions::FeatureSwitch;
using extensions::OptionsPageInfo;

namespace {

class OptionsPageManifestTest : public ChromeManifestTest {
 protected:
  // Tests how the options_ui manifest key affects the open-in-tab and
  // chromes-style behaviour.
  testing::AssertionResult TestOptionsUIChromeStyleAndOpenInTab() {
    // Explicitly specifying true in the manifest for options_ui.chrome_style
    // and options_ui.open_in_tab sets them both to true.
    scoped_refptr<extensions::Extension> extension =
        LoadAndExpectSuccess("options_ui_flags_true.json");
    EXPECT_TRUE(OptionsPageInfo::ShouldUseChromeStyle(extension.get()));
    EXPECT_TRUE(OptionsPageInfo::ShouldOpenInTab(extension.get()));

    // Explicitly specifying false in the manifest for options_ui.chrome_style
    // and options_ui.open_in_tab sets them both to false.
    extension = LoadAndExpectSuccess("options_ui_flags_false.json");
    EXPECT_FALSE(OptionsPageInfo::ShouldUseChromeStyle(extension.get()));
    EXPECT_FALSE(OptionsPageInfo::ShouldOpenInTab(extension.get()));

    // Specifying an options_ui key but neither options_ui.chrome_style nor
    // options_ui.open_in_tab uses the default values: false for open-in-tab,
    // false for use-chrome-style.
    extension = LoadAndExpectSuccess("options_ui_page_basic.json");
    EXPECT_FALSE(OptionsPageInfo::ShouldUseChromeStyle(extension.get()));
    EXPECT_FALSE(OptionsPageInfo::ShouldOpenInTab(extension.get()));

    // This extension has both options_page and options_ui specified. The
    // options_ui key should take precedence.
    extension = LoadAndExpectSuccess("options_ui_page_with_legacy_page.json");
    EXPECT_FALSE(OptionsPageInfo::ShouldUseChromeStyle(extension.get()));
    EXPECT_FALSE(OptionsPageInfo::ShouldOpenInTab(extension.get()));

    return testing::AssertionSuccess();
  }

  // Tests how the options_page manifest key affects the open-in-tab and
  // chromes-style behaviour.
  testing::AssertionResult TestOptionsPageChromeStyleAndOpenInTab(
      bool expect_open_in_tab) {
    scoped_refptr<extensions::Extension> extension =
        LoadAndExpectSuccess("init_valid_options.json");
    EXPECT_FALSE(OptionsPageInfo::ShouldUseChromeStyle(extension.get()));
    if (expect_open_in_tab) {
      EXPECT_TRUE(OptionsPageInfo::ShouldOpenInTab(extension.get()));
    } else {
      EXPECT_FALSE(OptionsPageInfo::ShouldOpenInTab(extension.get()));
    }
    return testing::AssertionSuccess();
  }
};

TEST_F(OptionsPageManifestTest, OptionsPageInApps) {
  // Allow options page with absolute URL in hosted apps.
  scoped_refptr<extensions::Extension> extension =
      LoadAndExpectSuccess("hosted_app_absolute_options.json");
  EXPECT_EQ("http://example.com/options.html",
            OptionsPageInfo::GetOptionsPage(extension.get()).spec());

  extension = LoadAndExpectSuccess("platform_app_with_options_page.json");
  EXPECT_TRUE(!OptionsPageInfo::HasOptionsPage(extension.get()));

  const Testcase testcases[] = {
      // Forbid options page with relative URL in hosted apps.
      Testcase("hosted_app_relative_options.json",
               extensions::manifest_errors::kInvalidOptionsPageInHostedApp),

      // Forbid options page with non-(http|https) scheme in hosted app.
      Testcase("hosted_app_file_options.json",
               extensions::manifest_errors::kInvalidOptionsPageInHostedApp),

      // Forbid absolute URL for options page in packaged apps.
      Testcase("packaged_app_absolute_options.json",
               extensions::manifest_errors::kInvalidOptionsPage)};
  RunTestcases(testcases, EXPECT_TYPE_ERROR);
}

// Tests for the options_ui.page manifest field.
TEST_F(OptionsPageManifestTest, OptionsUIPage) {
  FeatureSwitch::ScopedOverride enable_flag(
      FeatureSwitch::embedded_extension_options(), true);

  scoped_refptr<extensions::Extension> extension =
      LoadAndExpectSuccess("options_ui_page_basic.json");
  EXPECT_EQ(base::StringPrintf("chrome-extension://%s/options.html",
                               extension->id().c_str()),
            OptionsPageInfo::GetOptionsPage(extension.get()).spec());

  extension = LoadAndExpectSuccess("options_ui_page_with_legacy_page.json");
  EXPECT_EQ(base::StringPrintf("chrome-extension://%s/newoptions.html",
                               extension->id().c_str()),
            OptionsPageInfo::GetOptionsPage(extension.get()).spec());

  RunTestcase(Testcase("options_ui_page_bad_url.json",
                       "'page': expected page, got null"),
              EXPECT_TYPE_WARNING);
}

// Runs TestOptionsUIChromeStyleAndOpenInTab with and without the
// embedded-extension-options flag. The results should always be the same.
TEST_F(OptionsPageManifestTest, OptionsUIChromeStyleAndOpenInTab) {
  ASSERT_FALSE(FeatureSwitch::embedded_extension_options()->IsEnabled());
  EXPECT_TRUE(TestOptionsUIChromeStyleAndOpenInTab());
  {
    FeatureSwitch::ScopedOverride enable_flag(
        FeatureSwitch::embedded_extension_options(), true);
    EXPECT_TRUE(TestOptionsUIChromeStyleAndOpenInTab());
  }
}

// Runs TestOptionsPageChromeStyleAndOpenInTab with and without the
// embedded-extension-options flag. The default value of open-in-tab differs
// depending on the flag's value.
TEST_F(OptionsPageManifestTest, OptionsPageChromeStyleAndOpenInTab) {
  ASSERT_FALSE(FeatureSwitch::embedded_extension_options()->IsEnabled());
  EXPECT_TRUE(TestOptionsPageChromeStyleAndOpenInTab(true));
  {
    FeatureSwitch::ScopedOverride enable_flag(
        FeatureSwitch::embedded_extension_options(), true);
    EXPECT_TRUE(TestOptionsPageChromeStyleAndOpenInTab(false));
  }
}

// Tests that the chrome_style flag is not supported in manifest v3 and will
// trigger an error when specified.
TEST_F(OptionsPageManifestTest, OptionsPageChromeStyleManifestV3) {
  LoadAndExpectError(
      "options_ui_chrome_style_manifest_v3_false.json",
      extensions::manifest_errors::kChromeStyleInvalidForManifestV3);
  LoadAndExpectError(
      "options_ui_chrome_style_manifest_v3_true.json",
      extensions::manifest_errors::kChromeStyleInvalidForManifestV3);
}

}  // namespace