Best Python code snippet using molecule_python
roles_test.js
Source:roles_test.js  
1"use strict";2var config   = require("config");3var expect   = require("chai").expect;4var NCMB = require("../lib/ncmb");5describe("NCMB Role", function(){6  var ncmb = null;7  var userName1 = "Yamada Tarou";8  var userName2 = "Yamada Hanako";9  var userPassword1 = "password";10  var userPassword2 = "1234";11  before(function(){12    ncmb = new NCMB(config.apikey, config.clientkey);13    if(config.apiserver){14      ncmb.set("protocol", config.apiserver.protocol)15          .set("fqdn", config.apiserver.fqdn)16          .set("port", config.apiserver.port)17          .set("proxy", config.apiserver.proxy || "")18          .set("stub", config.apiserver.stub);19    }20    if(!ncmb.stub){21      userName1 = "roleUser1";22      userName1 = "roleUser2";23    }24  });25  var add_user_id     = null;26  var add_role_id     = null;27  var belong_user1_id = null;28  var belong_user2_id = null;29  var belong_role1_id = null;30  var belong_role2_id = null;31  var callback_id     = null;32  var promise_id      = null;33  describe("ãã¼ã«ç»é²", function(){34    describe("save", function(){35      it("ãã¼ã«åãæå®ãããç»é²ã«å¤±æ", function(done){36        expect(function(){37          new ncmb.Role();38        }).to.throw(Error);39        expect(function(){40          new ncmb.Role({});41        }).to.throw(Error);42        expect(function(){43          new ncmb.Role(undefined);44        }).to.throw(Error);45        expect(function(){46          new ncmb.Role(null);47        }).to.throw(Error);48        expect(function(){49          new ncmb.Role("");50        }).to.throw(Error);51        done();52      });53      context("åå¨ããªããã¼ã«åãæå®ããç»é²ã«æå", function(){54        var newRole = null;55        it("callback ã§åå¾ã§ãã", function(done){56          if(ncmb.stub){57            newRole = new ncmb.Role("new_role_name");58          }else{59            newRole = new ncmb.Role("new_role_name_callback");60          }61          newRole.save(function(err, obj){62            if(err){63              done(err);64            }else{65              expect(obj.objectId).to.exist;66              callback_id = obj.objectId;67              done();68            }69          });70        });71        it("promise ã§åå¾ã§ãã", function(done){72          if(ncmb.stub){73            newRole = new ncmb.Role("new_role_name");74          }else{75            newRole = new ncmb.Role("new_role_name_promise");76          }77          newRole.save()78              .then(function(obj){79                expect(obj.objectId).to.exist;80                promise_id = obj.objectId;81                done();82              })83              .catch(function(err){84                done(err);85              });86        });87      });88      context("åå¨ãããã¼ã«åãæå®ããç»é²ã«å¤±æ", function(){89        var newRole = null;90        var existRole = null;91        before(function(done){92          newRole   = new ncmb.Role("new_exist_role_name");93          existRole = new ncmb.Role("new_exist_role_name");94          if(!ncmb.stub){95            newRole.save()96                   .then(function(){97                     done();98                   })99                   .catch(function(){100                     done(new Error("åå¦çã«å¤±æãã¾ããã"));101                   });102          }else{103            done();104          }105        });106        it("callback ã§åå¾ã§ãã", function(done){107          existRole.save(function(err, obj){108            if(err){109              expect(err).to.be.an.instanceof(Error);110              done();111            }else{112              done(new Error("失æãã¹ã"));113            }114          });115        });116        it("promise ã§åå¾ã§ãã", function(done){117          existRole.save()118              .then(function(obj){119                done(new Error("失æãã¹ã"));120              })121              .catch(function(err){122                expect(err).to.be.an.instanceof(Error);123                done();124              });125        });126      });127    });128  });129  describe("ãã¼ã«æ´æ°", function(){130    describe("update", function(){131      context("åå¨ãããã¼ã«IDãæå®ããæ´æ°ã«æå", function(done){132        var updateRole = null;133        before(function(done){134          updateRole = new ncmb.Role("updated_role_name");135          if(!ncmb.stub){136            updateRole.save()137                      .then(function(obj){138                        expect(obj.objectId).to.exist;139                        done();140                      })141                      .catch(function(){142                        done(new Error("åå¦çã«å¤±æãã¾ããã"));143                      });144          }else{145            updateRole.objectId = "update_role_id"146            done();147          }148        });149        it("callback ã§åå¾ã§ãã", function(done){150          updateRole.update(function(err, obj){151            if(err){152              done(err);153            }else{154              expect(obj.updateDate).to.exist;155              done();156            }157          });158        });159        it("promise ã§åå¾ã§ãã", function(done){160          updateRole.update()161              .then(function(obj){162                expect(obj.updateDate).to.exist;163                done();164              })165              .catch(function(err){166                done(err);167              });168        });169      });170      context("åå¨ããªããã¼ã«IDãæå®ããæ´æ°ã«å¤±æ", function(){171        var noExistRole = null;172        before(function(){173          noExistRole = new ncmb.Role("updated_role_name", {objectId:"no_exist_role_id"});174        });175        it("callback ã§åå¾ã§ãã", function(done){176          noExistRole.update(function(err, obj){177            if(err){178              expect(err.code).to.be.eql('E404001');179              done();180            }else{181              done(new Error("error ãè¿ãããªããã°ãªããªã"));182            }183          });184        });185        it("promise ã§åå¾ã§ãã", function(done){186          noExistRole.update()187              .then(function(obj){188                done(new Error("error ãè¿ãããªããã°ãªããªã"));189              })190              .catch(function(err){191                expect(err).to.be.an.instanceof(Error);192                done();193              });194        });195      });196      context("objectIdããªãå ´åãæ´æ°ã«å¤±æ", function(){197        var noExistRole = null;198        before(function(){199          noExistRole = new ncmb.Role("updated_role_name");200        });201        it("callback ã§åå¾ã§ãã", function(done){202          noExistRole.update(function(err, obj){203            if(err){204              expect(err).to.be.an.instanceof(Error);205              done();206            }else{207              done(new Error("error ãè¿ãããªããã°ãªããªã"));208            }209          });210        });211        it("promise ã§åå¾ã§ãã", function(done){212          noExistRole.update()213              .then(function(obj){214                done(new Error("error ãè¿ãããªããã°ãªããªã"));215              })216              .catch(function(err){217                expect(err).to.be.an.instanceof(Error);218                done();219              });220        });221      });222    });223  });224  describe("ãã¼ã«åé¤", function(){225    describe("delete", function(){226      var roleName = "deleted_role_name";227      context("åå¨ãããã¼ã«åãæå®ããåé¤ã«æåãã¦", function(done){228        var deleteRole = null;229        beforeEach(function(done){230          deleteRole = new ncmb.Role(roleName);231          if(ncmb.stub){232            deleteRole.objectId = "delete_role_id";233            done();234          }else{235            deleteRole.save()236                      .then(function(obj){237                        expect(obj.objectId).to.exist;238                        done();239                      })240                      .catch(function(){241                        done(new Error("åå¦çã«å¤±æãã¾ããã"));242                      });243          }244        });245        it("callback ã§åå¾ã§ãã", function(done){246          deleteRole.delete(function(err, obj){247            if(err){248              done(err);249            }else{250              done();251            }252          });253        });254        it("promise ã§åå¾ã§ãã", function(done){255          deleteRole.delete()256              .then(function(){257                done();258              })259              .catch(function(err){260                done(err);261              });262        });263      });264      context("åå¨ããªãIDãæå®ããåé¤ã«å¤±æãã¦", function(){265        var noExistRole = null;266        before(function(){267          noExistRole = new ncmb.Role(roleName, {objectId: "no_exist_role_id"});268        });269        it("callback ã§åå¾ã§ãã", function(done){270          noExistRole.delete(function(err, obj){271            if(err){272              expect(err).to.be.an.instanceof(Error);273              done();274            }else{275              done(new Error("error ãè¿ãããªããã°ãªããªã"));276            }277          });278        });279        it("promise ã§åå¾ã§ãã", function(done){280          noExistRole.delete()281              .then(function(obj){282                done(new Error("error ãè¿ãããªããã°ãªããªã"));283              })284              .catch(function(err){285                expect(err).to.be.an.instanceof(Error);286                done();287              });288        });289      });290      context("objectIdãè¨å®ããã¦ããªãã¨ããåé¤ã«å¤±æãã¦", function(){291        var noExistRole = null;292        before(function(){293          noExistRole = new ncmb.Role(roleName);294        });295        it("callback ã§åé¤ã¨ã©ã¼ãåå¾ã§ãã", function(done){296          noExistRole.delete(function(err, obj){297            if(err){298              expect(err).to.be.an.instanceof(Error);299              done();300            }else{301              done(new Error("error ãè¿ãããªããã°ãªããªã"));302            }303          });304        });305        it("promise ã§åé¤ã¨ã©ã¼ãåå¾ã§ãã", function(done){306          noExistRole.delete()307              .then(function(obj){308                done(new Error("error ãè¿ãããªããã°ãªããªã"));309              })310              .catch(function(err){311                expect(err).to.be.an.instanceof(Error);312                done();313              });314        });315      });316    });317  });318  describe("åä¼å¡ã®è¿½å ", function(){319    var role = null;320    describe("addUser", function(){321      var user = null;322      var user2 = null;323      before(function(done){324        user = new ncmb.User({userName:userName1, password:userPassword1});325        user2 = new ncmb.User({userName:userName2, password:userPassword2});326        if(!ncmb.stub){327          var acl = new ncmb.Acl();328          acl.setPublicReadAccess(true);329          user.set("acl", acl);330          user2.set("acl", acl);331        }332        user.save()333            .then(function(obj){334              expect(obj.objectId).to.exist;335              belong_user1_id = obj.objectId;336              return user2.save();337            })338            .then(function(obj){339              expect(obj.objectId).to.exist;340              belong_user2_id = obj.objectId;341              done();342            })343            .catch(function(){344              done(new Error("åå¦çã«å¤±æãã¾ããã"));345            });346      })347      context("追å ããã¦ã¼ã¶ãæå®ãã¦ç»é²ããçµæãåå¾ãã", function(){348        it("callback ã§åå¾ã§ãã", function(done){349          if(ncmb.stub){350            role = new ncmb.Role("mainRole");351          }else{352            role = new ncmb.Role("addUserCallback");353          }354          role.addUser(user)355              .save(function(err, obj){356                if(err){357                  done(err);358                }else{359                  expect(obj.objectId).to.exist;360                  add_user_id = obj.objectId;361                  ncmb.User362                      .relatedTo(obj, "belongUser")363                      .fetchAll()364                      .then(function(obj){365                        expect(obj.length).to.be.eql(1);366                        expect(obj[0].userName).to.be.eql(user.userName);367                        done();368                      })369                      .catch(function(err){370                        done(err);371                      });372                }373              });374        });375        it("promise ã§åå¾ã§ãã", function(done){376          if(ncmb.stub){377            role = new ncmb.Role("mainRole");378          }else{379            role = new ncmb.Role("addUserPromise");380          }381          role.addUser(user)382              .save()383              .then(function(obj){384                expect(obj.objectId).to.exist;385                return ncmb.User386                           .relatedTo(obj, "belongUser")387                           .fetchAll();388              })389              .then(function(obj){390                expect(obj.length).to.be.eql(1);391                expect(obj[0].userName).to.be.eql(user.userName);392                done();393              })394              .catch(function(err){395                done(err);396              });397        });398      });399      context("追å ããã¦ã¼ã¶ãé
åã§æå®ãã¦ç»é²ããçµæãåå¾ãã", function(){400        it("callback ã§åå¾ã§ãã", function(done){401          if(ncmb.stub){402            role = new ncmb.Role("mainRole");403          }else{404            role = new ncmb.Role("addUserArrayallback");405          }406          role.addUser([user,user2])407              .save(function(err, obj){408                if(err){409                  done(err);410                }else{411                  expect(obj.objectId).to.exist;412                  ncmb.User413                      .relatedTo(obj, "belongUser")414                      .fetchAll()415                      .then(function(obj){416                        expect(obj.length).to.be.eql(2);417                        done();418                      })419                      .catch(function(err){420                        done(err);421                      });422                }423              });424        });425        it("promise ã§åå¾ã§ãã", function(done){426          if(ncmb.stub){427            role = new ncmb.Role("mainRole");428          }else{429            role = new ncmb.Role("addUserArrayPromise");430          }431          role.addUser([user,user2])432              .save()433              .then(function(obj){434                expect(obj.objectId).to.exist;435                return ncmb.User436                           .relatedTo(obj, "belongUser")437                           .fetchAll();438              })439              .then(function(obj){440                expect(obj.length).to.be.eql(2);441                done();442              })443              .catch(function(err){444                done(err);445              });446        });447      });448      context("追å ããã¦ã¼ã¶ãé£ç¶ã§æå®ãã¦ç»é²ããçµæãåå¾ãã", function(){449        it("callback ã§åå¾ã§ãã", function(done){450          if(ncmb.stub){451            role = new ncmb.Role("mainRole");452          }else{453            role = new ncmb.Role("addMultiUserCallback");454          }455          role.addUser(user)456              .addUser(user2)457              .save(function(err, obj){458                if(err){459                  done(err);460                }else{461                  expect(obj.objectId).to.exist;462                  ncmb.User463                      .relatedTo(obj, "belongUser")464                      .fetchAll()465                      .then(function(obj){466                        expect(obj.length).to.be.eql(2);467                        done();468                      })469                      .catch(function(err){470                        done(err);471                      });472                }473              });474        });475        it("promise ã§åå¾ã§ãã", function(done){476          if(ncmb.stub){477            role = new ncmb.Role("mainRole");478          }else{479            role = new ncmb.Role("addMultiUserPromise");480          }481          role.addUser(user)482              .addUser(user2)483              .save()484              .then(function(obj){485                expect(obj.objectId).to.exist;486                return ncmb.User487                           .relatedTo(obj, "belongUser")488                           .fetchAll();489              })490              .then(function(obj){491                expect(obj.length).to.be.eql(2);492                done();493              })494              .catch(function(err){495                done(err);496              });497        });498      });499    });500  });501  describe("åãã¼ã«ã®è¿½å ", function(){502    var role = null;503    var subrole = null;504    var subrole2 = null;505    describe("addRole", function(){506      before(function(done){507        subrole = new ncmb.Role("subRole");508        subrole2 = new ncmb.Role("subRole2");509        subrole.save()510               .then(function(obj){511                 expect(obj.objectId).to.exist;512                 belong_role1_id = obj.objectId;513                 return subrole2.save();514               })515               .then(function(obj){516                 expect(obj.objectId).to.exist;517                 belong_role2_id = obj.objectId;518                 done();519               })520               .catch(function(){521                 done(new Error("åå¦çã«å¤±æãã¾ããã"));522               });523      });524      context("追å ãããã¼ã«ãæå®ãã¦ç»é²ããçµæãåå¾ãã", function(){525        it("callback ã§åå¾ã§ãã", function(done){526          if(ncmb.stub){527            role = new ncmb.Role("mainRole");528          }else{529            role = new ncmb.Role("addRoleCallback");530          }531          role.addRole(subrole)532              .save(function(err, obj){533                if(err){534                  done(err);535                }else{536                  expect(obj.objectId).to.exist;537                  add_role_id = obj.objectId;538                  ncmb.Role539                  .relatedTo(obj, "belongRole")540                  .fetchAll()541                  .then(function(obj){542                    expect(obj.length).to.be.eql(1);543                    expect(obj[0].roleName).to.be.eql(subrole.roleName);544                    done()545                  })546                  .catch(function(err){547                    done(err);548                  });549                }550              });551        });552        it("promise ã§åå¾ã§ãã", function(done){553          if(ncmb.stub){554            role = new ncmb.Role("mainRole");555          }else{556            role = new ncmb.Role("addRolePromise");557          }558          role.addRole(subrole)559              .save()560              .then(function(obj){561                expect(obj.objectId).to.exist;562                return ncmb.Role563                           .relatedTo(obj, "belongRole")564                           .fetchAll();565              })566              .then(function(obj){567                expect(obj.length).to.be.eql(1);568                expect(obj[0].roleName).to.be.eql(subrole.roleName);569                done()570              })571              .catch(function(err){572                done(err);573              });574        });575      });576      context("追å ããåãã¼ã«ãé
åã§æå®ãã¦ç»é²ããçµæãåå¾ãã", function(){577        it("callback ã§åå¾ã§ãã", function(done){578          if(ncmb.stub){579            role = new ncmb.Role("mainRole");580          }else{581            role = new ncmb.Role("addRoleArrayCallback");582          }583          role.addRole([subrole,subrole2])584              .save(function(err, obj){585                if(err){586                  done(err);587                }else{588                  expect(obj.objectId).to.exist;589                  ncmb.Role590                      .relatedTo(obj, "belongRole")591                      .fetchAll()592                      .then(function(obj){593                        expect(obj.length).to.be.eql(2);594                        done();595                      })596                      .catch(function(err){597                        done(err);598                      });599                }600              });601        });602        it("promise ã§åå¾ã§ãã", function(done){603          if(ncmb.stub){604            role = new ncmb.Role("mainRole");605          }else{606            role = new ncmb.Role("addRoleArrayPromise");607          }608          role.addRole([subrole,subrole2])609              .save()610              .then(function(obj){611                expect(obj.objectId).to.exist;612                return ncmb.Role613                           .relatedTo(obj, "belongRole")614                           .fetchAll();615              })616              .then(function(obj){617                expect(obj.length).to.be.eql(2);618                done();619              })620              .catch(function(err){621                done(err);622              });623        });624      });625      context("追å ããåãã¼ã«ãé£ç¶ã§æå®ãã¦ç»é²ããçµæãåå¾ãã", function(){626        it("callback ã§åå¾ã§ãã", function(done){627          if(ncmb.stub){628            role = new ncmb.Role("mainRole");629          }else{630            role = new ncmb.Role("addMultiRoleCallback");631          }632          role.addRole(subrole)633              .addRole(subrole2)634              .save(function(err, obj){635                if(err){636                  done(err);637                }else{638                  expect(obj.objectId).to.exist;639                  ncmb.Role640                      .relatedTo(obj, "belongRole")641                      .fetchAll()642                      .then(function(obj){643                        expect(obj.length).to.be.eql(2);644                        done();645                      })646                      .catch(function(err){647                        done(err);648                      });649                }650              });651        });652        it("promise ã§åå¾ã§ãã", function(done){653          if(ncmb.stub){654            role = new ncmb.Role("mainRole");655          }else{656            role = new ncmb.Role("addMultiRolePromise");657          }658          role.addRole(subrole)659              .addRole(subrole2)660              .save()661              .then(function(obj){662                expect(obj.objectId).to.exist;663                return ncmb.Role664                           .relatedTo(obj, "belongRole")665                           .fetchAll();666              })667              .then(function(obj){668                expect(obj.length).to.be.eql(2);669                done();670              })671              .catch(function(err){672                done(err);673              });674        });675      });676    });677  });678  describe("åä¼å¡ã®åå¾", function(){679    context("fetchUser", function(){680      context("ãã¼ã«ãæã¤åä¼å¡ãæ¤ç´¢ããçµæã", function(){681        var role = null;682        before(function(){683          role = new ncmb.Role("mainRole");684          if(ncmb.stub){685            role.objectId = "role_id";686          }else{687            role.objectId = add_user_id;688          }689        });690        it("callback ã§åå¾ã§ãã", function(done){691          role.fetchUser(function(err, obj){692                if(err){693                  done(err);694                }else{695                  expect(obj.length).to.be.eql(1);696                  done();697                }698              });699        });700        it("promise ã§åå¾ã§ãã", function(done){701          role.fetchUser()702              .then(function(obj){703                expect(obj.length).to.be.eql(1);704                done();705              })706              .catch(function(err){707                done(err);708              });709        });710      });711    });712  });713  describe("åãã¼ã«ã®åå¾", function(){714    context("fetchRole", function(){715      context("ãã¼ã«ãæã¤åãã¼ã«ãæ¤ç´¢ããçµæã", function(){716        var role = null;717        before(function(){718          role = new ncmb.Role("mainRole");719          if(ncmb.stub){720            role.objectId = "role_id";721          }else{722            role.objectId = add_role_id;723          }724        });725        it("callback ã§åå¾ã§ãã", function(done){726          role.fetchRole(function(err, obj){727                if(err){728                  done(err);729                }else{730                  expect(obj.length).to.be.eql(1);731                  done();732                }733              });734        });735        it("promise ã§åå¾ã§ãã", function(done){736          role.fetchRole()737              .then(function(obj){738                expect(obj.length).to.be.eql(1);739                done();740              })741              .catch(function(err){742                done(err);743              });744        });745      });746    });747  });748  describe("åä¼å¡ã®åé¤", function(){749    var role = null;750    describe("removeUser", function(){751      context("åé¤ããã¦ã¼ã¶ãæå®ãã¦æ´æ°ããçµæãåå¾ãã", function(){752        var user = null;753        before(function(){754          user = new ncmb.User({userName:userName1, password:userPassword1});755          user.objectId = belong_user1_id;756        });757        it("callback ã§åå¾ã§ãã", function(done){758          if(ncmb.stub){759            role = new ncmb.Role("mainRole");760          }else{761            role = new ncmb.Role("removeUserCallback");762          }763          role.addUser(user)764              .save()765              .then(function(obj){766                obj.removeUser(user)767                   .update(function(err, obj){768                     if(err){769                       done(err);770                     }else{771                       expect(obj.updateDate).to.exist;772                       done();773                     }774                   });775              })776              .catch(function(err){777                done(err);778              });779        });780        it("promise ã§åå¾ã§ãã", function(done){781          if(ncmb.stub){782            role = new ncmb.Role("mainRole");783          }else{784            role = new ncmb.Role("removeUserPromise");785          }786          role.addUser(user)787              .save()788              .then(function(obj){789                obj.removeUser(user);790                return obj.update();791              })792              .then(function(obj){793                expect(obj.updateDate).to.exist;794                done();795              })796              .catch(function(err){797                done(err);798              });799        });800      });801      context("åé¤ããã¦ã¼ã¶ãé
åã§æå®ãã¦æ´æ°ããçµæãåå¾ãã", function(){802        var user = null;803        var user2 = null;804        before(function(){805          user = new ncmb.User({userName:userName1, password:userPassword1});806          user2 = new ncmb.User({userName:userName2, password:userPassword2});807          if(!ncmb.stub){808            user.objectId = belong_user1_id;809            user2.objectId = belong_user2_id;810          }811        });812        it("callback ã§åå¾ã§ãã", function(done){813          if(ncmb.stub){814            role = new ncmb.Role("mainRole");815          }else{816            role = new ncmb.Role("removeUserArrayCallback");817          }818          role.addUser([user,user2])819              .save()820              .then(function(obj){821                obj.removeUser([user,user2])822                   .update(function(err, obj){823                     if(err){824                       done(err);825                     }else{826                       expect(obj.updateDate).to.exist;827                       done();828                     }829                   });830              })831              .catch(function(err){832                done(err);833              });834        });835        it("promise ã§åå¾ã§ãã", function(done){836          if(ncmb.stub){837            role = new ncmb.Role("mainRole");838          }else{839            role = new ncmb.Role("removeUserArrayPromise");840          }841          role.addUser([user,user2])842              .save()843              .then(function(obj){844                obj.removeUser([user,user2]);845                return obj.update();846              })847              .then(function(obj){848                expect(obj.updateDate).to.exist;849                done();850              })851              .catch(function(err){852                done(err);853              });854        });855      });856      context("åé¤ããã¦ã¼ã¶ãé£ç¶ã§æå®ãã¦æ´æ°ããçµæãåå¾ãã", function(){857        var user = null;858        var user2 = null;859        before(function(){860          user = new ncmb.User({userName:userName1, password:userPassword1});861          user2 = new ncmb.User({userName:userName2, password:userPassword2});862          user.objectId = belong_user1_id;863          user2.objectId = belong_user2_id;864        });865        it("callback ã§åå¾ã§ãã", function(done){866          if(ncmb.stub){867            role = new ncmb.Role("mainRole");868          }else{869            role = new ncmb.Role("removeMultiUserCallback");870          }871          role.addUser([user,user2])872              .save()873              .then(function(obj){874                obj.removeUser(user)875                   .removeUser(user2)876                   .update(function(err, obj){877                     if(err){878                       done(err);879                     }else{880                       expect(obj.updateDate).to.exist;881                       done();882                     }883                   });884              })885              .catch(function(err){886                done(err);887              });888        });889        it("promise ã§åå¾ã§ãã", function(done){890          if(ncmb.stub){891            role = new ncmb.Role("mainRole");892          }else{893            role = new ncmb.Role("removeMultiUserPromise");894          }895          role.addUser([user,user2])896              .save()897              .then(function(obj){898                obj.removeUser(user)899                   .removeUser(user2);900                return obj.update();901              })902              .then(function(obj){903                expect(obj.updateDate).to.exist;904                done();905              })906              .catch(function(err){907                done(err);908              });909        });910      });911    });912  });913  describe("åãã¼ã«ã®åé¤", function(){914    var role = null;915    describe("removeRole", function(){916      context("åé¤ãããã¼ã«ãæå®ãã¦æ´æ°ããçµæãåå¾ãã", function(){917        var subrole = null;918        before(function(){919          subrole = new ncmb.Role("subRole");920          subrole.objectId = belong_role1_id;921        });922        it("callback ã§åå¾ã§ãã", function(done){923          if(ncmb.stub){924            role = new ncmb.Role("mainRole");925          }else{926            role = new ncmb.Role("removeRoleCallback");927          }928          role.addRole(subrole)929              .save()930              .then(function(obj){931                obj.removeRole(subrole)932                   .update(function(err, obj){933                     if(err){934                       done(err);935                     }else{936                       expect(obj.updateDate).to.exist;937                       done();938                     }939                   });940              })941              .catch(function(err){942                done(err);943              });944        });945        it("promise ã§åå¾ã§ãã", function(done){946          if(ncmb.stub){947            role = new ncmb.Role("mainRole");948          }else{949            role = new ncmb.Role("removeRolePromise");950          }951          role.addRole(subrole)952              .save()953              .then(function(obj){954                obj.removeRole(subrole);955                return obj.update();956              })957              .then(function(obj){958                expect(obj.updateDate).to.exist;959                done();960              })961              .catch(function(err){962                done(err);963              });964        });965      });966      context("åé¤ãããã¼ã«ãé
åã§æå®ãã¦æ´æ°ããçµæãåå¾ãã", function(){967        var subrole = null;968        var subrole2 = null;969        before(function(){970          subrole = new ncmb.Role("subRole");971          subrole2 = new ncmb.Role("subRole2");972          subrole.objectId = belong_role1_id;973          subrole2.objectId = belong_role2_id;974        });975        it("callback ã§åå¾ã§ãã", function(done){976          if(ncmb.stub){977            role = new ncmb.Role("mainRole");978          }else{979            role = new ncmb.Role("removeRoleArrayCallback");980          }981          role.addRole([subrole,subrole2])982              .save()983              .then(function(obj){984                obj.removeRole([subrole,subrole2])985                   .update(function(err, obj){986                     if(err){987                       done(err);988                     }else{989                       expect(obj.updateDate).to.exist;990                       done();991                     }992                   });993              })994              .catch(function(err){995                done(err);996              });997        });998        it("promise ã§åå¾ã§ãã", function(done){999          if(ncmb.stub){1000            role = new ncmb.Role("mainRole");1001          }else{1002            role = new ncmb.Role("removeRoleArrayPromise");1003          }1004          role.addRole([subrole,subrole2])1005              .save()1006              .then(function(obj){1007                obj.removeRole([subrole,subrole2]);1008                return obj.update();1009              })1010              .then(function(obj){1011                expect(obj.updateDate).to.exist;1012                done();1013              })1014              .catch(function(err){1015                done(err);1016              });1017        });1018      });1019      context("åé¤ãããã¼ã«ãé£ç¶ã§æå®ãã¦æ´æ°ããçµæãåå¾ãã", function(){1020        var subrole = null;1021        var subrole2 = null;1022        before(function(){1023          subrole = new ncmb.Role("subRole");1024          subrole2 = new ncmb.Role("subRole2");1025          subrole.objectId = belong_role1_id;1026          subrole2.objectId = belong_role2_id;1027        });1028        it("callback ã§åå¾ã§ãã", function(done){1029          if(ncmb.stub){1030            role = new ncmb.Role("mainRole");1031          }else{1032            role = new ncmb.Role("removeMultiRoleCallback");1033          }1034          role.addRole([subrole,subrole2])1035              .save()1036              .then(function(obj){1037                obj.removeRole(subrole)1038                   .removeRole(subrole2)1039                   .update(function(err, obj){1040                     if(err){1041                       done(err);1042                     }else{1043                       expect(obj.updateDate).to.exist;1044                       done();1045                     }1046                   });1047              })1048              .catch(function(err){1049                done(err);1050              });1051        });1052        it("promise ã§åå¾ã§ãã", function(done){1053          if(ncmb.stub){1054            role = new ncmb.Role("mainRole");1055          }else{1056            role = new ncmb.Role("removeRoleMultiPromise");1057          }1058          role.addRole([subrole,subrole2])1059              .save()1060              .then(function(obj){1061                obj.removeRole(subrole)1062                   .removeRole(subrole2);1063                return obj.update();1064              })1065              .then(function(obj){1066                expect(obj.updateDate).to.exist;1067                done();1068              })1069              .catch(function(err){1070                done(err);1071              });1072        });1073      });1074    });1075  });...Roles.test.js
Source:Roles.test.js  
1const { expectRevert, expectEvent } = require('@openzeppelin/test-helpers');2const RolesMock = artifacts.require('RolesMock');3contract('Roles', function ([_, owner, ...otherAccounts]) {4  beforeEach(async function () {5    this.contract = await RolesMock.new({ from: owner });6  });7  context('testing MINTER role', function () {8    shouldBehaveLikeAccessControl(owner, otherAccounts, 'MINTER');9  });10  context('testing OPERATOR role', function () {11    shouldBehaveLikeAccessControl(owner, otherAccounts, 'OPERATOR');12  });13  function shouldBehaveLikeAccessControl (admin, [authorized, otherAuthorized, other, otherAdmin], roleName) {14    const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';15    const ROLE = web3.utils.soliditySha3(roleName);16    const OTHER_ROLE = web3.utils.soliditySha3('OTHER_ROLE');17    describe('default admin', function () {18      it('deployer has default admin role', async function () {19        expect(await this.contract.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.equal(true);20      });21      it('other roles\'s admin is the default admin role', async function () {22        expect(await this.contract.getRoleAdmin(ROLE)).to.equal(DEFAULT_ADMIN_ROLE);23      });24      it('default admin role\'s admin is itself', async function () {25        expect(await this.contract.getRoleAdmin(DEFAULT_ADMIN_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);26      });27    });28    describe('granting', function () {29      it('admin can grant role to other accounts', async function () {30        const receipt = await this.contract.grantRole(ROLE, authorized, { from: admin });31        expectEvent(receipt, 'RoleGranted', { account: authorized, role: ROLE, sender: admin });32        expect(await this.contract.hasRole(ROLE, authorized)).to.equal(true);33      });34      it('non-admin cannot grant role to other accounts', async function () {35        await expectRevert(36          this.contract.grantRole(ROLE, authorized, { from: other }),37          'AccessControl: sender must be an admin to grant',38        );39      });40      it('accounts can be granted a role multiple times', async function () {41        await this.contract.grantRole(ROLE, authorized, { from: admin });42        const receipt = await this.contract.grantRole(ROLE, authorized, { from: admin });43        expectEvent.notEmitted(receipt, 'RoleRevoked');44      });45    });46    describe('revoking', function () {47      it('roles that are not had can be revoked', async function () {48        expect(await this.contract.hasRole(ROLE, authorized)).to.equal(false);49        const receipt = await this.contract.revokeRole(ROLE, authorized, { from: admin });50        expectEvent.notEmitted(receipt, 'RoleRevoked');51      });52      context('with granted role', function () {53        beforeEach(async function () {54          await this.contract.grantRole(ROLE, authorized, { from: admin });55        });56        it('admin can revoke role', async function () {57          const receipt = await this.contract.revokeRole(ROLE, authorized, { from: admin });58          expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: admin });59          expect(await this.contract.hasRole(ROLE, authorized)).to.equal(false);60        });61        it('non-admin cannot revoke role', async function () {62          await expectRevert(63            this.contract.revokeRole(ROLE, authorized, { from: other }),64            'AccessControl: sender must be an admin to revoke',65          );66        });67        it('a role can be revoked multiple times', async function () {68          await this.contract.revokeRole(ROLE, authorized, { from: admin });69          const receipt = await this.contract.revokeRole(ROLE, authorized, { from: admin });70          expectEvent.notEmitted(receipt, 'RoleRevoked');71        });72      });73    });74    describe('renouncing', function () {75      it('roles that are not had can be renounced', async function () {76        const receipt = await this.contract.renounceRole(ROLE, authorized, { from: authorized });77        expectEvent.notEmitted(receipt, 'RoleRevoked');78      });79      context('with granted role', function () {80        beforeEach(async function () {81          await this.contract.grantRole(ROLE, authorized, { from: admin });82        });83        it('bearer can renounce role', async function () {84          const receipt = await this.contract.renounceRole(ROLE, authorized, { from: authorized });85          expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: authorized });86          expect(await this.contract.hasRole(ROLE, authorized)).to.equal(false);87        });88        it('only the sender can renounce their roles', async function () {89          await expectRevert(90            this.contract.renounceRole(ROLE, authorized, { from: admin }),91            'AccessControl: can only renounce roles for self',92          );93        });94        it('a role can be renounced multiple times', async function () {95          await this.contract.renounceRole(ROLE, authorized, { from: authorized });96          const receipt = await this.contract.renounceRole(ROLE, authorized, { from: authorized });97          expectEvent.notEmitted(receipt, 'RoleRevoked');98        });99      });100    });101    describe('enumerating', function () {102      it('role bearers can be enumerated', async function () {103        await this.contract.grantRole(ROLE, authorized, { from: admin });104        await this.contract.grantRole(ROLE, otherAuthorized, { from: admin });105        const memberCount = await this.contract.getRoleMemberCount(ROLE);106        memberCount.should.be.bignumber.equal('3'); // two added above plus admin107        const bearers = [];108        for (let i = 0; i < memberCount; ++i) {109          bearers.push(await this.contract.getRoleMember(ROLE, i));110        }111        expect(bearers).to.have.members([admin, authorized, otherAuthorized]);112      });113    });114    describe('setting role admin', function () {115      beforeEach(async function () {116        const receipt = await this.contract.setRoleAdmin(ROLE, OTHER_ROLE);117        expectEvent(receipt, 'RoleAdminChanged', {118          role: ROLE,119          previousAdminRole: DEFAULT_ADMIN_ROLE,120          newAdminRole: OTHER_ROLE,121        });122        await this.contract.grantRole(OTHER_ROLE, otherAdmin, { from: admin });123      });124      it('a role\'s admin role can be changed', async function () {125        expect(await this.contract.getRoleAdmin(ROLE)).to.equal(OTHER_ROLE);126      });127      it('the new admin can grant roles', async function () {128        const receipt = await this.contract.grantRole(ROLE, authorized, { from: otherAdmin });129        expectEvent(receipt, 'RoleGranted', { account: authorized, role: ROLE, sender: otherAdmin });130      });131      it('the new admin can revoke roles', async function () {132        await this.contract.grantRole(ROLE, authorized, { from: otherAdmin });133        const receipt = await this.contract.revokeRole(ROLE, authorized, { from: otherAdmin });134        expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: otherAdmin });135      });136      it('a role\'s previous admins no longer grant roles', async function () {137        await expectRevert(138          this.contract.grantRole(ROLE, authorized, { from: admin }),139          'AccessControl: sender must be an admin to grant',140        );141      });142      it('a role\'s previous admins no longer revoke roles', async function () {143        await expectRevert(144          this.contract.revokeRole(ROLE, authorized, { from: admin }),145          'AccessControl: sender must be an admin to revoke',146        );147      });148    });149    describe('testing modifier', function () {150      beforeEach(async function () {151        await this.contract.grantRole(ROLE, authorized, { from: admin });152      });153      context('from authorized account', function () {154        const from = authorized;155        it('allows access', async function () {156          await this.contract[`only${roleName}Mock`]({ from });157        });158      });159      context('from unauthorized account', function () {160        const from = other;161        it('reverts', async function () {162          await expectRevert(163            this.contract[`only${roleName}Mock`]({ from }),164            `Roles: caller does not have the ${roleName} role`,165          );166        });167      });168    });169  }...utils.test.js
Source:utils.test.js  
1/*2 * Copyright (C) 2018 - present Instructure, Inc.3 *4 * This file is part of Canvas.5 *6 * Canvas is free software: you can redistribute it and/or modify it under7 * the terms of the GNU Affero General Public License as published by the Free8 * Software Foundation, version 3 of the License.9 *10 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR12 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more13 * details.14 *15 * You should have received a copy of the GNU Affero General Public License along16 * with this program. If not, see <http://www.gnu.org/licenses/>.17 */18import {getSortedRoles, roleSortedInsert, roleIsCourseBaseRole, roleIsBaseRole} from '../utils'19it('getSortedRoles sorts roles properly based on base_role_type', () => {20  const UNORDERED_ROLES = [21    {22      id: '13',23      role: 'NON_BASE_TYPE',24      base_role_type: 'BASE_TYPE_THREE'25    },26    {27      id: '1',28      role: 'BASE_TYPE_ONE',29      base_role_type: 'BASE_TYPE_ONE'30    },31    {32      id: '10',33      role: 'NON_BASE_TYPE',34      base_role_type: 'BASE_TYPE_FOUR'35    },36    {37      id: '2',38      role: 'BASE_TYPE_TWO',39      base_role_type: 'BASE_TYPE_TWO'40    },41    {42      id: '3',43      role: 'BASE_TYPE_THREE',44      base_role_type: 'BASE_TYPE_THREE'45    },46    {47      id: '4',48      role: 'BASE_TYPE_FOUR',49      base_role_type: 'BASE_TYPE_FOUR'50    }51  ]52  const ORDERED_ROLES = [53    {54      id: '1',55      role: 'BASE_TYPE_ONE',56      base_role_type: 'BASE_TYPE_ONE'57    },58    {59      id: '2',60      role: 'BASE_TYPE_TWO',61      base_role_type: 'BASE_TYPE_TWO'62    },63    {64      id: '3',65      role: 'BASE_TYPE_THREE',66      base_role_type: 'BASE_TYPE_THREE'67    },68    {69      id: '13',70      role: 'NON_BASE_TYPE',71      base_role_type: 'BASE_TYPE_THREE'72    },73    {74      id: '4',75      role: 'BASE_TYPE_FOUR',76      base_role_type: 'BASE_TYPE_FOUR'77    },78    {79      id: '10',80      role: 'NON_BASE_TYPE',81      base_role_type: 'BASE_TYPE_FOUR'82    }83  ]84  const orderedRoles = getSortedRoles(UNORDERED_ROLES)85  expect(orderedRoles).toMatchObject(ORDERED_ROLES)86})87it('getSortedRoles sorts roles properly for NON_BASE_TYPE at beginning', () => {88  const UNORDERED_ROLES = [89    {90      id: '20',91      role: 'NON_BASE_TYPE',92      base_role_type: 'BASE_TYPE_ONE'93    },94    {95      id: '13',96      role: 'NON_BASE_TYPE',97      base_role_type: 'BASE_TYPE_THREE'98    },99    {100      id: '1',101      role: 'BASE_TYPE_ONE',102      base_role_type: 'BASE_TYPE_ONE'103    },104    {105      id: '2',106      role: 'BASE_TYPE_TWO',107      base_role_type: 'BASE_TYPE_TWO'108    },109    {110      id: '3',111      role: 'BASE_TYPE_THREE',112      base_role_type: 'BASE_TYPE_THREE'113    },114    {115      id: '11',116      role: 'NON_BASE_TYPE',117      base_role_type: 'BASE_TYPE_ONE'118    }119  ]120  const ORDERED_ROLES = [121    {122      id: '1',123      role: 'BASE_TYPE_ONE',124      base_role_type: 'BASE_TYPE_ONE'125    },126    {127      id: '11',128      role: 'NON_BASE_TYPE',129      base_role_type: 'BASE_TYPE_ONE'130    },131    {132      id: '20',133      role: 'NON_BASE_TYPE',134      base_role_type: 'BASE_TYPE_ONE'135    },136    {137      id: '2',138      role: 'BASE_TYPE_TWO',139      base_role_type: 'BASE_TYPE_TWO'140    },141    {142      id: '3',143      role: 'BASE_TYPE_THREE',144      base_role_type: 'BASE_TYPE_THREE'145    },146    {147      id: '13',148      role: 'NON_BASE_TYPE',149      base_role_type: 'BASE_TYPE_THREE'150    }151  ]152  const orderedRoles = getSortedRoles(UNORDERED_ROLES)153  expect(orderedRoles).toMatchObject(ORDERED_ROLES)154})155it('getSortedRoles sorts roles properly for NON_BASE_TYPE in middle and seperated', () => {156  const UNORDERED_ROLES = [157    {158      id: '1',159      role: 'BASE_TYPE_ONE',160      base_role_type: 'BASE_TYPE_ONE'161    },162    {163      id: '11',164      role: 'NON_BASE_TYPE',165      base_role_type: 'BASE_TYPE_ONE'166    },167    {168      id: '2',169      role: 'BASE_TYPE_TWO',170      base_role_type: 'BASE_TYPE_TWO'171    },172    {173      id: '20',174      role: 'NON_BASE_TYPE',175      base_role_type: 'BASE_TYPE_ONE'176    },177    {178      id: '3',179      role: 'BASE_TYPE_THREE',180      base_role_type: 'BASE_TYPE_THREE'181    }182  ]183  const ORDERED_ROLES = [184    {185      id: '1',186      role: 'BASE_TYPE_ONE',187      base_role_type: 'BASE_TYPE_ONE'188    },189    {190      id: '11',191      role: 'NON_BASE_TYPE',192      base_role_type: 'BASE_TYPE_ONE'193    },194    {195      id: '20',196      role: 'NON_BASE_TYPE',197      base_role_type: 'BASE_TYPE_ONE'198    },199    {200      id: '2',201      role: 'BASE_TYPE_TWO',202      base_role_type: 'BASE_TYPE_TWO'203    },204    {205      id: '3',206      role: 'BASE_TYPE_THREE',207      base_role_type: 'BASE_TYPE_THREE'208    }209  ]210  const orderedRoles = getSortedRoles(UNORDERED_ROLES)211  expect(orderedRoles).toMatchObject(ORDERED_ROLES)212})213const ALL_ROLES = [214  {215    id: '1',216    role: 'BASE_TYPE_ONE',217    base_role_type: 'BASE_TYPE_ONE'218  },219  {220    id: '11',221    role: 'NON_BASE_TYPE',222    base_role_type: 'BASE_TYPE_ONE'223  },224  {225    id: '2',226    role: 'BASE_TYPE_TWO',227    base_role_type: 'BASE_TYPE_TWO'228  },229  {230    id: '3',231    role: 'BASE_TYPE_THREE',232    base_role_type: 'BASE_TYPE_THREE'233  }234]235it('roleSortedInsert sorts roles properly inserts Role into first item', () => {236  const ROLE_TO_INSERT = {237    id: '20',238    role: 'NON_BASE_TYPE',239    base_role_type: 'BASE_TYPE_ONE'240  }241  const ORDERED_ROLES = ALL_ROLES.slice()242  ORDERED_ROLES.splice(2, 0, ROLE_TO_INSERT)243  const orderedRoles = roleSortedInsert(ALL_ROLES, ROLE_TO_INSERT)244  expect(orderedRoles).toMatchObject(ORDERED_ROLES)245})246it('roleSortedInsert sorts roles properly inserts Role into last item', () => {247  const ROLE_TO_INSERT = {248    id: '20',249    role: 'NON_BASE_TYPE',250    base_role_type: 'BASE_TYPE_THREE'251  }252  const ORDERED_ROLES = ALL_ROLES.slice()253  ORDERED_ROLES.splice(ALL_ROLES.length, 0, ROLE_TO_INSERT)254  const orderedRoles = roleSortedInsert(ALL_ROLES, ROLE_TO_INSERT)255  expect(orderedRoles).toMatchObject(ORDERED_ROLES)256})257it('roleSortedInsert sorts roles properly inserts Role into middle item', () => {258  const ROLE_TO_INSERT = {259    id: '20',260    role: 'NON_BASE_TYPE',261    base_role_type: 'BASE_TYPE_TWO'262  }263  const ORDERED_ROLES = ALL_ROLES.slice()264  ORDERED_ROLES.splice(3, 0, ROLE_TO_INSERT)265  const orderedRoles = roleSortedInsert(ALL_ROLES, ROLE_TO_INSERT)266  expect(orderedRoles).toMatchObject(ORDERED_ROLES)267})268it('roleSortedInsert puts accountAdmin in the first position', () => {269  const ACCOUNT_ADMIN = {270    id: '1',271    role: 'AccountAdmin',272    base_role_type: 'AccountMembership'273  }274  const orderedRoles = getSortedRoles(ALL_ROLES, ACCOUNT_ADMIN)275  expect(orderedRoles[0]).toMatchObject(ACCOUNT_ADMIN)276})277it('roleSortedInsert removes account admin and replaces it in first position', () => {278  const ACCOUNT_ADMIN = {279    id: '1',280    role: 'AccountAdmin',281    base_role_type: 'AccountMembership'282  }283  const UNORDERED_ROLES = [284    {285      id: '13',286      role: 'NON_BASE_TYPE',287      base_role_type: 'BASE_TYPE_THREE'288    },289    {290      id: '5',291      role: 'BASE_TYPE_ONE',292      base_role_type: 'BASE_TYPE_ONE'293    },294    {295      id: '10',296      role: 'NON_BASE_TYPE',297      base_role_type: 'BASE_TYPE_FOUR'298    },299    {300      id: '2',301      role: 'BASE_TYPE_TWO',302      base_role_type: 'BASE_TYPE_TWO'303    },304    {305      id: '3',306      role: 'BASE_TYPE_THREE',307      base_role_type: 'BASE_TYPE_THREE'308    },309    {310      id: '4',311      role: 'BASE_TYPE_FOUR',312      base_role_type: 'BASE_TYPE_FOUR'313    },314    {315      id: '1',316      role: 'AccountAdmin',317      base_role_type: 'AccountMembership'318    }319  ]320  const orderedRoles = getSortedRoles(UNORDERED_ROLES, ACCOUNT_ADMIN)321  expect(orderedRoles[0]).toMatchObject(ACCOUNT_ADMIN)322  expect(orderedRoles).toHaveLength(UNORDERED_ROLES.length)323})324it('does not return account base roles as course base roles', () => {325  const accountBaserole = {326    id: '1',327    role: 'AccountAdmin',328    base_role_type: 'AccountMembership'329  }330  expect(roleIsCourseBaseRole(accountBaserole)).toBeFalsy()331  expect(roleIsBaseRole(accountBaserole)).toBeTruthy()...automation_predicate.js
Source:automation_predicate.js  
1// Copyright 2014 The Chromium Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4/**5 * @fileoverview ChromeVox predicates for the automation extension API.6 */7goog.provide('AutomationPredicate');8goog.provide('AutomationPredicate.Binary');9goog.provide('AutomationPredicate.Unary');10goog.scope(function() {11var AutomationNode = chrome.automation.AutomationNode;12var RoleType = chrome.automation.RoleType;13/**14 * @constructor15 */16AutomationPredicate = function() {};17/**18 * @typedef {function(!AutomationNode) : boolean}19 */20AutomationPredicate.Unary;21/**22 * @typedef {function(!AutomationNode, !AutomationNode) : boolean}23 */24AutomationPredicate.Binary;25/**26 * Constructs a predicate given a role.27 * @param {RoleType} role28 * @return {AutomationPredicate.Unary}29 */30AutomationPredicate.withRole = function(role) {31  return function(node) {32    return node.role == role;33  };34};35/** @type {AutomationPredicate.Unary} */36AutomationPredicate.checkBox = AutomationPredicate.withRole(RoleType.CHECK_BOX);37/** @type {AutomationPredicate.Unary} */38AutomationPredicate.comboBox = AutomationPredicate.withRole(RoleType.COMBO_BOX);39/** @type {AutomationPredicate.Unary} */40AutomationPredicate.heading = AutomationPredicate.withRole(RoleType.HEADING);41/** @type {AutomationPredicate.Unary} */42AutomationPredicate.inlineTextBox =43    AutomationPredicate.withRole(RoleType.INLINE_TEXT_BOX);44/** @type {AutomationPredicate.Unary} */45AutomationPredicate.link = AutomationPredicate.withRole(RoleType.LINK);46/** @type {AutomationPredicate.Unary} */47AutomationPredicate.table = AutomationPredicate.withRole(RoleType.TABLE);48/**49 * @param {!AutomationNode} node50 * @return {boolean}51 */52AutomationPredicate.button = function(node) {53  return /button/i.test(node.role);54};55/**56 * @param {!AutomationNode} node57 * @return {boolean}58 */59AutomationPredicate.editText = function(node) {60  return node.state.editable && node.parent && !node.parent.state.editable;61};62/**63 * @param {!AutomationNode} node64 * @return {boolean}65 */66AutomationPredicate.formField = function(node) {67  switch (node.role) {68    case 'button':69    case 'buttonDropDown':70    case 'checkBox':71    case 'comboBox':72    case 'date':73    case 'dateTime':74    case 'details':75    case 'disclosureTriangle':76    case 'form':77    case 'menuButton':78    case 'menuListPopup':79    case 'popUpButton':80    case 'radioButton':81    case 'searchBox':82    case 'slider':83    case 'spinButton':84    case 'switch':85    case 'tab':86    case 'textField':87    case 'time':88    case 'toggleButton':89    case 'tree':90      return true;91  }92  return false;93};94/**95 * @param {!AutomationNode} node96 * @return {boolean}97 */98AutomationPredicate.landmark = function(node) {99  switch (node.role) {100    case 'application':101    case 'banner':102    case 'complementary':103    case 'contentInfo':104    case 'form':105    case 'main':106    case 'navigation':107    case 'search':108      return true;109  }110  return false;111};112/**113 * @param {!AutomationNode} node114 * @return {boolean}115 */116AutomationPredicate.visitedLink = function(node) {117  return node.state.visited;118};119/**120 * @param {!AutomationNode} node121 * @return {boolean}122 */123AutomationPredicate.focused = function(node) {124  return node.state.focused;125};126/**127 * @param {!AutomationNode} node128 * @return {boolean}129 */130AutomationPredicate.leaf = function(node) {131  return !node.firstChild || node.role == RoleType.BUTTON ||132      node.role == RoleType.BUTTONDROPDOWN ||133      node.role == RoleType.POP_UP_BUTTON || node.role == RoleType.SLIDER ||134      node.role == RoleType.TEXT_FIELD || node.state.invisible ||135      node.children.every(function(n) {136        return n.state.invisible;137      });138};139/**140 * @param {!AutomationNode} node141 * @return {boolean}142 */143AutomationPredicate.leafWithText = function(node) {144  return AutomationPredicate.leaf(node) && !!(node.name || node.value);145};146/**147 * Non-inline textbox nodes which have an equivalent in the DOM.148 * @param {!AutomationNode} node149 * @return {boolean}150 */151AutomationPredicate.leafDomNode = function(node) {152  return AutomationPredicate.leaf(node) || node.role == RoleType.STATIC_TEXT;153};154/**155 * Matches against nodes visited during object navigation. An object as156 * defined below, are all nodes that are focusable or static text. When used in157 * tree walking, it should visit all nodes that tab traversal would as well as158 * non-focusable static text.159 * @param {!AutomationNode} node160 * @return {boolean}161 */162AutomationPredicate.object = function(node) {163  return node.state.focusable ||164      (AutomationPredicate.leafDomNode(node) &&165       (/\S+/.test(node.name) ||166        (node.role != RoleType.LINE_BREAK &&167         node.role != RoleType.STATIC_TEXT &&168         node.role != RoleType.INLINE_TEXT_BOX)));169};170/**171 * @param {!AutomationNode} first172 * @param {!AutomationNode} second173 * @return {boolean}174 */175AutomationPredicate.linebreak = function(first, second) {176  // TODO(dtseng): Use next/previousOnLin once available.177  var fl = first.location;178  var sl = second.location;179  return fl.top != sl.top || (fl.top + fl.height != sl.top + sl.height);180};181/**182 * Matches against a node that contains other interesting nodes.183 * These nodes should always have their subtrees scanned when navigating.184 * @param {!AutomationNode} node185 * @return {boolean}186 */187AutomationPredicate.container = function(node) {188  return AutomationPredicate.structuralContainer(node) ||189      node.role == RoleType.DIV || node.role == RoleType.DOCUMENT ||190      node.role == RoleType.GROUP || node.role == RoleType.LIST_ITEM ||191      node.role == RoleType.TOOLBAR || node.role == RoleType.WINDOW;192};193/**194 * Matches against nodes that contain interesting nodes, but should never be195 * visited.196 * @param {!AutomationNode} node197 * @return {boolean}198 */199AutomationPredicate.structuralContainer = function(node) {200  return node.role == RoleType.ROOT_WEB_AREA ||201      node.role == RoleType.EMBEDDED_OBJECT || node.role == RoleType.IFRAME ||202      node.role == RoleType.IFRAME_PRESENTATIONAL ||203      node.role == RoleType.PLUGIN_OBJECT;204};205/**206 * Returns whether the given node should not be crossed when performing207 * traversals up the ancestry chain.208 * @param {AutomationNode} node209 * @return {boolean}210 */211AutomationPredicate.root = function(node) {212  switch (node.role) {213    case RoleType.DIALOG:214    case RoleType.WINDOW:215      return true;216    case RoleType.TOOLBAR:217      return node.root.role == RoleType.DESKTOP;218    case RoleType.ROOT_WEB_AREA:219      return !node.parent || node.parent.root.role == RoleType.DESKTOP;220    default:221      return false;222  }223};224/**225 * Nodes that should be ignored while traversing the automation tree. For226 * example, apply this predicate when moving to the next object.227 * @param {!AutomationNode} node228 * @return {boolean}229 */230AutomationPredicate.shouldIgnoreNode = function(node) {231  // Ignore invisible nodes.232  if (node.state.invisible ||233      (node.location.height == 0 && node.location.width == 0))234    return true;235  // Ignore structural containres.236  if (AutomationPredicate.structuralContainer(node))237    return true;238  // Ignore list markers since we already announce listitem role.239  if (node.role == RoleType.LIST_MARKER)240    return true;241  // Don't ignore nodes with names.242  if (node.name || node.value || node.description)243    return false;244  // Ignore some roles.245  return AutomationPredicate.leaf(node) &&246      (node.role == RoleType.CLIENT || node.role == RoleType.DIV ||247       node.role == RoleType.GROUP || node.role == RoleType.IMAGE ||248       node.role == RoleType.STATIC_TEXT);249};...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!!
