OSDN Git Service

Unit tests for wificond looper backed event loop
authorNingyuan Wang <nywang@google.com>
Thu, 26 May 2016 22:49:07 +0000 (15:49 -0700)
committerNingyuan Wang <nywang@google.com>
Fri, 27 May 2016 20:47:19 +0000 (13:47 -0700)
BUG=28982981
TEST=compile
TEST=run out/host/linux-x86/bin/wificond_unit_test

Change-Id: If54c7e02b68ca82cbb1fddfae144a71c73561b8d

Android.mk
event_loop.h
looper_backed_event_loop.cpp
looper_backed_event_loop.h
tests/looper_backed_event_loop_unittest.cpp [new file with mode: 0644]
tests/wificond_unittest.cpp [deleted file]

index 9229728..99e5668 100644 (file)
 
 LOCAL_PATH := $(call my-dir)
 
+wificond_cpp_flags := -std=c++11 -Wall -Werror -Wno-unused-parameter
+wificond_cpp_src := looper_backed_event_loop.cpp
+
 ###
 ### wificond daemon.
 ###
 include $(CLEAR_VARS)
 LOCAL_MODULE := wificond
-LOCAL_CLANG := true
-LOCAL_CPPFLAGS := -std=c++11 -Wall -Werror -Wno-unused-parameter
+LOCAL_CPPFLAGS := $(wificond_cpp_flags)
 LOCAL_INIT_RC := wificond.rc
 LOCAL_SRC_FILES := \
-    main.cpp \
-    looper_backed_event_loop.cpp
+    main.cpp
 LOCAL_SHARED_LIBRARIES := \
     libbase \
     libutils
-LOCAL_MODULE_HOST_OS := linux
+LOCAL_STATIC_LIBRARIES := \
+    libwificond
 include $(BUILD_EXECUTABLE)
 
 ###
-### wificond unit tests.
+### wificond static library
+###
+include $(CLEAR_VARS)
+LOCAL_MODULE := libwificond
+LOCAL_CPPFLAGS := $(wificond_cpp_flags)
+LOCAL_SRC_FILES := $(wificond_cpp_src)
+LOCAL_SHARED_LIBRARIES := \
+    libbase \
+    libutils
+include $(BUILD_STATIC_LIBRARY)
+
+###
+### wificond host static library
+###
+include $(CLEAR_VARS)
+LOCAL_MODULE := libwificond_host
+LOCAL_CPPFLAGS := $(wificond_cpp_flags)
+LOCAL_SRC_FILES := $(wificond_cpp_src)
+LOCAL_STATIC_LIBRARIES := \
+    libbase \
+    libutils
+LOCAL_MODULE_HOST_OS := linux
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+###
+### wificond host unit tests.
 ###
 include $(CLEAR_VARS)
 LOCAL_MODULE := wificond_unit_test
-LOCAL_CPPFLAGS := -std=c++11 -Wall -Werror -Wno-unused-parameter
+LOCAL_CPPFLAGS := $(wificond_cpp_flags)
 LOCAL_SRC_FILES := \
     tests/main.cpp \
-    tests/wificond_unittest.cpp
+    tests/looper_backed_event_loop_unittest.cpp
 LOCAL_STATIC_LIBRARIES := \
-    libgmock_host
+    libgmock_host \
+    libwificond_host \
+    libbase \
+    libutils \
+    liblog
+LOCAL_MODULE_HOST_OS := linux
 include $(BUILD_HOST_NATIVE_TEST)
index 38db142..b7945d3 100644 (file)
@@ -1,18 +1,18 @@
-//
-// Copyright (C) 2016 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 #ifndef WIFICOND_EVENT_LOOP_H_
 #define WIFICOND_EVENT_LOOP_H_
index ef2389a..db7197a 100644 (file)
@@ -1,18 +1,18 @@
-//
-// Copyright (C) 2016 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 #include <looper_backed_event_loop.h>
 
index 0cb6d45..6da847d 100644 (file)
@@ -1,18 +1,18 @@
-//
-// Copyright (C) 2016 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 #ifndef WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
 #define WIFICOND_LOOPER_BACKED_EVENT_LOOP_H_
diff --git a/tests/looper_backed_event_loop_unittest.cpp b/tests/looper_backed_event_loop_unittest.cpp
new file mode 100644 (file)
index 0000000..180f406
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <memory>
+
+#include <gtest/gtest.h>
+#include <utils/StopWatch.h>
+
+#include <looper_backed_event_loop.h>
+
+namespace {
+
+const int kTimingToleranceMs = 25;
+
+}  // namespace
+
+namespace android {
+namespace wificond {
+
+class WificondLooperBackedEventLoopTest : public ::testing::Test {
+ protected:
+  std::unique_ptr<LooperBackedEventLoop> event_loop_;
+
+  virtual void SetUp() {
+    event_loop_.reset(new LooperBackedEventLoop());
+  }
+};
+
+TEST_F(WificondLooperBackedEventLoopTest, LooperBackedEventLoopPostTaskTest) {
+  bool task_executed = false;
+  event_loop_->PostTask([this, &task_executed]() mutable {
+      task_executed = true; event_loop_->TriggerExit();});
+  EXPECT_FALSE(task_executed);
+  event_loop_->Poll();
+  EXPECT_TRUE(task_executed);
+}
+
+TEST_F(WificondLooperBackedEventLoopTest,
+       LooperBackedEventLoopPostDelayedTaskTest) {
+  bool task_executed = false;
+  event_loop_->PostDelayedTask([this, &task_executed]() mutable {
+      task_executed = true; event_loop_->TriggerExit();}, 500);
+  EXPECT_FALSE(task_executed);
+  StopWatch stopWatch("DelayedTask");
+  event_loop_->Poll();
+  int32_t elapsedMillis = ns2ms(stopWatch.elapsedTime());
+  EXPECT_NEAR(500, elapsedMillis, kTimingToleranceMs);
+  EXPECT_TRUE(task_executed);
+}
+
+}  // namespace wificond
+}  // namespace android
diff --git a/tests/wificond_unittest.cpp b/tests/wificond_unittest.cpp
deleted file mode 100644 (file)
index ce21ead..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (C) 2016, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <gtest/gtest.h>
-
-namespace android {
-namespace wificond {
-
-class WificondTest : public ::testing::Test {
-};
-
-TEST_F(WificondTest, dummyTest) {
-  EXPECT_TRUE(true);
-}
-
-}  // namespace wificond
-}  // namespace android