File: internet_handler_unittest.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 (120 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download | duplicates (9)
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
// Copyright 2017 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/webui/ash/settings/pages/internet/internet_handler.h"

#include <memory>

#include "chrome/test/base/browser_with_test_window_test.h"
#include "chromeos/ash/components/tether/fake_gms_core_notifications_state_tracker.h"
#include "content/public/test/test_web_ui.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::settings {

namespace {

const char kWebCallbackFunctionName[] = "cr.webUIListenerCallback";
const char kSendDeviceNamesMessageType[] =
    "sendGmsCoreNotificationsDisabledDeviceNames";

class TestInternetHandler : public InternetHandler {
 public:
  // Pull WebUIMessageHandler::set_web_ui() into public so SetUp() can call it.
  using InternetHandler::set_web_ui;

  explicit TestInternetHandler(TestingProfile* profile)
      : InternetHandler(profile) {}
};

}  // namespace

class InternetHandlerTest : public BrowserWithTestWindowTest {
 public:
  InternetHandlerTest(const InternetHandlerTest&) = delete;
  InternetHandlerTest& operator=(const InternetHandlerTest&) = delete;

 protected:
  InternetHandlerTest() = default;

  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    web_ui_ = std::make_unique<content::TestWebUI>();

    handler_ = std::make_unique<TestInternetHandler>(profile());
    handler_->set_web_ui(web_ui_.get());
    handler_->RegisterMessages();
    handler_->AllowJavascriptForTesting();

    fake_tracker_ =
        std::make_unique<tether::FakeGmsCoreNotificationsStateTracker>();
    handler_->SetGmsCoreNotificationsStateTrackerForTesting(
        fake_tracker_.get());
  }

  void RequestGmsCoreNotificationsDisabledDeviceNames() {
    handler_->RequestGmsCoreNotificationsDisabledDeviceNames(
        base::Value::List());
  }

  void VerifyMostRecentDeviceNamesSent(
      const std::vector<std::string>& expected_device_names,
      size_t expected_num_updates) {
    EXPECT_EQ(expected_num_updates, web_ui_->call_data().size());

    const content::TestWebUI::CallData* last_call_data =
        web_ui_->call_data()[expected_num_updates - 1].get();
    EXPECT_TRUE(last_call_data);

    // The call is structured such that the function name is the "web callback"
    // name and the first argument is the name of the message being sent.
    EXPECT_EQ(kWebCallbackFunctionName, last_call_data->function_name());
    EXPECT_EQ(kSendDeviceNamesMessageType, last_call_data->arg1()->GetString());

    std::vector<std::string> actual_device_names;
    for (const auto& device_name_value : last_call_data->arg2()->GetList()) {
      actual_device_names.push_back(device_name_value.GetString());
    }
    EXPECT_EQ(expected_device_names, actual_device_names);
  }

  std::unique_ptr<content::TestWebUI> web_ui_;
  std::unique_ptr<tether::FakeGmsCoreNotificationsStateTracker> fake_tracker_;
  std::unique_ptr<TestInternetHandler> handler_;
};

TEST_F(InternetHandlerTest, TestSendsDeviceNames) {
  RequestGmsCoreNotificationsDisabledDeviceNames();
  VerifyMostRecentDeviceNamesSent({} /* expected_device_names */,
                                  1u /* expected_num_updates */);

  // Set two unique names.
  fake_tracker_->set_device_names({"device1", "device2"});
  fake_tracker_->NotifyGmsCoreNotificationStateChanged();
  VerifyMostRecentDeviceNamesSent(
      {"device1", "device2"} /* expected_device_names */,
      2u /* expected_num_updates */);

  // Set three names, two of them identical. Devices with the same name should
  // be supported, since it is possible for a user to have two phones of the
  // same model.
  fake_tracker_->set_device_names({"device1", "device1", "device3"});
  fake_tracker_->NotifyGmsCoreNotificationStateChanged();
  VerifyMostRecentDeviceNamesSent(
      {"device1", "device1", "device3"} /* expected_device_names */,
      3u /* expected_num_updates */);
}

TEST_F(InternetHandlerTest, TestSendsDeviceNames_StartsWithDevices) {
  // Start with two devices before the handler requests any names.
  fake_tracker_->set_device_names({"device1", "device2"});

  RequestGmsCoreNotificationsDisabledDeviceNames();
  VerifyMostRecentDeviceNamesSent(
      {"device1", "device2"} /* expected_device_names */,
      1u /* expected_num_updates */);
}

}  // namespace ash::settings