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

ash / components / arc / net / passpoint_dialog_view_unittest.cc [blame]

// 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 "ash/components/arc/net/passpoint_dialog_view.h"

#include <memory>

#include "ash/components/arc/mojom/net.mojom.h"
#include "ash/components/arc/net/browser_url_opener.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_utils.h"

namespace arc {
namespace {

class TestBrowserUrlOpener : public BrowserUrlOpener {
 public:
  ~TestBrowserUrlOpener() override = default;

  // BrowserUrlOpener:
  void OpenUrl(GURL url) override { url_open_count_ += 1; }

  int url_open_count() { return url_open_count_; }

 private:
  int url_open_count_ = 0;
};

class PasspointDialogViewTest : public views::ViewsTestBase {
 public:
  void SetUp() override {
    views::ViewsTestBase::SetUp();
    browser_delegate_ = std::make_unique<TestBrowserUrlOpener>();

    widget_ =
        CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
    widget_->SetBounds(gfx::Rect(800, 800));

    mojom::PasspointApprovalRequestPtr request =
        mojom::PasspointApprovalRequest::New(
            /*package_name=*/std::string(), /*app_name=*/std::string(),
            /*friendly_name=*/std::string(),
            /*subscription_expiration_time_ms=*/0);
    dialog_view_ =
        widget_->SetContentsView(std::make_unique<PasspointDialogView>(
            std::move(request),
            base::BindOnce(&PasspointDialogViewTest::OnClicked,
                           base::Unretained(this))));
    widget_->Show();
  }

  void TearDown() override {
    widget_->Close();
    widget_.reset();
    views::ViewsTestBase::TearDown();
  }

 protected:
  void LeftClickOnView(const views::Widget* widget,
                       const views::View* view) const {
    ui::test::EventGenerator event_generator(GetRootWindow(widget));
    event_generator.MoveMouseTo(view->GetBoundsInScreen().CenterPoint());
    event_generator.ClickLeftButton();
  }

  void ClickDialogButton(bool allow) {
    PasspointDialogView::TestApi dialog_view_test(dialog_view_);
    auto* target_button = allow ? dialog_view_test.allow_button()
                                : dialog_view_test.dont_allow_button();
    LeftClickOnView(widget_.get(), target_button);
  }

  void ClickLearnMoreLink() {
    PasspointDialogView::TestApi dialog_view_test(dialog_view_);
    auto* learn_more_link =
        dialog_view_test.body_text()->GetFirstLinkForTesting();
    LeftClickOnView(widget_.get(), learn_more_link);
  }

  bool callback_called() { return callback_called_; }
  bool callback_allowed() { return callback_allowed_; }

  int url_open_count() { return browser_delegate_->url_open_count(); }

 private:
  void OnClicked(mojom::PasspointApprovalResponsePtr response) {
    callback_called_ = true;
    callback_allowed_ = response->allowed;
  }

  // For callback checks.
  bool callback_called_{false};
  bool callback_allowed_{false};

  // A LayoutProvider must exist in scope in order to set up views.
  views::LayoutProvider layout_provider;

  // Handles URL open.
  std::unique_ptr<TestBrowserUrlOpener> browser_delegate_;

  raw_ptr<PasspointDialogView, DanglingUntriaged> dialog_view_;
  std::unique_ptr<views::Widget> widget_;
};

TEST_F(PasspointDialogViewTest, ClickAllow) {
  ClickDialogButton(/*allow=*/true);
  EXPECT_TRUE(callback_called());
  EXPECT_TRUE(callback_allowed());
}

TEST_F(PasspointDialogViewTest, ClickDontAllow) {
  ClickDialogButton(/*allow=*/false);
  EXPECT_TRUE(callback_called());
  EXPECT_FALSE(callback_allowed());
}

TEST_F(PasspointDialogViewTest, ClickLearnMore) {
  ClickLearnMoreLink();
  EXPECT_EQ(url_open_count(), 1);
  ClickLearnMoreLink();
  EXPECT_EQ(url_open_count(), 2);
}

}  // namespace
}  // namespace arc