File: cast_contextual_menu_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 (250 lines) | stat: -rw-r--r-- 9,726 bytes parent folder | download | duplicates (5)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2016 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/ui/toolbar/cast/cast_contextual_menu.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "build/branding_buildflags.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/load_error_reporter.h"
#include "chrome/browser/media/router/chrome_media_router_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/ui/media_router/media_router_ui_service.h"
#include "chrome/browser/ui/media_router/media_router_ui_service_factory.h"
#include "chrome/browser/ui/toolbar/cast/cast_toolbar_button_controller.h"
#include "chrome/browser/ui/toolbar/cast/mock_cast_toolbar_button_controller.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "components/media_router/browser/test/mock_media_router.h"
#include "components/media_router/common/pref_names.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

using testing::NiceMock;

namespace {

// These constants are used to inject the state of the Cast toolbar icon
// that would be inferred in the production code.
constexpr bool kShownByPolicy = true;
constexpr bool kShownByUser = false;

bool HasCommandId(ui::MenuModel* menu_model, int command_id) {
  for (size_t i = 0; i < menu_model->GetItemCount(); ++i) {
    if (menu_model->GetCommandIdAt(i) == command_id) {
      return true;
    }
  }
  return false;
}

std::unique_ptr<KeyedService> BuildUIService(content::BrowserContext* context) {
  Profile* profile = Profile::FromBrowserContext(context);
  auto controller =
      std::make_unique<NiceMock<MockCastToolbarButtonController>>(profile);
  return std::make_unique<media_router::MediaRouterUIService>(
      profile, std::move(controller));
}

class MockCastContextualMenuObserver : public CastContextualMenu::Observer {
 public:
  MOCK_METHOD(void, OnContextMenuShown, ());
  MOCK_METHOD(void, OnContextMenuHidden, ());
};

}  // namespace

class CastContextualMenuUnitTest : public BrowserWithTestWindowTest {
 public:
  CastContextualMenuUnitTest() {
    // These tests are replaced by the tests in CastContextualMenuBrowserTest.
    scoped_feature_list_.InitAndDisableFeature(features::kPinnedCastButton);
  }
  CastContextualMenuUnitTest(const CastContextualMenuUnitTest&) = delete;
  CastContextualMenuUnitTest& operator=(const CastContextualMenuUnitTest&) =
      delete;
  ~CastContextualMenuUnitTest() override = default;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();
    extensions::LoadErrorReporter::Init(true);

    identity_test_env_adaptor_ =
        std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile());

    // Pin the Cast icon to the toolbar.
    CastToolbarButtonController::SetAlwaysShowActionPref(profile(), true);

    media_router::MediaRouterUIServiceFactory::GetInstance()->SetTestingFactory(
        profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true),
        base::BindRepeating(&BuildUIService));
  }

  void TearDown() override {
    // |identity_test_env_adaptor_| must be destroyed before the TestingProfile,
    // which occurs in BrowserWithTestWindowTest::TearDown().
    identity_test_env_adaptor_.reset();
    BrowserWithTestWindowTest::TearDown();
  }

  TestingProfile::TestingFactories GetTestingFactories() override {
    return IdentityTestEnvironmentProfileAdaptor::
        GetIdentityTestEnvironmentFactoriesWithAppendedFactories(
            {TestingProfile::TestingFactory{
                 media_router::ChromeMediaRouterFactory::GetInstance(),
                 base::BindRepeating(&media_router::MockMediaRouter::Create)},
             TestingProfile::TestingFactory{
                 media_router::MediaRouterUIServiceFactory::GetInstance(),
                 base::BindRepeating(&BuildUIService)}});
  }

 protected:
  signin::IdentityTestEnvironment* identity_test_env() {
    DCHECK(identity_test_env_adaptor_);
    return identity_test_env_adaptor_->identity_test_env();
  }

  std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
      identity_test_env_adaptor_;

  raw_ptr<ToolbarActionsModel> toolbar_actions_model_ = nullptr;
  MockCastContextualMenuObserver observer_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Tests the basic state of the contextual menu.
