xpcom/tests/TestObserverService.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsISupports.h"
michael@0 7 #include "nsIComponentManager.h"
michael@0 8 #include "nsIObserverService.h"
michael@0 9 #include "nsIObserver.h"
michael@0 10 #include "nsISimpleEnumerator.h"
michael@0 11 #include "nsStringGlue.h"
michael@0 12 #include "nsWeakReference.h"
michael@0 13 #include "nsComponentManagerUtils.h"
michael@0 14 #include "mozilla/Attributes.h"
michael@0 15
michael@0 16 #include <stdio.h>
michael@0 17
michael@0 18 static nsIObserverService *anObserverService = nullptr;
michael@0 19
michael@0 20 static void testResult( nsresult rv ) {
michael@0 21 if ( NS_SUCCEEDED( rv ) ) {
michael@0 22 printf("...ok\n");
michael@0 23 } else {
michael@0 24 printf("...failed, rv=0x%x\n", (int)rv);
michael@0 25 }
michael@0 26 return;
michael@0 27 }
michael@0 28
michael@0 29 void printString(nsString &str) {
michael@0 30 printf("%s", NS_ConvertUTF16toUTF8(str).get());
michael@0 31 }
michael@0 32
michael@0 33 class TestObserver MOZ_FINAL : public nsIObserver,
michael@0 34 public nsSupportsWeakReference
michael@0 35 {
michael@0 36 public:
michael@0 37 TestObserver( const nsAString &name )
michael@0 38 : mName( name ) {
michael@0 39 }
michael@0 40 NS_DECL_ISUPPORTS
michael@0 41 NS_DECL_NSIOBSERVER
michael@0 42
michael@0 43 nsString mName;
michael@0 44
michael@0 45 private:
michael@0 46 ~TestObserver() {}
michael@0 47 };
michael@0 48
michael@0 49 NS_IMPL_ISUPPORTS( TestObserver, nsIObserver, nsISupportsWeakReference )
michael@0 50
michael@0 51 NS_IMETHODIMP
michael@0 52 TestObserver::Observe( nsISupports *aSubject,
michael@0 53 const char *aTopic,
michael@0 54 const char16_t *someData ) {
michael@0 55 nsCString topic( aTopic );
michael@0 56 nsString data( someData );
michael@0 57 /*
michael@0 58 The annoying double-cast below is to work around an annoying bug in
michael@0 59 the compiler currently used on wensleydale. This is a test.
michael@0 60 */
michael@0 61 printString(mName);
michael@0 62 printf(" has observed something: subject@%p", (void*)aSubject);
michael@0 63 printf(" name=");
michael@0 64 printString(reinterpret_cast<TestObserver*>(reinterpret_cast<void*>(aSubject))->mName);
michael@0 65 printf(" aTopic=%s", topic.get());
michael@0 66 printf(" someData=");
michael@0 67 printString(data);
michael@0 68 printf("\n");
michael@0 69 return NS_OK;
michael@0 70 }
michael@0 71
michael@0 72 int main(int argc, char *argv[])
michael@0 73 {
michael@0 74 nsCString topicA; topicA.Assign( "topic-A" );
michael@0 75 nsCString topicB; topicB.Assign( "topic-B" );
michael@0 76 nsresult rv;
michael@0 77
michael@0 78 nsresult res = CallCreateInstance("@mozilla.org/observer-service;1", &anObserverService);
michael@0 79
michael@0 80 if (res == NS_OK) {
michael@0 81
michael@0 82 nsIObserver *aObserver = new TestObserver(NS_LITERAL_STRING("Observer-A"));
michael@0 83 aObserver->AddRef();
michael@0 84 nsIObserver *bObserver = new TestObserver(NS_LITERAL_STRING("Observer-B"));
michael@0 85 bObserver->AddRef();
michael@0 86
michael@0 87 printf("Adding Observer-A as observer of topic-A...\n");
michael@0 88 rv = anObserverService->AddObserver(aObserver, topicA.get(), false);
michael@0 89 testResult(rv);
michael@0 90
michael@0 91 printf("Adding Observer-B as observer of topic-A...\n");
michael@0 92 rv = anObserverService->AddObserver(bObserver, topicA.get(), false);
michael@0 93 testResult(rv);
michael@0 94
michael@0 95 printf("Adding Observer-B as observer of topic-B...\n");
michael@0 96 rv = anObserverService->AddObserver(bObserver, topicB.get(), false);
michael@0 97 testResult(rv);
michael@0 98
michael@0 99 printf("Testing Notify(observer-A, topic-A)...\n");
michael@0 100 rv = anObserverService->NotifyObservers( aObserver,
michael@0 101 topicA.get(),
michael@0 102 MOZ_UTF16("Testing Notify(observer-A, topic-A)") );
michael@0 103 testResult(rv);
michael@0 104
michael@0 105 printf("Testing Notify(observer-B, topic-B)...\n");
michael@0 106 rv = anObserverService->NotifyObservers( bObserver,
michael@0 107 topicB.get(),
michael@0 108 MOZ_UTF16("Testing Notify(observer-B, topic-B)") );
michael@0 109 testResult(rv);
michael@0 110
michael@0 111 printf("Testing EnumerateObserverList (for topic-A)...\n");
michael@0 112 nsCOMPtr<nsISimpleEnumerator> e;
michael@0 113 rv = anObserverService->EnumerateObservers(topicA.get(), getter_AddRefs(e));
michael@0 114
michael@0 115 testResult(rv);
michael@0 116
michael@0 117 printf("Enumerating observers of topic-A...\n");
michael@0 118 if ( e ) {
michael@0 119 nsCOMPtr<nsIObserver> observer;
michael@0 120 bool loop = true;
michael@0 121 while( NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
michael@0 122 {
michael@0 123 nsCOMPtr<nsISupports> supports;
michael@0 124 e->GetNext(getter_AddRefs(supports));
michael@0 125 observer = do_QueryInterface(supports);
michael@0 126 printf("Calling observe on enumerated observer ");
michael@0 127 printString(reinterpret_cast<TestObserver*>
michael@0 128 (reinterpret_cast<void*>(observer.get()))->mName);
michael@0 129 printf("...\n");
michael@0 130 rv = observer->Observe( observer,
michael@0 131 topicA.get(),
michael@0 132 MOZ_UTF16("during enumeration") );
michael@0 133 testResult(rv);
michael@0 134 }
michael@0 135 }
michael@0 136 printf("...done enumerating observers of topic-A\n");
michael@0 137
michael@0 138 printf("Removing Observer-A...\n");
michael@0 139 rv = anObserverService->RemoveObserver(aObserver, topicA.get());
michael@0 140 testResult(rv);
michael@0 141
michael@0 142
michael@0 143 printf("Removing Observer-B (topic-A)...\n");
michael@0 144 rv = anObserverService->RemoveObserver(bObserver, topicB.get());
michael@0 145 testResult(rv);
michael@0 146 printf("Removing Observer-B (topic-B)...\n");
michael@0 147 rv = anObserverService->RemoveObserver(bObserver, topicA.get());
michael@0 148 testResult(rv);
michael@0 149
michael@0 150 }
michael@0 151 return 0;
michael@0 152 }

mercurial