Best Python code snippet using fMBT_python
test_Preferences.js
Source:test_Preferences.js
...156});157add_test(function test_observe_prefs_function() {158 let observed = false;159 let observer = function() { observed = !observed };160 Preferences.observe("test_observe_prefs_function", observer);161 Preferences.set("test_observe_prefs_function.subpref", "something");162 do_check_false(observed);163 Preferences.set("test_observe_prefs_function", "something");164 do_check_true(observed);165 Preferences.ignore("test_observe_prefs_function", observer);166 Preferences.set("test_observe_prefs_function", "something else");167 do_check_true(observed);168 // Clean up.169 Preferences.reset("test_observe_prefs_function");170 Preferences.reset("test_observe_prefs_function.subpref");171 run_next_test();172});173add_test(function test_observe_prefs_object() {174 let observer = {175 observed: false,176 observe() {177 this.observed = !this.observed;178 }179 };180 Preferences.observe("test_observe_prefs_object", observer.observe, observer);181 Preferences.set("test_observe_prefs_object.subpref", "something");182 do_check_false(observer.observed);183 Preferences.set("test_observe_prefs_object", "something");184 do_check_true(observer.observed);185 Preferences.ignore("test_observe_prefs_object", observer.observe, observer);186 Preferences.set("test_observe_prefs_object", "something else");187 do_check_true(observer.observed);188 // Clean up.189 Preferences.reset("test_observe_prefs_object");190 Preferences.reset("test_observe_prefs_object.subpref");191 run_next_test();192});193add_test(function test_observe_prefs_nsIObserver() {194 let observer = {195 observed: false,196 observe(subject, topic, data) {197 this.observed = !this.observed;198 do_check_true(subject instanceof Ci.nsIPrefBranch);199 do_check_eq(topic, "nsPref:changed");200 do_check_eq(data, "test_observe_prefs_nsIObserver");201 }202 };203 Preferences.observe("test_observe_prefs_nsIObserver", observer);204 Preferences.set("test_observe_prefs_nsIObserver.subpref", "something");205 Preferences.set("test_observe_prefs_nsIObserver", "something");206 do_check_true(observer.observed);207 Preferences.ignore("test_observe_prefs_nsIObserver", observer);208 Preferences.set("test_observe_prefs_nsIObserver", "something else");209 do_check_true(observer.observed);210 // Clean up.211 Preferences.reset("test_observe_prefs_nsIObserver");212 Preferences.reset("test_observe_prefs_nsIObserver.subpref");213 run_next_test();214});215// This should not need to be said, but *DO NOT DISABLE THIS TEST*.216//217// Existing consumers of the observer API depend on the observer only218// being triggered for the exact preference they are observing. This is219// expecially true for consumers of the function callback variant which220// passes the preference's new value but not its name.221add_test(function test_observe_exact_pref() {222 let observed = false;223 let observer = function() { observed = !observed };224 Preferences.observe("test_observe_exact_pref", observer);225 Preferences.set("test_observe_exact_pref.sub-pref", "something");226 do_check_false(observed);227 // Clean up.228 Preferences.ignore("test_observe_exact_pref", observer);229 Preferences.reset("test_observe_exact_pref.sub-pref");230 run_next_test();231});232add_test(function test_observe_value_of_set_pref() {233 let observer = function(newVal) { do_check_eq(newVal, "something") };234 Preferences.observe("test_observe_value_of_set_pref", observer);235 Preferences.set("test_observe_value_of_set_pref.subpref", "somethingelse");236 Preferences.set("test_observe_value_of_set_pref", "something");237 // Clean up.238 Preferences.ignore("test_observe_value_of_set_pref", observer);239 Preferences.reset("test_observe_value_of_set_pref");240 Preferences.reset("test_observe_value_of_set_pref.subpref");241 run_next_test();242});243add_test(function test_observe_value_of_reset_pref() {244 let observer = function(newVal) { do_check_true(typeof newVal == "undefined") };245 Preferences.set("test_observe_value_of_reset_pref", "something");246 Preferences.observe("test_observe_value_of_reset_pref", observer);247 Preferences.reset("test_observe_value_of_reset_pref");248 // Clean up.249 Preferences.ignore("test_observe_value_of_reset_pref", observer);250 run_next_test();251});252add_test(function test_has_pref() {253 do_check_false(Preferences.has("test_has_pref"));254 Preferences.set("test_has_pref", "foo");255 do_check_true(Preferences.has("test_has_pref"));256 Preferences.set("test_has_pref.foo", "foo");257 Preferences.set("test_has_pref.bar", "bar");258 let [hasFoo, hasBar, hasBaz] = Preferences.has(["test_has_pref.foo",259 "test_has_pref.bar",260 "test_has_pref.baz"]);...
observe.js
Source:observe.js
1// XXX maybe move these into another ObserveHelpers package or something2// _CachingChangeObserver is an object which receives observeChanges callbacks3// and keeps a cache of the current cursor state up to date in self.docs. Users4// of this class should read the docs field but not modify it. You should pass5// the "applyChange" field as the callbacks to the underlying observeChanges6// call. Optionally, you can specify your own observeChanges callbacks which are7// invoked immediately before the docs field is updated; this object is made8// available as `this` to those callbacks.9LocalCollection._CachingChangeObserver = function (options) {10 var self = this;11 options = options || {};12 var orderedFromCallbacks = options.callbacks &&13 LocalCollection._observeChangesCallbacksAreOrdered(options.callbacks);14 if (_.has(options, 'ordered')) {15 self.ordered = options.ordered;16 if (options.callbacks && options.ordered !== orderedFromCallbacks)17 throw Error("ordered option doesn't match callbacks");18 } else if (options.callbacks) {19 self.ordered = orderedFromCallbacks;20 } else {21 throw Error("must provide ordered or callbacks");22 }23 var callbacks = options.callbacks || {};24 if (self.ordered) {25 self.docs = new OrderedDict(LocalCollection._idStringify);26 self.applyChange = {27 addedBefore: function (id, fields, before) {28 var doc = EJSON.clone(fields);29 doc._id = id;30 callbacks.addedBefore && callbacks.addedBefore.call(31 self, id, fields, before);32 // This line triggers if we provide added with movedBefore.33 callbacks.added && callbacks.added.call(self, id, fields);34 // XXX could `before` be a falsy ID? Technically35 // idStringify seems to allow for them -- though36 // OrderedDict won't call stringify on a falsy arg.37 self.docs.putBefore(id, doc, before || null);38 },39 movedBefore: function (id, before) {40 var doc = self.docs.get(id);41 callbacks.movedBefore && callbacks.movedBefore.call(self, id, before);42 self.docs.moveBefore(id, before || null);43 }44 };45 } else {46 self.docs = new LocalCollection._IdMap;47 self.applyChange = {48 added: function (id, fields) {49 var doc = EJSON.clone(fields);50 callbacks.added && callbacks.added.call(self, id, fields);51 doc._id = id;52 self.docs.set(id, doc);53 }54 };55 }56 // The methods in _IdMap and OrderedDict used by these callbacks are57 // identical.58 self.applyChange.changed = function (id, fields) {59 var doc = self.docs.get(id);60 if (!doc)61 throw new Error("Unknown id for changed: " + id);62 callbacks.changed && callbacks.changed.call(63 self, id, EJSON.clone(fields));64 LocalCollection._applyChanges(doc, fields);65 };66 self.applyChange.removed = function (id) {67 callbacks.removed && callbacks.removed.call(self, id);68 self.docs.remove(id);69 };70};71LocalCollection._observeFromObserveChanges = function (cursor, observeCallbacks) {72 var transform = cursor.getTransform() || function (doc) {return doc;};73 var suppressed = !!observeCallbacks._suppress_initial;74 var observeChangesCallbacks;75 if (LocalCollection._observeCallbacksAreOrdered(observeCallbacks)) {76 // The "_no_indices" option sets all index arguments to -1 and skips the77 // linear scans required to generate them. This lets observers that don't78 // need absolute indices benefit from the other features of this API --79 // relative order, transforms, and applyChanges -- without the speed hit.80 var indices = !observeCallbacks._no_indices;81 observeChangesCallbacks = {82 addedBefore: function (id, fields, before) {83 var self = this;84 if (suppressed || !(observeCallbacks.addedAt || observeCallbacks.added))85 return;86 var doc = transform(_.extend(fields, {_id: id}));87 if (observeCallbacks.addedAt) {88 var index = indices89 ? (before ? self.docs.indexOf(before) : self.docs.size()) : -1;90 observeCallbacks.addedAt(doc, index, before);91 } else {92 observeCallbacks.added(doc);93 }94 },95 changed: function (id, fields) {96 var self = this;97 if (!(observeCallbacks.changedAt || observeCallbacks.changed))98 return;99 var doc = EJSON.clone(self.docs.get(id));100 if (!doc)101 throw new Error("Unknown id for changed: " + id);102 var oldDoc = transform(EJSON.clone(doc));103 LocalCollection._applyChanges(doc, fields);104 doc = transform(doc);105 if (observeCallbacks.changedAt) {106 var index = indices ? self.docs.indexOf(id) : -1;107 observeCallbacks.changedAt(doc, oldDoc, index);108 } else {109 observeCallbacks.changed(doc, oldDoc);110 }111 },112 movedBefore: function (id, before) {113 var self = this;114 if (!observeCallbacks.movedTo)115 return;116 var from = indices ? self.docs.indexOf(id) : -1;117 var to = indices118 ? (before ? self.docs.indexOf(before) : self.docs.size()) : -1;119 // When not moving backwards, adjust for the fact that removing the120 // document slides everything back one slot.121 if (to > from)122 --to;123 observeCallbacks.movedTo(transform(EJSON.clone(self.docs.get(id))),124 from, to, before || null);125 },126 removed: function (id) {127 var self = this;128 if (!(observeCallbacks.removedAt || observeCallbacks.removed))129 return;130 // technically maybe there should be an EJSON.clone here, but it's about131 // to be removed from self.docs!132 var doc = transform(self.docs.get(id));133 if (observeCallbacks.removedAt) {134 var index = indices ? self.docs.indexOf(id) : -1;135 observeCallbacks.removedAt(doc, index);136 } else {137 observeCallbacks.removed(doc);138 }139 }140 };141 } else {142 observeChangesCallbacks = {143 added: function (id, fields) {144 if (!suppressed && observeCallbacks.added) {145 var doc = _.extend(fields, {_id: id});146 observeCallbacks.added(transform(doc));147 }148 },149 changed: function (id, fields) {150 var self = this;151 if (observeCallbacks.changed) {152 var oldDoc = self.docs.get(id);153 var doc = EJSON.clone(oldDoc);154 LocalCollection._applyChanges(doc, fields);155 observeCallbacks.changed(transform(doc),156 transform(EJSON.clone(oldDoc)));157 }158 },159 removed: function (id) {160 var self = this;161 if (observeCallbacks.removed) {162 observeCallbacks.removed(transform(self.docs.get(id)));163 }164 }165 };166 }167 var changeObserver = new LocalCollection._CachingChangeObserver(168 {callbacks: observeChangesCallbacks});169 var handle = cursor.observeChanges(changeObserver.applyChange);170 suppressed = false;171 return handle;...
observableset.js
Source:observableset.js
...61 return false;62 }63 this.observers_.push(observer);64 goog.array.forEach(this.observables_, function(o) {65 o.observe(observer);66 });67 return true;68};69/**70 * Removes an observer from the set. The observer will be removed from71 * all observables in the set. Does nothing if the observer is not in72 * the set.73 * @param {!goog.labs.observe.Observer} observer The observer to remove.74 * @return {boolean} Whether the observer is actually removed.75 */76goog.labs.observe.ObservableSet.prototype.removeObserver = function(observer) {77 // Check that the observer exists before removing.78 var removed = goog.array.removeIf(this.observers_, goog.partial(79 goog.labs.observe.Observer.equals, observer));80 if (removed) {81 goog.array.forEach(this.observables_, function(o) {82 o.unobserve(observer);83 });84 }85 return removed;86};87/**88 * Removes all registered observers.89 */90goog.labs.observe.ObservableSet.prototype.removeAllObservers = function() {91 this.unregisterAll_();92 this.observers_.length = 0;93};94/**95 * Adds an observable to the set. All previously added and future96 * observers will be added to the new observable as well.97 *98 * The observable will not be added if it is already registered in the99 * set.100 *101 * @param {!goog.labs.observe.Observable} observable The observable to add.102 * @return {boolean} Whether the observable is actually added.103 */104goog.labs.observe.ObservableSet.prototype.addObservable = function(observable) {105 if (goog.array.contains(this.observables_, observable)) {106 return false;107 }108 this.observables_.push(observable);109 goog.array.forEach(this.observers_, function(observer) {110 observable.observe(observer);111 });112 return true;113};114/**115 * Removes an observable from the set. All observers registered on the116 * set will be removed from the observable as well.117 * @param {!goog.labs.observe.Observable} observable The observable to remove.118 * @return {boolean} Whether the observable is actually removed.119 */120goog.labs.observe.ObservableSet.prototype.removeObservable = function(121 observable) {122 var removed = goog.array.remove(this.observables_, observable);123 if (removed) {124 goog.array.forEach(this.observers_, function(observer) {125 observable.unobserve(observer);126 });127 }128 return removed;129};130/**131 * Removes all registered observables.132 */133goog.labs.observe.ObservableSet.prototype.removeAllObservables = function() {134 this.unregisterAll_();135 this.observables_.length = 0;136};137/**138 * Removes all registered observations and observables.139 */140goog.labs.observe.ObservableSet.prototype.removeAll = function() {141 this.removeAllObservers();142 this.observables_.length = 0;143};144/**145 * Unregisters all registered observers from all registered observables.146 * @private147 */148goog.labs.observe.ObservableSet.prototype.unregisterAll_ = function() {149 goog.array.forEach(this.observers_, function(observer) {150 goog.array.forEach(this.observables_, function(o) {151 o.unobserve(observer);152 });153 }, this);...
observationset.js
Source:observationset.js
...50 * @return {boolean} True if the observer is successfully registered.51 */52goog.labs.observe.ObservationSet.prototype.observe = function(53 observable, observer) {54 var success = observable.observe(observer);55 if (success) {56 this.storedObservations_.push(57 new goog.labs.observe.ObservationSet.Observation_(58 observable, observer));59 }60 return success;61};62/**63 * Observes the given function on the observable.64 * @param {!goog.labs.observe.Observable} observable The observable to65 * observe on.66 * @param {function(!goog.labs.observe.Notice)} fn The handler function.67 * @param {!Object=} opt_scope Optional scope.68 * @return {goog.labs.observe.Observer} The registered observer object.69 * If the observer is not successfully registered, this will be null.70 */71goog.labs.observe.ObservationSet.prototype.observeWithFunction = function(72 observable, fn, opt_scope) {73 var observer = goog.labs.observe.Observer.fromFunction(74 fn, opt_scope || this.defaultScope_);75 if (this.observe(observable, observer)) {76 return observer;77 }78 return null;79};80/**81 * Unobserves the given observer from the observable.82 * @param {!goog.labs.observe.Observable} observable The observable to83 * unobserve from.84 * @param {!goog.labs.observe.Observer} observer The observer.85 * @return {boolean} True if the observer is successfully removed.86 */87goog.labs.observe.ObservationSet.prototype.unobserve = function(88 observable, observer) {89 var removed = goog.array.removeIf(90 this.storedObservations_, function(o) {91 return o.observable == observable &&92 goog.labs.observe.Observer.equals(o.observer, observer);93 });94 if (removed) {95 observable.unobserve(observer);96 }97 return removed;98};99/**100 * Unobserves the given function from the observable.101 * @param {!goog.labs.observe.Observable} observable The observable to102 * unobserve from.103 * @param {function(!goog.labs.observe.Notice)} fn The handler function.104 * @param {!Object=} opt_scope Optional scope.105 * @return {boolean} True if the observer is successfully removed.106 */107goog.labs.observe.ObservationSet.prototype.unobserveWithFunction = function(108 observable, fn, opt_scope) {109 var observer = goog.labs.observe.Observer.fromFunction(110 fn, opt_scope || this.defaultScope_);111 return this.unobserve(observable, observer);112};113/**114 * Removes all observations registered through this set.115 */116goog.labs.observe.ObservationSet.prototype.removeAll = function() {117 goog.array.forEach(this.storedObservations_, function(observation) {118 var observable = observation.observable;119 var observer = observation.observer;120 observable.unobserve(observer);121 });122};123/**124 * A representation of an observation, which is defined uniquely by125 * the observable and observer.126 * @param {!goog.labs.observe.Observable} observable The observable.127 * @param {!goog.labs.observe.Observer} observer The observer.128 * @constructor129 * @private130 */131goog.labs.observe.ObservationSet.Observation_ = function(132 observable, observer) {133 this.observable = observable;134 this.observer = observer;...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!