media/mtransport/test/runnable_utils_unittest.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/test/runnable_utils_unittest.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,239 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Original author: ekr@rtfm.com
    1.11 +#include <iostream>
    1.12 +
    1.13 +#include "prio.h"
    1.14 +
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsNetCID.h"
    1.17 +#include "nsXPCOM.h"
    1.18 +#include "nsXPCOMGlue.h"
    1.19 +
    1.20 +#include "mozilla/RefPtr.h"
    1.21 +#include "nsIComponentManager.h"
    1.22 +#include "nsIComponentRegistrar.h"
    1.23 +#include "nsIIOService.h"
    1.24 +#include "nsIServiceManager.h"
    1.25 +#include "nsISocketTransportService.h"
    1.26 +
    1.27 +#include "nsASocketHandler.h"
    1.28 +#include "nsServiceManagerUtils.h"
    1.29 +#include "nsThreadUtils.h"
    1.30 +
    1.31 +#include "runnable_utils.h"
    1.32 +#include "mtransport_test_utils.h"
    1.33 +
    1.34 +#define GTEST_HAS_RTTI 0
    1.35 +#include "gtest/gtest.h"
    1.36 +#include "gtest_utils.h"
    1.37 +
    1.38 +using namespace mozilla;
    1.39 +MtransportTestUtils *test_utils;
    1.40 +
    1.41 +namespace {
    1.42 +
    1.43 +class Destructor {
    1.44 + public:
    1.45 +  Destructor(bool* destroyed) : destroyed_(destroyed) {}
    1.46 +  ~Destructor() {
    1.47 +    std::cerr << "Destructor called" << std::endl;
    1.48 +    *destroyed_ = true;
    1.49 +  }
    1.50 +
    1.51 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Destructor)
    1.52 +
    1.53 + private:
    1.54 +  bool *destroyed_;
    1.55 +};
    1.56 +
    1.57 +class TargetClass {
    1.58 + public:
    1.59 +  TargetClass(int *ran) : ran_(ran) {}
    1.60 +
    1.61 +  void m1(int x) {
    1.62 +    std::cerr << __FUNCTION__ << " " << x << std::endl;
    1.63 +    *ran_ = 1;
    1.64 +  }
    1.65 +
    1.66 +  void m2(int x, int y) {
    1.67 +    std::cerr << __FUNCTION__ << " " << x << " " << y << std::endl;
    1.68 +    *ran_ = 2;
    1.69 +  }
    1.70 +
    1.71 +  void m1set(bool *z) {
    1.72 +    std::cerr << __FUNCTION__ << std::endl;
    1.73 +    *z = true;
    1.74 +  }
    1.75 +  int return_int(int x) {
    1.76 +    std::cerr << __FUNCTION__ << std::endl;
    1.77 +    return x;
    1.78 +  }
    1.79 +  void destructor_target(Destructor*) {
    1.80 +  }
    1.81 +
    1.82 +  void destructor_target_ref(RefPtr<Destructor> destructor) {
    1.83 +  }
    1.84 +
    1.85 +  int *ran_;
    1.86 +};
    1.87 +
    1.88 +
    1.89 +class RunnableArgsTest : public ::testing::Test {
    1.90 + public:
    1.91 +  RunnableArgsTest() : ran_(0), cl_(&ran_){}
    1.92 +
    1.93 +  void Test1Arg() {
    1.94 +    nsRunnable * r = WrapRunnable(&cl_, &TargetClass::m1, 1);
    1.95 +    r->Run();
    1.96 +    ASSERT_EQ(1, ran_);
    1.97 +  }
    1.98 +
    1.99 +  void Test2Args() {
   1.100 +    nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
   1.101 +    r->Run();
   1.102 +    ASSERT_EQ(2, ran_);
   1.103 +  }
   1.104 +
   1.105 + private:
   1.106 +  int ran_;
   1.107 +  TargetClass cl_;
   1.108 +};
   1.109 +
   1.110 +class DispatchTest : public ::testing::Test {
   1.111 + public:
   1.112 +  DispatchTest() : ran_(0), cl_(&ran_) {}
   1.113 +
   1.114 +  void SetUp() {
   1.115 +    nsresult rv;
   1.116 +    target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
   1.117 +    ASSERT_TRUE(NS_SUCCEEDED(rv));
   1.118 +  }
   1.119 +
   1.120 +  void Test1Arg() {
   1.121 +    nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m1, 1);
   1.122 +    target_->Dispatch(r, NS_DISPATCH_SYNC);
   1.123 +    ASSERT_EQ(1, ran_);
   1.124 +  }
   1.125 +
   1.126 +  void Test2Args() {
   1.127 +    nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
   1.128 +    target_->Dispatch(r, NS_DISPATCH_SYNC);
   1.129 +    ASSERT_EQ(2, ran_);
   1.130 +  }
   1.131 +
   1.132 +  void Test1Set() {
   1.133 +    bool x = false;
   1.134 +    target_->Dispatch(WrapRunnable(&cl_, &TargetClass::m1set, &x),
   1.135 +                      NS_DISPATCH_SYNC);
   1.136 +    ASSERT_TRUE(x);
   1.137 +  }
   1.138 +
   1.139 +  void TestRet() {
   1.140 +    int z;
   1.141 +    int x = 10;
   1.142 +
   1.143 +    target_->Dispatch(WrapRunnableRet(&cl_, &TargetClass::return_int, x, &z),
   1.144 +                      NS_DISPATCH_SYNC);
   1.145 +    ASSERT_EQ(10, z);
   1.146 +  }
   1.147 +
   1.148 + protected:
   1.149 +  int ran_;
   1.150 +  TargetClass cl_;
   1.151 +  nsCOMPtr<nsIEventTarget> target_;
   1.152 +};
   1.153 +
   1.154 +
   1.155 +TEST_F(RunnableArgsTest, OneArgument) {
   1.156 +  Test1Arg();
   1.157 +}
   1.158 +
   1.159 +TEST_F(RunnableArgsTest, TwoArguments) {
   1.160 +  Test2Args();
   1.161 +}
   1.162 +
   1.163 +TEST_F(DispatchTest, OneArgument) {
   1.164 +  Test1Arg();
   1.165 +}
   1.166 +
   1.167 +TEST_F(DispatchTest, TwoArguments) {
   1.168 +  Test2Args();
   1.169 +}
   1.170 +
   1.171 +TEST_F(DispatchTest, Test1Set) {
   1.172 +  Test1Set();
   1.173 +}
   1.174 +
   1.175 +TEST_F(DispatchTest, TestRet) {
   1.176 +  TestRet();
   1.177 +}
   1.178 +
   1.179 +void SetNonMethod(TargetClass *cl, int x) {
   1.180 +  cl->m1(x);
   1.181 +}
   1.182 +
   1.183 +int SetNonMethodRet(TargetClass *cl, int x) {
   1.184 +  cl->m1(x);
   1.185 +
   1.186 +  return x;
   1.187 +}
   1.188 +
   1.189 +TEST_F(DispatchTest, TestNonMethod) {
   1.190 +  test_utils->sts_target()->Dispatch(
   1.191 +      WrapRunnableNM(SetNonMethod, &cl_, 10), NS_DISPATCH_SYNC);
   1.192 +
   1.193 +  ASSERT_EQ(1, ran_);
   1.194 +}
   1.195 +
   1.196 +TEST_F(DispatchTest, TestNonMethodRet) {
   1.197 +  int z;
   1.198 +
   1.199 +  test_utils->sts_target()->Dispatch(
   1.200 +      WrapRunnableNMRet(SetNonMethodRet, &cl_, 10, &z), NS_DISPATCH_SYNC);
   1.201 +
   1.202 +  ASSERT_EQ(1, ran_);
   1.203 +  ASSERT_EQ(10, z);
   1.204 +}
   1.205 +
   1.206 +TEST_F(DispatchTest, TestDestructor) {
   1.207 +  bool destroyed = false;
   1.208 +  RefPtr<Destructor> destructor = new Destructor(&destroyed);
   1.209 +  target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target,
   1.210 +                                 destructor),
   1.211 +                    NS_DISPATCH_SYNC);
   1.212 +  ASSERT_FALSE(destroyed);
   1.213 +  destructor = nullptr;
   1.214 +  ASSERT_TRUE(destroyed);
   1.215 +}
   1.216 +
   1.217 +TEST_F(DispatchTest, TestDestructorRef) {
   1.218 +  bool destroyed = false;
   1.219 +  RefPtr<Destructor> destructor = new Destructor(&destroyed);
   1.220 +  target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target_ref,
   1.221 +                                 destructor),
   1.222 +                    NS_DISPATCH_SYNC);
   1.223 +  ASSERT_FALSE(destroyed);
   1.224 +  destructor = nullptr;
   1.225 +  ASSERT_TRUE(destroyed);
   1.226 +}
   1.227 +
   1.228 +
   1.229 +} // end of namespace
   1.230 +
   1.231 +
   1.232 +int main(int argc, char **argv) {
   1.233 +  test_utils = new MtransportTestUtils();
   1.234 +
   1.235 +  // Start the tests
   1.236 +  ::testing::InitGoogleTest(&argc, argv);
   1.237 +
   1.238 +  int rv = RUN_ALL_TESTS();
   1.239 +  delete test_utils;
   1.240 +  return rv;
   1.241 +}
   1.242 +

mercurial