public void initialize(AwContents awContents) {
   mAwContents = awContents;
   if (isBackedByHardwareView()) {
     mHardwareView.initialize(
         mAwContents.getAwDrawGLFunction(), mAwContents.getAwDrawGLViewContext());
   }
 }
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testNullContentsClientClickableContent() throws Throwable {
    try {
      // The test will fire real intents through the test activity.
      // Need to temporarily suppress startActivity otherwise there will be a
      // handler selection window and the test can't dismiss that.
      getActivity().setIgnoreStartActivity(true);
      setupWithProvidedContentsClient(
          new NullContentsClient() {
            @Override
            public boolean hasWebViewClient() {
              return false;
            }
          });
      final String pageTitle = "Click Title";
      final String testEmail = "*****@*****.**";
      final String testUrl =
          mWebServer.setResponse(
              "/email_test.html",
              "<html><head><title>"
                  + pageTitle
                  + "</title></head>"
                  + "<body><span id='email'>"
                  + testEmail
                  + "</span></body>",
              null);

      // JS is required for the click simulator.
      mAwContents.getSettings().setJavaScriptEnabled(true);
      loadUrlAsync(mAwContents, testUrl);
      pollOnUiThread(
          new Callable<Boolean>() {
            @Override
            public Boolean call() {
              return mAwContents.getTitle().equals(pageTitle);
            }
          });

      // Clicking on an email should create an intent.
      DOMUtils.clickNode(this, mAwContents.getContentViewCore(), "email");
      pollOnUiThread(
          new Callable<Boolean>() {
            @Override
            public Boolean call() {
              return getActivity().getLastSentIntent() != null;
            }
          });
      assertEquals(
          "mailto:" + testEmail.replace("@", "%40"),
          getActivity().getLastSentIntent().getData().toString());
    } finally {
      getActivity().setIgnoreStartActivity(false);
    }
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testRejectFileSchemeCookies() throws Throwable {
    mCookieManager.setAcceptFileSchemeCookies(false);
    mAwContents.getSettings().setAllowFileAccess(true);

    mAwContents.getSettings().setAcceptThirdPartyCookies(true);
    assertFalse(fileURLCanSetCookie("3"));
    mAwContents.getSettings().setAcceptThirdPartyCookies(false);
    assertFalse(fileURLCanSetCookie("4"));
  }
  @MediumTest
  @Feature({"AndroidWebView", "Privacy"})
  public void testThirdPartyCookie() throws Throwable {
    TestWebServer webServer = null;
    try {
      // In theory we need two servers to test this, one server ('the first party')
      // which returns a response with a link to a second server ('the third party')
      // at different origin. This second server attempts to set a cookie which should
      // fail if AcceptThirdPartyCookie() is false.
      // Strictly according to the letter of RFC6454 it should be possible to set this
      // situation up with two TestServers on different ports (these count as having
      // different origins) but Chrome is not strict about this and does not check the
      // port. Instead we cheat making some of the urls come from localhost and some
      // from 127.0.0.1 which count (both in theory and pratice) as having different
      // origins.
      webServer = new TestWebServer(false);

      // Turn global allow on.
      mCookieManager.setAcceptCookie(true);
      assertTrue(mCookieManager.acceptCookie());

      // When third party cookies are disabled...
      mAwContents.getSettings().setAcceptThirdPartyCookies(false);
      assertFalse(mAwContents.getSettings().getAcceptThirdPartyCookies());

      // ...we can't set third party cookies.
      // First on the third party server we create a url which tries to set a cookie.
      String cookieUrl =
          toThirdPartyUrl(makeCookieUrl(webServer, "/cookie_1.js", "test1", "value1"));
      // Then we create a url on the first party server which links to the first url.
      String url = makeScriptLinkUrl(webServer, "/content_1.html", cookieUrl);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      assertNull(mCookieManager.getCookie(cookieUrl));

      // When third party cookies are enabled...
      mAwContents.getSettings().setAcceptThirdPartyCookies(true);
      assertTrue(mAwContents.getSettings().getAcceptThirdPartyCookies());

      // ...we can set third party cookies.
      cookieUrl = toThirdPartyUrl(makeCookieUrl(webServer, "/cookie_2.js", "test2", "value2"));
      url = makeScriptLinkUrl(webServer, "/content_2.html", cookieUrl);
      loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
      waitForCookie(cookieUrl);
      String cookie = mCookieManager.getCookie(cookieUrl);
      assertNotNull(cookie);
      validateCookies(cookie, "test2");
    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
 @Override
 public void onScrollChanged(int l, int t, int oldl, int oldt) {
   super.onScrollChanged(l, t, oldl, oldt);
   if (mAwContents != null) {
     mAwContents.onContainerViewScrollChanged(l, t, oldl, oldt);
   }
 }
 ThirdPartyCookiesTestHelper(TestWebServer webServer) throws Throwable {
   mWebServer = webServer;
   mContentsClient = new TestAwContentsClient();
   final AwTestContainerView containerView =
       createAwTestContainerViewOnMainSync(mContentsClient);
   mAwContents = containerView.getAwContents();
   mAwContents.getSettings().setJavaScriptEnabled(true);
 }
 @Override
 public void onDraw(Canvas canvas) {
   if (isBackedByHardwareView()) {
     mHardwareView.updateScroll(getScrollX(), getScrollY());
   }
   mAwContents.onDraw(canvas);
   super.onDraw(canvas);
 }
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testClickableContentInIframe() throws Throwable {
    standardSetup();

    final String pageTitle = "Click Title";
    final String testEmail = "*****@*****.**";
    final String testUrl =
        mWebServer.setResponse(
            "/email_test.html",
            "<html><head><title>"
                + pageTitle
                + "</title></head>"
                + "<body style='margin:0;'>"
                + " <iframe style='border:none;' srcdoc=\""
                + "   <body style='margin:0;'><span id='email'>"
                + testEmail
                + "</span></body>"
                + "\" src='iframe.html'></iframe>"
                + "</body>",
            null);

    // JS is required for the click simulator.
    mAwContents.getSettings().setJavaScriptEnabled(true);
    loadUrlAsync(mAwContents, testUrl);
    pollOnUiThread(
        new Callable<Boolean>() {
          @Override
          public Boolean call() {
            return mAwContents.getTitle().equals(pageTitle);
          }
        });

    int callCount = mShouldOverrideUrlLoadingHelper.getCallCount();
    DOMUtils.clickNodeByJs(
        this,
        mAwContents.getContentViewCore(),
        "window.frames[0].document.getElementById('email')");
    mShouldOverrideUrlLoadingHelper.waitForCallback(callCount);
    assertEquals(
        "mailto:" + testEmail.replace("@", "%40"),
        mShouldOverrideUrlLoadingHelper.getShouldOverrideUrlLoadingUrl());
    assertFalse(mShouldOverrideUrlLoadingHelper.isRedirect());
    assertTrue(mShouldOverrideUrlLoadingHelper.hasUserGesture());
    assertFalse(mShouldOverrideUrlLoadingHelper.isMainFrame());
  }
Example #9
0
  @SmallTest
  @Feature({"AndroidWebView"})
  public void testGetFavicon() throws Throwable {
    final AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
    final AwContents awContents = testView.getAwContents();

    TestWebServer webServer = null;
    try {
      webServer = new TestWebServer(false);

      final String faviconUrl =
          webServer.setResponseBase64(
              "/" + CommonResources.FAVICON_FILENAME,
              CommonResources.FAVICON_DATA_BASE64,
              CommonResources.getImagePngHeaders(false));
      final String pageUrl =
          webServer.setResponse("/favicon.html", CommonResources.FAVICON_STATIC_HTML, null);

      // The getFavicon will return the right icon a certain time after
      // the page load completes which makes it slightly hard to test.
      final Bitmap defaultFavicon = awContents.getFavicon();

      getAwSettingsOnUiThread(awContents).setImagesEnabled(true);
      loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);

      pollOnUiThread(
          new Callable<Boolean>() {
            @Override
            public Boolean call() {
              return awContents.getFavicon() != null
                  && !awContents.getFavicon().sameAs(defaultFavicon);
            }
          });

      final Object originalFaviconSource = (new URL(faviconUrl)).getContent();
      final Bitmap originalFavicon =
          BitmapFactory.decodeStream((InputStream) originalFaviconSource);
      assertNotNull(originalFavicon);

      assertTrue(awContents.getFavicon().sameAs(originalFavicon));

    } finally {
      if (webServer != null) webServer.shutdown();
    }
  }
 @Override
 public void onDetachedFromWindow() {
   super.onDetachedFromWindow();
   mAwContents.onDetachedFromWindow();
   if (mHardwareView != null) {
     mHardwareView.setReadyToRenderCallback(null);
   }
   mAttachedContents = false;
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   AwContents.setShouldDownloadFavicons();
   mContentsClient = new TestAwContentsClient();
   AwTestContainerView testContainerView = createAwTestContainerViewOnMainSync(mContentsClient);
   mAwContents = testContainerView.getAwContents();
   mWebServer = TestWebServer.start();
 }
Example #12
0
  public void testCreateAndGcManyTimes() throws Throwable {
    final int CONCURRENT_INSTANCES = 4;
    final int REPETITIONS = 16;
    // The system retains a strong ref to the last focused view (in InputMethodManager)
    // so allow for 1 'leaked' instance.
    final int MAX_IDLE_INSTANCES = 1;

    System.gc();

    pollOnUiThread(
        new Callable<Boolean>() {
          @Override
          public Boolean call() {
            return AwContents.getNativeInstanceCount() <= MAX_IDLE_INSTANCES;
          }
        });
    for (int i = 0; i < REPETITIONS; ++i) {
      for (int j = 0; j < CONCURRENT_INSTANCES; ++j) {
        AwTestContainerView view = createAwTestContainerViewOnMainSync(mContentsClient);
        loadUrlAsync(view.getAwContents(), "about:blank");
      }
      assertTrue(AwContents.getNativeInstanceCount() >= CONCURRENT_INSTANCES);
      assertTrue(AwContents.getNativeInstanceCount() <= (i + 1) * CONCURRENT_INSTANCES);
      runTestOnUiThread(
          new Runnable() {
            @Override
            public void run() {
              getActivity().removeAllViews();
            }
          });
    }

    System.gc();

    pollOnUiThread(
        new Callable<Boolean>() {
          @Override
          public Boolean call() {
            return AwContents.getNativeInstanceCount() <= MAX_IDLE_INSTANCES;
          }
        });
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    mCookieManager = new AwCookieManager();
    mContentsClient = new TestAwContentsClient();
    final AwTestContainerView testContainerView =
        createAwTestContainerViewOnMainSync(mContentsClient);
    mAwContents = testContainerView.getAwContents();
    mAwContents.getSettings().setJavaScriptEnabled(true);
    assertNotNull(mCookieManager);

    mCookieManager.setAcceptCookie(true);
    assertTrue(mCookieManager.acceptCookie());
  }
Example #14
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AwShellResourceProvider.registerResources(this);

    ContentApplication.initCommandLine(this);
    waitForDebuggerIfNeeded();

    AwBrowserProcess.loadLibrary(this);

    if (CommandLine.getInstance().hasSwitch(AwShellSwitches.ENABLE_ATRACE)) {
      Log.e(TAG, "Enabling Android trace.");
      TraceEvent.setATraceEnabled(true);
    }

    setContentView(R.layout.testshell_activity);

    mAwTestContainerView = createAwTestContainerView();

    mWebContents = mAwTestContainerView.getContentViewCore().getWebContents();
    mNavigationController = mWebContents.getNavigationController();
    LinearLayout contentContainer = (LinearLayout) findViewById(R.id.content_container);
    mAwTestContainerView.setLayoutParams(
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
    contentContainer.addView(mAwTestContainerView);
    mAwTestContainerView.requestFocus();

    initializeUrlField();
    initializeNavigationButtons();

    String startupUrl = getUrlFromIntent(getIntent());
    if (TextUtils.isEmpty(startupUrl)) {
      startupUrl = INITIAL_URL;
    }

    mAwTestContainerView.getAwContents().loadUrl(new LoadUrlParams(startupUrl));
    AwContents.setShouldDownloadFavicons();
    mUrlTextView.setText(startupUrl);

    if (CommandLine.getInstance().hasSwitch(ENABLE_DATA_REDUCTION_PROXY)) {
      String key = CommandLine.getInstance().getSwitchValue(DATA_REDUCTION_PROXY_KEY);
      if (key != null && !key.isEmpty()) {
        AwContentsStatics.setDataReductionProxyKey(key);
        AwContentsStatics.setDataReductionProxyEnabled(true);
      }
    }
  }
 @Override
 public void onAttachedToWindow() {
   super.onAttachedToWindow();
   if (mHardwareView == null || mHardwareView.isReadyToRender()) {
     mAwContents.onAttachedToWindow();
     mAttachedContents = true;
   } else {
     mHardwareView.setReadyToRenderCallback(
         new Runnable() {
           @Override
           public void run() {
             assert !mAttachedContents;
             mAwContents.onAttachedToWindow();
             mAttachedContents = true;
           }
         });
   }
 }
Example #16
0
  /** Loads the main html then triggers the popup window. */
  public void triggerPopup(
      final AwContents parentAwContents,
      TestAwContentsClient parentAwContentsClient,
      TestWebServer testWebServer,
      String mainHtml,
      String popupHtml,
      String popupPath,
      String triggerScript)
      throws Exception {
    enableJavaScriptOnUiThread(parentAwContents);
    getInstrumentation()
        .runOnMainSync(
            new Runnable() {
              @Override
              public void run() {
                parentAwContents.getSettings().setSupportMultipleWindows(true);
                parentAwContents.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
              }
            });

    final String parentUrl = testWebServer.setResponse("/popupParent.html", mainHtml, null);
    if (popupHtml != null) {
      testWebServer.setResponse(popupPath, popupHtml, null);
    } else {
      testWebServer.setResponseWithNoContentStatus(popupPath);
    }

    parentAwContentsClient.getOnCreateWindowHelper().setReturnValue(true);
    loadUrlSync(parentAwContents, parentAwContentsClient.getOnPageFinishedHelper(), parentUrl);

    TestAwContentsClient.OnCreateWindowHelper onCreateWindowHelper =
        parentAwContentsClient.getOnCreateWindowHelper();
    int currentCallCount = onCreateWindowHelper.getCallCount();
    parentAwContents.evaluateJavaScriptForTests(triggerScript, null);
    onCreateWindowHelper.waitForCallback(
        currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
  }
 @Override
 public void computeScroll() {
   mAwContents.computeScroll();
 }
 @Override
 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
   super.onInitializeAccessibilityEvent(event);
   event.setClassName(AwContents.class.getName());
   mAwContents.onInitializeAccessibilityEvent(event);
 }
 @Override
 public boolean performAccessibilityAction(int action, Bundle arguments) {
   return mAwContents.performAccessibilityAction(action, arguments);
 }
 @Override
 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
   AccessibilityNodeProvider provider = mAwContents.getAccessibilityNodeProvider();
   return provider == null ? super.getAccessibilityNodeProvider() : provider;
 }
 @Override
 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
   super.onInitializeAccessibilityNodeInfo(info);
   info.setClassName(AwContents.class.getName());
   mAwContents.onInitializeAccessibilityNodeInfo(info);
 }
 @Override
 public boolean onHoverEvent(MotionEvent ev) {
   super.onHoverEvent(ev);
   return mAwContents.onHoverEvent(ev);
 }
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
   return mAwContents.onKeyUp(keyCode, event);
 }
 @Override
 public void onWindowVisibilityChanged(int visibility) {
   super.onWindowVisibilityChanged(visibility);
   mAwContents.onWindowVisibilityChanged(visibility);
 }
 @Override
 public boolean dispatchKeyEvent(KeyEvent event) {
   return mAwContents.dispatchKeyEvent(event);
 }
 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   mAwContents.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }
 @Override
 public void onSizeChanged(int w, int h, int ow, int oh) {
   super.onSizeChanged(w, h, ow, oh);
   mAwContents.onSizeChanged(w, h, ow, oh);
 }
 @Override
 public void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
   mAwContents.onContainerViewOverScrolled(scrollX, scrollY, clampedX, clampedY);
 }
 AwSettings getSettings() {
   return mAwContents.getSettings();
 }
 @Override
 public void onVisibilityChanged(View changedView, int visibility) {
   super.onVisibilityChanged(changedView, visibility);
   mAwContents.onVisibilityChanged(changedView, visibility);
 }