Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 #include <utility> | |
| 7 #include <vector> | |
| 6 | 8 |
| 9 #include "ash/public/interfaces/tray_action_handler.mojom.h" | |
| 7 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/run_loop.h" | |
| 8 #include "base/test/scoped_command_line.h" | 12 #include "base/test/scoped_command_line.h" |
| 9 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" | 13 #include "chrome/browser/chromeos/lock_screen_apps/state_controller.h" |
| 10 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" | 14 #include "chrome/browser/chromeos/lock_screen_apps/state_observer.h" |
| 11 #include "chrome/browser/chromeos/lock_screen_apps/types.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 17 |
| 18 using ash::mojom::TrayActionState; | |
| 19 | |
| 14 namespace { | 20 namespace { |
| 15 | 21 |
| 16 const lock_screen_apps::Action kTestAction = lock_screen_apps::Action::kNewNote; | |
| 17 | |
| 18 class TestStateObserver : public lock_screen_apps::StateObserver { | 22 class TestStateObserver : public lock_screen_apps::StateObserver { |
| 19 public: | 23 public: |
| 20 TestStateObserver() = default; | 24 TestStateObserver() = default; |
| 21 ~TestStateObserver() override = default; | 25 ~TestStateObserver() override = default; |
| 22 | 26 |
| 23 void OnLockScreenAppsStateChanged( | 27 void OnLockScreenNoteStateChanged(TrayActionState state) override { |
| 24 lock_screen_apps::Action action, | 28 observed_states_.push_back(state); |
| 25 lock_screen_apps::ActionState state) override { | |
| 26 ASSERT_EQ(kTestAction, action); | |
| 27 | |
| 28 ++state_change_count_; | |
| 29 } | 29 } |
| 30 | 30 |
| 31 int state_change_count() const { return state_change_count_; } | 31 const std::vector<TrayActionState> observed_states() const { |
| 32 return observed_states_; | |
| 33 } | |
| 34 | |
| 35 void clear_observed_states() { observed_states_.clear(); } | |
| 32 | 36 |
| 33 private: | 37 private: |
| 34 int state_change_count_ = 0; | 38 std::vector<TrayActionState> observed_states_; |
| 35 | 39 |
| 36 DISALLOW_COPY_AND_ASSIGN(TestStateObserver); | 40 DISALLOW_COPY_AND_ASSIGN(TestStateObserver); |
| 37 }; | 41 }; |
| 38 | 42 |
| 39 class LockScreenAppStateControllerNotSupportedTest : public testing::Test { | 43 class TestTrayAction : public ash::mojom::TrayAction { |
| 40 public: | 44 public: |
| 41 LockScreenAppStateControllerNotSupportedTest() = default; | 45 TestTrayAction() : binding_(this) {} |
| 42 | 46 |
| 43 ~LockScreenAppStateControllerNotSupportedTest() override = default; | 47 ~TestTrayAction() override = default; |
| 48 | |
| 49 ash::mojom::TrayActionPtr CreateInterfacePtrAndBind() { | |
| 50 return binding_.CreateInterfacePtrAndBind(); | |
| 51 } | |
| 52 | |
| 53 void SetClient(ash::mojom::TrayActionClientPtr client, | |
| 54 TrayActionState state) override { | |
| 55 client_ = std::move(client); | |
| 56 EXPECT_EQ(TrayActionState::kNotSupported, state); | |
| 57 } | |
| 58 | |
| 59 void UpdateLockScreenNoteState(TrayActionState state) override { | |
| 60 observed_states_.push_back(state); | |
| 61 } | |
| 62 | |
| 63 void SendNewNoteRequest() { | |
| 64 ASSERT_TRUE(client_); | |
| 65 client_->RequestNewLockScreenNote(); | |
| 66 } | |
| 67 | |
| 68 const std::vector<TrayActionState>& observed_states() const { | |
| 69 return observed_states_; | |
| 70 } | |
| 71 | |
| 72 void clear_observed_states() { observed_states_.clear(); } | |
|
James Cook
2017/05/05 02:55:45
ClearObservedStates
tbarzic
2017/05/05 04:46:27
Done.
| |
| 73 | |
| 74 private: | |
| 75 mojo::Binding<ash::mojom::TrayAction> binding_; | |
| 76 ash::mojom::TrayActionClientPtr client_; | |
| 77 | |
| 78 std::vector<TrayActionState> observed_states_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(TestTrayAction); | |
| 81 }; | |
| 82 | |
| 83 class LockScreenAppStateNotSupportedTest : public testing::Test { | |
| 84 public: | |
| 85 LockScreenAppStateNotSupportedTest() = default; | |
| 86 | |
| 87 ~LockScreenAppStateNotSupportedTest() override = default; | |
| 44 | 88 |
| 45 void SetUp() override { | 89 void SetUp() override { |
| 46 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); | 90 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| 47 command_line_->GetProcessCommandLine()->InitFromArgv({""}); | 91 command_line_->GetProcessCommandLine()->InitFromArgv({""}); |
| 48 } | 92 } |
| 49 | 93 |
| 50 private: | 94 private: |
| 51 std::unique_ptr<base::test::ScopedCommandLine> command_line_; | 95 std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| 52 | 96 |
| 53 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateControllerNotSupportedTest); | 97 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateNotSupportedTest); |
| 54 }; | 98 }; |
| 55 | 99 |
| 56 class LockScreenAppStateControllerTest : public testing::Test { | 100 class LockScreenAppStateTest : public testing::Test { |
| 57 public: | 101 public: |
| 58 LockScreenAppStateControllerTest() = default; | 102 LockScreenAppStateTest() = default; |
| 59 ~LockScreenAppStateControllerTest() override = default; | 103 ~LockScreenAppStateTest() override = default; |
| 60 | 104 |
| 61 void SetUp() override { | 105 void SetUp() override { |
| 62 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); | 106 command_line_ = base::MakeUnique<base::test::ScopedCommandLine>(); |
| 63 command_line_->GetProcessCommandLine()->InitFromArgv( | 107 command_line_->GetProcessCommandLine()->InitFromArgv( |
| 64 {"", "--enable-lock-screen-apps"}); | 108 {"", "--enable-lock-screen-apps"}); |
| 65 | 109 |
| 66 state_controller_ = lock_screen_apps::StateController::Get(); | 110 ASSERT_TRUE(lock_screen_apps::StateController::IsEnabled()); |
| 67 ASSERT_TRUE(state_controller_); | 111 ash::mojom::TrayActionPtr action_controller_interface = |
| 68 | 112 tray_action_.CreateInterfacePtrAndBind(); |
| 113 state_controller_ = lock_screen_apps::StateController::CreateForTesting( | |
| 114 std::move(action_controller_interface)); | |
| 69 state_controller_->AddObserver(&observer_); | 115 state_controller_->AddObserver(&observer_); |
| 70 } | 116 } |
| 71 | 117 |
| 72 void TearDown() override { state_controller_->RemoveObserver(&observer_); } | 118 void TearDown() override { |
| 119 state_controller_->RemoveObserver(&observer_); | |
| 120 state_controller_.reset(); | |
| 121 } | |
| 73 | 122 |
| 74 const TestStateObserver& observer() const { return observer_; } | 123 TestStateObserver* observer() { return &observer_; } |
| 124 | |
| 125 TestTrayAction* tray_action() { return &tray_action_; } | |
| 75 | 126 |
| 76 lock_screen_apps::StateController* state_controller() { | 127 lock_screen_apps::StateController* state_controller() { |
| 77 return state_controller_; | 128 return state_controller_.get(); |
| 78 } | 129 } |
| 79 | 130 |
| 80 private: | 131 private: |
| 81 std::unique_ptr<base::test::ScopedCommandLine> command_line_; | 132 std::unique_ptr<base::test::ScopedCommandLine> command_line_; |
| 133 content::TestBrowserThreadBundle threads_; | |
| 82 | 134 |
| 83 lock_screen_apps::StateController* state_controller_; | 135 std::unique_ptr<lock_screen_apps::StateController> state_controller_; |
| 84 TestStateObserver observer_; | 136 TestStateObserver observer_; |
| 137 TestTrayAction tray_action_; | |
| 85 | 138 |
| 86 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateControllerTest); | 139 DISALLOW_COPY_AND_ASSIGN(LockScreenAppStateTest); |
| 87 }; | 140 }; |
| 88 | 141 |
| 89 } // namespace | 142 } // namespace |
| 90 | 143 |
| 91 TEST_F(LockScreenAppStateControllerNotSupportedTest, NoControllerInstance) { | 144 TEST_F(LockScreenAppStateNotSupportedTest, NoInstance) { |
| 92 EXPECT_FALSE(lock_screen_apps::StateController::Get()); | 145 EXPECT_FALSE(lock_screen_apps::StateController::IsEnabled()); |
| 93 } | 146 } |
| 94 | 147 |
| 95 TEST_F(LockScreenAppStateControllerTest, InitialState) { | 148 TEST_F(LockScreenAppStateTest, InitialState) { |
| 96 EXPECT_EQ(lock_screen_apps::ActionState::kNotSupported, | 149 EXPECT_EQ(TrayActionState::kNotSupported, |
| 97 state_controller()->GetActionState(kTestAction)); | 150 state_controller()->GetLockScreenNoteState()); |
| 98 | |
| 99 ASSERT_EQ(0, observer().state_change_count()); | |
| 100 | 151 |
| 101 state_controller()->MoveToBackground(); | 152 state_controller()->MoveToBackground(); |
| 102 | 153 |
| 103 EXPECT_EQ(lock_screen_apps::ActionState::kNotSupported, | 154 EXPECT_EQ(TrayActionState::kNotSupported, |
| 104 state_controller()->GetActionState(kTestAction)); | 155 state_controller()->GetLockScreenNoteState()); |
| 105 | 156 |
| 106 ASSERT_EQ(0, observer().state_change_count()); | 157 ASSERT_EQ(0u, observer()->observed_states().size()); |
|
James Cook
2017/05/05 02:55:45
EXPECT_EQ
tbarzic
2017/05/05 04:46:27
Done.
| |
| 158 ASSERT_EQ(0u, tray_action()->observed_states().size()); | |
| 107 } | 159 } |
| 108 | 160 |
| 109 TEST_F(LockScreenAppStateControllerTest, HandleActionWhenNotAvaiable) { | 161 TEST_F(LockScreenAppStateTest, MoveToBackgroundFromActive) { |
| 110 ASSERT_EQ(lock_screen_apps::ActionState::kNotSupported, | 162 state_controller()->SetLockScreenNoteStateForTesting( |
| 111 state_controller()->GetActionState(kTestAction)); | 163 TrayActionState::kActive); |
| 112 EXPECT_FALSE( | 164 |
| 113 state_controller()->HandleAction(lock_screen_apps::Action::kNewNote)); | 165 state_controller()->MoveToBackground(); |
| 166 base::RunLoop().RunUntilIdle(); | |
| 167 | |
| 168 EXPECT_EQ(TrayActionState::kBackground, | |
| 169 state_controller()->GetLockScreenNoteState()); | |
| 170 | |
| 171 ASSERT_EQ(1u, observer()->observed_states().size()); | |
| 172 EXPECT_EQ(TrayActionState::kBackground, observer()->observed_states()[0]); | |
| 173 ASSERT_EQ(1u, tray_action()->observed_states().size()); | |
| 174 EXPECT_EQ(TrayActionState::kBackground, tray_action()->observed_states()[0]); | |
| 114 } | 175 } |
| 176 | |
| 177 TEST_F(LockScreenAppStateTest, HandleActionWhenNotAvaiable) { | |
| 178 ASSERT_EQ(TrayActionState::kNotSupported, | |
| 179 state_controller()->GetLockScreenNoteState()); | |
| 180 | |
| 181 tray_action()->SendNewNoteRequest(); | |
| 182 base::RunLoop().RunUntilIdle(); | |
|
James Cook
2017/05/05 02:55:45
If you want to be explicit about why you have to s
tbarzic
2017/05/05 04:46:27
Done.
| |
| 183 | |
| 184 EXPECT_EQ(0u, observer()->observed_states().size()); | |
| 185 EXPECT_EQ(0u, tray_action()->observed_states().size()); | |
| 186 } | |
| 187 | |
| 188 TEST_F(LockScreenAppStateTest, HandleAction) { | |
| 189 state_controller()->SetLockScreenNoteStateForTesting( | |
| 190 TrayActionState::kAvailable); | |
| 191 | |
| 192 tray_action()->SendNewNoteRequest(); | |
| 193 base::RunLoop().RunUntilIdle(); | |
| 194 | |
| 195 EXPECT_EQ(TrayActionState::kActive, | |
| 196 state_controller()->GetLockScreenNoteState()); | |
| 197 ASSERT_EQ(1u, observer()->observed_states().size()); | |
| 198 EXPECT_EQ(TrayActionState::kActive, observer()->observed_states()[0]); | |
| 199 observer()->clear_observed_states(); | |
| 200 ASSERT_EQ(1u, tray_action()->observed_states().size()); | |
| 201 EXPECT_EQ(TrayActionState::kActive, tray_action()->observed_states()[0]); | |
| 202 tray_action()->clear_observed_states(); | |
| 203 | |
| 204 tray_action()->SendNewNoteRequest(); | |
| 205 base::RunLoop().RunUntilIdle(); | |
| 206 | |
| 207 EXPECT_EQ(0u, observer()->observed_states().size()); | |
|
James Cook
2017/05/05 02:55:45
A comment here would be helpful (since a trivial r
tbarzic
2017/05/05 04:46:27
Done.
| |
| 208 EXPECT_EQ(0u, tray_action()->observed_states().size()); | |
| 209 } | |
|
James Cook
2017/05/05 02:55:45
Thanks for adding good test coverage, both here an
| |
| OLD | NEW |