xpcom/tests/gtest/TestStateWatching.cpp
author Ben Kelly <ben@wanderview.com>
Mon, 12 Sep 2016 12:32:20 -0700
changeset 313586 60e82c1780e52d14e2f6d7c44907803d33943674
parent 313584 f2fd5c60f2b6591314e16faa01f03cd18455d41a
child 313622 79d2f549c2889aea13d2837844e883ad69c7bc78
permissions -rw-r--r--
Bug 1300118 P1 Make TaskQueue deliver runnables to nsIEventTarget. r=bholley

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "gtest/gtest.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/StateWatching.h"
#include "mozilla/TaskQueue.h"
#include "nsISupportsImpl.h"
#include "VideoUtils.h"

namespace TestStateWatching {

using namespace mozilla;

struct Foo {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Foo)
  void Notify() { mNotified = true; }
  bool mNotified = false;
private:
  ~Foo() {}
};

TEST(WatchManager, Shutdown)
{
  RefPtr<TaskQueue> queue = new TaskQueue(
    GetMediaThreadPool(MediaThreadType::PLAYBACK));

  RefPtr<Foo> p = new Foo;
  WatchManager<Foo> manager(p, queue);
  Watchable<bool> notifier(false, "notifier");

  queue->Dispatch(NS_NewRunnableFunction([&] () {
    manager.Watch(notifier, &Foo::Notify);
    notifier = true; // Trigger the call to Foo::Notify().
    manager.Shutdown(); // Shutdown() should cancel the call.
  }));

  queue->BeginShutdown();
  queue->AwaitShutdownAndIdle();
  EXPECT_FALSE(p->mNotified);
}

} // namespace TestStateWatching