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

ash / system / palette / palette_tool_manager_unittest.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "ash/system/palette/palette_tool.h"
#include "ash/system/palette/palette_tool_manager.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

namespace {

// A simple tool instance that exposes some additional data for testing.
class TestTool : public PaletteTool {
 public:
  TestTool(Delegate* delegate, PaletteGroup group, PaletteToolId tool_id)
      : PaletteTool(delegate), group_(group), tool_id_(tool_id) {}

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

  // PaletteTool:
  PaletteGroup GetGroup() const override { return group_; }
  PaletteToolId GetToolId() const override { return tool_id_; }

  // Shadows the parent declaration since PaletteTool::enabled is not virtual.
  bool enabled() const { return PaletteTool::enabled(); }

 private:
  // PaletteTool:
  views::View* CreateView() override { NOTREACHED(); }
  void OnViewDestroyed() override { FAIL(); }

  PaletteGroup group_;
  PaletteToolId tool_id_;
};

// Base class for tool manager unittests.
class PaletteToolManagerTest : public ::testing::Test,
                               public PaletteToolManager::Delegate,
                               public PaletteTool::Delegate {
 public:
  PaletteToolManagerTest()
      : palette_tool_manager_(new PaletteToolManager(this)) {}

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

  ~PaletteToolManagerTest() override = default;

 protected:
  // PaletteToolManager::Delegate:
  void HidePalette() override {}
  void HidePaletteImmediately() override {}
  void OnActiveToolChanged() override { ++tool_changed_count_; }
  aura::Window* GetWindow() override { NOTREACHED(); }

  // PaletteTool::Delegate:
  void EnableTool(PaletteToolId tool_id) override {}
  void DisableTool(PaletteToolId tool_id) override {}

  // Helper method for returning an unowned pointer to the constructed tool
  // while also adding it to the PaletteToolManager.
  TestTool* BuildTool(PaletteGroup group, PaletteToolId tool_id) {
    auto* tool = new TestTool(this, group, tool_id);
    palette_tool_manager_->AddTool(base::WrapUnique(tool));
    return tool;
  }

  int tool_changed_count_ = 0;
  std::unique_ptr<PaletteToolManager> palette_tool_manager_;
};

}  // namespace

// Verifies that tools can be enabled/disabled and that enabling a tool disables
// only active tools in the same group.
TEST_F(PaletteToolManagerTest, MultipleToolsActivateDeactivate) {
  // Register actions/modes.
  TestTool* action_1 =
      BuildTool(PaletteGroup::ACTION, PaletteToolId::CREATE_NOTE);
  TestTool* action_2 =
      BuildTool(PaletteGroup::ACTION, PaletteToolId::ENTER_CAPTURE_MODE);
  TestTool* mode_1 = BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY);

  EXPECT_FALSE(palette_tool_manager_->HasTool(PaletteToolId::LASER_POINTER));
  TestTool* mode_2 =
      BuildTool(PaletteGroup::MODE, PaletteToolId::LASER_POINTER);
  EXPECT_TRUE(palette_tool_manager_->HasTool(PaletteToolId::LASER_POINTER));

  // Enable mode 1.
  EXPECT_EQ(0, tool_changed_count_);
  palette_tool_manager_->ActivateTool(mode_1->GetToolId());
  EXPECT_FALSE(action_1->enabled());
  EXPECT_FALSE(action_2->enabled());
  EXPECT_TRUE(mode_1->enabled());
  EXPECT_FALSE(mode_2->enabled());

  // Turn a single action on/off. Enabling/disabling the tool does not change
  // any other group's state.
  palette_tool_manager_->ActivateTool(action_1->GetToolId());
  EXPECT_TRUE(action_1->enabled());
  EXPECT_FALSE(action_2->enabled());
  EXPECT_TRUE(mode_1->enabled());
  EXPECT_FALSE(mode_2->enabled());
  palette_tool_manager_->DeactivateTool(action_1->GetToolId());
  EXPECT_FALSE(action_1->enabled());
  EXPECT_FALSE(action_2->enabled());
  EXPECT_TRUE(mode_1->enabled());
  EXPECT_FALSE(mode_2->enabled());

  // Activating a tool on will deactivate any other active tools in the same
  // group.
  palette_tool_manager_->ActivateTool(action_1->GetToolId());
  EXPECT_TRUE(action_1->enabled());
  EXPECT_FALSE(action_2->enabled());
  palette_tool_manager_->ActivateTool(action_2->GetToolId());
  EXPECT_FALSE(action_1->enabled());
  EXPECT_TRUE(action_2->enabled());
  palette_tool_manager_->DeactivateTool(action_2->GetToolId());

  // Activating an already active tool will not do anything.
  palette_tool_manager_->ActivateTool(action_1->GetToolId());
  EXPECT_TRUE(action_1->enabled());
  EXPECT_FALSE(action_2->enabled());
  palette_tool_manager_->ActivateTool(action_1->GetToolId());
  EXPECT_TRUE(action_1->enabled());
  EXPECT_FALSE(action_2->enabled());
  palette_tool_manager_->DeactivateTool(action_1->GetToolId());
}

}  // namespace ash