1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "gtest/gtest.h"
18
19#include "chre/util/lock_guard.h"
20
21using chre::LockGuard;
22
23class MockMutex {
24 public:
25  MockMutex() : mLockCount(0) {}
26
27  void lock() {
28    mLockCount++;
29  }
30
31  void unlock() {
32    mLockCount--;
33  }
34
35  int getLockCount() {
36    return mLockCount;
37  }
38
39 private:
40  int mLockCount;
41};
42
43TEST(LockGuard, LockUnlock) {
44  MockMutex mutex;
45  EXPECT_EQ(mutex.getLockCount(), 0);
46
47  {
48    LockGuard<MockMutex> lock(mutex);
49    EXPECT_EQ(mutex.getLockCount(), 1);
50  }
51
52  EXPECT_EQ(mutex.getLockCount(), 0);
53}
54