summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/chromeos/add_supervision/add_supervision_ui_browsertest.cc
blob: f0995d1b6d788ad0f8e8686f8fcc5652920077dd (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/macros.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/webui/chromeos/add_supervision/add_supervision_metrics_recorder.h"
#include "chrome/browser/ui/webui/chromeos/add_supervision/add_supervision_ui.h"
#include "chrome/browser/ui/webui/chromeos/add_supervision/confirm_signout_dialog.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/network_connection_change_simulator.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

// NOTE: This test is flaky and therefore disabled under MSAN:
// https://crbug.com/1002560
#if !defined(MEMORY_SANITIZER)

namespace {

const char kGetAddSupervisionUIElementJS[] =
    "document.querySelector('add-supervision-ui')";
}

// Base class for AddSupervision tests.
class AddSupervisionBrowserTest : public InProcessBrowserTest {
 public:
  AddSupervisionBrowserTest() {
    scoped_feature_list_.InitWithFeatures(
        {chromeos::features::kParentalControlsSettings}, {});
  }
  ~AddSupervisionBrowserTest() override = default;

  void SetUpOnMainThread() override {
    // TODO(danan):  See if this is possible to do this instead using
    // FakeGaia.IssueOAuthToken().
    identity_test_env_ = std::make_unique<signin::IdentityTestEnvironment>();
    identity_test_env_->MakePrimaryAccountAvailable("example@gmail.com");
    // This makes the identity manager return the string "access_token" for the
    // access token.
    identity_test_env_->SetAutomaticIssueOfAccessTokens(true);
    AddSupervisionUI::SetUpForTest(identity_test_env_->identity_manager());

    // Set start_time_ so that the DCHECK(!start_time_.is_null()) in
    // AddSupervisionMetricsRecorder::RecordUserTime() doesn't throw.
    AddSupervisionMetricsRecorder::GetInstance()
        ->RecordAddSupervisionEnrollment(
            AddSupervisionMetricsRecorder::EnrollmentState::kInitiated);
  }

  chromeos::AddSupervisionUI* GetAddSupervisionUI() {
    return static_cast<chromeos::AddSupervisionUI*>(
        contents()->GetWebUI()->GetController());
  }

  content::WebContents* contents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  GURL settings_webui_url() { return GURL(chrome::kChromeUISettingsURL); }

  GURL add_supervision_webui_url() {
    return GURL(chrome::kChromeUIAddSupervisionURL);
  }

  bool IsElementVisible(const std::string& element_selector) {
    bool found;
    bool hidden;
    std::string script = std::string("domAutomationController.send(") +
                         element_selector + ".hidden);";
    LOG(ERROR) << "Script: " << script;
    found = content::ExecuteScriptAndExtractBool(contents(), script, &hidden);
    return found && !hidden;
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  std::unique_ptr<signin::IdentityTestEnvironment> identity_test_env_;

  DISALLOW_COPY_AND_ASSIGN(AddSupervisionBrowserTest);
};

// Disabled on ASan and LSAn builds, because it's very flaky. See
// crbug.com/1004237
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER)
#define MAYBE_URLParameters DISABLED_URLParameters
#else
#define MAYBE_URLParameters URLParameters
#endif
IN_PROC_BROWSER_TEST_F(AddSupervisionBrowserTest, MAYBE_URLParameters) {
  // Open the Add Supervision URL.
  ui_test_utils::NavigateToURL(browser(), add_supervision_webui_url());
  content::WaitForLoadStop(contents());

  // Get the URL from the embedded webview.
  std::string webview_url;
  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
      contents(),
      std::string("domAutomationController.send(") +
          std::string(kGetAddSupervisionUIElementJS) +
          ".shadowRoot.querySelector('#webview').getAttribute('src')" +
          std::string(");"),
      &webview_url));

  GURL webview_gurl(webview_url);
  ASSERT_TRUE(webview_gurl.has_query());

  // Split the query string into a map of keys to values.
  std::string query_str = webview_gurl.query();
  url::Component query(0, query_str.length());
  url::Component key;
  url::Component value;
  std::map<std::string, std::string> query_parts;
  while (url::ExtractQueryKeyValue(query_str.c_str(), &query, &key, &value)) {
    query_parts[query_str.substr(key.begin, key.len)] =
        query_str.substr(value.begin, value.len);
  }

  // Validate the query parameters.
  ASSERT_EQ(query_parts.at("flow_type"), "1");
  ASSERT_EQ(query_parts.at("platform_version"),
            base::SysInfo::OperatingSystemVersion());
  ASSERT_EQ(query_parts.at("access_token"), "access_token");
  ASSERT_EQ(query_parts.at("hl"), "en-US");
}

