File: extension_function_registration_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (83 lines) | stat: -rw-r--r-- 3,551 bytes parent folder | download | duplicates (2)
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
// Copyright 2018 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/containers/contains.h"
#include "base/one_shot_event.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/extension_function_registry.h"
#include "extensions/browser/extension_system.h"

namespace extensions {

using ExtensionFunctionRegistrationTest = ExtensionBrowserTest;

// Test that all functions are registered with unique names, histogram values,
// and factories. This is a browser test (rather than a unit test) to (help)
// ensure that all the optional factories and services are indeed instantiated.
IN_PROC_BROWSER_TEST_F(ExtensionFunctionRegistrationTest,
                       CheckForDuplicateEntries) {
  // Verify the ExtensionSystem is ready (and thus all extension functions
  // registered) before checking.
  base::RunLoop run_loop;
  ExtensionSystem::Get(profile())->ready().Post(FROM_HERE,
                                                run_loop.QuitClosure());
  run_loop.Run();

  const ExtensionFunctionRegistry::FactoryMap& factories =
      ExtensionFunctionRegistry::GetInstance().GetFactoriesForTesting();
  // Sanity check: Many, many functions should have been registered.
  EXPECT_GT(factories.size(), 500u);

  std::set<std::string> seen_names;
  std::map<functions::HistogramValue, std::string> seen_histograms;

  // The following are methods that are undocumented and may or may not ship
  // with a final API. We allow them to use the UNKNOWN histogram entry in the
  // meantime.
  // Each entry should have a bug number associated with it.
  static const constexpr char* kAllowedUnknownHistogramEntries[] = {
      // https://crbug.com/1339382.
      "offscreen.hasDocument",
  };

  for (const auto& key_value : factories) {
    const ExtensionFunctionRegistry::FactoryEntry& entry = key_value.second;
    SCOPED_TRACE(entry.function_name_);
    EXPECT_TRUE(seen_names.insert(entry.function_name_).second);
    // NOTE: We explicitly don't check the factory here. On certain platforms
    // with enough compiler optimization, the templated factories are re-used
    // for different functions.
    // EXPECT_TRUE(seen_factories.insert(entry.factory_).second);

    if (entry.histogram_value_ == functions::UNKNOWN) {
      // The chrome.test API uses UNKNOWN; it's only used in tests.
      if (base::StartsWith(entry.function_name_, "test.",
                           base::CompareCase::SENSITIVE)) {
        continue;
      }
      // Some undocumented, unlaunched APIs may also use UNKNOWN if it's unclear
      // (or unlikely) if they will ever launch.
      if (base::Contains(kAllowedUnknownHistogramEntries,
                         std::string(entry.function_name_))) {
        continue;
      }
      ADD_FAILURE() << "Un-allowlisted API found using UNKNOWN histogram entry."
                    << entry.function_name_;
    } else {
      bool is_success =
          seen_histograms.emplace(entry.histogram_value_, entry.function_name_)
              .second;
      if (!is_success) {
        ADD_FAILURE() << "Histogram " << entry.function_name_ << " with value "
                      << entry.histogram_value_
                      << " already exists with another name - "
                      << seen_histograms.find(entry.histogram_value_)->second;
      }
    }
  }
}

}  // namespace extensions