@Override protected void setUp() throws Exception { super.setUp(); clearAppData(); // Mock out the account manager on the device. mContext = new SyncTestUtil.SyncTestContext(getInstrumentation().getTargetContext()); mAccountManager = new MockAccountManager(mContext, getInstrumentation().getContext()); AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAccountManager); MockSyncContentResolverDelegate syncContentResolverDelegate = new MockSyncContentResolverDelegate(); syncContentResolverDelegate.setMasterSyncAutomatically(true); SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, syncContentResolverDelegate); // This call initializes the ChromeSigninController to use our test context. ChromeSigninController.get(mContext); startChromeBrowserProcessSync(getInstrumentation().getTargetContext()); ThreadUtils.runOnUiThreadBlocking( new Runnable() { @Override public void run() { mSyncController = SyncController.get(mContext); } }); SyncTestUtil.verifySyncServerIsRunning(); }
private Account setupTestAccount(String accountName) { Account account = AccountManagerHelper.createAccountFromName(accountName); AccountHolder.Builder accountHolder = AccountHolder.create().account(account).password("password").alwaysAccept(true); mAccountManager.addAccountHolderExplicitly(accountHolder.build()); return account; }
@HostDrivenTest public void testDisableAndEnableSync() throws InterruptedException { setupTestAccountAndSignInToSync(FOREIGN_SESSION_TEST_MACHINE_ID); Account account = AccountManagerHelper.createAccountFromName(SyncTestUtil.DEFAULT_TEST_ACCOUNT); // Disabling Android sync should turn Chrome sync engine off. SyncStatusHelper.get(mContext).disableAndroidSync(account); SyncTestUtil.verifySyncIsDisabled(mContext, account); // Enabling Android sync should turn Chrome sync engine on. SyncStatusHelper.get(mContext).enableAndroidSync(account); SyncTestUtil.ensureSyncInitialized(mContext); SyncTestUtil.verifySignedInWithAccount(mContext, account); }
private void setupSync(boolean syncEnabled) { MockSyncContentResolverDelegate contentResolver = new MockSyncContentResolverDelegate(); // Android master sync can safely always be on. contentResolver.setMasterSyncAutomatically(true); // We don't want to use the system content resolver, so we override it. SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, contentResolver); Account account = AccountManagerHelper.createAccountFromName("*****@*****.**"); SyncStatusHelper syncStatusHelper = SyncStatusHelper.get(mContext); syncStatusHelper.setSignedInAccountName(account.name); if (syncEnabled) { syncStatusHelper.enableAndroidSync(account); } else { syncStatusHelper.disableAndroidSync(account); } }
/** * Triggers a send email intent. If no application has registered to receive these intents, this * will fail silently. * * @param context The context for issuing the intent. * @param email The email address to send to. * @param subject The subject of the email. * @param body The body of the email. * @param chooserTitle The title of the activity chooser. * @param fileToAttach The file name of the attachment. */ @CalledByNative static void sendEmail( Context context, String email, String subject, String body, String chooserTitle, String fileToAttach) { Set<String> possibleEmails = new HashSet<String>(); if (!TextUtils.isEmpty(email)) { possibleEmails.add(email); } else { Pattern emailPattern = Patterns.EMAIL_ADDRESS; Account[] accounts = AccountManagerHelper.get(context).getGoogleAccounts(); for (Account account : accounts) { if (emailPattern.matcher(account.name).matches()) { possibleEmails.add(account.name); } } } Intent send = new Intent(Intent.ACTION_SEND); send.setType("message/rfc822"); if (possibleEmails.size() != 0) { send.putExtra(Intent.EXTRA_EMAIL, possibleEmails.toArray(new String[possibleEmails.size()])); } send.putExtra(Intent.EXTRA_SUBJECT, subject); send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body)); if (!TextUtils.isEmpty(fileToAttach)) { File fileIn = new File(fileToAttach); send.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileIn)); } try { Intent chooser = Intent.createChooser(send, chooserTitle); // we start this activity outside the main activity. chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(chooser); } catch (android.content.ActivityNotFoundException ex) { // If no app handles it, do nothing. } }
@SmallTest @Feature({"Sync"}) public void testRefreshShouldReadValuesFromDiskWithSpecificTypes() { // Store some preferences for ModelTypes and account. We are using the helper class // for this, so we don't have to deal with low-level details such as preference keys. InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext); InvalidationPreferences.EditContext edit = invalidationPreferences.edit(); Set<String> storedModelTypes = new HashSet<String>(); storedModelTypes.add(ModelType.BOOKMARK.name()); storedModelTypes.add(ModelType.TYPED_URL.name()); invalidationPreferences.setSyncTypes(edit, storedModelTypes); Account storedAccount = AccountManagerHelper.createAccountFromName("*****@*****.**"); invalidationPreferences.setAccount(edit, storedAccount); invalidationPreferences.commit(edit); // Ensure all calls to {@link InvalidationController#setRegisteredTypes} store values // we can inspect in the test. final AtomicReference<Account> resultAccount = new AtomicReference<Account>(); final AtomicBoolean resultAllTypes = new AtomicBoolean(); final AtomicReference<Set<ModelType>> resultTypes = new AtomicReference<Set<ModelType>>(); InvalidationController controller = new InvalidationController(mContext) { @Override public void setRegisteredTypes(Account account, boolean allTypes, Set<ModelType> types) { resultAccount.set(account); resultAllTypes.set(allTypes); resultTypes.set(types); } }; // Execute the test. controller.refreshRegisteredTypes(); // Validate the values. assertEquals(storedAccount, resultAccount.get()); assertEquals(false, resultAllTypes.get()); assertEquals(ModelType.syncTypesToModelTypes(storedModelTypes), resultTypes.get()); }
private void setupTestAccounts(Context context) { mAccountManager = new MockAccountManager(context, context); AccountManagerHelper.overrideAccountManagerHelperForTests(context, mAccountManager); mAccount = setupTestAccount("*****@*****.**"); mAlternateAccount = setupTestAccount("*****@*****.**"); }