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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/camera/camera_general_survey_handler.h"
#include <memory>
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Exactly;
using ::testing::Return;
namespace ash {
namespace {
class MockDelegate : public CameraGeneralSurveyHandler::Delegate {
public:
MOCK_METHOD(void,
AddActiveCameraClientObserver,
(media::CameraActiveClientObserver * observer),
(override));
MOCK_METHOD(void,
RemoveActiveCameraClientObserver,
(media::CameraActiveClientObserver * observer),
(override));
MOCK_METHOD(void, LoadConfig, (), (override));
MOCK_METHOD(bool, ShouldShowSurvey, (), (const, override));
MOCK_METHOD(void, ShowSurvey, (), (override));
};
cros::mojom::CameraClientType kSupportedCameraTypes[] = {
cros::mojom::CameraClientType::CHROME,
cros::mojom::CameraClientType::ANDROID,
cros::mojom::CameraClientType::PLUGINVM,
cros::mojom::CameraClientType::ASH_CHROME};
constexpr base::TimeDelta kMinCameraOpenDurationForSurveyTest =
base::Seconds(100);
// Any duration shorter than |kMinCameraOpenDurationForSurveyTest|
constexpr base::TimeDelta kShortOpenDuration = base::Seconds(10);
// Any duration longer than |kMinCameraOpenDurationForSurveyTest|
constexpr base::TimeDelta kLongOpenDuration = base::Seconds(110);
constexpr base::TimeDelta kCameraSurveyTriggerTimerDurationTest =
base::Seconds(5);
// Any duration shorter than |kCameraSurveyTriggerTimerDurationTest|
constexpr base::TimeDelta kShortCloseDuration = base::Seconds(1);
// Any duration longer than |kCameraSurveyTriggerTimerDurationTest|
constexpr base::TimeDelta kLongCloseDuration = base::Seconds(10);
class CameraGeneralSurveyHandlerTest : public testing::Test {
protected:
CameraGeneralSurveyHandlerTest() {
delegate_ = nullptr;
survey_handler_ = nullptr;
}
void InitializeSurveyHandler(std::unique_ptr<MockDelegate> delegate,
bool is_enabled = true) {
survey_handler_ = std::make_unique<CameraGeneralSurveyHandler>(
is_enabled, std::move(delegate), kMinCameraOpenDurationForSurveyTest,
kCameraSurveyTriggerTimerDurationTest);
}
// testing::Test:
void SetUp() override {
auto delegate = std::make_unique<MockDelegate>();
delegate_ = delegate.get();
InitializeSurveyHandler(std::move(delegate));
}
void TearDown() override {
survey_handler_ = nullptr;
// The delegate_ pointer was already deleted because survey_handler_
// took ownership.
delegate_ = nullptr;
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
raw_ptr<MockDelegate, DanglingUntriaged> delegate_ = nullptr;
std::unique_ptr<CameraGeneralSurveyHandler> survey_handler_;
};
class CameraGeneralSurveyHandlerInitializationTest
: public CameraGeneralSurveyHandlerTest,
public testing::WithParamInterface<bool> {
protected:
// testing::Test:
void SetUp() override {
auto delegate = std::make_unique<MockDelegate>();
delegate_ = delegate.get();
const bool is_enabled = GetParam();
EXPECT_CALL(*delegate_, AddActiveCameraClientObserver)
.Times(Exactly(is_enabled ? 1 : 0));
EXPECT_CALL(*delegate_, LoadConfig).Times(Exactly(is_enabled ? 1 : 0));
InitializeSurveyHandler(std::move(delegate), is_enabled);
}
// testing::Test:
void TearDown() override {
EXPECT_CALL(*delegate_, RemoveActiveCameraClientObserver)
.Times(Exactly(GetParam() ? 1 : 0));
CameraGeneralSurveyHandlerTest::TearDown();
}
};
TEST_P(CameraGeneralSurveyHandlerInitializationTest, InitializeNoOp) {
task_environment_.AdvanceClock(kLongOpenDuration);
}
INSTANTIATE_TEST_SUITE_P(CameraGeneralSurveyFeatureEnablement,
CameraGeneralSurveyHandlerInitializationTest,
testing::Values(true, false));
class CameraGeneralSurveyHandlerOpenCloseTest
: public CameraGeneralSurveyHandlerTest,
public testing::WithParamInterface<cros::mojom::CameraClientType> {
protected:
void SetUp() override {
auto delegate = std::make_unique<MockDelegate>();
delegate_ = delegate.get();
EXPECT_CALL(*delegate_, AddActiveCameraClientObserver).Times(Exactly(1));
EXPECT_CALL(*delegate_, LoadConfig).Times(Exactly(1));
InitializeSurveyHandler(std::move(delegate));
}
void TearDown() override {
EXPECT_CALL(*delegate_, RemoveActiveCameraClientObserver).Times(Exactly(1));
CameraGeneralSurveyHandlerTest::TearDown();
}
// Simulates opening a camera and then keeping it open for |duration|
void OpenCamera(base::TimeDelta duration = base::Seconds(0)) {
survey_handler_->OnActiveClientChange(GetParam(),
/*is_new_active_client*/ true,
base::flat_set<std::string>({"0"}));
task_environment_.AdvanceClock(duration);
}
// Simulates closing all camera and then keeping it closed for |duration|
void CloseCamera(base::TimeDelta duration = base::Seconds(0)) {
survey_handler_->OnActiveClientChange(GetParam(),
/*is_new_active_client*/ false,
base::flat_set<std::string>({}));
task_environment_.AdvanceClock(duration);
}
};
TEST_P(CameraGeneralSurveyHandlerOpenCloseTest, CameraOpenNotYetClosed) {
// It shouldn't show survey when camera is still open.
EXPECT_CALL(*delegate_, ShowSurvey).Times(Exactly(0));
OpenCamera(kLongOpenDuration);
}
TEST_P(CameraGeneralSurveyHandlerOpenCloseTest, CameraOpenShortTime) {
// It shouldn't show survey after camera was just open for a duration
// shorter than |kMinCameraOpenDurationForSurveyTest|.
EXPECT_CALL(*delegate_, ShowSurvey).Times(Exactly(0));
OpenCamera(kShortOpenDuration);
CloseCamera();
}
TEST_P(CameraGeneralSurveyHandlerOpenCloseTest, CameraReopenImmediately) {
// It shouldn't show survey after camera was just closed for a duration
// shorter than |kCameraSurveyTriggerTimerDurationTest| (after being
// open for duration longer than |kMinCameraOpenDurationForSurveyTest|).
// Switching between front and back camera falls into this test scenario.
EXPECT_CALL(*delegate_, ShowSurvey).Times(Exactly(0));
EXPECT_CALL(*delegate_, ShouldShowSurvey).WillOnce(Return(true));
OpenCamera(kLongOpenDuration);
CloseCamera(kShortCloseDuration);
OpenCamera(kLongOpenDuration);
base::RunLoop().RunUntilIdle();
}
TEST_P(CameraGeneralSurveyHandlerOpenCloseTest, ShowSurvey) {
// It should show survey exactly once after all conditions fulfilled.
EXPECT_CALL(*delegate_, ShowSurvey).Times(Exactly(1));
EXPECT_CALL(*delegate_, ShouldShowSurvey).WillOnce(Return(true));
OpenCamera(kLongOpenDuration);
CloseCamera(kLongCloseDuration);
base::RunLoop().RunUntilIdle();
}
TEST_P(CameraGeneralSurveyHandlerOpenCloseTest, UserNotSelectedForSurvey) {
// All conditions have been fulfilled except that the user is not selected
// for the survey, therefore it shouldn't show the survey.
EXPECT_CALL(*delegate_, ShowSurvey).Times(Exactly(0));
EXPECT_CALL(*delegate_, ShouldShowSurvey).WillOnce(Return(false));
OpenCamera(kLongOpenDuration);
CloseCamera(kLongCloseDuration);
base::RunLoop().RunUntilIdle();
}
INSTANTIATE_TEST_SUITE_P(CameraGeneralSurveyAllCameraClientTypes,
CameraGeneralSurveyHandlerOpenCloseTest,
testing::ValuesIn(kSupportedCameraTypes));
} // namespace
} // namespace ash
|