GooglePlayJobWriter { public Bundle writeToBundle(JobParameters job, Bundle b) { b.putString(REQUEST_PARAM_TAG, job.getTag()); b.putBoolean(REQUEST_PARAM_UPDATE_CURRENT, job.shouldReplaceCurrent()); boolean persisted = job.getLifetime() == Lifetime.FOREVER; b.putBoolean(REQUEST_PARAM_PERSISTED, persisted); b.putString(REQUEST_PARAM_SERVICE, GooglePlayReceiver.class.getName()); writeTriggerToBundle(job, b); writeConstraintsToBundle(job, b); writeRetryStrategyToBundle(job, b); Bundle userExtras = job.getExtras(); if (userExtras == null) { userExtras = new Bundle(); } b.putBundle(REQUEST_PARAM_EXTRAS, jobCoder.encode(job, userExtras)); return b; }  Bundle writeToBundle(JobParameters job, Bundle b);  }
@Test public void testWriteToBundle_requiresIdle() { assertTrue( "requiresIdle", writer .writeToBundle( initializeDefaultBuilder().setConstraints(Constraint.DEVICE_IDLE).build(), new Bundle()) .getBoolean("requiresIdle")); for (Integer constraint : Arrays.asList(Constraint.ON_ANY_NETWORK, Constraint.ON_UNMETERED_NETWORK)) { assertFalse( "requiresIdle", writer .writeToBundle( initializeDefaultBuilder().setConstraints(constraint).build(), new Bundle()) .getBoolean("requiresIdle")); } }
@Test public void testWriteToBundle_retryPolicy() { assertEquals( "retry_policy", GooglePlayJobWriter.LEGACY_RETRY_POLICY_EXPONENTIAL, writer .writeToBundle( initializeDefaultBuilder() .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) .build(), new Bundle()) .getBundle("retryStrategy") .getInt("retry_policy")); assertEquals( "retry_policy", GooglePlayJobWriter.LEGACY_RETRY_POLICY_LINEAR, writer .writeToBundle( initializeDefaultBuilder().setRetryStrategy(RetryStrategy.DEFAULT_LINEAR).build(), new Bundle()) .getBundle("retryStrategy") .getInt("retry_policy")); }
@Test public void testWriteToBundle_backoffSeconds() { for (RetryStrategy retryStrategy : Arrays.asList(RetryStrategy.DEFAULT_EXPONENTIAL, RetryStrategy.DEFAULT_LINEAR)) { Bundle b = writer .writeToBundle( initializeDefaultBuilder().setRetryStrategy(retryStrategy).build(), new Bundle()) .getBundle("retryStrategy"); assertEquals( "initial_backoff_seconds", retryStrategy.getInitialBackoff(), b.getInt("initial_backoff_seconds")); assertEquals( "maximum_backoff_seconds", retryStrategy.getMaximumBackoff(), b.getInt("maximum_backoff_seconds")); } }
@Test public void testWriteToBundle_triggers() { Bundle b = writer.writeToBundle( initializeDefaultBuilder().setTrigger(Trigger.NOW).build(), new Bundle()); assertEquals("window_start", 0, b.getLong("window_start")); assertEquals("window_end", 1, b.getLong("window_end")); JobTrigger.ExecutionWindowTrigger t = Trigger.executionWindow(631, 978); b = writer.writeToBundle(initializeDefaultBuilder().setTrigger(t).build(), new Bundle()); assertEquals("window_start", t.getWindowStart(), b.getLong("window_start")); assertEquals("window_end", t.getWindowEnd(), b.getLong("window_end")); b = writer.writeToBundle( initializeDefaultBuilder().setRecurring(true).setTrigger(t).build(), new Bundle()); assertEquals("period", t.getWindowEnd(), b.getLong("period")); assertEquals("period_flex", t.getWindowEnd() - t.getWindowStart(), b.getLong("period_flex")); }
@Test public void testWriteToBundle_contentUriTrigger() { ObservedUri observedUri = new ObservedUri(ContactsContract.AUTHORITY_URI, Flags.FLAG_NOTIFY_FOR_DESCENDANTS); ContentUriTrigger contentUriTrigger = Trigger.contentUriTrigger(Arrays.asList(observedUri)); Bundle bundle = writer.writeToBundle( initializeDefaultBuilder().setTrigger(contentUriTrigger).build(), new Bundle()); Uri[] uris = (Uri[]) bundle.getParcelableArray(BundleProtocol.PACKED_PARAM_CONTENT_URI_ARRAY); int[] flags = bundle.getIntArray(BundleProtocol.PACKED_PARAM_CONTENT_URI_FLAGS_ARRAY); assertTrue("Array size", uris.length == flags.length && flags.length == 1); assertEquals( BundleProtocol.PACKED_PARAM_CONTENT_URI_ARRAY, ContactsContract.AUTHORITY_URI, uris[0]); assertEquals( BundleProtocol.PACKED_PARAM_CONTENT_URI_FLAGS_ARRAY, Flags.FLAG_NOTIFY_FOR_DESCENDANTS, flags[0]); }
@Test public void testWriteToBundle_extras() { Bundle extras = new Bundle(); extras.putString("bar", "foo"); extras.putInt("an_int", 1); extras.putBoolean("a bool", true); Bundle result = writer .writeToBundle(initializeDefaultBuilder().setExtras(extras).build(), new Bundle()) .getBundle("extras"); for (String key : extras.keySet()) { assertEquals('"' + key + '"' + " mismatch", extras.get(key), result.get(key)); } }
@Test public void testWriteToBundle_tags() { for (String tag : Arrays.asList("foo", "bar", "foobar", "this is a tag")) { Bundle b = writer.writeToBundle(initializeDefaultBuilder().setTag(tag).build(), new Bundle()); assertEquals("tag", tag, b.getString("tag")); } }
@Test public void testWriteToBundle_updateCurrent() { for (boolean replaceCurrent : ALL_BOOLEANS) { Bundle b = writer.writeToBundle( initializeDefaultBuilder().setReplaceCurrent(replaceCurrent).build(), new Bundle()); assertEquals("update_current", replaceCurrent, b.getBoolean("update_current")); } }
@Test public void testWriteToBundle_persisted() { Bundle b = writer.writeToBundle( initializeDefaultBuilder().setLifetime(Lifetime.FOREVER).build(), new Bundle()); assertTrue("persisted", b.getBoolean("persisted")); for (int lifetime : new int[] {Lifetime.UNTIL_NEXT_BOOT}) { b = writer.writeToBundle( initializeDefaultBuilder().setLifetime(lifetime).build(), new Bundle()); assertFalse("persisted", b.getBoolean("persisted")); } }
@Test public void testWriteToBundle_service() { Bundle b = writer.writeToBundle( initializeDefaultBuilder().setService(TestJobService.class).build(), new Bundle()); assertEquals("service", GooglePlayReceiver.class.getName(), b.getString("service")); }
@Test public void testWriteToBundle_requiredNetwork() { Map<Integer, Integer> mapping = new HashMap<>(); mapping.put(Constraint.ON_ANY_NETWORK, GooglePlayJobWriter.LEGACY_NETWORK_CONNECTED); mapping.put(Constraint.ON_UNMETERED_NETWORK, GooglePlayJobWriter.LEGACY_NETWORK_UNMETERED); mapping.put(0, GooglePlayJobWriter.LEGACY_NETWORK_ANY); for (Entry<Integer, Integer> testCase : mapping.entrySet()) { @SuppressWarnings("WrongConstant") Bundle b = writer.writeToBundle( initializeDefaultBuilder().setConstraints(testCase.getKey()).build(), new Bundle()); assertEquals("requiredNetwork", (int) testCase.getValue(), b.getInt("requiredNetwork")); } }
@Test public void testWriteToBundle_unmeteredConstraintShouldTakePrecendence() { Bundle b = writer.writeToBundle( initializeDefaultBuilder() .setConstraints(Constraint.ON_ANY_NETWORK, Constraint.ON_UNMETERED_NETWORK) .build(), new Bundle()); assertEquals( "expected ON_UNMETERED_NETWORK to take precendence over ON_ANY_NETWORK", GooglePlayJobWriter.LEGACY_NETWORK_UNMETERED, b.getInt("requiredNetwork")); }
@Test public void testWriteToBundle_requiresCharging() { assertTrue( "requiresCharging", writer .writeToBundle( initializeDefaultBuilder().setConstraints(Constraint.DEVICE_CHARGING).build(), new Bundle()) .getBoolean("requiresCharging")); for (Integer constraint : Arrays.asList(Constraint.ON_ANY_NETWORK, Constraint.ON_UNMETERED_NETWORK)) { assertFalse( "requiresCharging", writer .writeToBundle( initializeDefaultBuilder().setConstraints(constraint).build(), new Bundle()) .getBoolean("requiresCharging")); } }