Bug 1573184 - Add unit tests to KeyValueStorage class. r=jya
authorAlex Chronopoulos <achronop@gmail.com>
Wed, 14 Aug 2019 10:41:16 +0000
changeset 487881 53882dede69af2fa4ea4656af80422c941058de7
parent 487880 de0b1d5a9b6d6a677533fe57fdcb31cdd3e9d818
child 487882 5cea00ec903887cfe90b843b83d93b528483ea1f
push id36433
push userbtara@mozilla.com
push dateWed, 14 Aug 2019 21:57:52 +0000
treeherdermozilla-central@7d9a2196d313 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1573184
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1573184 - Add unit tests to KeyValueStorage class. r=jya Differential Revision: https://phabricator.services.mozilla.com/D41577
dom/media/gtest/TestKeyValueStorage.cpp
dom/media/gtest/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/gtest/TestKeyValueStorage.cpp
@@ -0,0 +1,109 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/KeyValueStorage.h"
+
+#include "gmock/gmock.h"
+#include "gtest/gtest-printers.h"
+#include "gtest/gtest.h"
+
+#include "GMPTestMonitor.h"
+
+using ::testing::Return;
+using namespace mozilla;
+
+TEST(TestKeyValueStorage, BasicPutGet)
+{
+  auto kvs = MakeRefPtr<KeyValueStorage>();
+
+  nsCString name("database_name");
+  nsCString key("key1");
+  int32_t value = 100;
+
+  GMPTestMonitor mon;
+
+  kvs->Put(name, key, value)
+      ->Then(
+          GetCurrentThreadSerialEventTarget(), __func__,
+          [&](bool) { return kvs->Get(name, key); },
+          [](nsresult rv) {
+            EXPECT_TRUE(false) << "Put promise has been rejected";
+            return KeyValueStorage::GetPromise::CreateAndReject(rv, __func__);
+          })
+      ->Then(
+          GetCurrentThreadSerialEventTarget(), __func__,
+          [&](int32_t aValue) {
+            EXPECT_EQ(aValue, value) << "Values are the same";
+            mon.SetFinished();
+          },
+          [&](nsresult rv) {
+            EXPECT_TRUE(false) << "Get Promise has been rejected";
+            mon.SetFinished();
+          });
+
+  mon.AwaitFinished();
+}
+
+TEST(TestKeyValueStorage, GetNonExistedKey)
+{
+  auto kvs = MakeRefPtr<KeyValueStorage>();
+
+  nsCString name("database_name");
+  nsCString key("NonExistedKey");
+
+  GMPTestMonitor mon;
+
+  kvs->Get(name, key)->Then(
+      GetCurrentThreadSerialEventTarget(), __func__,
+      [&mon](int32_t aValue) {
+        EXPECT_EQ(aValue, -1) << "When key does not exist return -1";
+        mon.SetFinished();
+      },
+      [&mon](nsresult rv) {
+        EXPECT_TRUE(false) << "Get Promise has been rejected";
+        mon.SetFinished();
+      });
+
+  mon.AwaitFinished();
+}
+
+TEST(TestKeyValueStorage, Clear)
+{
+  auto kvs = MakeRefPtr<KeyValueStorage>();
+
+  nsCString name("database_name");
+  nsCString key("key1");
+  int32_t value = 100;
+
+  GMPTestMonitor mon;
+
+  kvs->Put(name, key, value)
+      ->Then(
+          GetCurrentThreadSerialEventTarget(), __func__,
+          [&](bool) { return kvs->Clear(name); },
+          [](nsresult rv) {
+            EXPECT_TRUE(false) << "Put promise has been rejected";
+            return GenericPromise::CreateAndReject(rv, __func__);
+          })
+      ->Then(
+          GetCurrentThreadSerialEventTarget(), __func__,
+          [&](bool) { return kvs->Get(name, key); },
+          [](nsresult rv) {
+            EXPECT_TRUE(false) << "Clear promise has been rejected";
+            return KeyValueStorage::GetPromise::CreateAndReject(rv, __func__);
+          })
+      ->Then(
+          GetCurrentThreadSerialEventTarget(), __func__,
+          [&](int32_t aValue) {
+            EXPECT_EQ(aValue, -1) << "After clear the key does not exist";
+            mon.SetFinished();
+          },
+          [&](nsresult rv) {
+            EXPECT_TRUE(false) << "Get Promise has been rejected";
+            mon.SetFinished();
+          });
+
+  mon.AwaitFinished();
+}
--- a/dom/media/gtest/moz.build
+++ b/dom/media/gtest/moz.build
@@ -26,16 +26,17 @@ UNIFIED_SOURCES += [
     'TestBitWriter.cpp',
     'TestBlankVideoDataCreator.cpp',
     'TestBufferReader.cpp',
     'TestDataMutex.cpp',
     'TestDriftCompensation.cpp',
     'TestGMPUtils.cpp',
     'TestGroupId.cpp',
     'TestIntervalSet.cpp',
+    'TestKeyValueStorage.cpp',
     'TestMediaDataDecoder.cpp',
     'TestMediaDataEncoder.cpp',
     'TestMediaEventSource.cpp',
     'TestMediaMIMETypes.cpp',
     'TestMediaSpan.cpp',
     'TestMP3Demuxer.cpp',
     'TestMP4Demuxer.cpp',
     'TestMuxer.cpp',