File: event_router_forwarder_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 (155 lines) | stat: -rw-r--r-- 5,922 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/extensions/event_router_forwarder.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/thread_test_helper.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/common/extension_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace extensions {

namespace {

const events::HistogramValue kHistogramValue = events::FOR_TEST;
const char kEventName[] = "event_name";

class MockEventRouterForwarder : public EventRouterForwarder {
 public:
  MOCK_METHOD3(CallEventRouter,
               void(Profile*, events::HistogramValue, const std::string&));

  void CallEventRouter(Profile* profile,
                       events::HistogramValue histogram_value,
                       const std::string& event_name,
                       base::Value::List args) override {
    CallEventRouter(profile, histogram_value, event_name);
  }

 protected:
  ~MockEventRouterForwarder() override = default;
};

static void BroadcastEventToRenderers(
    EventRouterForwarder* event_router,
    events::HistogramValue histogram_value,
    const std::string& event_name,
    bool dispatch_to_off_the_record_profiles) {
  event_router->BroadcastEventToRenderers(histogram_value, event_name,
                                          base::Value::List(),
                                          dispatch_to_off_the_record_profiles);
}

}  // namespace

class EventRouterForwarderTest : public testing::Test {
 protected:
  EventRouterForwarderTest()
      : task_environment_(content::BrowserTaskEnvironment::REAL_IO_THREAD),
        profile_manager_(TestingBrowserProcess::GetGlobal()) {
  }

  void SetUp() override {
    ASSERT_TRUE(profile_manager_.SetUp());

    // Inject a BrowserProcess with a ProfileManager.
    profile1_ = profile_manager_.CreateTestingProfile("one");
    profile2_ = profile_manager_.CreateTestingProfile("two");
  }

  content::BrowserTaskEnvironment task_environment_;
  TestingProfileManager profile_manager_;
  // Profiles are weak pointers, owned by ProfileManager in |browser_process_|.
  raw_ptr<TestingProfile> profile1_;
  raw_ptr<TestingProfile> profile2_;
};

TEST_F(EventRouterForwarderTest, BroadcastRendererUI) {
  scoped_refptr<MockEventRouterForwarder> event_router(
      new MockEventRouterForwarder);
  GURL url;
  EXPECT_CALL(*event_router,
              CallEventRouter(profile1_.get(), kHistogramValue, kEventName));
  EXPECT_CALL(*event_router,
              CallEventRouter(profile2_.get(), kHistogramValue, kEventName));
  BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName,
                            false);
}

TEST_F(EventRouterForwarderTest, BroadcastRendererUIIncognito) {
  scoped_refptr<MockEventRouterForwarder> event_router(
      new MockEventRouterForwarder);
  using ::testing::_;
  GURL url;
  Profile* incognito =
      profile1_->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  EXPECT_CALL(*event_router,
              CallEventRouter(profile1_.get(), kHistogramValue, kEventName));
  EXPECT_CALL(*event_router, CallEventRouter(incognito, _, _)).Times(0);
  EXPECT_CALL(*event_router,
              CallEventRouter(profile2_.get(), kHistogramValue, kEventName));
  BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName,
                            false);
}

TEST_F(EventRouterForwarderTest,
       BroadcastRendererUIIncognitoWithDispatchToOffTheRecordProfiles) {
  scoped_refptr<MockEventRouterForwarder> event_router(
      new MockEventRouterForwarder);
  using ::testing::_;
  GURL url;
  Profile* incognito1 =
      profile1_->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  Profile* incognito2 =
      profile2_->GetPrimaryOTRProfile(/*create_if_needed=*/true);
  EXPECT_CALL(*event_router,
              CallEventRouter(profile1_.get(), kHistogramValue, kEventName));
  EXPECT_CALL(*event_router, CallEventRouter(incognito1, _, _));
  EXPECT_CALL(*event_router,
              CallEventRouter(profile2_.get(), kHistogramValue, kEventName));
  EXPECT_CALL(*event_router, CallEventRouter(incognito2, _, _));
  BroadcastEventToRenderers(event_router.get(), kHistogramValue, kEventName,
                            true);
}

// This is the canonical test for passing control flow from the IO thread
// to the UI thread. Repeating this for all public functions of
// EventRouterForwarder would not increase coverage.
TEST_F(EventRouterForwarderTest, BroadcastRendererIO) {
  scoped_refptr<MockEventRouterForwarder> event_router(
      new MockEventRouterForwarder);
  GURL url;
  EXPECT_CALL(*event_router,
              CallEventRouter(profile1_.get(), kHistogramValue, kEventName));
  EXPECT_CALL(*event_router,
              CallEventRouter(profile2_.get(), kHistogramValue, kEventName));
  content::GetIOThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(&BroadcastEventToRenderers,
                                base::Unretained(event_router.get()),
                                kHistogramValue, kEventName, false));

  // Wait for IO thread's message loop to be processed
  scoped_refptr<base::ThreadTestHelper> helper(
      new base::ThreadTestHelper(content::GetIOThreadTaskRunner({}).get()));
  ASSERT_TRUE(helper->Run());

  base::RunLoop().RunUntilIdle();
}

}  // namespace extensions