1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <memory>
6 
7 #include "ash/system/palette/palette_tool.h"
8 #include "ash/system/palette/palette_tool_manager.h"
9 #include "base/bind.h"
10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/notreached.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace ash {
16 
17 namespace {
18 
19 // A simple tool instance that exposes some additional data for testing.
20 class TestTool : public PaletteTool {
21  public:
TestTool(Delegate * delegate,PaletteGroup group,PaletteToolId tool_id)22   TestTool(Delegate* delegate, PaletteGroup group, PaletteToolId tool_id)
23       : PaletteTool(delegate), group_(group), tool_id_(tool_id) {}
24 
25   // PaletteTool:
GetGroup() const26   PaletteGroup GetGroup() const override { return group_; }
GetToolId() const27   PaletteToolId GetToolId() const override { return tool_id_; }
28 
29   // Shadows the parent declaration since PaletteTool::enabled is not virtual.
enabled() const30   bool enabled() const { return PaletteTool::enabled(); }
31 
32  private:
33   // PaletteTool:
CreateView()34   views::View* CreateView() override {
35     NOTREACHED();
36     return nullptr;
37   }
OnViewDestroyed()38   void OnViewDestroyed() override { FAIL(); }
39 
40   PaletteGroup group_;
41   PaletteToolId tool_id_;
42 
43   DISALLOW_COPY_AND_ASSIGN(TestTool);
44 };
45 
46 // Base class for tool manager unittests.
47 class PaletteToolManagerTest : public ::testing::Test,
48                                public PaletteToolManager::Delegate,
49                                public PaletteTool::Delegate {
50  public:
PaletteToolManagerTest()51   PaletteToolManagerTest()
52       : palette_tool_manager_(new PaletteToolManager(this)) {}
53   ~PaletteToolManagerTest() override = default;
54 
55  protected:
56   // PaletteToolManager::Delegate:
HidePalette()57   void HidePalette() override {}
HidePaletteImmediately()58   void HidePaletteImmediately() override {}
OnActiveToolChanged()59   void OnActiveToolChanged() override { ++tool_changed_count_; }
GetWindow()60   aura::Window* GetWindow() override {
61     NOTREACHED();
62     return nullptr;
63   }
RecordPaletteOptionsUsage(PaletteTrayOptions option,PaletteInvocationMethod method)64   void RecordPaletteOptionsUsage(PaletteTrayOptions option,
65                                  PaletteInvocationMethod method) override {}
RecordPaletteModeCancellation(PaletteModeCancelType type)66   void RecordPaletteModeCancellation(PaletteModeCancelType type) override {}
67 
68   // PaletteTool::Delegate:
EnableTool(PaletteToolId tool_id)69   void EnableTool(PaletteToolId tool_id) override {}
DisableTool(PaletteToolId tool_id)70   void DisableTool(PaletteToolId tool_id) override {}
71 
72   // Helper method for returning an unowned pointer to the constructed tool
73   // while also adding it to the PaletteToolManager.
BuildTool(PaletteGroup group,PaletteToolId tool_id)74   TestTool* BuildTool(PaletteGroup group, PaletteToolId tool_id) {
75     auto* tool = new TestTool(this, group, tool_id);
76     palette_tool_manager_->AddTool(base::WrapUnique(tool));
77     return tool;
78   }
79 
80   int tool_changed_count_ = 0;
81   std::unique_ptr<PaletteToolManager> palette_tool_manager_;
82 
83  private:
84   DISALLOW_COPY_AND_ASSIGN(PaletteToolManagerTest);
85 };
86 
87 }  // namespace
88 
89 // Verifies that tools can be enabled/disabled and that enabling a tool disables
90 // only active tools in the same group.
TEST_F(PaletteToolManagerTest,MultipleToolsActivateDeactivate)91 TEST_F(PaletteToolManagerTest, MultipleToolsActivateDeactivate) {
92   // Register actions/modes.
93   TestTool* action_1 =
94       BuildTool(PaletteGroup::ACTION, PaletteToolId::CREATE_NOTE);
95   TestTool* action_2 =
96       BuildTool(PaletteGroup::ACTION, PaletteToolId::CAPTURE_REGION);
97   TestTool* mode_1 = BuildTool(PaletteGroup::MODE, PaletteToolId::MAGNIFY);
98 
99   EXPECT_FALSE(palette_tool_manager_->HasTool(PaletteToolId::LASER_POINTER));
100   TestTool* mode_2 =
101       BuildTool(PaletteGroup::MODE, PaletteToolId::LASER_POINTER);
102   EXPECT_TRUE(palette_tool_manager_->HasTool(PaletteToolId::LASER_POINTER));
103 
104   // Enable mode 1.
105   EXPECT_EQ(0, tool_changed_count_);
106   palette_tool_manager_->ActivateTool(mode_1->GetToolId());
107   EXPECT_FALSE(action_1->enabled());
108   EXPECT_FALSE(action_2->enabled());
109   EXPECT_TRUE(mode_1->enabled());
110   EXPECT_FALSE(mode_2->enabled());
111 
112   // Turn a single action on/off. Enabling/disabling the tool does not change
113   // any other group's state.
114   palette_tool_manager_->ActivateTool(action_1->GetToolId());
115   EXPECT_TRUE(action_1->enabled());
116   EXPECT_FALSE(action_2->enabled());
117   EXPECT_TRUE(mode_1->enabled());
118   EXPECT_FALSE(mode_2->enabled());
119   palette_tool_manager_->DeactivateTool(action_1->GetToolId());
120   EXPECT_FALSE(action_1->enabled());
121   EXPECT_FALSE(action_2->enabled());
122   EXPECT_TRUE(mode_1->enabled());
123   EXPECT_FALSE(mode_2->enabled());
124 
125   // Activating a tool on will deactivate any other active tools in the same
126   // group.
127   palette_tool_manager_->ActivateTool(action_1->GetToolId());
128   EXPECT_TRUE(action_1->enabled());
129   EXPECT_FALSE(action_2->enabled());
130   palette_tool_manager_->ActivateTool(action_2->GetToolId());
131   EXPECT_FALSE(action_1->enabled());
132   EXPECT_TRUE(action_2->enabled());
133   palette_tool_manager_->DeactivateTool(action_2->GetToolId());
134 
135   // Activating an already active tool will not do anything.
136   palette_tool_manager_->ActivateTool(action_1->GetToolId());
137   EXPECT_TRUE(action_1->enabled());
138   EXPECT_FALSE(action_2->enabled());
139   palette_tool_manager_->ActivateTool(action_1->GetToolId());
140   EXPECT_TRUE(action_1->enabled());
141   EXPECT_FALSE(action_2->enabled());
142   palette_tool_manager_->DeactivateTool(action_1->GetToolId());
143 }
144 
145 }  // namespace ash