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
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270

ash / shelf / shelf_button_pressed_metric_tracker_unittest.cc [blame]

// Copyright 2015 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/shelf/shelf_button_pressed_metric_tracker.h"

#include <utility>

#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_button_pressed_metric_tracker_test_api.h"
#include "ash/shelf/shelf_view_test_api.h"
#include "ash/test/ash_test_base.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/simple_test_tick_clock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event.h"
#include "ui/events/test/test_event.h"
#include "ui/views/controls/button/button.h"

namespace ash {
namespace {

// A simple light weight test double dummy for a views::Button.
class DummyButton : public views::Button {
 public:
  DummyButton();

  DummyButton(const DummyButton&) = delete;
  DummyButton& operator=(const DummyButton&) = delete;
};

DummyButton::DummyButton() : views::Button(views::Button::PressedCallback()) {}

// Test fixture for the ShelfButtonPressedMetricTracker class. Relies on
// AshTestBase to initilize the UserMetricsRecorder and it's dependencies.
class ShelfButtonPressedMetricTrackerTest : public AshTestBase {
 public:
  static const char*
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName;

  ShelfButtonPressedMetricTrackerTest();

  ShelfButtonPressedMetricTrackerTest(
      const ShelfButtonPressedMetricTrackerTest&) = delete;
  ShelfButtonPressedMetricTrackerTest& operator=(
      const ShelfButtonPressedMetricTrackerTest&) = delete;

  ~ShelfButtonPressedMetricTrackerTest() override;

  // AshTestBase:
  void SetUp() override;
  void TearDown() override;

  // Calls ButtonPressed on the test target with the given |event|
  // and dummy values for the |sender| and |performed_action| parameters.
  void ButtonPressed(const ui::Event& event);

  // Calls ButtonPressed on the test target with the given |performed_action|
  // and dummy values for the |event| and |sender| parameters.
  void ButtonPressed(ShelfAction performed_action);

  // Calls ButtonPressed on the test target with the given |sender| and
  // |performed_action| and a dummy value for the |event| parameter.
  void ButtonPressed(const views::Button* sender, ShelfAction performed_action);

 protected:
  // The test target. Not owned.
  raw_ptr<ShelfButtonPressedMetricTracker, DanglingUntriaged> metric_tracker_;

  // The TickClock injected in to the test target.
  base::SimpleTestTickClock tick_clock_;
};

const char* ShelfButtonPressedMetricTrackerTest::
    kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName =
        ShelfButtonPressedMetricTracker::
            kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName;

ShelfButtonPressedMetricTrackerTest::ShelfButtonPressedMetricTrackerTest() =
    default;

ShelfButtonPressedMetricTrackerTest::~ShelfButtonPressedMetricTrackerTest() =
    default;

void ShelfButtonPressedMetricTrackerTest::SetUp() {
  AshTestBase::SetUp();

  Shelf* shelf = GetPrimaryShelf();
  ShelfViewTestAPI shelf_view_test_api(shelf->GetShelfViewForTesting());

  metric_tracker_ = shelf_view_test_api.shelf_button_pressed_metric_tracker();

  ShelfButtonPressedMetricTrackerTestAPI test_api(metric_tracker_);

  test_api.SetTickClock(&tick_clock_);

  // Ensure the TickClock->NowTicks() doesn't return base::TimeTicks because
  // ShelfButtonPressedMetricTracker interprets that value as unset.
  tick_clock_.Advance(base::Milliseconds(100));
}

void ShelfButtonPressedMetricTrackerTest::TearDown() {
  AshTestBase::TearDown();
}

void ShelfButtonPressedMetricTrackerTest::ButtonPressed(
    const ui::Event& event) {
  const DummyButton kDummyButton;
  metric_tracker_->ButtonPressed(event, &kDummyButton, SHELF_ACTION_NONE);
}

void ShelfButtonPressedMetricTrackerTest::ButtonPressed(
    ShelfAction performed_action) {
  const ui::test::TestEvent kDummyEvent(ui::EventType::kGestureTap);
  const DummyButton kDummyButton;
  metric_tracker_->ButtonPressed(kDummyEvent, &kDummyButton, performed_action);
}

void ShelfButtonPressedMetricTrackerTest::ButtonPressed(
    const views::Button* sender,
    ShelfAction performed_action) {
  const ui::test::TestEvent kDummyEvent(ui::EventType::kGestureTap);
  metric_tracker_->ButtonPressed(kDummyEvent, sender, performed_action);
}

}  // namespace

// Verifies that a Launcher_ButtonPressed_Mouse UMA user action is recorded when
// a button is pressed by a mouse event.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByMouse) {
  const ui::MouseEvent mouse_event(ui::EventType::kMousePressed, gfx::Point(),
                                   gfx::Point(), base::TimeTicks(), 0, 0);

