File: diagnostics_app_integration_browsertest.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 (234 lines) | stat: -rw-r--r-- 9,297 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
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/webui/diagnostics_ui/url_constants.h"
#include "ash/webui/system_apps/public/system_web_app_type.h"
#include "ash/wm/window_pin_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/run_until.h"
#include "chrome/browser/ash/system_web_apps/test_support/system_web_app_integration_test.h"
#include "chrome/browser/lifetime/application_lifetime_desktop.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/webui/ash/diagnostics_dialog/diagnostics_dialog.h"
#include "chrome/browser/ui/webui/ash/system_web_dialog/system_web_dialog_delegate.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"

namespace {
// Same as apps::DefaultAppName::kDiagnosticsApp enum.
const size_t kDiagnosticsApp = 42;
const char kDiagnosticsUmaFeatureFullPath[] =
    "ChromeOS.FeatureUsage.DiagnosticsUi";
const char kDiagnosticsUmaFeatureUsetimeFullPath[] =
    "ChromeOS.FeatureUsage.DiagnosticsUi.Usetime";
const char kDiagnosticsUmaOpenDurationFullPath[] =
    "ChromeOS.DiagnosticsUi.OpenDuration";
const char kFromChromeLaunch[] = "Apps.DefaultAppLaunch.FromChromeInternal";
const char kFindDiagnosticsAppScript[] =
    R"(document.querySelectorAll('diagnostics-app').length === 1)";
// Same as feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess enum.
const size_t kUsedWithSuccess = 2;
}  // namespace

class DiagnosticsAppIntegrationTest : public ash::SystemWebAppIntegrationTest {
 public:
  DiagnosticsAppIntegrationTest() = default;

  static bool IsDiagnosticsDialogVisible() {
    return ash::SystemWebDialogDelegate::HasInstance(
        GURL("chrome://diagnostics"));
  }

 protected:
  base::HistogramTester histogram_tester_;

  content::WebContents* LaunchDiagnosticsApp(
      const std::string override_url = "") {
    WaitForTestSystemAppInstall();
    auto params = GetAppLaunchParams(override_url);

    return LaunchApp(std::move(params));
  }

  apps::AppLaunchParams GetAppLaunchParams(
      const std::string override_url = "") {
    auto params = LaunchParamsForApp(ash::SystemWebAppType::DIAGNOSTICS);

    // Override starting URL when provided.
    if (override_url != "") {
      params.override_url = GURL(override_url);
    }

    return params;
  }
};

