File: kerberos_ticket_expiry_notification_test.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 (89 lines) | stat: -rw-r--r-- 3,055 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
// Copyright 2019 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/ash/kerberos/kerberos_ticket_expiry_notification.h"

#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_display_service_tester.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/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/public/cpp/notification.h"

using message_center::Notification;

namespace ash {

namespace {

constexpr char kUser[] = "user@EXAMPLE.COM";
constexpr char16_t kUser16[] = u"user@EXAMPLE.COM";

constexpr char kNotificationId[] = "kerberos.ticket-expiry-notification";

class KerberosTicketExpiryNotificationTest : public testing::Test {
 public:
  void SetUp() override {
    ASSERT_TRUE(profile_manager_.SetUp());
    profile_ = profile_manager_.CreateTestingProfile("test");
    display_service_tester_ =
        std::make_unique<NotificationDisplayServiceTester>(profile_);
  }

  void TearDown() override { display_service_tester_.reset(); }

  void OnNotificationClick(const std::string& principal_name) {
    notification_click_count_[principal_name]++;
  }

 protected:
  std::optional<Notification> Notification() {
    return display_service_tester_->GetNotification(kNotificationId);
  }

  void Show() {
    kerberos_ticket_expiry_notification::Show(
        profile_, kUser,
        base::BindRepeating(
            &KerberosTicketExpiryNotificationTest::OnNotificationClick,
            base::Unretained(this)));
  }

  content::BrowserTaskEnvironment test_environment_{
      base::test::TaskEnvironment::MainThreadType::UI};
  TestingProfileManager profile_manager_{TestingBrowserProcess::GetGlobal()};
  raw_ptr<TestingProfile> profile_ = nullptr;
  std::unique_ptr<NotificationDisplayServiceTester> display_service_tester_;

  // Counts how many times a notification for a given user was clicked.
  std::map<std::string, int> notification_click_count_;
};

}  // namespace

TEST_F(KerberosTicketExpiryNotificationTest, ShowClose) {
  Show();
  ASSERT_TRUE(Notification().has_value());

  // Don't check the exact text here, just check if the username is there.
  EXPECT_NE(std::string::npos, Notification()->message().find(kUser16));

  kerberos_ticket_expiry_notification::Close(profile_);
  EXPECT_FALSE(Notification().has_value());
}

TEST_F(KerberosTicketExpiryNotificationTest, Click) {
  Show();
  EXPECT_EQ(0, notification_click_count_[kUser]);
  display_service_tester_->SimulateClick(
      NotificationHandler::Type::TRANSIENT, kNotificationId,
      std::nullopt /* action_index */, std::nullopt /* reply */);
  EXPECT_EQ(1, notification_click_count_[kUser]);
}

}  // namespace ash