File: price_tracking_email_dialog_view_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (131 lines) | stat: -rw-r--r-- 4,387 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 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/views/commerce/price_tracking_email_dialog_view.h"

#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/views/chrome_test_widget.h"
#include "components/commerce/core/pref_names.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/unique_widget_ptr.h"

class PriceTrackingEmailDialogViewUnitTest : public BrowserWithTestWindowTest {
 public:
  void SetUp() override {
    BrowserWithTestWindowTest::SetUp();

    anchor_widget_ =
        views::UniqueWidgetPtr(std::make_unique<ChromeTestWidget>());
    views::Widget::InitParams widget_params(
        views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET);
    widget_params.context = GetContext();
    anchor_widget_->Init(std::move(widget_params));

    dialog_coordinator_ = std::make_unique<PriceTrackingEmailDialogCoordinator>(
        anchor_widget_->GetContentsView());

    signin::MakePrimaryAccountAvailable(
        IdentityManagerFactory::GetForProfile(profile()), "test@example.com",
        signin::ConsentLevel::kSync);
  }

  void TearDown() override {
    // Make sure the bubble is destroyed before the profile to avoid a crash.
    if (dialog_coordinator_->GetBubble()) {
      views::test::WidgetDestroyedWaiter destroyed_waiter(
          dialog_coordinator_->GetBubble()->GetWidget());
      dialog_coordinator_->GetBubble()->GetWidget()->Close();
      destroyed_waiter.Wait();
    }

    dialog_coordinator_.reset();
    anchor_widget_.reset();

    BrowserWithTestWindowTest::TearDown();
  }

  TestingProfile::TestingFactories GetTestingFactories() override {
    return IdentityTestEnvironmentProfileAdaptor::
        GetIdentityTestEnvironmentFactories();
  }

  void CreateAndShowDialog() {
    dialog_coordinator_->Show(browser()->tab_strip_model()->GetWebContentsAt(0),
                              profile(), base::DoNothing());
  }

  PriceTrackingEmailDialogCoordinator* GetCoordinator() {
    return dialog_coordinator_.get();
  }

  PrefService* GetPrefs() { return profile()->GetPrefs(); }

 private:
  views::UniqueWidgetPtr anchor_widget_;
  std::unique_ptr<PriceTrackingEmailDialogCoordinator> dialog_coordinator_;
};

// Accpting the dialog should result in setting the email preference to "true".
TEST_F(PriceTrackingEmailDialogViewUnitTest, DialogAccepted) {
  CreateAndShowDialog();

  EXPECT_FALSE(
      GetPrefs()->GetBoolean(commerce::kPriceEmailNotificationsEnabled));
  EXPECT_FALSE(
      GetPrefs()->HasPrefPath(commerce::kPriceEmailNotificationsEnabled));

  auto* bubble = GetCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);

  bubble->Accept();

  EXPECT_TRUE(
      GetPrefs()->GetBoolean(commerce::kPriceEmailNotificationsEnabled));
  EXPECT_TRUE(
      GetPrefs()->HasPrefPath(commerce::kPriceEmailNotificationsEnabled));
}

// Rejecting the dialog should result in setting the email preference to
// "false".
TEST_F(PriceTrackingEmailDialogViewUnitTest, DialogRejected) {
  CreateAndShowDialog();

  EXPECT_FALSE(
      GetPrefs()->GetBoolean(commerce::kPriceEmailNotificationsEnabled));
  EXPECT_FALSE(
      GetPrefs()->HasPrefPath(commerce::kPriceEmailNotificationsEnabled));

  auto* bubble = GetCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);

  bubble->Cancel();

  EXPECT_FALSE(
      GetPrefs()->GetBoolean(commerce::kPriceEmailNotificationsEnabled));
  EXPECT_TRUE(
      GetPrefs()->HasPrefPath(commerce::kPriceEmailNotificationsEnabled));
}

// Closing the dialog should result in no state change in preferences.
TEST_F(PriceTrackingEmailDialogViewUnitTest, DialogClosed) {
  CreateAndShowDialog();

  EXPECT_FALSE(
      GetPrefs()->GetBoolean(commerce::kPriceEmailNotificationsEnabled));
  EXPECT_FALSE(
      GetPrefs()->HasPrefPath(commerce::kPriceEmailNotificationsEnabled));

  auto* bubble = GetCoordinator()->GetBubble();
  EXPECT_TRUE(bubble);

  bubble->Close();

  EXPECT_FALSE(
      GetPrefs()->GetBoolean(commerce::kPriceEmailNotificationsEnabled));
  EXPECT_FALSE(
      GetPrefs()->HasPrefPath(commerce::kPriceEmailNotificationsEnabled));
}