// Test that the Diagnostics App installs and launches correctly by running some
// spot checks on the manifest.
IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppInLauncher) {
  const GURL url(ash::kChromeUIDiagnosticsAppUrl);
  EXPECT_NO_FATAL_FAILURE(ExpectSystemWebAppValid(
      ash::SystemWebAppType::DIAGNOSTICS, url, "Diagnostics"));
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest, LaunchMetricsTest) {
  WaitForTestSystemAppInstall();

  const GURL url(ash::kChromeUIDiagnosticsAppUrl);
  content::TestNavigationObserver observer(url);
  observer.StartWatchingNewWebContents();
  ash::LaunchSystemWebAppAsync(profile(), ash::SystemWebAppType::DIAGNOSTICS);
  observer.Wait();

  histogram_tester_.ExpectUniqueSample(kFromChromeLaunch, kDiagnosticsApp, 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest, UsageMetricsTest) {
  WaitForTestSystemAppInstall();

  Browser* system_app_browser;
  // Launch app and allow UI to load.
  LaunchApp(ash::SystemWebAppType::DIAGNOSTICS, &system_app_browser);

  // Find system browser for diagnostics and close it to trigger usage metrics.
  EXPECT_TRUE(ash::IsSystemWebApp(system_app_browser));
  chrome::CloseWindow(system_app_browser);
  ui_test_utils::WaitForBrowserToClose();

  histogram_tester_.ExpectBucketCount(kDiagnosticsUmaFeatureFullPath,
                                      kUsedWithSuccess, 1);
  histogram_tester_.ExpectTotalCount(kDiagnosticsUmaFeatureUsetimeFullPath, 1);
  histogram_tester_.ExpectTotalCount(kDiagnosticsUmaOpenDurationFullPath, 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppRecordsInitialScreen) {
  LaunchDiagnosticsApp();

  // One maps to the "CrosDiagnosticsNavigationView" system enum.
  histogram_tester_.ExpectUniqueSample("ChromeOS.DiagnosticsUi.InitialScreen",
                                       0, 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppRecordsInitialScreenSystem) {
  // Launch Diagnostics at System UI.
  LaunchDiagnosticsApp("chrome://diagnostics/?system");

  // One maps to the "CrosDiagnosticsNavigationView" system enum.
  histogram_tester_.ExpectUniqueSample("ChromeOS.DiagnosticsUi.InitialScreen",
                                       0, 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppRecordsInitialScreenConnectivity) {
  // Launch Diagnostics at Connectivity UI.
  LaunchDiagnosticsApp("chrome://diagnostics/?connectivity");

  // One maps to the "CrosDiagnosticsNavigationView" connectivity enum.
  histogram_tester_.ExpectUniqueSample("ChromeOS.DiagnosticsUi.InitialScreen",
                                       1, 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppRecordsInitialScreenInput) {
  // Launch Diagnostics at Input UI.
  LaunchDiagnosticsApp("chrome://diagnostics/?input");

  // One maps to the "CrosDiagnosticsNavigationView" input enum.
  histogram_tester_.ExpectUniqueSample("ChromeOS.DiagnosticsUi.InitialScreen",
                                       2, 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppRecordsScreenOpenDuration) {
  content::WebContents* web_contents = LaunchDiagnosticsApp();

  histogram_tester_.ExpectUniqueSample("ChromeOS.DiagnosticsUi.InitialScreen",
                                       0, 1);

  EXPECT_TRUE(content::ExecJs(web_contents,
                              "chrome.send('recordNavigation', [0, 1]);"));

  chrome::CloseAllBrowsers();

  histogram_tester_.ExpectTotalCount(
      "ChromeOS.DiagnosticsUi.System.OpenDuration", 1);
  histogram_tester_.ExpectTotalCount(
      "ChromeOS.DiagnosticsUi.Connectivity.OpenDuration", 1);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppIgnoresInvalidRecordNavigationCall) {
  content::WebContents* web_contents = LaunchDiagnosticsApp();

  histogram_tester_.ExpectUniqueSample("ChromeOS.DiagnosticsUi.InitialScreen",
                                       0, 1);

  // Simulate sending invalid navigation view value.
  EXPECT_TRUE(content::ExecJs(
      web_contents, "chrome.send('recordNavigation', [1000, -550]);"));
  EXPECT_TRUE(content::ExecJs(
      web_contents, "chrome.send('recordNavigation', ['1000', '-550']);"));
  EXPECT_TRUE(
      content::ExecJs(web_contents, "chrome.send('recordNavigation');"));
  EXPECT_TRUE(
      content::ExecJs(web_contents, "chrome.send('recordNavigation', []);"));

  chrome::CloseAllBrowsers();

  histogram_tester_.ExpectTotalCount(
      "ChromeOS.DiagnosticsUi.System.OpenDuration", 1);
  histogram_tester_.ExpectTotalCount(
      "ChromeOS.DiagnosticsUi.Connectivity.OpenDuration", 0);
  histogram_tester_.ExpectTotalCount(
      "ChromeOS.DiagnosticsUi.Input.OpenDuration", 0);
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsAppCapturesNavigation) {
  auto* app_web_contents = LaunchDiagnosticsApp();

  const auto* app_browser = ash::FindSystemWebAppBrowser(
      profile(), ash::SystemWebAppType::DIAGNOSTICS);
  EXPECT_TRUE(app_browser);
  // DiagnosticsApp launched in its own browser.
  EXPECT_NE(browser(), app_browser);

  // Attempting to navigate to app URL returns to the existing window with
  // DiagnosticsSystemAppDelegate::ShouldCaptureNavigations() set to true.
  const GURL url(ash::kChromeUIDiagnosticsAppUrl);
  ui_test_utils::NavigateToURLWithDispositionBlockUntilNavigationsComplete(
      browser(), url, /*number_of_navigations=*/2,
      WindowOpenDisposition::CURRENT_TAB, /*browser_test_flags=*/0);

  auto* browser_web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  EXPECT_FALSE(content::EvalJs(browser_web_contents, kFindDiagnosticsAppScript)
                   .ExtractBool());
  EXPECT_TRUE(content::EvalJs(app_web_contents, kFindDiagnosticsAppScript)
                  .ExtractBool());
}

IN_PROC_BROWSER_TEST_P(DiagnosticsAppIntegrationTest,
                       DiagnosticsDialogClosesForLockedFullscreen) {
  ash::DiagnosticsDialog::ShowDialog();
  EXPECT_TRUE(IsDiagnosticsDialogVisible());

  // Enter locked fullscreen.
  PinWindow(browser()->window()->GetNativeWindow(), /*trusted=*/true);
  EXPECT_TRUE(
      base::test::RunUntil([&]() { return !IsDiagnosticsDialogVisible(); }));
}

INSTANTIATE_SYSTEM_WEB_APP_MANAGER_TEST_SUITE_REGULAR_PROFILE_P(
    DiagnosticsAppIntegrationTest);