TEST_F(CastContextualMenuUnitTest, Basic) {
  // About
  // -----
  // Learn more
  // Help
  // Always show icon (checkbox)
  // Optimize fullscreen videos (checkbox)
  // -----
  // Report an issue

  // Number of menu items, including separators.
  size_t expected_number_items = 6;
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  expected_number_items += 2;
#endif

  CastContextualMenu menu(browser(), kShownByUser, &observer_);
  std::unique_ptr<ui::SimpleMenuModel> model = menu.CreateMenuModel();
  EXPECT_EQ(model->GetItemCount(), expected_number_items);

  for (size_t i = 0; i < expected_number_items; ++i) {
    EXPECT_TRUE(model->IsEnabledAt(i));
    EXPECT_TRUE(model->IsVisibleAt(i));
  }

  // Set up an authenticated account.
  identity_test_env()->SetPrimaryAccount("foo@bar.com",
                                         signin::ConsentLevel::kSync);

  // Run the same checks as before. All existing menu items should be now
  // enabled and visible.
  EXPECT_EQ(model->GetItemCount(), expected_number_items);
  for (size_t i = 0; i < expected_number_items; ++i) {
    EXPECT_TRUE(model->IsEnabledAt(i));
    EXPECT_TRUE(model->IsVisibleAt(i));
  }
}

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// "Report an issue" should be present for normal profiles as well as for
// incognito.
TEST_F(CastContextualMenuUnitTest, EnableAndDisableReportIssue) {
  CastContextualMenu menu(browser(), kShownByPolicy, &observer_);
  EXPECT_TRUE(
      menu.CreateMenuModel()
          ->GetIndexOfCommandId(IDC_MEDIA_TOOLBAR_CONTEXT_REPORT_CAST_ISSUE)
          .has_value());

  std::unique_ptr<BrowserWindow> window(CreateBrowserWindow());
  std::unique_ptr<Browser> incognito_browser(
      CreateBrowser(profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true),
                    Browser::TYPE_NORMAL, false, window.get()));

  CastContextualMenu incognito_menu(incognito_browser.get(), kShownByPolicy,
                                    &observer_);
  EXPECT_TRUE(
      incognito_menu.CreateMenuModel()
          ->GetIndexOfCommandId(IDC_MEDIA_TOOLBAR_CONTEXT_REPORT_CAST_ISSUE)
          .has_value());
}
#endif

TEST_F(CastContextualMenuUnitTest, ToggleMediaRemotingItem) {
  CastContextualMenu menu(browser(), kShownByPolicy, &observer_);

  PrefService* pref_service = browser()->profile()->GetPrefs();
  pref_service->SetBoolean(
      media_router::prefs::kMediaRouterMediaRemotingEnabled, false);
  EXPECT_FALSE(menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_TOGGLE_MEDIA_REMOTING));

  menu.ExecuteCommand(IDC_MEDIA_ROUTER_TOGGLE_MEDIA_REMOTING, 0);
  EXPECT_TRUE(menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_TOGGLE_MEDIA_REMOTING));
  EXPECT_TRUE(pref_service->GetBoolean(
      media_router::prefs::kMediaRouterMediaRemotingEnabled));

  menu.ExecuteCommand(IDC_MEDIA_ROUTER_TOGGLE_MEDIA_REMOTING, 0);
  EXPECT_FALSE(menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_TOGGLE_MEDIA_REMOTING));
  EXPECT_FALSE(pref_service->GetBoolean(
      media_router::prefs::kMediaRouterMediaRemotingEnabled));
}

TEST_F(CastContextualMenuUnitTest, ToggleAlwaysShowIconItem) {
  CastContextualMenu menu(browser(), kShownByUser, &observer_);

  // Whether the option is checked should reflect the pref.
  CastToolbarButtonController::SetAlwaysShowActionPref(profile(), true);
  EXPECT_TRUE(
      menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION));

  CastToolbarButtonController::SetAlwaysShowActionPref(profile(), false);
  EXPECT_FALSE(
      menu.IsCommandIdChecked(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION));

  // Executing the option should toggle the pref.
  menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0);
  EXPECT_TRUE(CastToolbarButtonController::GetAlwaysShowActionPref(profile()));

  menu.ExecuteCommand(IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION, 0);
  EXPECT_FALSE(CastToolbarButtonController::GetAlwaysShowActionPref(profile()));
}

TEST_F(CastContextualMenuUnitTest, ActionShownByPolicy) {
  // Create a contextual menu for an icon shown by administrator policy.
  CastContextualMenu menu(browser(), kShownByPolicy, &observer_);

  // The item "Added by your administrator" should be shown disabled.
  EXPECT_TRUE(menu.IsCommandIdVisible(IDC_MEDIA_ROUTER_SHOWN_BY_POLICY));
  EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_MEDIA_ROUTER_SHOWN_BY_POLICY));

  // The checkbox item "Always show icon" should not be shown.
  EXPECT_FALSE(HasCommandId(menu.CreateMenuModel().get(),
                            IDC_MEDIA_ROUTER_ALWAYS_SHOW_TOOLBAR_ACTION));
}

TEST_F(CastContextualMenuUnitTest, NotifyActionController) {
  EXPECT_CALL(observer_, OnContextMenuShown());
  auto menu =
      std::make_unique<CastContextualMenu>(browser(), kShownByUser, &observer_);
  std::unique_ptr<ui::SimpleMenuModel> model = menu->CreateMenuModel();
  menu->OnMenuWillShow(model.get());

  EXPECT_CALL(observer_, OnContextMenuHidden());
  menu->MenuClosed(model.get());
  menu.reset();
}