  base::UserActionTester user_action_tester;
  ButtonPressed(mouse_event);
  EXPECT_EQ(1,
            user_action_tester.GetActionCount("Launcher_ButtonPressed_Mouse"));
}

// Verifies that a Launcher_ButtonPressed_Touch UMA user action is recorded when
// a button is pressed by a touch event.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByTouch) {
  const ui::TouchEvent touch_event(
      ui::EventType::kGestureTap, gfx::Point(), base::TimeTicks(),
      ui::PointerDetails(ui::EventPointerType::kTouch, 0));

  base::UserActionTester user_action_tester;
  ButtonPressed(touch_event);
  EXPECT_EQ(1,
            user_action_tester.GetActionCount("Launcher_ButtonPressed_Touch"));
}

// Verifies that a Launcher_LaunchTask UMA user action is recorded when
// pressing a button causes a new window to be created.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       Launcher_LaunchTaskIsRecordedWhenNewWindowIsCreated) {
  base::UserActionTester user_action_tester;
  ButtonPressed(SHELF_ACTION_NEW_WINDOW_CREATED);
  EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_LaunchTask"));
}

// Verifies that a Launcher_MinimizeTask UMA user action is recorded when
// pressing a button causes an existing window to be minimized.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       Launcher_MinimizeTaskIsRecordedWhenWindowIsMinimized) {
  base::UserActionTester user_action_tester;
  ButtonPressed(SHELF_ACTION_WINDOW_MINIMIZED);
  EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_MinimizeTask"));
}

// Verifies that a Launcher_SwitchTask UMA user action is recorded when
// pressing a button causes an existing window to be activated.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       Launcher_SwitchTaskIsRecordedWhenExistingWindowIsActivated) {
  base::UserActionTester user_action_tester;
  ButtonPressed(SHELF_ACTION_WINDOW_ACTIVATED);
  EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_SwitchTask"));
}

// Verify that a window activation action will record a data point if it was
// subsequent to a minimize action.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       VerifyDataRecordedAfterMinimizedAndSubsequentActivatedAction) {
  const DummyButton kDummyButton;

  base::HistogramTester histogram_tester;

  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED);
  histogram_tester.ExpectTotalCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0);

  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED);
  histogram_tester.ExpectTotalCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
}

// Verify that a multiple window activation actions will record a single data
// point if they are subsequent to a minimize action.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       VerifyDataRecordedAfterMinimizedAndMultipleSubsequentActivatedActions) {
  const DummyButton kDummyButton;

  base::HistogramTester histogram_tester;

  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED);
  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED);
  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED);

  histogram_tester.ExpectTotalCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
}

// Verify that a window activation action will not record a data point if it was
// not subsequent to a minimize action.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       VerifyDataRecordedAfterMinimizedAndNonSubsequentActivatedAction) {
  const DummyButton kDummyButton;

  base::HistogramTester histogram_tester;

  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED);
  ButtonPressed(&kDummyButton, SHELF_ACTION_APP_LIST_SHOWN);
  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED);

  histogram_tester.ExpectTotalCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0);
}

// Verify no data is recorded if a second source button is pressed in between
// subsequent minimized and activated actions on the same source.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       VerifyDataRecordedAfterMinimizedButtonA) {
  const DummyButton kDummyButton;
  const DummyButton kSecondDummyButton;

  base::HistogramTester histogram_tester;

  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED);
  ButtonPressed(&kSecondDummyButton, SHELF_ACTION_WINDOW_MINIMIZED);
  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED);

  histogram_tester.ExpectTotalCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 0);
}

// Verify the data value recorded when a window activation action is subsequent
// to a minimize action.
TEST_F(ShelfButtonPressedMetricTrackerTest,
       VerifyTheValueRecordedBySubsequentMinimizedAndActivateActions) {
  const int kTimeDeltaInMilliseconds = 17;
  const DummyButton kDummyButton;

  base::HistogramTester histogram_tester;

  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_MINIMIZED);
  tick_clock_.Advance(base::Milliseconds(kTimeDeltaInMilliseconds));
  ButtonPressed(&kDummyButton, SHELF_ACTION_WINDOW_ACTIVATED);

  histogram_tester.ExpectTotalCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
  histogram_tester.ExpectBucketCount(
      kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName,
      kTimeDeltaInMilliseconds, 1);
}

}  // namespace ash