IN_PROC_BROWSER_TEST_F(AddSupervisionBrowserTest, ShowOfflineScreen) {
  // Open the Add Supervision URL.
  ui_test_utils::NavigateToURL(browser(), add_supervision_webui_url());
  content::WaitForLoadStop(contents());

  // Webview div should be initially visible.
  ASSERT_TRUE(IsElementVisible(std::string(kGetAddSupervisionUIElementJS) +
                               std::string(".webviewDiv")));

  // Simulate going offline.
  ASSERT_TRUE(content::ExecuteScript(
      contents(), "window.dispatchEvent(new CustomEvent('offline'));"));

  // Ensure the offline content view is shown.
  ASSERT_TRUE(IsElementVisible(std::string(kGetAddSupervisionUIElementJS) +
                               std::string(".offlineContentDiv")));

  // Ensure the online webview content content is hidden.
  ASSERT_FALSE(IsElementVisible(std::string(kGetAddSupervisionUIElementJS) +
                                std::string(".webviewDiv")));

  // Simulate going online.
  ASSERT_TRUE(content::ExecuteScript(
      contents(), "window.dispatchEvent(new CustomEvent('online'));"));

  // Offline div should be hidden.
  ASSERT_FALSE(IsElementVisible(std::string(kGetAddSupervisionUIElementJS) +
                                std::string(".offlineContentDiv")));

  // Webview div should be shown.
  ASSERT_TRUE(IsElementVisible(std::string(kGetAddSupervisionUIElementJS) +
                               std::string(".webviewDiv")));
}

// Disabled on ASan and LSAn builds, because it's very flaky. See
// crbug.com/1004237
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER)
#define MAYBE_ShowConfirmSignoutDialog DISABLED_ShowConfirmSignoutDialog
#else
#define MAYBE_ShowConfirmSignoutDialog ShowConfirmSignoutDialog
#endif
IN_PROC_BROWSER_TEST_F(AddSupervisionBrowserTest,
                       MAYBE_ShowConfirmSignoutDialog) {
  // Open the Add Supervision URL.
  ui_test_utils::NavigateToURL(browser(), add_supervision_webui_url());
  content::WaitForLoadStop(contents());

  // Request that the dialog close before supervision has been enabled.
  ASSERT_TRUE(content::ExecuteScript(
      contents(), std::string(kGetAddSupervisionUIElementJS) +
                      std::string(".server.requestClose()")));
  // Confirm that the signout dialog isn't showing
  ASSERT_FALSE(ConfirmSignoutDialog::IsShowing());

  // Simulate supervision being enabled.
  ASSERT_TRUE(content::ExecuteScript(
      contents(), std::string(kGetAddSupervisionUIElementJS) +
                      std::string(".server.notifySupervisionEnabled()")));

  // Request that the dialog is closed again.
  ASSERT_TRUE(content::ExecuteScript(
      contents(), std::string(kGetAddSupervisionUIElementJS) +
                      std::string(".server.requestClose()")));

  // Confirm that the dialog is showing.
  ASSERT_TRUE(ConfirmSignoutDialog::IsShowing());
}

IN_PROC_BROWSER_TEST_F(AddSupervisionBrowserTest, UMATest) {
  base::HistogramTester histogram_tester;
  base::UserActionTester user_action_tester;

  // Should see 0 Add Supervision enrollment metrics at first.
  histogram_tester.ExpectTotalCount("AddSupervisionDialog.Enrollment", 0);

  // Should see 0 user actions at first.
  EXPECT_EQ(user_action_tester.GetActionCount(
                "AddSupervisionDialog_AttemptedSignoutAfterEnrollment"),
            0);

  // Open the Add Supervision URL.
  ui_test_utils::NavigateToURL(browser(), add_supervision_webui_url());
  content::WaitForLoadStop(contents());

  // Simulate supervision being enabled.
  ASSERT_TRUE(content::ExecuteScript(
      contents(), std::string(kGetAddSupervisionUIElementJS) +
                      std::string(".server.notifySupervisionEnabled()")));

  // Should see 1 Add Supervision process completed.
  histogram_tester.ExpectUniqueSample(
      "AddSupervisionDialog.Enrollment",
      AddSupervisionMetricsRecorder::EnrollmentState::kCompleted, 1);
  histogram_tester.ExpectTotalCount("AddSupervisionDialog.Enrollment", 1);

  // Should see 1 EnrollmentCompleted action.
  EXPECT_EQ(user_action_tester.GetActionCount(
                "AddSupervisionDialog_EnrollmentCompleted"),
            1);
}

#endif  // !defined(MEMORY_SANITIZER)

}  // namespace chromeos